<?php
/**
 * USHS Admin — Unified Header + Slide-out Sidebar
 * Sidebar is HIDDEN by default. Opens/closes via hamburger button.
 */
if (!function_exists('render_admin_sidebar')) {
    function render_admin_sidebar($activePage = null) {
        $current   = $activePage ?: basename($_SERVER['PHP_SELF']);
        $adminName = $_SESSION['admin_name'] ?? 'Admin';
        $parts     = explode(' ', trim($adminName));
        $initials  = strtoupper(
            substr($parts[0] ?? 'A', 0, 1) .
            substr($parts[1] ?? 'D', 0, 1)
        );

        $items = [
            ['dashboard.php',     'fas fa-chart-line',        'Dashboard'],
            ['requests.php',      'fas fa-clipboard-list',    'Requests'],
            ['cities.php',        'fas fa-city',              'Cities'],
            ['customers.php',     'fas fa-users',             'Customers'],
            ['staff.php',         'fas fa-user-tie',          'Staff Management'],
            ['appliances.php',    'fas fa-tools',             'Appliances'],
            ['brands.php',        'fas fa-tag',               'Brands'],
            ['activity_logs.php', 'fas fa-clock-rotate-left', 'Activity Logs'],
            ['announcements.php', 'fas fa-bullhorn',          'Announcements'],
            ['mistake.php',       'fas fa-circle-exclamation','Mistake'],
            ['warning.php',       'fas fa-triangle-exclamation','Warning'],
            ['settings.php',      'fas fa-cog',               'Settings'],
        ];

        $pageTitles = [
            'dashboard.php'     => 'Admin Dashboard',
            'requests.php'      => 'Requests',
            'cities.php'        => 'Cities',
            'customers.php'     => 'Customers',
            'staff.php'         => 'Staff Management',
            'appliances.php'    => 'Appliances',
            'brands.php'        => 'Brands',
            'activity_logs.php' => 'Activity Logs',
            'announcements.php' => 'Announcements',
            'mistake.php'       => 'Mistake',
            'warning.php'       => 'Warning',
            'settings.php'      => 'Settings',
        ];
        $pageTitle = $pageTitles[$current] ?? 'Admin';
        ?>
<link rel="stylesheet" href="../assets/admin-sidebar.css">
<link rel="stylesheet" href="../assets/ushs-header.css">

<!-- ============================================================
     HEADER — fixed, full width, always on top
     ============================================================ -->
<header class="ushs-header admin-top-header" id="adminTopHeader">

    <!-- Hamburger — opens sidebar on click -->
    <button type="button"
            class="ushs-header__menu-btn"
            id="adminSidebarToggle"
            aria-label="Toggle admin menu"
            style="flex-shrink:0; margin-right:6px;">
        <i class="fas fa-bars"></i>
    </button>

    <!-- Brand (logo + name) — sits just to the right of the hamburger -->
    <a class="ushs-header__brand" href="dashboard.php" style="text-decoration:none; margin-left:4px;">
        <div class="ushs-header__logo">
            <img src="../assets/icons/ushs.avif" alt="USHS"
                 onerror="this.style.display='none';
                          this.parentNode.innerHTML='<i class=\'fas fa-tools\' style=\'font-size:15px;color:inherit;\'></i>'">
        </div>
        <div class="ushs-header__brand-text">
            <span class="ushs-header__brand-name">US In Home Services</span>
            <span class="ushs-header__brand-sub">Admin Console</span>
        </div>
    </a>

    <!-- Current page title -->
    <span class="ushs-header__title"><?= htmlspecialchars($pageTitle, ENT_QUOTES, 'UTF-8') ?></span>

    <!-- Right-side actions -->
    <div class="ushs-header__actions">

        <!-- Refresh -->
        <button class="ushs-header__icon-btn" onclick="location.reload()" title="Refresh">
            <i class="fas fa-sync-alt"></i>
        </button>

        <!-- Admin profile dropdown -->
        <div class="dropdown admin-profile-dropdown">
            <button type="button"
                    class="ushs-header__user admin-profile-trigger"
                    id="adminProfileDropdown"
                    data-bs-toggle="dropdown"
                    aria-expanded="false"
                    style="cursor:pointer; border:0;">
                <div class="ushs-header__avatar"><?= htmlspecialchars($initials, ENT_QUOTES, 'UTF-8') ?></div>
                <div class="ushs-header__user-info">
                    <span class="ushs-header__user-name"><?= htmlspecialchars($adminName, ENT_QUOTES, 'UTF-8') ?></span>
                    <span class="ushs-header__user-role">Administrator</span>
                </div>
                <i class="fas fa-chevron-down ushs-header__user-chevron"></i>
            </button>
            <div class="dropdown-menu dropdown-menu-end admin-profile-menu" aria-labelledby="adminProfileDropdown">
                <a class="dropdown-item" href="settings.php">
                    <i class="fas fa-user-cog me-2"></i>Settings
                </a>
                <div class="dropdown-divider"></div>
                <a class="dropdown-item admin-profile-logout"
                   href="logout.php"
                   onclick="return adminLogout();">
                    <i class="fas fa-sign-out-alt me-2"></i>Logout
                </a>
            </div>
        </div>

    </div>
</header>

<!-- ============================================================
     BACKDROP — darkens page when sidebar is open
     ============================================================ -->
<div class="admin-sidebar-backdrop" id="adminSidebarBackdrop"></div>

<aside class="admin-sidebar" id="adminSidebar" aria-hidden="true">

<br><br><br>

    <!-- Nav links -->
    <nav class="admin-sidebar-menu" style="padding-top:8px;">
        <?php foreach ($items as $item): ?>
            <?php [$href, $icon, $label] = $item; ?>
            <a href="<?= htmlspecialchars($href, ENT_QUOTES, 'UTF-8') ?>"
               class="<?= $current === $href ? 'active' : '' ?>"
               <?= $href === 'logout.php'
                     ? 'onclick="return adminLogout();" style="color:oklch(0.78 0.17 27)!important;"'
                     : '' ?>>
                <i class="<?= htmlspecialchars($icon, ENT_QUOTES, 'UTF-8') ?>"></i>
                <span><?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ?></span>
            </a>
        <?php endforeach; ?>
    </nav>

</aside>

<style>
.admin-profile-dropdown { position: relative; }
.admin-profile-dropdown .admin-profile-trigger { background: rgba(15, 23, 42, 0.72); }
.admin-profile-menu {
    min-width: 210px;
    padding: 8px;
    border-radius: 16px;
    border: 1px solid rgba(148, 163, 184, 0.24);
    background: rgba(15, 23, 42, 0.98);
    box-shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
}
.admin-profile-menu .dropdown-item {
    color: #e5edf8 !important;
    border-radius: 12px;
    padding: 10px 12px;
    font-weight: 700;
}
.admin-profile-menu .dropdown-item:hover,
.admin-profile-menu .dropdown-item:focus {
    background: rgba(56, 189, 248, 0.14);
    color: #ffffff !important;
}
.admin-profile-menu .admin-profile-logout { color: #fb7185 !important; }
.admin-profile-menu .admin-profile-logout:hover { background: rgba(244, 63, 94, 0.14); }
</style>

<script>
/* ── Admin sidebar / logout ─────────────────────────────── */
(function () {
    var sidebar  = document.getElementById('adminSidebar');
    var backdrop = document.getElementById('adminSidebarBackdrop');
    var toggleBtn = document.getElementById('adminSidebarToggle');
    var closeBtn  = document.getElementById('adminSidebarClose');

    function openSidebar() {
        if (!sidebar) return;
        sidebar.classList.add('active');
        sidebar.setAttribute('aria-hidden', 'false');
        if (backdrop) backdrop.classList.add('active');
        document.body.style.overflow = 'hidden';
    }

    function closeSidebar() {
        if (!sidebar) return;
        sidebar.classList.remove('active');
        sidebar.setAttribute('aria-hidden', 'true');
        if (backdrop) backdrop.classList.remove('active');
        document.body.style.overflow = '';
    }

    if (toggleBtn) toggleBtn.addEventListener('click', function (e) {
        e.preventDefault();
        sidebar && sidebar.classList.contains('active') ? closeSidebar() : openSidebar();
    });

    if (closeBtn)  closeBtn.addEventListener('click',  closeSidebar);
    if (backdrop)  backdrop.addEventListener('click',  closeSidebar);

    document.addEventListener('keydown', function (e) {
        if (e.key === 'Escape') closeSidebar();
    });

    /* Legacy: wire any old #menuToggle buttons that pages may have */
    var legacy = document.getElementById('menuToggle');
    if (legacy) legacy.addEventListener('click', function (e) {
        e.preventDefault();
        sidebar && sidebar.classList.contains('active') ? closeSidebar() : openSidebar();
    });

    /* Fallback for hosts/pages where Bootstrap dropdown JS is not loaded */
    var profileTrigger = document.getElementById('adminProfileDropdown');
    var profileMenu = document.querySelector('.admin-profile-menu');
    if (profileTrigger && profileMenu) {
        profileTrigger.addEventListener('click', function (e) {
            if (typeof bootstrap !== 'undefined' && bootstrap.Dropdown) return;
            e.preventDefault();
            e.stopPropagation();
            profileMenu.classList.toggle('show');
            profileTrigger.setAttribute('aria-expanded', profileMenu.classList.contains('show') ? 'true' : 'false');
        });
        document.addEventListener('click', function (e) {
            if (!profileTrigger.contains(e.target) && !profileMenu.contains(e.target)) {
                profileMenu.classList.remove('show');
                profileTrigger.setAttribute('aria-expanded', 'false');
            }
        });
    }
}());

function adminLogout() {
    return confirm('Log out of Admin Portal?');
}
</script>
        <?php
    }
}
?>
