<!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="mt-2">
        <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; ?>

        <?php
        $subnets = Subnet::getAll();
        if ($subnets):
        ?>
        <div class="nav-section mt-2">Subnets</div>
        <?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 ?>">
                <span class="nav-icon"><i class="bi bi-hdd-network"></i></span><?= h($s['name']) ?><?= $badge ?>
            </a>
        <?php endforeach; ?>
        <?php endif; ?>

        <div class="nav-section mt-2">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>

<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">
