<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= h($page_title ?? 'DHCPMan') ?> — <?= h(Settings::get('app_name', 'DHCPMan')) ?></title>
    <link rel="stylesheet" href="assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/css/bootstrap-icons.min.css">
    <link rel="stylesheet" href="assets/css/app.css">
</head>
<body>

<?php
$current = basename($_SERVER['PHP_SELF']);
function navLink(string $href, string $icon, string $label, string $current): string {
    $file   = basename(parse_url($href, PHP_URL_PATH) ?? $href);
    $active = ($current === $file) ? ' active' : '';
    return sprintf(
        '<a href="%s" class="nav-link%s"><span class="nav-icon"><i class="bi bi-%s"></i></span>%s</a>',
        h($href), $active, h($icon), h($label)
    );
}
?>

<div id="sidebar">
    <a href="dashboard.php" class="sidebar-brand">
        <span class="brand-icon">&#127760;</span>
        <?= h(Settings::get('app_name', 'DHCPMan')) ?>
    </a>

    <div class="sidebar-nav">
        <div class="nav-section">Navigation</div>
        <?= navLink('dashboard.php',  'speedometer2',   'Tableau de bord', $current) ?>
        <?= navLink('subnets.php',    'diagram-3',      'Subnets',         $current) ?>
        <?= navLink('leases.php',     'clock-history',  'Leases',          $current) ?>
        <?= navLink('search.php',     'search',         'Recherche',       $current) ?>
        <?php if (in_array(Auth::currentUserRole(), ['admin', 'tech'])):
            $pendingCount = ReservationRequest::count();
        ?>
        <a href="requests.php" class="nav-link<?= $current === 'requests.php' ? ' active' : '' ?>">
            <span class="nav-icon"><i class="bi bi-envelope-plus"></i></span>Demandes<?php if ($pendingCount > 0): ?> <span class="badge bg-danger ms-1" style="font-size:.65rem"><?= $pendingCount ?></span><?php endif; ?>
        </a>
        <?php endif; ?>
    </div>

    <?php $subnets = Subnet::getAll(); ?>
    <div class="sidebar-subnets">
        <div class="nav-section">Subnets</div>
        <?php if ($subnets): ?>
        <div class="subnet-filter">
            <i class="bi bi-search"></i>
            <input type="text" id="subnet-filter-input" placeholder="Filtrer…" autocomplete="off">
            <button type="button" id="subnet-filter-clear" title="Effacer" hidden>&times;</button>
        </div>
        <div class="subnet-list" id="subnet-list">
            <?php foreach ($subnets as $s):
                $file   = 'subnet.php';
                $active = ($current === $file && ($_GET['id'] ?? '') == $s['id']) ? ' active' : '';
                $badge  = $s['is_active'] ? '' : ' <span class="badge bg-secondary ms-1" style="font-size:.6rem">off</span>';
            ?>
                <a href="subnet.php?id=<?= $s['id'] ?>" class="nav-link<?= $active ?>" data-subnet-name="<?= h(mb_strtolower($s['name'])) ?>">
                    <span class="nav-icon"><i class="bi bi-hdd-network"></i></span><span class="subnet-label"><?= h($s['name']) ?></span><?= $badge ?>
                </a>
            <?php endforeach; ?>
            <div class="subnet-empty text-muted small px-3 py-2" hidden>Aucun subnet.</div>
        </div>
        <?php endif; ?>
    </div>

    <div class="sidebar-admin">
        <div class="nav-section">Administration</div>
        <?php if (Auth::currentUserRole() === 'admin'): ?>
        <?= navLink('settings.php', 'gear', 'Paramètres', $current) ?>
        <?php endif; ?>
        <?= navLink('audit.php',    'clock-history', 'Journal', $current) ?>
    </div>

    <div class="sidebar-footer">
        <i class="bi bi-person-circle"></i>
        <?= h(Auth::currentUsername()) ?>
        <span class="badge bg-secondary ms-1" style="font-size:.65rem"><?= h(Auth::currentUserRole()) ?></span>
        <br>
        <a href="change_password.php"><i class="bi bi-key"></i> Mot de passe</a>
        &nbsp;·&nbsp;
        <a href="logout.php"><i class="bi bi-box-arrow-right"></i> Déconnexion</a>
    </div>
</div>

<script>
(function () {
    var input = document.getElementById('subnet-filter-input');
    if (!input) return;
    var list  = document.getElementById('subnet-list');
    var clear = document.getElementById('subnet-filter-clear');
    var empty = list ? list.querySelector('.subnet-empty') : null;
    var links = list ? list.querySelectorAll('a[data-subnet-name]') : [];

    function norm(s) {
        return s.toLowerCase().normalize('NFD').replace(/[̀-ͯ]/g, '');
    }
    function apply() {
        var q = norm(input.value.trim());
        var visible = 0;
        links.forEach(function (a) {
            var match = norm(a.getAttribute('data-subnet-name')).indexOf(q) !== -1;
            a.hidden = !match;
            if (match) visible++;
        });
        if (empty) empty.hidden = (visible !== 0);
        clear.hidden = (input.value === '');
    }
    input.addEventListener('input', apply);
    clear.addEventListener('click', function () { input.value = ''; apply(); input.focus(); });
}());
</script>

<div id="main-content">
    <div class="topbar">
        <div class="page-title"><?= h($page_title ?? '') ?></div>
        <div class="user-info">
            <i class="bi bi-person"></i> <?= h(Auth::currentUsername()) ?>
        </div>
    </div>
    <div class="page-content">
