<?php
require_once __DIR__ . '/../includes/header.php';
require_admin();
$pdo = db();

$stmt = $pdo->query("
    SELECT mi.*, u.email AS user_email
    FROM monitored_ips mi
    JOIN users u ON u.id = mi.user_id
    ORDER BY mi.created_at DESC
    LIMIT 500
");
$ips = $stmt->fetchAll();
?>

<div class="card">
    <h2>Todos os IPs monitorados</h2>
    <table>
        <thead>
            <tr>
                <th>Usuário</th>
                <th>Nome</th>
                <th>Host</th>
                <th>Intervalo</th>
                <th>Status</th>
                <th>Latência</th>
                <th>Último check</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($ips as $ip): ?>
            <tr>
                <td><?php echo h($ip['user_email']); ?></td>
                <td><?php echo h($ip['label']); ?></td>
                <td><?php echo h($ip['host']); ?></td>
                <td><?php echo (int)$ip['interval_seconds']; ?>s</td>
                <td><?php echo h($ip['last_status']); ?></td>
                <td><?php echo $ip['last_latency_ms'] !== null ? (int)$ip['last_latency_ms'] : '-'; ?></td>
                <td><?php echo $ip['last_check_at'] ? (new DateTimeImmutable($ip['last_check_at']))->format('d/m/Y H:i:s') : '-'; ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>
