<?php
if (session_status() === PHP_SESSION_NONE) session_start();
require_once __DIR__ . '/../../includes/csrf.php';
csrf_generate();
$cp = basename($_SERVER['PHP_SELF']);
$isAdmin = ($_SESSION['admin']['role'] ?? 'admin') === 'admin';
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<?php csrf_meta(); ?>
<title>Perna Events — Admin</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&family=JetBrains+Mono:wght@500&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
<link rel="stylesheet" href="assets/admin.css?v=1">
</head>
<body>
<div class="adm-topbar">
  <div class="adm-brand">
    <div class="adm-brand-dot"><svg viewBox="0 0 20 20"><path d="M10 2l2.4 4.9 5.4.8-3.9 3.8.9 5.3L10 14.4l-4.8 2.5.9-5.3L2.2 7.7l5.4-.8z"/></svg></div>
    Perna Events
  </div>
  <div class="adm-topbar-right">
    <div class="adm-avatar"><?= strtoupper(substr($_SESSION['admin']['username']??'A',0,1)) ?></div>
    <span style="font-size:13px;color:var(--muted)"><?= htmlspecialchars($_SESSION['admin']['username']??'Admin') ?></span>
  </div>
</div>
<nav class="adm-sidebar">
  <div class="adm-sidebar-section">Main</div>
  <a href="dashboard.php" class="adm-nav-link <?= $cp==='dashboard.php'?'active':'' ?>"><i class="bi bi-grid-1x2-fill"></i>Dashboard</a>
  <?php if ($isAdmin): ?>
  <a href="manage_vouchers.php" class="adm-nav-link <?= $cp==='manage_vouchers.php'?'active':'' ?>"><i class="bi bi-ticket-fill"></i>Manage Vouchers</a>
  <a href="vouchers.php"  class="adm-nav-link <?= $cp==='vouchers.php' ?'active':'' ?>" style="padding-left:28px;font-size:12.5px"><i class="bi bi-people-fill" style="font-size:14px"></i>Release to Users</a>
  <?php endif; ?>
  <div class="adm-sidebar-section">Operations</div>
  <a href="scan.php"           class="adm-nav-link <?= $cp==='scan.php'           ?'active':'' ?>"><i class="bi bi-qr-code-scan"></i>Scan QR</a>
  <a href="users.php"          class="adm-nav-link <?= $cp==='users.php'         ?'active':'' ?>"><i class="bi bi-people-fill"></i>Users</a>
  <?php if ($isAdmin): ?>
  <a href="user_settings.php"  class="adm-nav-link <?= $cp==='user_settings.php' ?'active':'' ?>"><i class="bi bi-sliders"></i>User Page</a>
  <a href="staff.php"          class="adm-nav-link <?= $cp==='staff.php'         ?'active':'' ?>"><i class="bi bi-person-badge-fill"></i>Staff</a>
  <?php endif; ?>
  <a href="claims.php"         class="adm-nav-link <?= $cp==='claims.php'        ?'active':'' ?>"><i class="bi bi-bar-chart-fill"></i>Analytics</a>

  <div class="adm-sidebar-foot">
    <a href="logout.php" class="adm-nav-link"><i class="bi bi-box-arrow-right"></i>Logout</a>
  </div>
</nav>
<nav class="adm-bottom-nav">
  <a href="dashboard.php" class="<?= $cp==='dashboard.php'?'active':'' ?>"><i class="bi bi-grid-1x2-fill"></i>Home</a>
  <?php if ($isAdmin): ?>
  <a href="manage_vouchers.php" class="<?= $cp==='manage_vouchers.php'?'active':'' ?>"><i class="bi bi-ticket-fill"></i>Vouchers</a>
  <?php endif; ?>
  <a href="scan.php"      class="<?= $cp==='scan.php'     ?'active':'' ?>"><i class="bi bi-qr-code-scan"></i>Scan QR</a>
  <a href="users.php"     class="<?= $cp==='users.php'    ?'active':'' ?>"><i class="bi bi-people-fill"></i>Users</a>
  <?php if ($isAdmin): ?>
  <a href="user_settings.php" class="<?= $cp==='user_settings.php'?'active':'' ?>"><i class="bi bi-sliders"></i>Settings</a>
  <a href="staff.php" class="<?= $cp==='staff.php'?'active':'' ?>"><i class="bi bi-person-badge-fill"></i>Staff</a>
  <?php endif; ?>
  <a href="claims.php"    class="<?= $cp==='claims.php'   ?'active':'' ?>"><i class="bi bi-bar-chart-fill"></i>Reports</a>
  <a href="logout.php"><i class="bi bi-box-arrow-right"></i>Logout</a>
</nav>
<div id="adm-toast"><i class="ti bi" id="ti"></i><span id="tm"></span></div>
<script>
// ── Global toast — defined here so all page scripts can use it ────────────────
let _toastTimer;
function showToast(msg, type = 'info') {
  const t  = document.getElementById('adm-toast');
  const ic = document.getElementById('ti');
  const tx = document.getElementById('tm');
  const map = {
    success: { icon: 'bi-check-circle-fill',        color: '#2dbd87', border: 'rgba(45,189,135,.4)'  },
    error:   { icon: 'bi-x-circle-fill',             color: '#e05252', border: 'rgba(224,82,82,.4)'   },
    info:    { icon: 'bi-info-circle-fill',           color: '#4b9eff', border: 'rgba(75,158,255,.35)' },
    warning: { icon: 'bi-exclamation-circle-fill',   color: '#f5c518', border: 'rgba(245,197,24,.4)'  },
  };
  const cfg = map[type] ?? map.info;
  ic.className       = 'ti bi ' + cfg.icon;
  ic.style.color     = cfg.color;
  t.style.borderColor = cfg.border;
  tx.textContent     = msg;
  t.classList.add('show');
  clearTimeout(_toastTimer);
  _toastTimer = setTimeout(() => t.classList.remove('show'), 3000);
}

// ── CSRF token for all AJAX requests ─────────────────────────────────────────
const CSRF = document.querySelector('meta[name="csrf-token"]')?.content ?? '';
</script>
<main class="adm-main">
