<?php
// admin/header.php
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: login.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">
    <title>Admin Panel - BMYA</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        :root {
            --sidebar-width: 260px;
            --sidebar-collapsed-width: 70px;
            --header-height: 60px;
            --primary-dark: #1a252f;
            --sidebar-bg: #2c3e50;
            --sidebar-hover: #3498db;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            background-color: #f5f5f5;
            overflow-x: hidden;
        }
        
        /* Navbar Styles */
        .navbar-dark {
            background: var(--primary-dark);
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 1030;
            min-height: var(--header-height);
            padding: 0.5rem 1rem;
        }
        
        .navbar-brand {
            font-weight: 600;
            font-size: 1.2rem;
        }
        
        .navbar-brand i {
            font-size: 1.3rem;
        }
        
        /* Menu Toggle Button */
        .menu-toggle {
            background: transparent;
            border: none;
            color: white;
            font-size: 1.3rem;
            padding: 8px 12px;
            border-radius: 8px;
            transition: all 0.3s ease;
            cursor: pointer;
        }
        
        .menu-toggle:hover {
            background: rgba(255,255,255,0.1);
        }
        
        /* Sidebar Styles */
        .sidebar {
            position: fixed;
            top: var(--header-height);
            left: 0;
            bottom: 0;
            width: var(--sidebar-width);
            background: var(--sidebar-bg);
            z-index: 1020;
            transition: transform 0.3s ease-in-out;
            overflow-y: auto;
            box-shadow: 2px 0 10px rgba(0,0,0,0.1);
        }
        
        /* Sidebar Closed State (Mobile) */
        .sidebar.closed {
            transform: translateX(-100%);
        }
        
        /* Sidebar Open State */
        .sidebar.open {
            transform: translateX(0);
        }
        
        /* Sidebar Overlay for Mobile */
        .sidebar-overlay {
            position: fixed;
            top: var(--header-height);
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 1019;
            display: none;
            transition: all 0.3s ease;
        }
        
        .sidebar-overlay.show {
            display: block;
        }
        
        /* Sidebar Navigation */
        .sidebar .nav {
            padding: 10px 0;
        }
        
        .sidebar .nav-link {
            color: rgba(255,255,255,0.85);
            padding: 12px 20px;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 0.95rem;
            border-left: 3px solid transparent;
        }
        
        .sidebar .nav-link i {
            width: 24px;
            text-align: center;
            font-size: 1.1rem;
        }
        
        .sidebar .nav-link:hover {
            background: rgba(255,255,255,0.1);
            color: white;
            border-left-color: var(--sidebar-hover);
            padding-left: 25px;
        }
        
        .sidebar .nav-link.active {
            background: var(--sidebar-hover);
            color: white;
            border-left-color: white;
        }
        
        /* Sidebar Subtitle */
        .sidebar-title {
            color: rgba(255,255,255,0.5);
            font-size: 0.7rem;
            text-transform: uppercase;
            letter-spacing: 1px;
            padding: 15px 20px 5px;
        }
        
        /* Main Content */
        .main-content {
            margin-left: var(--sidebar-width);
            padding: calc(var(--header-height) + 20px) 20px 20px;
            transition: margin-left 0.3s ease;
            min-height: 100vh;
        }
        
        /* Desktop View */
        @media (min-width: 992px) {
            .sidebar.closed {
                transform: translateX(0);
                width: var(--sidebar-collapsed-width);
            }
            
            .sidebar.closed .nav-link span {
                display: none;
            }
            
            .sidebar.closed .nav-link i {
                width: 100%;
                text-align: center;
                font-size: 1.3rem;
            }
            
            .sidebar.closed .sidebar-title {
                text-align: center;
                font-size: 0.6rem;
                padding: 15px 5px;
            }
            
            .sidebar.closed .nav-link {
                justify-content: center;
                padding: 15px 0;
            }
            
            .sidebar.closed:hover {
                width: var(--sidebar-width);
            }
            
            .sidebar.closed:hover .nav-link span {
                display: inline;
            }
            
            .sidebar.closed:hover .nav-link {
                justify-content: flex-start;
                padding: 12px 20px;
            }
            
            .sidebar.closed:hover .sidebar-title {
                text-align: left;
                padding: 15px 20px 5px;
                font-size: 0.7rem;
            }
            
            .main-content.sidebar-collapsed {
                margin-left: var(--sidebar-collapsed-width);
            }
        }
        
        /* Mobile View */
        @media (max-width: 991.98px) {
            .main-content {
                margin-left: 0;
                padding-top: calc(var(--header-height) + 15px);
                padding-left: 15px;
                padding-right: 15px;
            }
            
            .sidebar {
                width: 280px;
            }
            
            .menu-toggle {
                display: block;
            }
        }
        
        /* Desktop Menu Toggle */
        @media (min-width: 992px) {
            .menu-toggle {
                display: block;
                margin-right: 15px;
            }
        }
        
        /* User Info Dropdown */
        .user-dropdown .dropdown-menu {
            border: none;
            border-radius: 10px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.15);
            margin-top: 10px;
        }
        
        .user-dropdown .dropdown-item {
            padding: 10px 20px;
            transition: all 0.3s ease;
        }
        
        .user-dropdown .dropdown-item:hover {
            background: var(--sidebar-hover);
            color: white;
            transform: translateX(5px);
        }
        
        .user-avatar {
            width: 35px;
            height: 35px;
            border-radius: 50%;
            object-fit: cover;
            border: 2px solid rgba(255,255,255,0.3);
        }
        
        /* Scrollbar Styling */
        .sidebar::-webkit-scrollbar {
            width: 5px;
        }
        
        .sidebar::-webkit-scrollbar-track {
            background: rgba(255,255,255,0.1);
        }
        
        .sidebar::-webkit-scrollbar-thumb {
            background: rgba(255,255,255,0.3);
            border-radius: 5px;
        }
        
        /* Card Styles */
        .card {
            border: none;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }
        
        /* Responsive Table */
        .table-responsive {
            overflow-x: auto;
        }
        
        /* Loading Spinner */
        .spinner-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 9999;
            display: none;
            justify-content: center;
            align-items: center;
        }
        
        .spinner-overlay.show {
            display: flex;
        }
        
        .spinner-border-custom {
            width: 3rem;
            height: 3rem;
            border: 0.25rem solid #f3f3f3;
            border-top: 0.25rem solid var(--sidebar-hover);
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        /* Footer */
        .admin-footer {
            background: white;
            padding: 15px 0;
            margin-top: 30px;
            text-align: center;
            border-top: 1px solid #e0e0e0;
            font-size: 0.85rem;
            color: #6c757d;
        }
    </style>
</head>
<body>

<!-- Loading Spinner -->
<div class="spinner-overlay" id="loadingSpinner">
    <div class="spinner-border-custom"></div>
</div>

<!-- Navbar -->
<nav class="navbar navbar-dark">
    <div class="container-fluid">
        <div class="d-flex align-items-center">
            <button class="menu-toggle" id="menuToggle">
                <i class="fas fa-bars"></i>
            </button>
            <a class="navbar-brand ms-2" href="dashboard.php">
                <i class="fas fa-shield-alt me-2"></i>BMYA Admin
            </a>
        </div>
        <div class="d-flex align-items-center">
            <span class="navbar-text me-3 d-none d-sm-inline">
                <i class="fas fa-user me-1"></i> <?php echo $_SESSION['admin_email'] ?? 'Admin'; ?>
            </span>
            <div class="dropdown user-dropdown">
                <a href="#" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" data-bs-toggle="dropdown">
                    <i class="fas fa-user-circle fa-2x"></i>
                </a>
                <ul class="dropdown-menu dropdown-menu-end">
                    <li><a class="dropdown-item" href="profile.php">
                        <i class="fas fa-user me-2"></i> My Profile
                    </a></li>
                    <li><a class="dropdown-item" href="settings.php">
                        <i class="fas fa-cog me-2"></i> Settings
                    </a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item text-danger" href="logout.php">
                        <i class="fas fa-sign-out-alt me-2"></i> Logout
                    </a></li>
                </ul>
            </div>
        </div>
    </div>
</nav>

<!-- Sidebar Overlay (Mobile) -->
<div class="sidebar-overlay" id="sidebarOverlay"></div>

<!-- Sidebar -->
<div class="sidebar" id="sidebar">
    <div class="sidebar-title">MAIN NAVIGATION</div>
    <ul class="nav flex-column">
        <li class="nav-item">
            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'dashboard.php' ? 'active' : ''; ?>" href="dashboard.php">
                <i class="fas fa-tachometer-alt"></i> <span>Dashboard</span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'members.php' ? 'active' : ''; ?>" href="members.php">
                <i class="fas fa-users"></i> <span>Members</span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'membership_types.php' ? 'active' : ''; ?>" href="membership_types.php">
                <i class="fas fa-tags"></i> <span>Membership Types</span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'renewals.php' ? 'active' : ''; ?>" href="renewals.php">
                <i class="fas fa-sync-alt"></i> <span>Renewals</span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'events.php' ? 'active' : ''; ?>" href="events.php">
                <i class="fas fa-calendar-alt"></i> <span>Events</span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'reports.php' ? 'active' : ''; ?>" href="reports.php">
                <i class="fas fa-chart-bar"></i> <span>Reports</span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'settings.php' ? 'active' : ''; ?>" href="settings.php">
                <i class="fas fa-cog"></i> <span>Settings</span>
            </a>
        </li>
    </ul>
    <div class="sidebar-title mt-3">ACCOUNT</div>
    <ul class="nav flex-column">
        <li class="nav-item">
            <a class="nav-link" href="profile.php">
                <i class="fas fa-user-circle"></i> <span>My Profile</span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link text-danger" href="logout.php">
                <i class="fas fa-sign-out-alt"></i> <span>Logout</span>
            </a>
        </li>
    </ul>
</div>

<!-- Main Content -->
<main class="main-content" id="mainContent">

<script>
// Mobile Responsive Sidebar JavaScript
document.addEventListener('DOMContentLoaded', function() {
    const sidebar = document.getElementById('sidebar');
    const menuToggle = document.getElementById('menuToggle');
    const sidebarOverlay = document.getElementById('sidebarOverlay');
    const mainContent = document.getElementById('mainContent');
    
    // Check localStorage for sidebar state (desktop only)
    let isSidebarCollapsed = false;
    const isMobile = window.innerWidth < 992;
    
    if (!isMobile && localStorage.getItem('sidebarCollapsed') === 'true') {
        isSidebarCollapsed = true;
        sidebar.classList.add('closed');
        mainContent.classList.add('sidebar-collapsed');
    }
    
    // Toggle Sidebar Function
    function toggleSidebar() {
        if (window.innerWidth < 992) {
            // Mobile: Open/Close with transform
            sidebar.classList.toggle('open');
            sidebar.classList.toggle('closed');
            sidebarOverlay.classList.toggle('show');
            
            if (sidebar.classList.contains('open')) {
                document.body.style.overflow = 'hidden';
            } else {
                document.body.style.overflow = '';
            }
        } else {
            // Desktop: Collapse/Expand
            isSidebarCollapsed = !isSidebarCollapsed;
            sidebar.classList.toggle('closed', isSidebarCollapsed);
            mainContent.classList.toggle('sidebar-collapsed', isSidebarCollapsed);
            localStorage.setItem('sidebarCollapsed', isSidebarCollapsed);
        }
    }
    
    // Close Sidebar Function (Mobile only)
    function closeSidebar() {
        if (window.innerWidth < 992) {
            sidebar.classList.remove('open');
            sidebar.classList.add('closed');
            sidebarOverlay.classList.remove('show');
            document.body.style.overflow = '';
        }
    }
    
    // Event Listeners
    if (menuToggle) {
        menuToggle.addEventListener('click', toggleSidebar);
    }
    
    if (sidebarOverlay) {
        sidebarOverlay.addEventListener('click', closeSidebar);
    }
    
    // Close sidebar when clicking on nav links (mobile only)
    const navLinks = document.querySelectorAll('.sidebar .nav-link');
    navLinks.forEach(link => {
        link.addEventListener('click', function() {
            if (window.innerWidth < 992) {
                setTimeout(closeSidebar, 150);
            }
        });
    });
    
    // Handle window resize
    window.addEventListener('resize', function() {
        if (window.innerWidth >= 992) {
            // Desktop view
            sidebarOverlay.classList.remove('show');
            sidebar.classList.remove('open');
            document.body.style.overflow = '';
            
            // Apply collapsed state from localStorage
            if (localStorage.getItem('sidebarCollapsed') === 'true') {
                sidebar.classList.add('closed');
                mainContent.classList.add('sidebar-collapsed');
            } else {
                sidebar.classList.remove('closed');
                mainContent.classList.remove('sidebar-collapsed');
            }
        } else {
            // Mobile view
            sidebar.classList.add('closed');
            sidebar.classList.remove('open');
            mainContent.classList.remove('sidebar-collapsed');
        }
    });
    
    // Active link highlighting
    const currentUrl = window.location.pathname.split('/').pop();
    navLinks.forEach(link => {
        const href = link.getAttribute('href');
        if (href === currentUrl) {
            link.classList.add('active');
        }
    });
    
    // Show notification function
    window.showNotification = function(message, type = 'success') {
        const alertDiv = document.createElement('div');
        alertDiv.className = `alert alert-${type} alert-dismissible fade show position-fixed top-0 end-0 m-3`;
        alertDiv.style.zIndex = '9999';
        alertDiv.style.minWidth = '300px';
        alertDiv.innerHTML = `
            <i class="fas fa-${type === 'success' ? 'check-circle' : 'exclamation-triangle'} me-2"></i>
            ${message}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        `;
        document.body.appendChild(alertDiv);
        setTimeout(() => alertDiv.remove(), 5000);
    };
});
</script>