<?php
declare(strict_types=1);
$flash = $_SESSION["flash"] ?? null;
if ($flash !== null) {
    unset($_SESSION["flash"]);
}
$scriptName = $_SERVER["SCRIPT_NAME"] ?? "";
$normalizedScript = str_replace("\\", "/", $scriptName);
$isHomePage = preg_match("#/PIS/index\\.php$#i", $normalizedScript) === 1;
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pole Informatique et Services</title>
    <link rel="stylesheet" href="/PIS/assets/css/style.css">
</head>
<body class="<?= $isHomePage ? "home-page" : "" ?>">
<div class="app-layout">
<aside class="sidebar">
    <div class="brand">
        <h1>PÔLE INFORMATIQUE ET SERVICES</h1>
        <p class="subtitle">Gestion de centre de formation</p>
    </div>
    <nav class="nav-links">
        <a href="/PIS/index.php">Accueil</a>
        <a href="/PIS/stagiaires/index.php">Apprenants</a>
        <a href="/PIS/formateurs/index.php">Formateurs</a>
        <a href="/PIS/formations/index.php">Formations</a>
        <a href="/PIS/inscriptions/index.php">Inscriptions</a>
        <?php if (hasRole(["admin", "secretaire"])): ?>
            <a href="/PIS/attestations/index.php">Attestations</a>
        <?php endif; ?>
        <a href="/PIS/attestations/list.php">Stockage</a>
        <?php if (($_SESSION["user_role"] ?? "") === "admin"): ?>
            <a href="/PIS/utilisateurs.php">Utilisateurs</a>
        <?php endif; ?>
        <a href="/PIS/logout.php">Deconnexion</a>
    </nav>
</aside>
<div class="sidebar-overlay" id="sidebarOverlay"></div>
<div class="content-area">
<header class="topbar">
    <?php if ($isHomePage): ?>
        <button type="button" class="menu-toggle" id="menuToggle" aria-label="Ouvrir le menu">☰</button>
    <?php else: ?>
        <span class="topbar-spacer" aria-hidden="true"></span>
    <?php endif; ?>
    <h1 class="topbar-title">
        <span class="topbar-brand">
            <img class="topbar-brand-logo" src="/PIS/assets/images/pis_logo.png" alt="Logo PIS">
            <span>PÔLE INFORMATIQUE ET SERVICES</span>
        </span>
    </h1>
    <div class="user-pill">
        <div class="user-top">
            <a class="profile-inline" href="/PIS/profil.php"><?= htmlspecialchars($_SESSION["user_nom"] ?? "Utilisateur") ?></a>
        </div>
        <small><?= htmlspecialchars($_SESSION["user_role"] ?? "utilisateur") ?></small>
    </div>
</header>
<main class="container">
    <?php if ($flash !== null): ?>
        <div class="message <?= $flash["type"] === "error" ? "error" : "success" ?>">
            <?= htmlspecialchars((string) $flash["message"]) ?>
        </div>
    <?php endif; ?>
