<?php

require_once __DIR__ . '/../lib/init.php';
Auth::requireLogin();

$id = (int)($_GET['id'] ?? 0);
$subnet = Subnet::getById($id);
if (!$subnet) {
    header('Location: dashboard.php');
    exit;
}

$reservations = Reservation::getBySubnet($subnet['kea_subnet_id']);
$relayMode    = Settings::get('relay_mode', '0') === '1';
$page_title   = h($subnet['name']) . ' — Réservations';
require_once APP_ROOT . '/lib/layout_header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <div>
        <h5 class="mb-0">
            <code><?= h($subnet['cidr']) ?></code>
            <?php if ($relayMode && $subnet['relay_address']): ?>
                <small class="text-muted ms-2">
                    <i class="bi bi-arrow-left-right"></i> Relay: <?= h($subnet['relay_address']) ?>
                </small>
            <?php elseif (!$relayMode && $subnet['interface']): ?>
                <small class="text-muted ms-2"><?= h($subnet['interface']) ?></small>
            <?php endif; ?>
        </h5>
        <?php if ($subnet['gateway']): ?>
        <small class="text-muted">
            GW : <?= h($subnet['gateway']) ?>
            <?php if ($subnet['dns_servers']): ?>
                &nbsp;· DNS : <?= h($subnet['dns_servers']) ?>
            <?php endif; ?>
        </small>
        <?php endif; ?>
    </div>
    <div class="d-flex gap-2 align-items-center">
        <?php if (Auth::currentUserRole() !== 'viewer'): ?>
        <a href="subnet_edit.php?id=<?= $subnet['id'] ?>" class="btn btn-outline-secondary" title="Modifier le subnet">
            <i class="bi bi-pencil"></i>
        </a>
        <a href="subnet_import.php?subnet_id=<?= $subnet['id'] ?>" class="btn btn-outline-secondary">
            <i class="bi bi-upload me-1"></i>Importer
        </a>
        <?php endif; ?>

        <div class="dropdown">
            <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
                <i class="bi bi-download me-1"></i>Exporter
            </button>
            <ul class="dropdown-menu dropdown-menu-end">
                <li>
                    <a class="dropdown-item" href="subnet_export.php?subnet_id=<?= $subnet['id'] ?>">
                        <i class="bi bi-filetype-csv me-2"></i>Exporter les réservations
                    </a>
                </li>
                <li>
                    <a class="dropdown-item" href="subnet_export.php?subnet_id=<?= $subnet['id'] ?>&template=1">
                        <i class="bi bi-file-earmark-spreadsheet me-2"></i>Télécharger le modèle vide
                    </a>
                </li>
            </ul>
        </div>

        <?php if (Auth::currentUserRole() !== 'viewer'): ?>
        <a href="reservation_add.php?subnet_id=<?= $subnet['id'] ?>" class="btn btn-primary">
            <i class="bi bi-plus-lg"></i> Ajouter une réservation
        </a>
        <?php endif; ?>
    </div>
</div>

<?php
$flash = $_SESSION['flash'] ?? null;
unset($_SESSION['flash']);
if ($flash):
?>
<div class="alert alert-<?= h($flash['type']) ?> alert-dismissible fade show" role="alert">
    <?= h($flash['message']) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center py-3">
        <span>
            <i class="bi bi-hdd-network me-2"></i>
            <?= count($reservations) ?> réservation<?= count($reservations) > 1 ? 's' : '' ?>
        </span>
        <div class="d-flex flex-column gap-1 text-end">
        <?php if ($subnet['has_dynamic_pool'] && $subnet['range_start']): ?>
        <small class="text-muted">
            Pool dynamique : <?= h($subnet['range_start']) ?> – <?= h($subnet['range_end']) ?>
        </small>
        <?php endif; ?>
        <?php if (!empty($subnet['short_lease_pool_start'])): ?>
        <small class="text-muted">
            <i class="bi bi-hourglass-split text-warning me-1"></i>Pool short-lease : <?= h($subnet['short_lease_pool_start']) ?> – <?= h($subnet['short_lease_pool_end']) ?>
        </small>
        <?php endif; ?>
        </div>
    </div>
    <div class="card-body p-0">
        <?php if ($reservations): ?>
        <table class="table table-hover mb-0">
            <thead class="table-light">
                <tr>
                    <th>Hostname</th>
                    <th>Adresse IP</th>
                    <th>Adresse MAC</th>
                    <th>Description</th>
                    <th>Modifié le</th>
                    <th style="width:100px"></th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($reservations as $r): ?>
            <tr>
                <td class="fw-semibold">
                    <?= h($r['hostname']) ?>
                    <?php if ($r['has_options']): ?>
                    <i class="bi bi-sliders text-primary ms-1"
                       style="font-size:.8em"
                       data-bs-toggle="tooltip"
                       title="Options DHCP spécifiques (gateway / routes)"></i>
                    <?php endif; ?>
                    <?php if ($r['has_ntp']): ?>
                    <i class="bi bi-clock text-info ms-1"
                       style="font-size:.8em"
                       data-bs-toggle="tooltip"
                       title="NTP activé (option 42)"></i>
                    <?php endif; ?>
                    <?php if ($r['short_lease']): ?>
                    <i class="bi bi-hourglass-split text-warning ms-1"
                       style="font-size:.8em"
                       data-bs-toggle="tooltip"
                       title="Lease court (1h) — client class short-lease"></i>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if ($r['ip_address'] !== '' && $r['ip_address'] !== '0.0.0.0'): ?>
                    <span class="ip-address"><?= h($r['ip_address']) ?></span>
                    <?php else: ?>
                    <span class="text-muted fst-italic">dynamique</span>
                    <?php endif; ?>
                </td>
                <td><span class="mac-address"><?= h($r['mac']) ?></span></td>
                <td><small class="text-muted"><?= h($r['description'] ?? '') ?></small></td>
                <td><small class="text-muted"><?= h(substr($r['updated_at'] ?? $r['created_at'] ?? '', 0, 16)) ?></small></td>
                <td class="text-end">
                    <?php if (Auth::currentUserRole() !== 'viewer'): ?>
                    <a href="reservation_edit.php?id=<?= $r['host_id'] ?>&subnet_id=<?= $subnet['id'] ?>"
                       class="btn btn-sm btn-outline-secondary me-1" title="Modifier">
                        <i class="bi bi-pencil"></i>
                    </a>
                    <a href="reservation_delete.php?id=<?= $r['host_id'] ?>&subnet_id=<?= $subnet['id'] ?>"
                       class="btn btn-sm btn-outline-danger" title="Supprimer">
                        <i class="bi bi-trash"></i>
                    </a>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php else: ?>
        <div class="p-5 text-center text-muted">
            <i class="bi bi-hdd-network fs-1"></i>
            <p class="mt-3 mb-1">Aucune réservation sur ce subnet.</p>
            <?php if (Auth::currentUserRole() !== 'viewer'): ?>
            <a href="reservation_add.php?subnet_id=<?= $subnet['id'] ?>" class="btn btn-primary mt-2">
                <i class="bi bi-plus-lg"></i> Ajouter la première réservation
            </a>
            <?php endif; ?>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php require_once APP_ROOT . '/lib/layout_footer.php'; ?>
<script>
document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(function (el) {
    new bootstrap.Tooltip(el);
});
</script>
