<?php
// Filename: menu.php

if (session_status() === PHP_SESSION_NONE) session_start();

// 1. SMART PATH CORRECTION (FIXED)
// We check the URL structure instead of file existence.
// This prevents 404 errors if the system is hosted in a subfolder.
$base = '';
$path = $_SERVER['PHP_SELF']; // Gets the current path, e.g., /sas/booking/index.php

if (strpos($path, '/booking/admin/') !== false) {
    $base = '../../'; // Deep inside booking admin
} elseif (strpos($path, '/booking/') !== false) {
    $base = '../';    // Inside booking folder
}
// Default remains '' (root)

// 2. CURRENT PAGE & SETTINGS
$cur_page = basename($_SERVER['PHP_SELF']);
$sys_mode = 'live'; 

// 3. DETECT USER ROLES
$my_roles = explode(',', $_SESSION['role'] ?? '');
$is_akp   = (strpos($_SESSION['role'] ?? '', 'akp') !== false);
$is_form6 = (strpos($_SESSION['role'] ?? '', 'form6_teacher') !== false);

// 4. ADMIN PAGES LIST
$adminFiles = [
    'admin_dashboard.php', 'admin_teachers.php', 'admin_students.php',
    'admin_classes.php', 'admin_timetable.php', 'admin_announcements.php',
    'admin_settings.php', 'admin_system.php', 'student_reports_admin.php',
    'admin_quotes.php', 'reports.php', 'admin_feedback.php',
    'admin_qrcodes.php', 'admin_holidays.php', 'absent_management.php',
    'admin_subjects.php', 'admin_discipline.php', 'admin_relief.php',
    'manage.php' 
];
$isAdminPage = in_array($cur_page, $adminFiles);
?>

