<?php
$navGroups = [
    'Overview' => [
        ['label' => 'Dashboard', 'href' => 'index.php', 'icon' => 'fa-gauge'],
        ['label' => 'Leads Inbox', 'href' => 'leads.php', 'icon' => 'fa-address-book'],
        ['label' => 'Contact Messages', 'href' => 'contact_messages.php', 'icon' => 'fa-envelope'],
    ],
    'Homepage' => [
        ['label' => 'Hero Sliders', 'href' => 'crud.php?module=sliders', 'icon' => 'fa-images'],
        ['label' => 'Why Choose Us', 'href' => 'crud.php?module=features', 'icon' => 'fa-star'],
        ['label' => 'Services', 'href' => 'crud.php?module=services', 'icon' => 'fa-briefcase'],
    ],
    'About Us' => [
        ['label' => 'Company & Chairman', 'href' => 'about.php', 'icon' => 'fa-building'],
        ['label' => 'Team Members', 'href' => 'crud.php?module=team', 'icon' => 'fa-people-group'],
        ['label' => 'FAQs', 'href' => 'crud.php?module=faqs', 'icon' => 'fa-circle-question'],
    ],
    'Study Programs' => [
        ['label' => 'Study Abroad Countries', 'href' => 'countries.php', 'icon' => 'fa-earth-americas'],
        ['label' => 'Test Preparation', 'href' => 'test_preparations.php', 'icon' => 'fa-book-open-reader'],
    ],
    'Content' => [
        ['label' => 'Testimonials', 'href' => 'crud.php?module=testimonials', 'icon' => 'fa-quote-left'],
        ['label' => 'Partners', 'href' => 'crud.php?module=partners', 'icon' => 'fa-handshake'],
        ['label' => 'Gallery Categories', 'href' => 'crud.php?module=gallery_categories', 'icon' => 'fa-folder'],
        ['label' => 'Gallery Items', 'href' => 'crud.php?module=gallery_items', 'icon' => 'fa-image'],
        ['label' => 'Blog Categories', 'href' => 'crud.php?module=blog_categories', 'icon' => 'fa-tags'],
        ['label' => 'Blog Posts', 'href' => 'blogs.php', 'icon' => 'fa-newspaper'],
    ],
    'System' => [
        ['label' => 'General Settings', 'href' => 'settings.php', 'icon' => 'fa-gear'],
        ['label' => 'Users & Roles', 'href' => 'users.php', 'icon' => 'fa-user-shield'],
        ['label' => 'Change Password', 'href' => 'change-password.php', 'icon' => 'fa-key'],
    ],
];
$currentFile = basename($_SERVER['PHP_SELF']);
$currentModule = $_GET['module'] ?? null;
?>
<aside :class="sidebarOpen ? 'translate-x-0' : '-translate-x-full lg:translate-x-0'"
       class="fixed lg:static inset-y-0 left-0 z-40 w-72 bg-brand-black text-gray-300 flex flex-col transition-transform duration-300 shrink-0">
  <div class="flex items-center gap-3 px-5 py-5 border-b border-white/10">
    <img src="../assets/img/logo-icon.png" alt="Logo" class="h-10 w-10 rounded-full bg-white p-1">
    <div class="min-w-0">
      <p class="text-white font-heading font-bold text-sm leading-tight truncate">Excellent Education</p>
      <p class="text-xs text-gray-400">Admin Panel</p>
    </div>
  </div>

  <nav class="flex-1 overflow-y-auto px-3 py-4 space-y-6">
    <?php foreach ($navGroups as $group => $items): ?>
      <div>
        <p class="px-3 text-[11px] font-semibold uppercase tracking-widest text-gray-500 mb-2"><?= e($group) ?></p>
        <div class="space-y-1">
          <?php foreach ($items as $item):
            $hrefFile = strtok($item['href'], '?');
            $hrefModule = null;
            if (str_contains($item['href'], 'module=')) {
                parse_str(parse_url($item['href'], PHP_URL_QUERY) ?? '', $q);
                $hrefModule = $q['module'] ?? null;
            }
            $isActive = ($currentFile === $hrefFile) && ($hrefModule === null || $hrefModule === $currentModule);
          ?>
          <a href="<?= e($item['href']) ?>"
             class="flex items-center gap-3 px-3 py-2.5 rounded-xl text-sm font-medium transition <?= $isActive ? 'bg-gradient-to-r from-brand-red to-brand-green text-white' : 'hover:bg-white/5 text-gray-300' ?>">
            <i class="fa-solid <?= e($item['icon']) ?> w-4"></i> <?= e($item['label']) ?>
          </a>
          <?php endforeach; ?>
        </div>
      </div>
    <?php endforeach; ?>
  </nav>

  <div class="px-3 py-4 border-t border-white/10">
    <a href="../index.php" target="_blank" class="flex items-center gap-3 px-3 py-2.5 rounded-xl text-sm font-medium text-gray-300 hover:bg-white/5 transition">
      <i class="fa-solid fa-arrow-up-right-from-square w-4"></i> View Live Site
    </a>
    <a href="logout.php" class="flex items-center gap-3 px-3 py-2.5 rounded-xl text-sm font-medium text-brand-red hover:bg-white/5 transition">
      <i class="fa-solid fa-right-from-bracket w-4"></i> Logout
    </a>
  </div>
</aside>
<div x-show="sidebarOpen" x-cloak @click="sidebarOpen = false" class="fixed inset-0 bg-black/50 z-30 lg:hidden"></div>
