<?php
/**
 * Menu latéral pour le portail élèves / parents
 */
$_portalSchoolLogo = null;
$_portalSchoolName = null;
if (!empty($_SESSION['school_id']) && isset($pdo)) {
    $_ps = $pdo->prepare("SELECT name, logo FROM schools WHERE id = ?");
    $_ps->execute([$_SESSION['school_id']]);
    $_psRow = $_ps->fetch();
    if ($_psRow) {
        $_portalSchoolName = $_psRow['name'];
        $_portalSchoolLogo = !empty($_psRow['logo'])
            ? BASE_URL . 'uploads/logos/' . rawurlencode($_psRow['logo'])
            : null;
    }
}
$_portalRole     = $_SESSION['user_role'] ?? '';
$_portalCurrent  = basename($_SERVER['PHP_SELF']);
?>
<aside class="sidebar">
    <button class="menu-toggle portal-toggle" aria-label="Menu" style="position:fixed;top:.75rem;left:0;right:auto;background:#2d3748;color:white;border:none;width:40px;height:40px;border-radius:0 8px 8px 0;cursor:pointer;z-index:1001;font-size:1.2rem;">
        <i class="fas fa-bars"></i>
    </button>
    <div class="sidebar-brand">
        <?php if ($_portalSchoolLogo): ?>
            <img src="<?= htmlspecialchars($_portalSchoolLogo, ENT_QUOTES, 'UTF-8') ?>"
                 alt="Logo école" class="sidebar-school-logo">
        <?php else: ?>
            <i class="fas fa-graduation-cap"></i>
        <?php endif; ?>
        <span><?= htmlspecialchars($_portalSchoolName ?? 'Mon Espace', ENT_QUOTES, 'UTF-8') ?></span>
    </div>

    <ul class="sidebar-menu">
        <li>
            <a href="<?= BASE_URL ?>portal/dashboard.php"
               class="<?= $_portalCurrent === 'dashboard.php' ? 'active' : '' ?>">
                <i class="fas fa-tachometer-alt"></i> Tableau de bord
            </a>
        </li>
        <li>
            <a href="<?= BASE_URL ?>portal/grades.php"
               class="<?= $_portalCurrent === 'grades.php' ? 'active' : '' ?>">
                <i class="fas fa-star-half-alt"></i> Mes notes
            </a>
        </li>
        <li>
            <a href="<?= BASE_URL ?>portal/payments.php"
               class="<?= $_portalCurrent === 'payments.php' ? 'active' : '' ?>">
                <i class="fas fa-money-bill-wave"></i> Mes paiements
            </a>
        </li>
        <?php if ($_portalRole === 'parent'): ?>
        <li>
            <a href="<?= BASE_URL ?>portal/children.php"
               class="<?= $_portalCurrent === 'children.php' ? 'active' : '' ?>">
                <i class="fas fa-child"></i> Mes enfants
            </a>
        </li>
        <?php endif; ?>
        <li>
            <a href="<?= BASE_URL ?>portal/profile.php"
               class="<?= $_portalCurrent === 'profile.php' ? 'active' : '' ?>">
                <i class="fas fa-user-circle"></i> Mon profil
            </a>
        </li>
    </ul>

    <div class="sidebar-footer">
        <div class="sidebar-user">
            <div class="sidebar-user-avatar">
                <?= userPhotoImg($_SESSION['user_photo'] ?? null, $_SESSION['user_name'] ?? 'U') ?>
            </div>
            <div class="sidebar-user-info">
                <span class="sidebar-user-name"><?= htmlspecialchars($_SESSION['user_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></span>
                <span class="sidebar-user-role"><?= htmlspecialchars(getRoleLabel($_SESSION['user_role'] ?? ''), ENT_QUOTES, 'UTF-8') ?></span>
            </div>
        </div>
        <a href="<?= BASE_URL ?>auth/logout.php" class="sidebar-logout" title="Se déconnecter">
            <i class="fas fa-sign-out-alt"></i>
            <span>Déconnexion</span>
        </a>
    </div>
</aside>
<script src="<?= defined('BASE_URL') ? BASE_URL : '../' ?>assets/js/main.js"></script>
<script>
(function(){
    var toggle = document.querySelector('.menu-toggle');
    var sidebar = document.querySelector('.sidebar');
    if (toggle && sidebar) {
        function placeToggle() {
            if (window.innerWidth <= 768) {
                toggle.style.left = sidebar.classList.contains('active') ? '260px' : '0px';
            } else {
                toggle.style.left = '0px';
            }
        }
        toggle.addEventListener('click', function(e) {
            e.stopPropagation();
            sidebar.classList.toggle('active');
            placeToggle();
        });
        document.addEventListener('click', function(e) {
            if (sidebar.classList.contains('active') && !sidebar.contains(e.target) && !toggle.contains(e.target)) {
                sidebar.classList.remove('active');
                placeToggle();
            }
        });
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape' && sidebar.classList.contains('active')) {
                sidebar.classList.remove('active');
                placeToggle();
            }
        });
        window.addEventListener('resize', function() {
            if (window.innerWidth > 768) sidebar.classList.remove('active');
            placeToggle();
        });
        placeToggle();
    }
})();
</script>
