<?php
include '../functions.php';
if (isset($_SESSION['username']) == true) {
	$username = $_SESSION['username'];
}

// Ensure admin_read column exists on support_tickets
$_acol = $conn->query("SHOW COLUMNS FROM support_tickets LIKE 'admin_read'");
if ($_acol && $_acol->num_rows === 0) {
    $conn->query("ALTER TABLE support_tickets ADD COLUMN admin_read TINYINT(1) NOT NULL DEFAULT 0");
}

// Fetch unread support tickets for admin notification bell
$_admin_notif_rows  = [];
$_admin_notif_count = 0;
$_anq = $conn->query("SELECT id, user_name, user_email, subject, priority, date_added FROM support_tickets WHERE admin_read = 0 ORDER BY date_added DESC LIMIT 3");
if ($_anq) {
    $_admin_notif_rows  = $_anq->fetch_all(MYSQLI_ASSOC);
    $_admin_notif_count = count($_admin_notif_rows);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
	<!-- Required meta tags -->
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
	<title><?php echo getsiteconfig('sitename'); ?> - Admin</title>
	<!--favicon-->
	<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
	<!--plugins-->
	<link href="assets/plugins/simplebar/css/simplebar.css" rel="stylesheet" />
	<!--Data Tables -->
	<link href="assets/plugins/datatable/css/dataTables.bootstrap4.min.css" rel="stylesheet" type="text/css">
	<link href="assets/plugins/datatable/css/buttons.bootstrap4.min.css" rel="stylesheet" type="text/css">
	<!--plugins-->
	<link href="assets/plugins/simplebar/css/simplebar.css" rel="stylesheet" />
	<link href="assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet" />
	<link href="assets/plugins/metismenu/css/metisMenu.min.css" rel="stylesheet" />
	<!-- loader-->
	<link href="assets/css/pace.min.css" rel="stylesheet" />
	<script src="assets/js/pace.min.js"></script>
	<!-- Bootstrap CSS -->
	<link rel="stylesheet" href="assets/css/bootstrap.min.css" />
	<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600&amp;family=Roboto&amp;display=swap" />
	<!-- Icons CSS -->
	<link rel="stylesheet" href="assets/css/icons.css" />
	<!-- App CSS -->
	<link rel="stylesheet" href="assets/css/app.css" />
	<link rel="stylesheet" href="assets/css/dark-sidebar.css" />
	<link rel="stylesheet" href="assets/css/dark-theme.css" />
	<script type="text/javascript" src="js/sweetalert.min.js"></script>
	<script type="text/javascript" src="../ckeditor/ckeditor.js"></script>

	<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
</head>

<body>
	<!-- wrapper -->
	<div class="wrapper">
		<!--sidebar-wrapper-->
		<div class="sidebar-wrapper" data-simplebar="true">
			<div class="sidebar-header">
				<div class="">
					<img src="assets/images/logo-icon.png" class="logo-icon-2" alt="" />
				</div>
				<div>
					<h4 class="logo-text">Syndash</h4>
				</div>
				<a href="javascript:;" class="toggle-btn ml-auto"> <i class="bx bx-menu"></i>
				</a>
			</div>
			<!--navigation-->
			<ul class="metismenu" id="menu">
				<li>
					<a href="./">
						<div class="parent-icon icon-color-1"><i class="bx bx-home-alt"></i>
						</div>
						<div class="menu-title">Dashboard</div>
					</a>
				</li>
				<li>
					<a class="has-arrow" href="javascript:;">
						<div class="parent-icon icon-color-6"><i class="bx bx-money"></i>
						</div>
						<div class="menu-title">Transactions</div>
					</a>
					<ul>
						<li> <a href="transfers"><i class="bx bx-right-arrow-alt"></i>Transfers</a>
						<li> <a href="add_history"><i class="bx bx-right-arrow-alt"></i>Create Transactions</a>
						</li><!--
						<li> <a href="pending_transfers"><i class="bx bx-right-arrow-alt"></i>Pending transfers</a>
						</li>
						<li> <a href="approved_transfers"><i class="bx bx-right-arrow-alt"></i>Approved transfers</a>
						</li> -->
						</li>
					</ul>
				</li>
				<li>
					<a class="" href="../register">
						<div class="parent-icon icon-color-6"><i class="bx bx-user"></i>
						</div>
						<div class="menu-title">Register User</div>
					</a>
				</li>
				<li>
					<a class="has-arrow" href="javascript:;">
						<div class="parent-icon icon-color-6"><i class="bx bx-menu"></i>
						</div>
						<div class="menu-title">Settings</div>
					</a>
					<ul>
						<li> <a href="profile"><i class="bx bx-right-arrow-alt"></i>Profile</a>
						</li>
						<li> <a href="change_password"><i class="bx bx-right-arrow-alt"></i>Change Password</a>
						</li>
					</ul>
				</li>
	<li>
				<!--	<a class="has-arrow" href="javascript:;">
						<div class="parent-icon icon-color-6"><i class="bx bx-code"></i>
						</div>
						<div class="menu-title">IMF Codes</div>
					</a>
					<ul>
						<li> <a href="new-imf-code"><i class="bx bx-right-arrow-alt"></i>Add IMF Code</a>
						</li><li> <a href="active-imf-codes"><i class="bx bx-right-arrow-alt"></i>Active IMF Code</a>
						</li>
					</li><li> <a href="expired-imf-codes"><i class="bx bx-right-arrow-alt"></i>Expired IMF Code</a>
						</li>
					</ul>
				</li>


					<li>
					<a class="has-arrow" href="javascript:;">
						<div class="parent-icon icon-color-6"><i class="bx bx-code"></i>
						</div>
						<div class="menu-title">Swift Codes</div>
					</a>
					<ul>
						<li> <a href="new-swift-code"><i class="bx bx-right-arrow-alt"></i>Add Swift Code</a>
						</li><li> <a href="active-swift-codes"><i class="bx bx-right-arrow-alt"></i>Active Swift Code</a>
						</li>
					</li><li> <a href="expired-swift-codes"><i class="bx bx-right-arrow-alt"></i>Expired Swift Code</a>
						</li>
					</ul>
				</li>
-->

				<li>
					<a class="has-arrow" href="javascript:;">
						<div class="parent-icon icon-color-6"><i class="bx bx-code"></i>
						</div>
						<div class="menu-title">Cot Codes</div>
					</a>
					<ul>
						<li> <a href="new-cot-code"><i class="bx bx-right-arrow-alt"></i>Add Cot Code</a>
						</li><li> <a href="active-cot-codes"><i class="bx bx-right-arrow-alt"></i>Active Cot Code</a>
						</li>
					</li><li> <a href="expired-cot-codes"><i class="bx bx-right-arrow-alt"></i>Expired Cot Code</a>
						</li>
					</ul>
				</li>
				<li>
					<a class="has-arrow" href="javascript:;">
						<div class="parent-icon icon-color-6"><i class="bx bx-code"></i>
						</div>
						<div class="menu-title">Tax Codes</div>
					</a>
					<ul>
						<li> <a href="new-tax-code"><i class="bx bx-right-arrow-alt"></i>Add Tax Code</a>
						</li><li> <a href="active-tax-codes"><i class="bx bx-right-arrow-alt"></i>Active Tax Code</a>
						</li>
					</li><li> <a href="expired-tax-codes"><i class="bx bx-right-arrow-alt"></i>Expired Tax Code</a>
						</li>
					</ul>
				</li>
				<li>
					<a class="has-arrow" href="javascript:;">
						<div class="parent-icon icon-color-6"><i class="bx bx-user"></i>
						</div>
						<div class="menu-title">Members</div>
					</a>
					<ul>
						<li> <a href="members"><i class="bx bx-right-arrow-alt"></i>Members</a>
						</li>
						<li> <a href="admins"><i class="bx bx-right-arrow-alt"></i>Admin</a>
						</li>
					</ul>
				</li>
				<li>
					<a href="kyc-submissions">
						<div class="parent-icon icon-color-6"><i class="bx bx-id-card"></i>
						</div>
						<div class="menu-title">KYC Submissions</div>
					</a>
				</li>
				<li>
					<a href="tax-refunds">
						<div class="parent-icon icon-color-6"><i class="bx bx-receipt"></i>
						</div>
						<div class="menu-title">Tax Refunds</div>
					</a>
				</li>
				<li>
					<a href="support-tickets">
						<div class="parent-icon icon-color-6"><i class="bx bx-support"></i>
						</div>
						<div class="menu-title">Support Tickets</div>
					</a>
				</li>
				<li>
					<a class="has-arrow" href="logout">
						<div class="parent-icon icon-color-6"><i class="bx bx-user"></i>
						</div>
						<div class="menu-title">Logout</div>
					</a>
				</li>
			</ul>
			<!--end navigation-->
		</div>
		<!--end sidebar-wrapper-->
		<!--header-->
		<header class="top-header">
			<nav class="navbar navbar-expand">
				<div class="left-topbar d-flex align-items-center">
					<a href="javascript:;" class="toggle-btn">	<i class="bx bx-menu"></i>
					</a>
				</div>
				<div class="flex-grow-1 search-bar">
					<div class="input-group">
						<div class="input-group-prepend search-arrow-back">
							<button class="btn btn-search-back" type="button"><i class="bx bx-arrow-back"></i>
							</button>
						</div>
						<input type="text" class="form-control" placeholder="search" />
						<div class="input-group-append">
							<button class="btn btn-search" type="button"><i class="lni lni-search-alt"></i>
							</button>
						</div>
					</div>
				</div>
				<div class="right-topbar ml-auto">
					<ul class="navbar-nav">
						<li class="nav-item search-btn-mobile">
							<a class="nav-link position-relative" href="javascript:;">	<i class="bx bx-search vertical-align-middle"></i>
							</a>
						</li>
						<li class="nav-item dropdown dropdown-lg">
							<a class="nav-link dropdown-toggle dropdown-toggle-nocaret position-relative" href="javascript:;" data-toggle="dropdown">
								<i class="bx bx-bell vertical-align-middle"></i>
								<?php if ($_admin_notif_count > 0): ?>
								<span class="msg-count"><?php echo $_admin_notif_count; ?></span>
								<?php endif; ?>
							</a>
							<div class="dropdown-menu dropdown-menu-right">
								<a href="javascript:;">
									<div class="msg-header">
										<h6 class="msg-header-title"><?php echo $_admin_notif_count > 0 ? $_admin_notif_count . ' New' : 'No New'; ?></h6>
										<p class="msg-header-subtitle">Support Ticket<?php echo $_admin_notif_count !== 1 ? 's' : ''; ?></p>
									</div>
								</a>
								<div class="header-notifications-list">
									<?php if (empty($_admin_notif_rows)): ?>
									<div class="dropdown-item text-center py-3 text-muted small">No new support tickets</div>
									<?php else: ?>
									<?php foreach ($_admin_notif_rows as $_an):
										$_an_priority_cls = ['low'=>'bg-light-success text-success','medium'=>'bg-light-warning text-warning','high'=>'bg-light-danger text-danger'][$_an['priority']] ?? 'bg-light-primary text-primary';
										$_an_time = date('M j, g:ia', strtotime($_an['date_added']));
									?>
									<a class="dropdown-item" href="view-ticket?id=<?php echo $_an['id']; ?>">
										<div class="media align-items-center">
											<div class="notify <?php echo $_an_priority_cls; ?>">
												<i class="bx bx-support"></i>
											</div>
											<div class="media-body">
												<h6 class="msg-name"><?php echo htmlspecialchars($_an['user_name']); ?> <span class="msg-time float-right"><?php echo $_an_time; ?></span></h6>
												<p class="msg-info"><?php echo htmlspecialchars($_an['subject']); ?></p>
											</div>
										</div>
									</a>
									<?php endforeach; ?>
									<?php endif; ?>
								</div>
								<a href="support-tickets">
									<div class="text-center msg-footer">View All Tickets</div>
								</a>
							</div>
						</li>
						<li class="nav-item dropdown dropdown-user-profile">
							<a class="nav-link dropdown-toggle dropdown-toggle-nocaret" href="javascript:;" data-toggle="dropdown">
								<div class="media user-box align-items-center">
									<div class="media-body user-info">
										<p class="user-name mb-0"><?php echo $username;?></p>
									</div>
									<img src="../images/user.png" class="user-img" alt="user avatar">
								</div>
							</a>
							<div class="dropdown-menu dropdown-menu-right">
								<a class="dropdown-item" href="profile"><i class="bx bx-user"></i><span>Profile</span></a>
								<a class="dropdown-item" href="./"><i class="bx bx-tachometer"></i><span>Dashboard</span></a>
								<div class="dropdown-divider mb-0"></div>
								<a class="dropdown-item" href="logout"><i class="bx bx-power-off"></i><span>Logout</span></a>
							</div>
						</li>
					</ul>
				</div>
			</nav>
		</header>
		<!--end header-->