<?php
$current_page = basename($_SERVER['PHP_SELF']);

// Fetch System Version
$sys_ver = '1.0.0';
try {
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'system_version'");
    $stmt->execute();
    if ($v = $stmt->fetchColumn()) {
        $sys_ver = $v;
    }
} catch (Exception $e) {
    // table might not exist yet
}
?>
<!-- FontAwesome for Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

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

<aside class="sidebar" id="sidebar">
    <div class="sidebar-header">
        <div class="brand">
            <i class="fas fa-graduation-cap"></i>
            <span>Admin Portal</span>
        </div>
        <button class="close-sidebar-btn" id="closeSidebarBtn">
            <i class="fas fa-times"></i>
        </button>
    </div>
    
    <nav class="menu">
        <div class="menu-label">Main</div>
        <a href="../admin/dashboard.php" class="<?php echo $current_page == 'dashboard.php' ? 'active' : ''; ?>">
            <i class="fas fa-th-large"></i>
            <span>Dashboard</span>
        </a>
        
        <?php if ($user->hasPermission('manage_settings') || $user->hasPermission('manage_questions')): ?>
        <div class="menu-label">Academic</div>
        <?php endif; ?>

        <?php if ($user->hasPermission('manage_settings')): ?>
        <a href="../admin/classes.php" class="<?php echo $current_page == 'classes.php' ? 'active' : ''; ?>">
            <i class="fas fa-chalkboard"></i>
            <span>Classes</span>
        </a>
        <a href="../admin/subjects.php" class="<?php echo $current_page == 'subjects.php' ? 'active' : ''; ?>">
            <i class="fas fa-book"></i>
            <span>Subjects</span>
        </a>
        <?php endif; ?>

        <?php if ($user->hasPermission('manage_questions')): ?>
        <a href="../admin/questions.php" class="<?php echo $current_page == 'questions.php' ? 'active' : ''; ?>">
            <i class="fas fa-question-circle"></i>
            <span>Question Bank</span>
        </a>
        <?php endif; ?>
        
        <?php if ($user->hasPermission('manage_exams') || $user->hasPermission('manage_students') || $user->hasPermission('manage_teachers')): ?>
        <div class="menu-label">Management</div>
        <?php endif; ?>

        <?php if ($user->hasPermission('manage_exams')): ?>
        <a href="../admin/exams.php" class="<?php echo $current_page == 'exams.php' ? 'active' : ''; ?>">
            <i class="fas fa-clock"></i>
            <span>Exam Control</span>
        </a>
        <?php endif; ?>

        <?php if ($user->hasPermission('manage_students') || $user->hasPermission('manage_teachers')): ?>
        <a href="../admin/users.php" class="<?php echo $current_page == 'users.php' ? 'active' : ''; ?>">
            <i class="fas fa-users"></i>
            <span>User Management</span>
        </a>
        <?php endif; ?>

        <?php if ($user->hasPermission('manage_results')): ?>
        <div class="menu-label">Reports</div>
        <a href="../admin/results.php" class="<?php echo $current_page == 'results.php' ? 'active' : ''; ?>">
            <i class="fas fa-chart-bar"></i>
            <span>Result Portal</span>
        </a>
        <a href="../admin/broadsheet.php" class="<?php echo $current_page == 'broadsheet.php' ? 'active' : ''; ?>">
            <i class="fas fa-table"></i>
            <span>Broadsheet</span>
        </a>
        <?php endif; ?>

        <div class="menu-label">System</div>
        
        <?php if ($user->hasPermission('manage_settings')): ?>
        <a href="../admin/settings.php" class="<?php echo $current_page == 'settings.php' ? 'active' : ''; ?>">
            <i class="fas fa-cogs"></i>
            <span>Settings</span>
        </a>
        <?php endif; ?>

        <a href="../logout.php" class="logout-link">
            <i class="fas fa-sign-out-alt"></i>
            <span>Logout</span>
        </a>
    </nav>
    
    <div style="padding: 15px; text-align: center; color: #6c757d; font-size: 0.8rem; border-top: 1px solid rgba(255,255,255,0.1);">
        v<?php echo htmlspecialchars($sys_ver); ?>
    </div>
</aside>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const sidebar = document.getElementById('sidebar');
        const overlay = document.getElementById('sidebarOverlay');
        const closeBtn = document.getElementById('closeSidebarBtn');
        const toggleBtn = document.getElementById('sidebarToggle'); // This is in page_header.php

        // Function to close sidebar
        function closeSidebar() {
            if (window.innerWidth <= 768) {
                sidebar.classList.remove('active');
                overlay.classList.remove('active');
            }
        }

        // Close button inside sidebar
        if (closeBtn) {
            closeBtn.addEventListener('click', closeSidebar);
        }

        // Overlay click to close
        if (overlay) {
            overlay.addEventListener('click', closeSidebar);
        }

        // Sync overlay with toggle button
        if (toggleBtn) {
            toggleBtn.addEventListener('click', function() {
                if (window.innerWidth <= 768) {
                    // We need to wait slightly for the class to be toggled by the other script, 
                    // or just toggle it here too. 
                    // Since we can't guarantee order, let's check the state after a microtask.
                    setTimeout(() => {
                        if (sidebar.classList.contains('active')) {
                            overlay.classList.add('active');
                        } else {
                            overlay.classList.remove('active');
                        }
                    }, 10);
                }
            });
        }
        
        // Handle window resize
        window.addEventListener('resize', function() {
            if (window.innerWidth > 768) {
                overlay.classList.remove('active');
            }
        });
    });
</script>
