<?php
// header.php — unified site header for public and admin sections.
// Reads: $isAdmin (bool), $currentNav (string, public only)
$_isAdmin      = !empty($isAdmin);
$_navCurrent   = $currentNav ?? '';
$_currentPage  = basename($_SERVER['PHP_SELF']);
$_thesisId     = $_GET['id'] ?? null;
?>
<header>
    <a href="#main-content" class="skip-link">Aller au contenu principal</a>

    <?php if ($_isAdmin): ?>

    <nav aria-label="Navigation admin">
        <ul class="nav-left-links">
            <li><a href="/" target="_blank" rel="noopener noreferrer" class="nav-logo">
                <svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 256 256"><path d="M208,72H128V32a8,8,0,0,0-13.66-5.66l-96,96a8,8,0,0,0,0,11.32l96,96A8,8,0,0,0,128,224V184h80a16,16,0,0,0,16-16V88A16,16,0,0,0,208,72Zm0,96H120a8,8,0,0,0-8,8v28.69L35.31,128,112,51.31V80a8,8,0,0,0,8,8h88Z"></path></svg>XAMXAM<span class="sr-only"> (site public, nouvel onglet)</span>
            </a></li>
        </ul>
        <ul class="nav-right-links">
            <li><a href="/admin/" <?= $_currentPage === 'index.php' ? 'aria-current="page"' : '' ?>>Liste des TFE</a></li>
            <li><a href="/admin/contenus.php" <?= in_array($_currentPage, ['contenus.php', 'contenus-edit.php']) ? 'aria-current="page"' : '' ?>>Contenus</a></li>
            <li><a href="/admin/acces.php" <?= in_array($_currentPage, ['acces.php', 'file-access.php', 'acces-etudiante.php']) ? 'aria-current="page"' : '' ?>>
                Accès
                <?php if (isset($pendingCount) && $pendingCount > 0): ?>
                    <span class="admin-nav-badge"><?= $pendingCount ?></span>
                <?php endif; ?>
            </a></li>
            <li><a href="/admin/parametres.php" <?= in_array($_currentPage, ['parametres.php', 'system.php', 'status.php', 'logs.php']) ? 'aria-current="page"' : '' ?>>Paramètres</a></li>
            <?php if ($_thesisId && $_currentPage === 'recapitulatif.php'): ?>
            <li><a href="/admin/edit.php?id=<?= intval($_thesisId) ?>">Modifier</a></li>
            <?php endif; ?>
            <?php if ($_isAdmin && AdminAuth::hasPassword()): ?>
            <li data-nav-logout><a href="/admin/logout.php" aria-label="Déconnexion"><svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" viewBox="0 0 256 256"><path d="M120,216a8,8,0,0,1-8,8H48a8,8,0,0,1-8-8V40a8,8,0,0,1,8-8h64a8,8,0,0,1,0,16H56V208h56A8,8,0,0,1,120,216Zm109.66-93.66-40-40a8,8,0,0,0-11.32,11.32L204.69,120H112a8,8,0,0,0,0,16h92.69l-26.35,26.34a8,8,0,0,0,11.32,11.32l40-40A8,8,0,0,0,229.66,122.34Z"></path></svg><span class="sr-only">Déconnexion</span></a></li>
            <?php endif; ?>
        </ul>
    </nav>

    <?php else: ?>

    <input class="menu-btn" type="checkbox" id="menu-btn" name="menu-btn" />
    <nav aria-label="Navigation principale">
        <div class="nav-top-row">
            <ul class="nav-left-links">
                <li><a href="/" class="nav-logo">Xamxam</a></li>
                <li>
                    <a href="/repertoire"
                       <?= ($_navCurrent === 'repertoire') ? 'aria-current="page"' : '' ?>>Répertoire</a>
                </li>
            </ul>
            <ul class="nav-right-links">
                <li>
                    <a href="/licence"
                       <?= ($_navCurrent === 'licence') ? 'aria-current="page"' : '' ?>>Licences</a>
                </li>
                <li>
                    <a href="/apropos"
                       <?= ($_navCurrent === 'apropos') ? 'aria-current="page"' : '' ?>>À Propos</a>
                </li>
            </ul>
            <label class="menu-icon" for="menu-btn">
                <span class="navicon" aria-label="Menu de navigation"></span>
            </label>
        </div>
        <ul class="nav-mobile-links">
            <li>
                <a href="/repertoire"
                   <?= ($_navCurrent === 'repertoire') ? 'aria-current="page"' : '' ?>>Répertoire</a>
            </li>
            <li>
                <a href="/licence"
                   <?= ($_navCurrent === 'licence') ? 'aria-current="page"' : '' ?>>Licences</a>
            </li>
            <li>
                <a href="/apropos"
                   <?= ($_navCurrent === 'apropos') ? 'aria-current="page"' : '' ?>>À Propos</a>
            </li>
        </ul>
    </nav>

    <?php endif; ?>

</header>

<?php if ($_isAdmin): ?>
<div class="admin-mobile-block">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" aria-hidden="true"><rect width="256" height="256" fill="none"/><rect x="24" y="56" width="208" height="144" rx="16" fill="none" stroke="currentColor" stroke-width="16" stroke-linecap="round" stroke-linejoin="round"/><line x1="24" y1="168" x2="104" y2="88" fill="none" stroke="currentColor" stroke-width="16" stroke-linecap="round" stroke-linejoin="round"/><line x1="152" y1="168" x2="232" y2="88" fill="none" stroke="currentColor" stroke-width="16" stroke-linecap="round" stroke-linejoin="round"/></svg>
    <h2>Section administrateur</h2>
    <p>L'administration n'est pas accessible sur mobile. Veuillez utiliser un ordinateur.</p>
</div>
<?php endif; ?>

<?php if (!$_isAdmin): ?>
<?php
// Search bar — public section only (rendered below header for equal height)
$searchBarValue = $searchBarValue ?? $_GET['query'] ?? '';
?>
<div class="header-search-wrap">
    <form method="GET" action="/search"
          role="search" aria-label="Recherche" class="header-search-form">
        <label for="site-search-input" class="sr-only">Recherche</label>
        <div class="header-search-input-wrap">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
                 fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
                 aria-hidden="true" focusable="false" class="header-search-icon">
                <circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/>
            </svg>
            <input
                id="site-search-input"
                type="text"
                name="query"
                placeholder="Recherche..."
                value="<?= htmlspecialchars($searchBarValue) ?>"
                autocomplete="off"
            >
        </div>
    </form>
</div>
<?php endif; ?>
