<?php
// Get and normalize the role
$role = strtolower(session()->get('role') ?? 'guest');
?>

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
        aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav me-auto">

            <!-- Administrator Menus -->
            <?php if ($role === 'administrator'): ?>
                <!-- User Management -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="userManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Users
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="<?= base_url('notifications/active') ?>">Active Notifications</a>
                        <a class="dropdown-item" href="<?= base_url('notifications/deleted') ?>">Deleted Notifications</a>
                        <a class="dropdown-item" href="/user/login_activity">Login Activity</a>
                        <a class="dropdown-item" href="/user/user_list">User List</a>
                    </div>
                </li>

                <!-- Roles -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="roleManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Roles
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/rolepermission/assign_role">Assign User Role</a>
                        <a class="dropdown-item" href="/rolepermission/roles">Role Management</a>
                    </div>
                </li>

                <!-- Configuration -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="configManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Configuration
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/configuration/configuration_view">Add/Edit Configuration</a>
                    </div>
                </li>
                <!-- Staff Management Dropdown -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="parentsManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Staffing
                    </a>
                    <div class="dropdown-menu" aria-labelledby="parentsManagementDropdown">
                        <a class="dropdown-item" href="/staff/index">Staff Profile</a>
                        <a class="dropdown-item" href="/administrator/teacher_class_assignment">Teacher Class Assignment</a>
                    </div>
                </li>
                <!-- Parents Management Dropdown -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="parentsManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Parents
                    </a>
                    <div class="dropdown-menu" aria-labelledby="parentsManagementDropdown">
                        <a class="dropdown-item" href="/administrator/parent_profiles">Parent Profile</a>

                    </div>
                </li>
                <!-- Students -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="studentManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Student-Affairs
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/administrator/daily_attendance">Attendance Management</a>
                        <a class="dropdown-item" href="<?= base_url('attendance/violations') ?>">Attendance Tracking System</a>
                        <a class="dropdown-item" href="/rfid_coming_soon">Attendance Scans</a>
                        <a class="dropdown-item" href="/administrator/class_assignment">Classes List</a>
                        <a class="dropdown-item" href="/administrator/emergency_contact">Emergency Contact</a>
                        <a class="dropdown-item" href="/enroll_withdraw/enrollment_withdrawal">Enrollment-Withdrawal</a>
                        <!--a class="dropdown-item" href="/administrator/exam_management">Exams Management</a-->
                        <a class="dropdown-item" href="/flags/flags_management">Flags Management</a>
                        <a class="dropdown-item" href="/administrator/calendar_view">School Calendar</a>
                        <a class="dropdown-item" href="/report/combined">Score Analysis</a>
                        <a class="dropdown-item" href="/grading">Score Management</a>
                        <a class="dropdown-item" href="/administrator/student_class_assignment">Student Class Assignment</a>
                        <a class="dropdown-item" href="/administrator/student_profiles">Student Profile</a>
                    </div>
                </li>

                <!-- Classes -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="classesManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Classes
                    </a>
                    <div class="dropdown-menu">
                <a class="dropdown-item" href="/administrator/class_assignment">Classes List</a>
                <a class="dropdown-item" href="/administrator/subject-curriculum">Subject Curriculum</a>
                <a class="dropdown-item" href="/administrator/trophy">
                    <i class="bi bi-trophy-fill text-warning me-1"></i>Trophy Awards
                </a>
            </div>
        </li>

                <!-- Communication -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="communicationToolsDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Communication
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/emails/parent_email_extractor">Parent Email Extractor</a>
                        <a class="dropdown-item" href="/administrator/parent_profiles">Parent Profile</a>
                        <a class="dropdown-item" href="/administrator/student_profiles">Student Profile</a>
                    </div>
                </li>

                <!-- Financial -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="financialManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Financial
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/discounts/list">Discount Management</a>
                        <a class="dropdown-item" href="/expenses/index">Expenses Management</a>
                        <a class="dropdown-item" href="/payment/financial_report">Financial Report</a>
                        <a class="dropdown-item" href="/administrator/tuition-forecast">Tuition Forecast</a>
                        <a class="dropdown-item" href="/invoice_payment/invoice_management">Invoices Management</a>
                        <a class="dropdown-item" href="/payment/manual_pay">Manual Payment</a>
                        <a class="dropdown-item" href="/refunds/list">Refund Management</a>
                        <a class="dropdown-item" href="/reimbursements/index">Reimbursement Management</a>
                        <a class="dropdown-item" href="/payment/notification_management">Payment Notification Management</a>
                    </div>
                </li>
                <!-- Analytics -->
                <li class="nav-item dropdown">
                    <!--a class="nav-link dropdown-toggle" href="#" id="reportsAnalyticsDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Analytics
                    </a-->
                    <div class="dropdown-menu">
                        <!-- Placeholder -->
                    </div>
                </li>

                <!-- Printables -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="printablesReportsDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Printables
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/printables_reports/badge_form">Badges</a>
                        <a class="dropdown-item" href="<?= site_url('class-prep') ?>">Class Prep</a>
                        <!--a class="dropdown-item" href="/printables_reports/certificates">Certificates</a-->
                        <a class="dropdown-item" href="/printables_reports/report_card">Report Cards</a>
                        <a class="dropdown-item" href="/printables_reports/stickers">Stickers</a>
                    </div>
                </li>

                <!-- Extra -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="extracurricularActivitiesDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Event Management
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="<?= site_url('administrator/calendar') ?>"> Calendar</a>
                        <a class="dropdown-item" href="<?= site_url('administrator/events') ?>">Events</a>
                    </div>
                </li>

                <!-- Principal -->
            <?php elseif ($role === 'principal' || $role === 'vice_principal'): ?>
                <!-- Staff Management Dropdown -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="parentsManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Staffing
                    </a>
                    <div class="dropdown-menu" aria-labelledby="parentsManagementDropdown">
                        <a class="dropdown-item" href="/staff/index">Staff Profile</a>
                        <a class="dropdown-item" href="/administrator/teacher_class_assignment">Teacher Class Assignment</a>
                    </div>
                </li>
                <!-- Parents Management Dropdown -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="parentsManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Parents
                    </a>
                    <div class="dropdown-menu" aria-labelledby="parentsManagementDropdown">
                        <a class="dropdown-item" href="/administrator/parent_profiles">Parent Profile</a>
                    </div>
                </li>
                <!-- Students -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="studentManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Student-Affairs
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/administrator/daily_attendance">Attendance Management</a>
                        <!--a class="dropdown-item" href="/rfid_coming_soon">Attendance Scans</a-->
                        <a class="dropdown-item" href="/administrator/class_assignment">Classes List</a>
                        <a class="dropdown-item" href="/administrator/emergency_contact">Emergency Contact</a>
                        <a class="dropdown-item" href="/enroll_withdraw/enrollment_withdrawal">Enrollment-Withdrawal</a>
                        <!--a class="dropdown-item" href="/administrator/exam_management">Exams Management</a-->
                        <a class="dropdown-item" href="/flags/flags_management">Flags Management</a>
                        <a class="dropdown-item" href="/administrator/calendar_view">School Calendar</a>
                        <a class="dropdown-item" href="/report/combined">Score Analysis</a>
                        <a class="dropdown-item" href="/grading">Score Management</a>
                        <a class="dropdown-item" href="/administrator/student_class_assignment">Student Class Assignment</a>
                        <a class="dropdown-item" href="/administrator/student_profiles">Student Profile</a>
                    </div>
                </li>
                <!-- Financial -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="financialManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Financial
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/discounts/list">Discount Management</a>
                        <a class="dropdown-item" href="/expenses/index">Expenses Management</a>
                        <a class="dropdown-item" href="/payment/financial_report">Financial Report</a>
                        <a class="dropdown-item" href="/administrator/tuition-forecast">Tuition Forecast</a>
                        <a class="dropdown-item" href="/invoice_payment/invoice_management">Invoices Management</a>
                        <a class="dropdown-item" href="/payment/manual_pay">Manual Payment</a>
                        <a class="dropdown-item" href="/refunds/list">Refund Management</a>
                        <a class="dropdown-item" href="/reimbursements/index">Reimbursement Management</a>
                    </div>
                </li>
                <!-- Printables -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="printablesReportsDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Printables
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/printables_reports/badge_form">Badges</a>
                        <a class="dropdown-item" href="<?= site_url('class-prep') ?>">Class Prep</a>
                        <!--a class="dropdown-item" href="/printables_reports/certificates">Certificates</a-->
                        <a class="dropdown-item" href="/printables_reports/report_card">Report Cards</a>
                        <!--a class="dropdown-item" href="/printables_reports/stickers">Stickers</a-->
                    </div>
                </li>
                <!-- Extra -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="extracurricularActivitiesDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Event Management
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="<?= site_url('administrator/events') ?>">Events</a>
                    </div>
                </li>

                <!--admin-->
            <?php elseif ($role === 'admin'): ?>
                <li class="nav-item"><a class="nav-link" href="/enroll_withdraw/enrollment_withdrawal">Enrollment</a></li>
                <li class="nav-item"><a class="nav-link" href="/invoice_payment/invoice_management">Invoice Management</a></li>

                <!--head of department (communication)-->
            <?php elseif ($role === 'head of department (communication)'): ?>
                <!-- Communication -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="communicationToolsDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Communication
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/administrator/parent_profiles">Parent Profile</a>
                        <a class="dropdown-item" href="/administrator/student_profiles">Student Profile</a>
                    </div>
                </li>

                <!--head of department (information technology-->
            <?php elseif ($role === 'head of department (information technology)'): ?>
                <li class="nav-item"><a class="nav-link" href="/grading">IT Dept Grades</a></li>

                <!--head of department (education)-->
            <?php elseif ($role === 'head of department (education)'): ?>
                <!-- Students -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="studentManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Student-Affairs
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/administrator/daily_attendance">Attendance Management</a>
                        <!--a class="dropdown-item" href="/rfid_coming_soon">Attendance Scans</a-->
                        <a class="dropdown-item" href="/administrator/class_assignment">Classes List</a>
                        <a class="dropdown-item" href="/administrator/emergency_contact">Emergency Contact</a>
                        <a class="dropdown-item" href="/enroll_withdraw/enrollment_withdrawal">Enrollment-Withdrawal</a>
                        <!--a class="dropdown-item" href="/administrator/exam_management">Exams Management</a-->
                        <a class="dropdown-item" href="/flags/flags_management">Flags Management</a>
                        <a class="dropdown-item" href="/administrator/calendar_view">School Calendar</a>
                        <a class="dropdown-item" href="/report/combined">Score Analysis</a>
                        <a class="dropdown-item" href="/grading">Score Management</a>
                        <a class="dropdown-item" href="/administrator/student_class_assignment">Student Class Assignment</a>
                        <a class="dropdown-item" href="/administrator/student_profiles">Student Profile</a>
                    </div>
                </li>

                <!--head of department (finance)-->
            <?php elseif ($role === 'head of department (finance)'): ?>
                <!-- Financial -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="financialManagementDropdown" role="button"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Financial
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="/expenses/index">Expenses Management</a>
                        <a class="dropdown-item" href="/payment/financial_report">Financial Report</a>
                        <a class="dropdown-item" href="/administrator/tuition-forecast">Tuition Forecast</a>
                        <a class="dropdown-item" href="/invoice_payment/invoice_management">Invoices Management</a>
                        <a class="dropdown-item" href="/payment/manual_pay">Manual Payment</a>
                        <a class="dropdown-item" href="/refunds/list">Refund Management</a>
                        <a class="dropdown-item" href="/reimbursements/index">Reimbursement Management</a>
                    </div>
                </li>

            <?php endif; ?>
            <?php
                // Back office: combine role switch + style under one dropdown
                $allRoles = array_map(fn($r)=> strtolower(trim((string)$r)), (array) (session()->get('roles') ?? []));
                $current  = strtolower((string) (session()->get('role') ?? 'guest'));
                $other    = array_values(array_filter($allRoles, fn($r) => $r !== '' && $r !== $current));

                $styleCfg = config('Style'); 
                $sess = session();
                $currentAccent = $sess->get('style_color') ?? ($styleCfg->defaultStyle ?? 'blue');
                $currentMenu   = $sess->get('menu_color')  ?? ($styleCfg->defaultMenu  ?? 'white');
            ?>
            <li class="nav-item dropdown">
                
                <div class="dropdown-menu dropdown-menu-end quick-menu" aria-labelledby="quickMenuBack" style="max-height: 70vh; overflow-y: auto; overflow-x: hidden;">
                    <?php if (!empty($other)): ?>
                        <h6 class="dropdown-header">Switch Role</h6>
                        <?php foreach ($other as $r): ?>
                            <form method="post" action="<?= base_url('set-role') ?>" class="px-2">
                                <?= csrf_field() ?>
                                <input type="hidden" name="selected_role" value="<?= esc($r) ?>">
                                <button type="submit" class="dropdown-item">Switch to <?= esc(ucfirst(str_replace('_', ' ', $r))) ?></button>
                            </form>
                        <?php endforeach; ?>
                        <div class="dropdown-divider"></div>
                    <?php endif; ?>

                    <h6 class="dropdown-header">Style</h6>
                    <div class="px-2 small text-muted">Accent</div>
                    <?php foreach (array_keys($styleCfg->stylePalettes ?? []) as $opt): ?>
                        <?php $isActive = ($opt === $currentAccent); ?>
                        <a class="dropdown-item <?= $isActive ? 'active' : '' ?>" href="<?= site_url('ui/style?accent=' . urlencode($opt)) ?>" <?= $isActive ? 'aria-current="true"' : '' ?>>
                            <?= ucfirst(esc($opt)) ?><?= $isActive ? ' <i class=\"bi bi-check-lg ms-2\"></i>' : '' ?>
                        </a>
                    <?php endforeach; ?>
                    <div class="dropdown-divider"></div>
                    <div class="px-2 small text-muted">Menu</div>
                    <?php foreach (array_keys($styleCfg->menuPalettes ?? []) as $opt): ?>
                        <?php $isActive = ($opt === $currentMenu); ?>
                        <a class="dropdown-item <?= $isActive ? 'active' : '' ?>" href="<?= site_url('ui/style?menu=' . urlencode($opt)) ?>" <?= $isActive ? 'aria-current="true"' : '' ?>>
                            <?= ucfirst(esc($opt)) ?><?= $isActive ? ' <i class=\"bi bi-check-lg ms-2\"></i>' : '' ?>
                        </a>
                    <?php endforeach; ?>
                </div>
            </li>

            <div class="d-flex">
                <button class="btn btn-outline-light logout-btn ms-lg-3" onclick="window.location.href='<?= base_url('/logout'); ?>'">
                    Log Out
                </button>
            </div>
        </ul>
    </div>
</nav>


<!-- Admins Management Dropdown -->
<!--li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="adminsManagementDropdown" role="button"
                    data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Admins
                </a>
                <div class="dropdown-menu" aria-labelledby="adminsManagementDropdown">
                    <Additional admin management options can be added here 
                    
                    >
                </div>
            </li-->

<!-- Teacher Management Dropdown -->
<!--li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="teacherManagementDropdown" role="button"
                    data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Teachers
                </a>
                <div class="dropdown-menu" aria-labelledby="teacherManagementDropdown">
                    < Add teacher management options here >
                </div>
            </li-->

<!-- Parents Management Dropdown -->
<!--li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="parentsManagementDropdown" role="button"
                    data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Parents
                </a>
                <div class="dropdown-menu" aria-labelledby="parentsManagementDropdown">
                    < Add parents management options here >
                </div>
            </li-->
