<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Current page
$current = basename($_SERVER['PHP_SELF']);
$currentPath = $_SERVER['PHP_SELF'];

// Role
$role = $_SESSION['role'] ?? 'guest';
?>

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<link rel="stylesheet"
href="/SEAT/assets/css/templates/layout.css">

<!-- OPTIONAL OVERLAY (for mobile UX) -->
<div class="sidebar-overlay" onclick="toggleSidebar()"></div>

<!-- SIDEBAR -->
<div class="sidebar" id="sidebar">

    <!-- ================= LOGO ================= -->
    <div class="sidebar-logo">

        <div class="logo-circle">
            <img src="/SEAT/assets/images/sidebarlogo2.png" alt="SEAT Logo">
        </div>

        <div>
            <b>SEAT</b><br>

            <small>
                <?php
                if($role == 'organizer') echo 'Organizer Panel';
                elseif($role == 'student') echo 'Student Panel';
                elseif($role == 'staff') echo 'Staff Panel';
                elseif($role == 'admin') echo 'Admin Panel';
                else echo 'User Panel';
                ?>
            </small>
        </div>

    </div>

    <!-- ================= MENU ================= -->
    <div class="menu">

        <div class="menu-title">MAIN</div>

        <!-- ORGANIZER -->
        <?php if($role == 'organizer'): ?>

            <a href="/SEAT/modules/User/organizer/dashboard.php"
               class="<?= $current=='dashboard.php'?'active':'' ?>">
                <i class="fa-solid fa-table"></i><span>Overview</span>
            </a>

            <a href="/SEAT/modules/Event/organizer/manage_events.php"
               class="<?= $current=='manage_events.php'?'active':'' ?>">
                <i class="fa-solid fa-calendar"></i><span>Event Management</span>
            </a>

            <a href="/SEAT/modules/Ticket/scanner.php"
               class="<?= $current=='scanner.php'?'active':'' ?>">
                <i class="fa-solid fa-qrcode"></i><span>QR Scanner</span>
            </a>

            <a href="/SEAT/modules/Analytics/analytics.php"
               class="<?= $current=='analytics.php'?'active':'' ?>">
                <i class="fa-solid fa-chart-line"></i><span>Analytics & Reports</span>
            </a>

        <!-- STUDENT / STAFF -->
        <?php elseif($role == 'student' || $role == 'staff'): ?>

            <a href="/SEAT/modules/User/participant/dashboard.php"
               class="<?= $current=='dashboard.php'?'active':'' ?>">
                <i class="fa-solid fa-user"></i><span>My Dashboard</span>
            </a>

            <a href="/SEAT/modules/Event/participant/browseEvent.php"
               class="<?= $current=='browseEvent.php'?'active':'' ?>">
                <i class="fa-solid fa-calendar"></i><span>Events</span>
            </a>

            <a href="/SEAT/modules/Ticket/MyTicket.php"
               class="<?= $current=='MyTicket.php'?'active':'' ?>">
                <i class="fa-solid fa-ticket"></i><span>My Tickets</span>
            </a>

            <a href="/SEAT/modules/Event/participant/announcement.php"
               class="<?= $current=='announcement.php'?'active':'' ?>">
                <i class="fa-solid fa-comments"></i><span>Announcement</span>
            </a>

        <!-- ADMIN -->
        <?php elseif($role == 'admin'): ?>

            <a href="/SEAT/modules/User/admin/dashboard.php"
               class="<?= strpos($currentPath,'dashboard.php')!==false ? 'active' : '' ?>">
                <i class="fa-solid fa-chart-line"></i><span>Dashboard</span>
            </a>

            <a href="/SEAT/modules/User/admin/userlist.php"
               class="<?= (strpos($currentPath,'userlist.php')!==false || strpos($currentPath,'create_subAdmin.php')!==false) ? 'active' : '' ?>">
                <i class="fa-solid fa-users"></i><span>User Management</span>
            </a>

            <a href="/SEAT/modules/User/admin/manage_organizer.php"
               class="<?= (strpos($currentPath,'manage_organizer.php')!==false || strpos($currentPath,'edit_bank.php')!==false || strpos($currentPath,'organizer_view.php')!==false) ? 'active' : '' ?>">
                <i class="fa-solid fa-layer-group"></i><span>Organizer Management</span>
            </a>

            <a href="/SEAT/modules/Event/admin/viewEvent.php"
               class="<?= strpos($currentPath,'/Event/admin/')!==false ? 'active' : '' ?>">
                <i class="fa-solid fa-calendar-days"></i><span>Events</span>
            </a>

        <?php endif; ?>

        <!-- SETTINGS -->
        <div class="menu-title">SETTINGS</div>

        <a href="/SEAT/modules/User/auth/change_password.php"
           class="<?= $current=='change_password.php'?'active':'' ?>">
            <i class="fa-solid fa-lock"></i><span>Change Password</span>
        </a>

    </div>
</div>


<script>
function toggleSidebar() {
    document.body.classList.toggle("sidebar-open");
}

/* Close sidebar when clicking overlay */
document.querySelector(".sidebar-overlay")?.addEventListener("click", () => {
    document.body.classList.remove("sidebar-open");
});

/* Close sidebar when clicking menu item on mobile */
document.querySelectorAll(".sidebar a").forEach(link => {
    link.addEventListener("click", () => {
        if (window.innerWidth <= 900) {
            document.body.classList.remove("sidebar-open");
        }
    });
});

/* Reset sidebar when resizing to desktop */
window.addEventListener("resize", () => {
    if (window.innerWidth > 900) {
        document.body.classList.remove("sidebar-open");
    }
});

/* Close sidebar when pressing ESC */
document.addEventListener("keydown", (e) => {
    if (e.key === "Escape") {
        document.body.classList.remove("sidebar-open");
    }
});
</script>