<?php

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

$subnets    = Subnet::getAll();
$totalRes   = array_sum(array_column($subnets, 'reservation_count'));
$recentLogs = AuditLog::getRecent(10);

$page_title = 'Tableau de bord';
require_once APP_ROOT . '/lib/layout_header.php';
?>

<div class="row g-3 mb-4">
    <div class="col-sm-6 col-lg-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:#e8f4fd; color:#0d6efd">
                <i class="bi bi-diagram-3"></i>
            </div>
            <div>
                <div class="stat-value"><?= count($subnets) ?></div>
                <div class="stat-label">Subnet<?= count($subnets) > 1 ? 's' : '' ?></div>
            </div>
        </div>
    </div>
    <div class="col-sm-6 col-lg-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:#e8fdf0; color:#198754">
                <i class="bi bi-hdd-network"></i>
            </div>
            <div>
                <div class="stat-value"><?= $totalRes ?></div>
                <div class="stat-label">Réservation<?= $totalRes > 1 ? 's' : '' ?></div>
            </div>
        </div>
    </div>
    <div class="col-sm-6 col-lg-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:#fff8e8; color:#fd7e14">
                <i class="bi bi-people"></i>
            </div>
            <div>
                <div class="stat-value"><?= Auth::countUsers() ?></div>
                <div class="stat-label">Utilisateur<?= Auth::countUsers() > 1 ? 's' : '' ?></div>
            </div>
        </div>
    </div>
    <div class="col-sm-6 col-lg-3">
        <div class="stat-card">
            <div class="stat-icon" style="background:#fde8e8; color:#dc3545">
                <i class="bi bi-router"></i>
            </div>
            <div>
                <div class="stat-value" style="font-size:1rem; font-weight:600">
                    <?= Settings::get('freebsd_mode', '0') == '1' ? 'FreeBSD' : 'AlmaLinux' ?>
                </div>
                <div class="stat-label">Mode sync</div>
            </div>
        </div>
    </div>
</div>

<div class="row g-4">
    <!-- Subnets -->
    <div class="col-lg-7">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center py-3">
                <span><i class="bi bi-diagram-3 me-2"></i>Subnets</span>
                <a href="subnets.php" class="btn btn-sm btn-outline-primary">
                    <i class="bi bi-list-ul"></i> Gérer
                </a>
            </div>
            <div class="card-body p-0">
                <?php if ($subnets): ?>
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Nom</th>
                            <th>CIDR</th>
                            <th>Interface</th>
                            <th class="text-end">Réservations</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($subnets as $s): ?>
                        <tr>
                            <td class="fw-semibold"><?= h($s['name']) ?></td>
                            <td><code><?= h($s['cidr']) ?></code></td>
                            <td><small class="text-muted"><?= h($s['interface'] ?? '—') ?></small></td>
                            <td class="text-end">
                                <span class="badge bg-primary rounded-pill"><?= (int)$s['reservation_count'] ?></span>
                            </td>
                            <td class="text-end">
                                <a href="subnet.php?id=<?= $s['id'] ?>" class="btn btn-sm btn-outline-secondary">
                                    <i class="bi bi-arrow-right"></i>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
                <?php else: ?>
                <div class="p-4 text-center text-muted">
                    <i class="bi bi-diagram-3 fs-2"></i>
                    <p class="mt-2 mb-0">Aucun subnet configuré. <a href="subnet_add.php">Ajouter un subnet</a></p>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Journal récent -->
    <div class="col-lg-5">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center py-3">
                <span><i class="bi bi-clock-history me-2"></i>Activité récente</span>
                <a href="audit.php" class="btn btn-sm btn-outline-secondary">Tout voir</a>
            </div>
            <div class="card-body p-0">
                <?php if ($recentLogs): ?>
                <ul class="list-group list-group-flush">
                <?php foreach ($recentLogs as $log): ?>
                    <li class="list-group-item py-2 px-3">
                        <div class="d-flex justify-content-between">
                            <small class="fw-semibold text-dark"><?= h($log['action']) ?></small>
                            <small class="text-muted"><?= h(substr($log['created_at'], 0, 16)) ?></small>
                        </div>
                        <small class="text-muted">
                            <?= h($log['username'] ?? '—') ?>
                            <?php if ($log['details']): ?>
                                — <?= h(mb_strimwidth($log['details'], 0, 60, '…')) ?>
                            <?php endif; ?>
                        </small>
                    </li>
                <?php endforeach; ?>
                </ul>
                <?php else: ?>
                <div class="p-4 text-center text-muted">
                    <i class="bi bi-clock-history fs-2"></i>
                    <p class="mt-2 mb-0">Aucune activité.</p>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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