<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require __DIR__ . '/config.php';

// Botão "Excluir Todos"
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['clear_logs'])) {
    file_put_contents(LOG_FILE, '');
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}

$logs   = readLogs();
$total  = count($logs);
$unique = count(array_unique(array_column($logs, 'ip')));
$lib    = count(array_filter($logs, function($r){ return isset($r['status']) && $r['status'] === 'liberado'; }));
$block  = count(array_filter($logs, function($r){ return isset($r['status']) && $r['status'] === 'bloqueado'; }));
$last   = array_slice(array_reverse($logs), 0, 100);
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
  <meta charset="UTF-8">
  <title>Dashboard de Tráfego</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
</head>
<body class="bg-dark text-white">
<div class="container py-4">
  <h2>Dashboard – Estatísticas de Tráfego</h2>

  <form method="post" onsubmit="return confirm('Confirma apagar todos os acessos?');">
    <button name="clear_logs" type="submit" class="btn btn-danger mb-3">Excluir Todos os Acessos</button>
  </form>

  <div class="row text-center mb-4">
    <div class="col">
      <div class="card bg-success mb-2"><div class="card-body">
        <h3><?php echo $unique; ?></h3><small>Únicos</small>
      </div></div>
    </div>
    <div class="col">
      <div class="card bg-secondary mb-2"><div class="card-body">
        <h3><?php echo $total; ?></h3><small>Totais</small>
      </div></div>
    </div>
    <div class="col">
      <div class="card bg-primary mb-2"><div class="card-body">
        <h3><?php echo $lib; ?></h3><small>Liberações</small>
      </div></div>
    </div>
    <div class="col">
      <div class="card bg-danger mb-2"><div class="card-body">
        <h3><?php echo $block; ?></h3><small>Bloqueios</small>
      </div></div>
    </div>
  </div>

  <div style="max-height:400px; overflow:auto;">
    <table class="table table-dark table-striped">
      <thead>
        <tr><th>Data/Hora</th><th>IP</th><th>País</th><th>ASN</th><th>Disp.</th><th>Status</th></tr>
      </thead>
      <tbody>
      <?php foreach ($last as $r): ?>
        <tr class="<?php echo (isset($r['status']) && $r['status']==='bloqueado') ? 'table-danger' : ''; ?>">
          <td><?php echo htmlspecialchars(isset($r['time']) ? $r['time'] : ''); ?></td>
          <td><?php echo htmlspecialchars(isset($r['ip']) ? $r['ip'] : ''); ?></td>
          <td><?php echo htmlspecialchars(isset($r['country']) ? $r['country'] : ''); ?></td>
          <td><?php echo htmlspecialchars(isset($r['asn']) ? $r['asn'] : ''); ?></td>
          <td><?php echo htmlspecialchars(isset($r['device']) ? $r['device'] : ''); ?></td>
          <td><?php echo htmlspecialchars(isset($r['status']) ? ucfirst($r['status']) : ''); ?></td>
        </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
</body>
</html>

