<?php
global $pdo;
$siteName = getSetting($pdo, 'site_name', SITE_NAME);
$adminUser = currentAdmin($pdo);
$pageTitle = ($pageTitle ?? 'Admin Panel') . ' | ' . $siteName . ' Admin';

// Unread tickets count
$openTickets = (int)$pdo->query("SELECT COUNT(*) FROM support_tickets WHERE status='open'")->fetchColumn();
$pendingKyc  = (int)$pdo->query("SELECT COUNT(*) FROM kyc_documents WHERE status='pending'")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= htmlspecialchars($pageTitle) ?></title>
<link rel="icon" href="/assets/img/logo.png">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<link rel="stylesheet" href="/admin/assets/css/admin.css">
</head>
<body>
<div class="admin-wrapper">
<!-- Sidebar -->
<aside class="admin-sidebar" id="adminSidebar">
  <div class="sidebar-brand">
    <img src="/assets/img/logo.png" alt="<?= htmlspecialchars($siteName) ?>" height="32">
    <span><?= htmlspecialchars($siteName) ?></span>
  </div>
  <nav class="sidebar-nav">
    <div class="nav-section">Main</div>
    <a href="/admin/index.php" class="sidebar-link <?= basename($_SERVER['PHP_SELF'])==='index.php'&&dirname($_SERVER['PHP_SELF'])==='/admin'?'active':'' ?>"><i class="fa fa-tachometer-alt"></i>Dashboard</a>
    <a href="/admin/users.php" class="sidebar-link <?= basename($_SERVER['PHP_SELF'])==='users.php'?'active':'' ?>"><i class="fa fa-users"></i>Users</a>
    <a href="/admin/transactions.php" class="sidebar-link <?= basename($_SERVER['PHP_SELF'])==='transactions.php'?'active':'' ?>"><i class="fa fa-exchange-alt"></i>Transactions</a>

    <div class="nav-section">Verification</div>
    <a href="/admin/kyc.php" class="sidebar-link <?= basename($_SERVER['PHP_SELF'])==='kyc.php'?'active':'' ?>">
      <i class="fa fa-id-card"></i>KYC Review
      <?php if ($pendingKyc): ?><span class="badge bg-warning text-dark ms-auto"><?= $pendingKyc ?></span><?php endif; ?>
    </a>
    <a href="/admin/support.php" class="sidebar-link <?= basename($_SERVER['PHP_SELF'])==='support.php'?'active':'' ?>">
      <i class="fa fa-headset"></i>Support Tickets
      <?php if ($openTickets): ?><span class="badge bg-danger ms-auto"><?= $openTickets ?></span><?php endif; ?>
    </a>

    <div class="nav-section">Management</div>
    <a href="/admin/loans.php" class="sidebar-link <?= basename($_SERVER['PHP_SELF'])==='loans.php'?'active':'' ?>"><i class="fa fa-hand-holding-usd"></i>Loans</a>
    <a href="/admin/investments.php" class="sidebar-link <?= basename($_SERVER['PHP_SELF'])==='investments.php'?'active':'' ?>"><i class="fa fa-chart-line"></i>Investments</a>
    <a href="/admin/announcements.php" class="sidebar-link <?= basename($_SERVER['PHP_SELF'])==='announcements.php'?'active':'' ?>"><i class="fa fa-bullhorn"></i>Announcements</a>

    <div class="nav-section">Admin</div>
    <?php if ($_SESSION['admin_role']==='super_admin'): ?>
    <a href="/admin/sub-admins.php" class="sidebar-link <?= basename($_SERVER['PHP_SELF'])==='sub-admins.php'?'active':'' ?>"><i class="fa fa-user-shield"></i>Sub-Admins</a>
    <a href="/admin/settings.php" class="sidebar-link <?= basename($_SERVER['PHP_SELF'])==='settings.php'?'active':'' ?>"><i class="fa fa-cog"></i>Settings</a>
    <?php endif; ?>
    <a href="/admin/activity.php" class="sidebar-link <?= basename($_SERVER['PHP_SELF'])==='activity.php'?'active':'' ?>"><i class="fa fa-history"></i>Activity Log</a>
    <a href="/admin/logout.php" class="sidebar-link text-danger"><i class="fa fa-sign-out-alt"></i>Logout</a>
  </nav>
</aside>

<!-- Main -->
<div class="admin-main">
  <header class="admin-topbar">
    <button class="btn btn-sm btn-outline-secondary" id="sidebarToggle"><i class="fa fa-bars"></i></button>
    <div class="d-flex align-items-center gap-3 ms-auto">
      <a href="/admin/kyc.php" class="btn btn-sm btn-outline-warning position-relative">
        <i class="fa fa-id-card"></i>
        <?php if ($pendingKyc): ?><span class="badge bg-warning text-dark position-absolute top-0 start-100 translate-middle"><?= $pendingKyc ?></span><?php endif; ?>
      </a>
      <a href="/admin/support.php" class="btn btn-sm btn-outline-danger position-relative">
        <i class="fa fa-headset"></i>
        <?php if ($openTickets): ?><span class="badge bg-danger position-absolute top-0 start-100 translate-middle"><?= $openTickets ?></span><?php endif; ?>
      </a>
      <div class="dropdown">
        <button class="btn btn-sm btn-outline-secondary dropdown-toggle" data-bs-toggle="dropdown">
          <i class="fa fa-user-circle me-1"></i><?= htmlspecialchars($adminUser['display_name'] ?? $adminUser['username'] ?? 'Admin') ?>
        </button>
        <ul class="dropdown-menu dropdown-menu-end">
          <li><span class="dropdown-item-text small text-muted"><?= htmlspecialchars($adminUser['email'] ?? '') ?></span></li>
          <li><span class="dropdown-item-text small"><span class="badge bg-primary"><?= ucfirst(str_replace('_',' ',$adminUser['role']??'')) ?></span></span></li>
          <li><hr class="dropdown-divider"></li>
          <li><a class="dropdown-item text-danger" href="/admin/logout.php"><i class="fa fa-sign-out-alt me-2"></i>Logout</a></li>
        </ul>
      </div>
    </div>
  </header>
  <div class="admin-content">
<?php if ($flash = get_flash('success')): ?>
<div class="alert alert-success alert-dismissible fade show mb-3"><i class="fa fa-check-circle me-2"></i><?= htmlspecialchars($flash) ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
<?php endif; ?>
<?php if ($flash = get_flash('error')): ?>
<div class="alert alert-danger alert-dismissible fade show mb-3"><i class="fa fa-exclamation-circle me-2"></i><?= htmlspecialchars($flash) ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
<?php endif; ?>
