<nav class="navbar navbar-expand-lg navbar-teacher">
    <div class="container-fluid px-2">
        <!-- Navbar brand -->
        <a class="navbar-brand text-white" href="<?= base_url('/teacher_dashboard'); ?>"
            data-bs-toggle="tooltip" data-bs-placement="bottom" title="Go to teacher dashboard">
            <i class="bi bi-house-door"></i> Dashboard
        </a>

        <!-- Hamburger menu for small screens -->
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#teacherNavbar"
            aria-controls="teacherNavbar" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <!-- Navigation links -->
        <div class="collapse navbar-collapse" id="teacherNavbar">
            <ul class="navbar-nav ms-auto flex-lg-row flex-column">
                <li class="nav-item mx-lg-2 my-1">
                    <a class="nav-link text-white" href="<?= base_url('/teacher/class_view'); ?>"
                        data-bs-toggle="tooltip" data-bs-placement="bottom" title="View your class list">
                        <i class="bi bi-pencil-square"></i> Class
                    </a>
                </li>

                <li class="nav-item mx-lg-2 my-1">
                    <?php $class_section_id = session()->get('class_section_id'); ?>
                    <a class="nav-link text-white"
                        href="<?= base_url('/teacher/showupdate_attendance?class_section_id=' . $class_section_id); ?>"
                        data-bs-toggle="tooltip" data-bs-placement="bottom" title="Mark or review attendance">
                        <i class="bi bi-calendar-check"></i> Attendance
                    </a>
                </li>

                <li class="nav-item mx-lg-2 my-1">
                    <a class="nav-link text-white" href="<?= base_url('/teacher/scores'); ?>" data-bs-toggle="tooltip"
                        data-bs-placement="bottom" title="Enter or view student scores">
                        <i class="bi bi-clipboard-data"></i> Scores
                    </a>
                </li>

                <li class="nav-item mx-lg-2 my-1">
                    <a class="nav-link text-white" href="<?= base_url('books/distribute'); ?>" data-bs-toggle="tooltip"
                        data-bs-placement="bottom" title="Enter number of given book to students">
                        <i class="bi bi-clipboard-data"></i> Books
                    </a>
                </li>

                <li class="nav-item mx-lg-2 my-1">
                    <a class="nav-link text-white" href="<?= base_url('/teacher/calendar'); ?>" data-bs-toggle="tooltip"
                        data-bs-placement="bottom" title="View class calendar">
                        <i class="bi bi-calendar"></i> Calendar
                    </a>
                </li>

                <li class="nav-item mx-lg-2 my-1">
                    <a class="nav-link text-white" href="<?= base_url('teacher/progress/submit'); ?>" data-bs-toggle="tooltip"
                        data-bs-placement="bottom" title="Submit weekly progress">
                        <i class="bi bi-clipboard-check"></i> Progress
                    </a>
                </li>

                <?php
                    $userId = session()->get('user_id');
                    $activeClassId = (int)(session()->get('class_section_id') ?? 0);
                    $classOptions = [];
                    if ($userId) {
                        $configModel = new \App\Models\ConfigurationModel();
                        $teacherClassModel = new \App\Models\TeacherClassModel();
                        try {
                            $sy = service('schoolYearContext')->resolve(service('request'))->yearName();
                        } catch (\Throwable $e) {
                            $sy = $configModel->getConfig('school_year');
                        }
                        $sem = $configModel->getConfig('semester');
                        $classOptions = $teacherClassModel->getClassAssignmentsByUserId((int)$userId, (string)$sy, (string)$sem);
                    }
                ?>
                <?php if (!empty($classOptions) && count($classOptions) > 1): ?>
                <li class="nav-item mx-lg-2 my-1">
                    <?php
                        $qs = $_SERVER['QUERY_STRING'] ?? '';
                        $redirectTo = current_url() . ($qs ? '?' . $qs : '');
                    ?>
                    <form class="d-flex align-items-center gap-2" method="post" action="<?= base_url('/teacher/switch-class'); ?>">
                        <?= csrf_field(); ?>
                        <input type="hidden" name="redirect_to" value="<?= esc($redirectTo) ?>">
                        <label for="teacherClassSwitch" class="text-white small mb-0">Class:</label>
                        <select name="class_section_id" id="teacherClassSwitch" class="form-select form-select-sm"
                                onchange="this.form.submit();">
                            <?php foreach ($classOptions as $opt): ?>
                                <?php $cid = (int)($opt['class_section_id'] ?? 0); ?>
                                <option value="<?= $cid ?>" <?= $cid === $activeClassId ? 'selected' : '' ?>>
                                    <?= esc($opt['class_section_name'] ?? 'Class'); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </form>
                </li>
                <?php endif; ?>
            </ul>
        </div>
    </div>
</nav>

<!-- Tooltip activation script (include once near bottom of page) -->
<script>
    document.addEventListener('DOMContentLoaded', function() {
        if (!window.bootstrap || !bootstrap.Tooltip) return;
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.forEach(function(tooltipTriggerEl) {
            bootstrap.Tooltip.getOrCreateInstance(tooltipTriggerEl);
        });
    });
</script>
