<?php
// Layout commun du back-office. Usage :
//   $page_title = '...'; $active='medias'; require '_layout.php'; (ouvre)
//   ... contenu ...
//   layout_end();  (ferme)
require_once dirname(__DIR__) . '/inc/auth.php';
auth_require();

function layout_start(string $title, string $active = ''): void {
    $nom = $_SESSION['nom'] ?? $_SESSION['login'] ?? '';
    $items = [
        'principal' => '— Principal',
        'dashboard' => ['Tableau de bord', 'dashboard.php'],
        'medias'    => ['Médiathèque', 'medias.php'],
        'contenus'  => '— Contenus',
        'pages'     => ['Pages du site', 'pages.php'],
        'temoignages'=> ['Témoignages', 'temoignages.php'],
        'attente'   => ['Témoignages à modérer', 'moderation.php'],
        'messages'  => ['Messages reçus', 'messages.php'],
        'articles'  => ['Articles', 'articles.php'],
        'etudes'    => ['Études de cas', 'etudes.php'],
        'categories'=> ['Catégories', 'categories.php'],
        'visuel'    => '— Visuel',
        'galeries'  => ['Galeries', 'galeries.php'],
        'slides'    => ['Sliders', 'slides.php'],
        'config'    => '— Configuration',
        'reglages'  => ['Réglages e-mail', 'reglages.php'],
    ];
    echo '<!DOCTYPE html><html lang="fr"><head><meta charset="UTF-8">';
    echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
    echo '<title>' . e($title) . ' — Admin ' . SITE_NAME . '</title>';
    echo '<link rel="stylesheet" href="admin.css"></head><body><div class="layout">';
    echo '<aside class="sidebar"><div class="brand"><b>' . SITE_NAME . '</b><span>Administration</span></div><nav>';
    foreach ($items as $key => $it) {
        if (is_string($it)) { echo '<div class="grp">' . e(ltrim($it,'— ')) . '</div>'; continue; }
        $cls = ($active === $key) ? ' class="active"' : '';
        echo '<a href="' . $it[1] . '"' . $cls . '>' . e($it[0]) . '</a>';
    }
    echo '</nav></aside><main class="main">';
    echo '<div class="topbar"><h1>' . e($title) . '</h1>';
    echo '<div class="user">' . e($nom) . ' · <a href="../index.php" target="_blank">Voir le site</a> · <a href="logout.php">Déconnexion</a></div></div>';
}

function layout_end(): void {
    echo '</main></div></body></html>';
}
