<?php
/**
 * Menu latéral — Admin (arborescent 3 niveaux)
 */
$_sidebarSchoolLogo = null;
$_sidebarSchoolName = null;
if (!empty($_SESSION['school_id']) && isset($pdo)) {
    $_ss = $pdo->prepare("SELECT name, logo FROM schools WHERE id = ?");
    $_ss->execute([$_SESSION['school_id']]);
    $_sRow = $_ss->fetch();
    if ($_sRow) {
        $_sidebarSchoolName = $_sRow['name'];
        $_sidebarSchoolLogo = !empty($_sRow['logo'])
            ? BASE_URL . 'uploads/logos/' . rawurlencode($_sRow['logo'])
            : null;
    }
}

function _mi(string $f, string $i, string $l): string {
    $a = basename($_SERVER['PHP_SELF']) === $f ? 'active' : '';
    return "<li><a href=\"$f\" class=\"$a\"><i class=\"fas fa-$i\"></i> $l</a></li>";
}
function _mp(string $l, string $i, string $c): string {
    $o = (strpos($c, 'active') !== false) ? ' open' : '';
    return "<li class=\"menu-parent$o\">
        <a onclick=\"this.parentElement.classList.toggle('open')\">
            <i class=\"fas fa-$i\"></i> $l
            <i class=\"fas fa-chevron-right sub-toggle\"></i>
        </a>
        <ul class=\"sub-menu\">$c</ul>
    </li>";
}
?>
<aside class="sidebar">
    <div class="sidebar-brand">
        <?php if ($_sidebarSchoolLogo): ?>
            <img src="<?= htmlspecialchars($_sidebarSchoolLogo, ENT_QUOTES,'UTF-8') ?>"
                 alt="Logo" class="sidebar-school-logo">
        <?php else: ?>
            <i class="fas fa-school"></i>
        <?php endif; ?>
        <span><?= htmlspecialchars($_sidebarSchoolName ?? 'Nukunu Mofiala', ENT_QUOTES,'UTF-8') ?></span>
    </div>

    <ul class="sidebar-menu">
        <?php
        // ── Tableau de bord ──
        $tdb = '';
        $tdb .= _mi('academic_years.php', 'calendar', 'Années scolaires');
        $tdb .= _mi('grading_periods.php', 'calendar-alt', 'Périodes saisie');
        if (hasRole(['super_admin','school_admin']))
            $tdb .= _mi('class_types.php', 'layer-group', 'Niveaux classes');
        echo _mp('Tableau de bord', 'tachometer-alt', $tdb);

        // ── Établissement ──
        $etab = '';
        if (isSuperAdmin())
            $etab .= _mi('schools.php', 'school', 'Écoles');
        if (hasRole(['super_admin','school_admin']))
            $etab .= _mi('directors.php', 'user-tie', 'Directeurs');
        $etab .= _mi('teachers.php', 'chalkboard-teacher', 'Enseignants');
        if (hasRole(['super_admin','school_admin']))
            $etab .= _mi('class_teachers.php', 'user-graduate', 'Titulaires');
        $etab .= _mi('students.php', 'user-graduate', 'Élèves');
        if (hasRole(['super_admin','school_admin']))
            $etab .= _mi('parent_accounts.php', 'user-friends', 'Comptes parents');
        if (isSuperAdmin())
            $etab .= _mi('classes.php', 'users', 'Classes');
        echo _mp('Établissement', 'university', $etab);

        // ── Pédagogie ──
        $peda = '';
        if (hasRole(['super_admin','school_admin'])) {
            $peda .= _mi('council_thresholds.php', 'sliders-h', 'Seuils Conseil');
            $peda .= _mi('promotion.php', 'arrows-alt', 'Reclassement');
            $peda .= _mi('baccalaureat.php', 'user-graduate', 'Baccalauréat & sortants');
            $peda .= _mi('attestation.php', 'certificate', 'Attestation scolaire');
        }
        if ($peda) echo _mp('Pédagogie', 'book-open', $peda);

        // ── Matières (2 sous-niveaux) ──
        if (!hasRole('accountant')) {
            $matieres = '';
            $matieres .= _mi('subjects.php', 'cog', 'Gestion matières & affectations');

            $saisie = '';
            $saisie .= _mi('grade_saisie.php', 'pen-alt', 'Saisie complète');
            $saisie .= _mi('grade_entry.php', 'edit', 'Saisie par classe');
            $saisie .= _mi('grade_verification.php', 'check-double', 'Vérification notes');
            $saisie .= _mi('class_stats.php', 'chart-bar', 'Stats classe');

            $bulletins = '';
            $bulletins .= _mi('report_templates.php', 'cog', 'Modèles bulletins');
            $bulletins .= _mi('bulletin_templates.php', 'file-pdf', 'Fonds PDF');
            $bulletins .= _mi('import_bulletins.php', 'file-upload', 'Import bulletins');
            $bulletins .= _mi('duplicata.php', 'copy', 'Duplicata');

            $matieres .= _mp('Saisie des notes', 'pen-alt', $saisie);
            $matieres .= _mp('Bulletins', 'file-alt', $bulletins);
            echo _mp('Matières', 'book', $matieres);
        }

        // ── Cartes d'identité ──
        if (hasRole(['super_admin','school_admin','teacher'])) {
            $cartes = '';
            $cartes .= _mi('id_cards.php?type=students', 'user-graduate', 'Cartes élèves');
            $cartes .= _mi('id_cards.php?type=teachers', 'chalkboard-teacher', 'Cartes enseignants');
            $cartes .= _mi('user_photos.php', 'camera', 'Photos passeport');
            $cartes .= _mi('import_idcards.php', 'file-upload', 'Import cartes');
            echo _mp('Cartes d\'identité', 'id-card', $cartes);
        }

        // ── Administration ──
        if (hasRole(['super_admin','school_admin','accountant'])) {
            $fact = '';
            if (!hasRole('accountant'))
                $fact .= _mi('payments.php', 'list', 'Paiements');
            if (hasRole(['super_admin','accountant']))
                $fact .= _mi('receipts.php', 'receipt', 'Reçus');
            $fact .= _mi('expenses.php', 'shopping-cart', 'Dépenses');
            $fact .= _mi('payment_history.php', 'history', 'Historique');

            $admin = '';
            $admin .= _mp('Facturation', 'money-bill-wave', $fact);
            $admin .= _mi('fees.php', 'graduation-cap', 'Scolarité');
            echo _mp('Administration', 'shield-alt', $admin);
        }

        // ── Recettes ──
        if (hasRole(['super_admin','school_admin','accountant']))
            echo _mi('recettes.php', 'chart-line', 'Recettes');

        // ── Paramètres ──
        $params = '';
        if (hasRole(['super_admin','school_admin']))
            $params .= _mi('users.php', 'user-shield', 'Utilisateurs');
        if (hasRole('super_admin')) {
            $params .= _mi('credentials.php', 'key', 'Mots de passe');
            $params .= _mi('permissions.php', 'shield-alt', 'Droits & Rôles');
            $params .= _mi('settings.php', 'cogs', 'Paramètres');
        }
        if ($params) echo _mp('Paramètres', 'cogs', $params);
        ?>
    </ul>
</aside>
<?php renderBackToTop(); ?>