<style>
    /* GLOBAL MENU CSS */
    .bottom-nav { 
        position: fixed; bottom: 0; left: 0; right: 0; 
        background: rgba(255, 255, 255, 0.95); 
        backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); 
        padding: 12px 25px 14px 30px; 
        display: flex; justify-content: space-between; align-items: flex-start;
        border-radius: 24px 24px 0 0; 
        box-shadow: 0 -5px 20px rgba(0,0,0,0.05); 
        z-index: 1050; 
        border-top: 1px solid rgba(0,0,0,0.05); 
    }
    
    .nav-btn { 
        display: flex; flex-direction: column; align-items: center; 
        color: #94a3b8; text-decoration: none; 
        font-size: 0.65rem; font-weight: 600; 
        transition: color 0.2s; background:none; border:none; padding: 0;
        width: 60px; 
        z-index: 1;
    }
    
    .nav-btn.active { color: #8B5CF6; }
    .nav-btn i { font-size: 1.4rem; margin-bottom: 4px; }
    
    /* Center Scan Button (Lifted Effect) */
    .nav-btn-scan-wrapper {
        position: relative;
        display: flex;
        justify-content: center;
        align-items: center;
        flex: 0 0 auto;
    }

    .nav-btn-scan {
        background: linear-gradient(135deg, #6366f1 0%, #8B5CF6 100%);
        width: 60px; height: 60px; 
        border-radius: 50%; 
        display: flex; align-items: center; justify-content: center; 
        color: white; 
        margin-top: -35px; 
        border: 5px solid white; 
        box-shadow: 0 -5px 15px rgba(99, 102, 241, 0.25);
        transition: transform 0.2s;
        flex-shrink: 0;
        z-index: 10;
    }
    
    .nav-btn-scan:active { transform: scale(0.95); }
    .nav-btn-scan i { font-size: 1.5rem; margin: 0; }

    .modal { z-index: 1060; }
    .modal-backdrop { z-index: 1055; }
</style>

<?php if ( ($cur_page == 'dashboard.php' || $cur_page == 'form6_dashboard.php') && isset($classes) ): ?>
<div class="modal fade" id="agendaModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
        <div class="modal-content border-0 shadow-lg rounded-4">
            <div class="modal-header bg-white border-0 pb-0">
                <div><h5 class="modal-title fw-bold text-dark">Today's Agenda</h5><p class="text-muted small mb-0"><?= date('l, d F Y') ?></p></div>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-3">
                <?php if(empty($classes)): ?>
                    <div class="text-center py-5"><i class="bi bi-journal-x display-1 text-muted opacity-25"></i><p class="text-muted mt-2 fw-bold">No classes scheduled.</p></div>
                <?php else: 
                    $now_t = date('H:i:s');
                    $up_c = [];
                    foreach($classes as $c) { if ($now_t <= $c['end_time']) $up_c[] = $c; }
                ?>
                    <div class="d-flex flex-column gap-3 mb-3">
                        <?php foreach($up_c as $slot): 
                            $is_act = ($now_t >= $slot['start_time'] && $now_t <= $slot['end_time']);
                            $c_bg = 'bg-white shadow-sm border-start border-4 border-primary';
                            $badge = $is_act ? '<span class="badge bg-danger rounded-pill mb-2">NOW</span>' : '<span class="badge bg-warning text-dark rounded-pill mb-2">UPCOMING</span>';
                            // Link logic for scanner
                            $lnk = ($sys_mode == 'live') ? $base."scanner_view.php" : $base."class_session.php?timetable_id=".$slot['id'];
                            $btn = $is_act ? '<a href="'.$lnk.'" class="btn btn-primary w-100 rounded-pill btn-sm fw-bold mt-2"><i class="bi bi-qr-code-scan me-1"></i> Scan</a>' : '';
                        ?>
                        <div class="card <?= $c_bg ?> rounded-3 border-0 overflow-hidden"><div class="card-body p-3"><div class="d-flex justify-content-between align-items-start"><div><?= $badge ?><h6 class="fw-bold mb-0 text-dark"><?= htmlspecialchars($slot['subject']) ?></h6><div class="small text-muted"><?= htmlspecialchars($slot['class_name']) ?></div></div><div class="text-end"><div class="fw-bold text-dark"><?= substr($slot['start_time'], 0, 5) ?></div><div class="small text-muted"><?= substr($slot['end_time'], 0, 5) ?></div></div></div><?= $btn ?></div></div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>


<nav class="bottom-nav">
    
    <?php if ($isAdminPage): ?>
        <a href="<?= $base ?>admin_dashboard.php" class="nav-btn active">
            <i class="bi bi-speedometer2"></i>
            <span>Admin</span>
        </a>

        <a href="<?= $base ?>dashboard.php" class="nav-btn">
            <i class="bi bi-phone"></i>
            <span>Home View</span>
        </a>

        <a href="<?= ($sys_mode == 'live') ? $base.'scanner_view.php' : $base.'class_session.php' ?>" class="nav-btn-scan-wrapper">
            <div class="nav-btn-scan"><i class="bi bi-qr-code-scan"></i></div>
        </a>

        <a href="<?= $base ?>profile.php" class="nav-btn <?= $cur_page == 'profile.php' ? 'active' : '' ?>">
            <i class="bi bi-person-circle"></i>
            <span>Profile</span>
        </a>

        <a href="<?= $base ?>logout.php" class="nav-btn text-danger" onclick="return confirm('Logout?');">
            <i class="bi bi-box-arrow-right"></i>
            <span>Logout</span>
        </a>

    <?php elseif ($is_akp): ?>
        <a href="<?= $base ?>akp_dashboard.php" class="nav-btn <?= ($cur_page == 'akp_dashboard.php') ? 'active' : '' ?>">
            <i class="bi bi-grid-fill"></i>
            <span>Home</span>
        </a>

        <a href="<?= $base ?>calendar.php" class="nav-btn <?= ($cur_page == 'calendar.php') ? 'active' : '' ?>">
            <i class="bi bi-calendar-event"></i>
            <span>Events</span>
        </a>

        <a href="<?= $base ?>public_directory.php" class="nav-btn <?= ($cur_page == 'public_directory.php') ? 'active' : '' ?>">
            <i class="bi bi-journal-album"></i>
            <span>Staff</span>
        </a>

        <a href="<?= $base ?>profile.php" class="nav-btn <?= ($cur_page == 'profile.php') ? 'active' : '' ?>">
            <i class="bi bi-person-circle"></i>
            <span>Profile</span>
        </a>

    <?php elseif ($is_form6): ?>
        <a href="<?= $base ?>form6_dashboard.php" class="nav-btn <?= ($cur_page == 'form6_dashboard.php' && !isset($_GET['open_agenda'])) ? 'active' : '' ?>">
            <i class="bi bi-grid-fill"></i>
            <span>Home</span>
        </a>

        <?php 
            // Form 6 Agenda Link Logic
            $f6_ag_href = ($cur_page == 'form6_dashboard.php') ? '#' : $base . 'form6_dashboard.php?open_agenda=1';
            $f6_ag_attr = ($cur_page == 'form6_dashboard.php') ? 'data-bs-toggle="modal" data-bs-target="#agendaModal"' : '';
        ?>
        <a href="<?= $f6_ag_href ?>" <?= $f6_ag_attr ?> class="nav-btn <?= (isset($_GET['open_agenda'])) ? 'active' : '' ?>">
            <i class="bi bi-calendar2-check-fill"></i>
            <span>Agenda</span>
        </a>

        <a href="<?= ($sys_mode == 'live') ? $base.'scanner_view.php' : $base.'class_session.php' ?>" class="nav-btn-scan-wrapper">
            <div class="nav-btn-scan"><i class="bi bi-qr-code-scan"></i></div>
        </a>

        <a href="<?= $base ?>teacher_students.php" class="nav-btn <?= ($cur_page == 'teacher_students.php') ? 'active' : '' ?>">
            <i class="bi bi-people-fill"></i>
            <span>Students</span>
        </a>

        <a href="<?= $base ?>logout.php" class="nav-btn text-danger" onclick="return confirm('Are you sure you want to logout?');">
            <i class="bi bi-box-arrow-right"></i>
            <span>Logout</span>
        </a>

    <?php else: ?>
        <a href="<?= $base ?>dashboard.php" class="nav-btn <?= ($cur_page == 'dashboard.php' && !isset($_GET['open_agenda'])) ? 'active' : '' ?>">
            <i class="bi bi-grid-fill"></i>
            <span>Home</span>
        </a>
        
        <?php 
            // Standard Teacher Agenda Logic
            $ag_href = ($cur_page == 'dashboard.php') ? '#' : $base . 'dashboard.php?open_agenda=1';
            $ag_attr = ($cur_page == 'dashboard.php') ? 'data-bs-toggle="modal" data-bs-target="#agendaModal"' : '';
        ?>
        <a href="<?= $ag_href ?>" <?= $ag_attr ?> class="nav-btn <?= (isset($_GET['open_agenda'])) ? 'active' : '' ?>">
            <i class="bi bi-calendar2-check-fill"></i>
            <span>Agenda</span>
        </a>

        <a href="<?= ($sys_mode == 'live') ? $base.'scanner_view.php' : $base.'class_session.php' ?>" class="nav-btn-scan-wrapper">
            <div class="nav-btn-scan"><i class="bi bi-qr-code-scan"></i></div>
        </a>

        <a href="<?= $base ?>booking/index.php" class="nav-btn <?= (strpos($_SERVER['REQUEST_URI'], 'booking/') !== false) ? 'active' : '' ?>">
            <i class="bi bi-calendar-plus"></i>
            <span>Booking</span>
        </a>
        
        <a href="<?= $base ?>logout.php" class="nav-btn text-danger" onclick="return confirm('Are you sure you want to logout?');">
            <i class="bi bi-box-arrow-right"></i>
            <span>Logout</span>
        </a>

    <?php endif; ?>
</nav>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const urlParams = new URLSearchParams(window.location.search);
    if (urlParams.has('open_agenda')) {
        // Clean URL
        const newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname;
        window.history.replaceState({path: newUrl}, '', newUrl);
        
        // Open Modal
        const agModal = document.getElementById('agendaModal');
        if(agModal && typeof bootstrap !== 'undefined') {
            new bootstrap.Modal(agModal).show();
        }
    }
});
</script>