<?php
/**
 * Barre du haut — commune à toutes les pages admin
 * Usage : include '../includes/topbar.php';
 * Variable $pageTitle doit être définie avant l'include.
 */
?>
<div class="topbar">
    <button class="menu-toggle" aria-label="Menu">
        <i class="fas fa-bars"></i>
    </button>
    <div class="topbar-left">
        <h1 class="topbar-title"><?= $pageTitle ?? '' ?></h1>
<?php // Note: $pageTitle devrait utiliser e() pour l'échappement si des données utilisateur y sont insérées ?>
    </div>
    <div class="topbar-center">
        <?php if (!empty($_SESSION['school_id'])): ?>
        <span class="topbar-school">
            <i class="fas fa-school"></i>
            <?php
            $schoolName = '';
            if (!empty($_SESSION['school_id']) && isset($pdo)) {
                $snStmt = $pdo->prepare("SELECT name FROM schools WHERE id=?");
                $snStmt->execute([$_SESSION['school_id']]);
                $snRow = $snStmt->fetch();
                if ($snRow) $schoolName = $snRow['name'];
            }
            ?><?= htmlspecialchars($schoolName ?: 'Nukunu Mofiala', ENT_QUOTES, 'UTF-8') ?>
        </span>
        <?php endif; ?>
    </div>
    <div class="topbar-right">
        <div class="topbar-user">
            <div class="topbar-avatar">
                <?= userPhotoImg($_SESSION['user_photo'] ?? null, $_SESSION['user_name'] ?? 'U') ?>
            </div>
            <div class="topbar-user-info">
                <span class="topbar-user-name"><?= htmlspecialchars($_SESSION['user_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></span>
                <span class="topbar-user-role"><?= htmlspecialchars(getRoleLabel($_SESSION['user_role'] ?? ''), ENT_QUOTES, 'UTF-8') ?></span>
            </div>
        </div>
        <a href="<?= BASE_URL ?>auth/logout.php" class="topbar-logout" title="Se déconnecter">
            <i class="fas fa-sign-out-alt"></i>
            <span>Déconnexion</span>
        </a>
    </div>
</div>
