<?php
declare(strict_types=1);
$pageTitle = 'Status do Sistema';
require_once __DIR__ . '/includes/header.php';
$db = gpc_db();
$today = date('Y-m-d');
$totalDns = (int)$db->query('SELECT COUNT(*) FROM dns_servers')->fetchColumn();
$activeDns = (int)$db->query("SELECT COUNT(*) FROM dns_servers ds LEFT JOIN resellers r ON r.id=ds.reseller_id WHERE ds.status=1 AND (ds.validity='' OR ds.validity IS NULL OR ds.validity >= date('now')) AND (ds.reseller_id IS NULL OR (r.active=1 AND (r.validity='' OR r.validity IS NULL OR r.validity >= date('now'))))")->fetchColumn();
$totalResellers = (int)$db->query('SELECT COUNT(*) FROM resellers')->fetchColumn();
$activeResellers = (int)$db->query("SELECT COUNT(*) FROM resellers WHERE active=1 AND (validity='' OR validity IS NULL OR validity >= date('now'))")->fetchColumn();
$totalDevices = (int)$db->query('SELECT COUNT(*) FROM devices')->fetchColumn();
$activeDevices = (int)$db->query("SELECT COUNT(*) FROM devices WHERE expire_date='' OR expire_date IS NULL OR expire_date >= date('now')")->fetchColumn();
$lastLogs = $db->query('SELECT * FROM registration_logs ORDER BY id DESC LIMIT 10')->fetchAll();
?>
<div class="section-title"><div><span class="eyebrow">SYNTRA V2.0</span><h2>Status operacional</h2><p>Visão rápida da saúde do painel, sincronização, DNS/códigos e revendas.</p></div></div>
<div class="card"><div class="card-body status-grid">
  <div class="status-item"><span class="dot ok"></span><strong>Painel Admin</strong><small>Online</small></div>
  <div class="status-item"><span class="dot ok"></span><strong>Banco de dados</strong><small>SQLite conectado</small></div>
  <div class="status-item"><span class="dot <?= $activeDns>0?'ok':'warn' ?>"></span><strong>DNS / SYNTRA ID</strong><small><?= $activeDns ?> ativas de <?= $totalDns ?></small></div>
  <div class="status-item"><span class="dot <?= $activeResellers>0?'ok':'warn' ?>"></span><strong>Revendas</strong><small><?= $activeResellers ?> ativas de <?= $totalResellers ?></small></div>
  <div class="status-item"><span class="dot <?= $activeDevices>0?'ok':'warn' ?>"></span><strong>MACs / Sync</strong><small><?= $activeDevices ?> ativos de <?= $totalDevices ?></small></div>
  <div class="status-item"><span class="dot ok"></span><strong>Auto MAC</strong><small><?= gpc_setting('auto_mac_enabled','1')==='1'?'Ativado':'Desativado' ?></small></div>
</div></div>
<div class="grid-2"><div class="card"><div class="card-head"><h2>Resumo</h2></div><div class="card-body license-info"><div class="info-row"><span class="badge badge-ok">Painel OK</span><p>Login admin e estrutura principal carregando normalmente.</p></div><div class="info-row"><span class="badge <?= $activeDns>0?'badge-ok':'badge-bad' ?>">DNS</span><p><?= $activeDns ?> DNS/códigos estão liberados pela hierarquia atual.</p></div><div class="info-row"><span class="badge <?= $activeResellers>0?'badge-ok':'badge-bad' ?>">Revendas</span><p><?= $activeResellers ?> revenda(s) com validade e status ativo.</p></div></div></div><div class="card"><div class="card-head"><h2>Últimos acessos</h2></div><div class="card-body flush"><div class="table-scroll"><table><thead><tr><th>MAC</th><th>Código</th><th>DNS</th><th>Data</th></tr></thead><tbody><?php foreach($lastLogs as $l): ?><tr><td><code><?= gpc_h($l['mac_address']) ?></code></td><td><?= gpc_h($l['cod_revenda'] ?: '-') ?></td><td><code><?= gpc_h($l['dns_url']) ?></code></td><td><?= gpc_h($l['registered_at']) ?></td></tr><?php endforeach; if(!$lastLogs): ?><tr><td colspan="4" class="empty">Sem registros</td></tr><?php endif; ?></tbody></table></div></div></div></div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
