<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/auth.php';
gpc_require_login();
$db = gpc_db();
$tab = $_GET['tab'] ?? 'access';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && gpc_csrf_check($_POST['csrf'] ?? null)) {
    if (($_POST['action'] ?? '') === 'clear') {
        if ($tab === 'login') { $db->exec('DELETE FROM client_login_attempts'); }
        else { $db->exec('DELETE FROM client_access_logs'); }
        gpc_flash('ok', 'Logs limpos com sucesso.');
        header('Location: client_security_logs.php?tab=' . urlencode((string)$tab));
        exit;
    }
}
$csrf = gpc_csrf_token();
$flash = gpc_take_flash();
$rows = [];
if ($tab === 'login') {
    $rows = $db->query('SELECT * FROM client_login_attempts ORDER BY id DESC LIMIT 200')->fetchAll();
} else {
    $rows = $db->query('SELECT * FROM client_access_logs ORDER BY id DESC LIMIT 200')->fetchAll();
}
include __DIR__ . '/includes/header.php';
?>
<div class="page-title"><div><h1>Segurança do Painel Cliente</h1><p>Monitore tentativas de login, acessos e ações sensíveis.</p></div></div>
<?php if ($flash): ?><div class="alert alert-<?= gpc_h($flash['type']) ?>"><?= gpc_h($flash['msg']) ?></div><?php endif; ?>
<div class="card"><div class="card-body">
    <div style="display:flex;gap:10px;flex-wrap:wrap;margin-bottom:14px">
        <a class="btn <?= $tab !== 'login' ? 'btn-primary' : 'btn-outline' ?>" href="client_security_logs.php?tab=access">Ações do cliente</a>
        <a class="btn <?= $tab === 'login' ? 'btn-primary' : 'btn-outline' ?>" href="client_security_logs.php?tab=login">Tentativas de login</a>
        <form method="post" onsubmit="return confirm('Limpar estes logs?')" style="margin-left:auto"><input type="hidden" name="csrf" value="<?= gpc_h($csrf) ?>"><input type="hidden" name="action" value="clear"><button class="btn btn-danger" type="submit">Limpar logs</button></form>
    </div>
    <div class="table-responsive"><table class="table"><thead><tr>
        <?php if ($tab === 'login'): ?><th>Data</th><th>MAC</th><th>IP</th><th>Status</th><th>Motivo</th>
        <?php else: ?><th>Data</th><th>MAC</th><th>Ação</th><th>IP</th><th>Detalhes</th><?php endif; ?>
    </tr></thead><tbody>
    <?php foreach ($rows as $r): ?><tr>
        <?php if ($tab === 'login'): ?>
        <td><?= gpc_h($r['created_at'] ?? '') ?></td><td><?= gpc_h($r['mac_address'] ?? '') ?></td><td><?= gpc_h($r['ip'] ?? '') ?></td><td><?= ((int)($r['success'] ?? 0)===1) ? 'OK' : 'Falha' ?></td><td><?= gpc_h($r['reason'] ?? '') ?></td>
        <?php else: ?>
        <td><?= gpc_h($r['created_at'] ?? '') ?></td><td><?= gpc_h($r['mac_address'] ?? '') ?></td><td><?= gpc_h($r['action'] ?? '') ?></td><td><?= gpc_h($r['ip'] ?? '') ?></td><td><?= gpc_h($r['details'] ?? '') ?></td>
        <?php endif; ?>
    </tr><?php endforeach; ?>
    <?php if (!$rows): ?><tr><td colspan="5">Nenhum registro encontrado.</td></tr><?php endif; ?>
    </tbody></table></div>
</div></div>
<?php include __DIR__ . '/includes/footer.php'; ?>
