<?php

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

$subnets    = Subnet::getAll();
$relayMode  = Settings::get('relay_mode', '0') === '1';
$page_title = 'Gestion des subnets';
require_once APP_ROOT . '/lib/layout_header.php';
?>

<?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="d-flex justify-content-between align-items-center mb-3">
    <p class="text-muted mb-0">Les subnets correspondent aux <code>dhcp4_subnet_id</code> dans Kea.</p>
    <?php if (Auth::currentUserRole() !== 'viewer'): ?>
    <div class="d-flex gap-2">
        <a href="subnet_import.php" class="btn btn-outline-secondary">
            <i class="bi bi-upload"></i> Import CSV global
        </a>
        <a href="subnet_add.php" class="btn btn-primary">
            <i class="bi bi-plus-lg"></i> Ajouter un subnet
        </a>
    </div>
    <?php endif; ?>
</div>

<div class="card">
    <div class="card-body p-0">
        <?php if ($subnets): ?>
        <table class="table table-hover mb-0">
            <thead class="table-light">
                <tr>
                    <th>ID Kea</th>
                    <th>Nom</th>
                    <th>CIDR</th>
                    <th><?= $relayMode ? 'Relay (GIADDR)' : 'Interface' ?></th>
                    <th>Gateway</th>
                    <th>Pool dyn.</th>
                    <th class="text-end">Rés.</th>
                    <th style="width:160px"></th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($subnets as $s): ?>
            <tr class="<?= $s['is_active'] ? '' : 'text-muted' ?>">
                <td><span class="badge bg-secondary"><?= $s['kea_subnet_id'] ?></span></td>
                <td class="fw-semibold">
                    <a href="subnet.php?id=<?= $s['id'] ?>" class="text-decoration-none <?= $s['is_active'] ? '' : 'text-muted' ?>">
                        <?= h($s['name']) ?>
                    </a>
                    <?php if (!$s['is_active']): ?>
                        <span class="badge bg-secondary ms-1">inactif</span>
                    <?php endif; ?>
                    <?php if ($s['is_public']): ?>
                        <span class="badge bg-success ms-1">public</span>
                    <?php endif; ?>
                </td>
                <td><code><?= h($s['cidr']) ?></code></td>
                <td><small><?= $relayMode ? h($s['relay_address'] ?? '—') : h($s['interface'] ?? '—') ?></small></td>
                <td><small><?= h($s['gateway'] ?? '—') ?></small></td>
                <td>
                    <?php if ($s['has_dynamic_pool']): ?>
                        <span class="badge bg-info text-dark">
                            <?= h($s['range_start']) ?>–<?= h($s['range_end']) ?>
                        </span>
                    <?php else: ?>
                        <span class="text-muted small">Non</span>
                    <?php endif; ?>
                    <?php if (!empty($s['short_lease_pool_start'])): ?>
                        <br><span class="badge bg-warning text-dark mt-1"
                              data-bs-toggle="tooltip"
                              title="Pool short-lease (1h)">
                            <i class="bi bi-hourglass-split"></i>
                            <?= h($s['short_lease_pool_start']) ?>–<?= h($s['short_lease_pool_end']) ?>
                        </span>
                    <?php endif; ?>
                </td>
                <td class="text-end">
                    <span class="badge bg-primary rounded-pill"><?= (int)$s['reservation_count'] ?></span>
                </td>
                <td class="text-end text-nowrap">
                    <?php if (Auth::currentUserRole() !== 'viewer'): ?>
                    <form method="post" action="subnet_toggle.php" class="d-inline">
                        <input type="hidden" name="subnet_id" value="<?= $s['id'] ?>">
                        <button type="submit" class="btn btn-sm <?= $s['is_active'] ? 'btn-outline-secondary' : 'btn-outline-success' ?> me-1"
                                title="<?= $s['is_active'] ? 'Désactiver' : 'Activer' ?>">
                            <i class="bi bi-<?= $s['is_active'] ? 'pause' : 'play' ?>"></i>
                        </button>
                    </form>
                    <a href="subnet_edit.php?id=<?= $s['id'] ?>"
                       class="btn btn-sm btn-outline-secondary me-1">
                        <i class="bi bi-pencil"></i>
                    </a>
                    <a href="subnet_delete.php?id=<?= $s['id'] ?>"
                       class="btn btn-sm btn-outline-danger">
                        <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-diagram-3 fs-1"></i>
            <p class="mt-3 mb-0">Aucun subnet configuré.</p>
            <?php if (Auth::currentUserRole() !== 'viewer'): ?>
            <a href="subnet_add.php" class="btn btn-primary mt-3">Ajouter le premier subnet</a>
            <?php endif; ?>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php require_once APP_ROOT . '/lib/layout_footer.php'; ?>
