<?php
session_start();
if (!isset($_SESSION['username'])) { header("location:login"); exit; }
include 'master.php';

// Mark all tickets as read when admin views this page
$conn->query("UPDATE support_tickets SET admin_read = 1 WHERE admin_read = 0");
?>
		<!--page-wrapper-->
		<div class="page-wrapper">
			<div class="page-content-wrapper">
				<div class="page-content">
					<div class="page-breadcrumb d-none d-md-flex align-items-center mb-3">
						<div class="breadcrumb-title pr-3">Support</div>
						<div class="pl-3">
							<nav aria-label="breadcrumb">
								<ol class="breadcrumb mb-0 p-0">
									<li class="breadcrumb-item"><a href="javascript:;"><i class='bx bx-home-alt'></i></a></li>
									<li class="breadcrumb-item active" aria-current="page">Support Tickets</li>
								</ol>
							</nav>
						</div>
					</div><br><br>

					<?php if (!empty($_GET['replied'])): ?>
						<div class="alert alert-success alert-dismissible fade show" role="alert">
							Reply sent successfully.
							<button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
						</div>
					<?php endif; ?>

					<div class="card">
						<div class="card-body">
							<div class="card-title"><h4 class="mb-0">Support Tickets</h4></div>
							<hr/>
							<div class="table-responsive">
								<table id="example" class="table table-striped table-bordered" style="width:100%">
									<thead>
										<tr>
											<th>S/N</th>
											<th>User</th>
											<th>Subject</th>
											<th>Priority</th>
											<th>Status</th>
											<th>Date</th>
											<th class="text-center">Action</th>
										</tr>
									</thead>
									<tbody>
									<?php
									$res = $conn->query("SELECT * FROM support_tickets ORDER BY id DESC");
									$i = 1;
									if (!$res || $res->num_rows == 0) {
										echo "<tr><td colspan='7' style='color:red;text-align:center;'>No tickets yet</td></tr>";
									} else {
										foreach ($res as $row) {
											$pBadge = ['low'=>'success','medium'=>'warning','high'=>'danger'][$row['priority']] ?? 'secondary';
											$sBadge = ['Open'=>'primary','In Progress'=>'warning','Resolved'=>'success','Closed'=>'secondary'][$row['status']] ?? 'secondary';
									?>
										<tr>
											<td><?php echo $i; ?></td>
											<td><?php echo htmlspecialchars($row['user_name']); ?><br><small class="text-muted"><?php echo htmlspecialchars($row['user_email']); ?></small></td>
											<td><?php echo htmlspecialchars($row['subject']); ?></td>
											<td><span class="badge badge-<?php echo $pBadge; ?> text-capitalize"><?php echo htmlspecialchars($row['priority']); ?></span></td>
											<td><span class="badge badge-<?php echo $sBadge; ?>"><?php echo htmlspecialchars($row['status']); ?></span></td>
											<td><?php echo $row['date_added']; ?></td>
											<td class="text-center">
												<a href="view-ticket?id=<?php echo $row['id']; ?>" class="btn btn-primary btn-sm">View &amp; Reply</a>
											</td>
										</tr>
									<?php $i++; } } ?>
									</tbody>
									<tfoot>
										<tr>
											<th>S/N</th><th>User</th><th>Subject</th><th>Priority</th><th>Status</th><th>Date</th><th class="text-center">Action</th>
										</tr>
									</tfoot>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="overlay toggle-btn-mobile"></div>
		<a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>

<script>
	$(document).ready(function () { $('#example').DataTable(); });
</script>
<?php include 'footer.php'; ?>
