<?php
require_once __DIR__.'/../config/config.php';require_once __DIR__.'/../config/database.php';require_once __DIR__.'/../includes/functions.php';require_once __DIR__.'/../includes/admin_functions.php';afAdminRequireAuth();
$q=sanitize($_GET['q']??'');$where='1=1';$params=[];if($q!==''){$where='(l.acao LIKE ? OR l.entidade_tipo LIKE ? OR u.nome LIKE ? OR u.email LIKE ?)';$like='%'.$q.'%';$params=[$like,$like,$like,$like];}$st=$pdo->prepare("SELECT COUNT(*) FROM af_admin_logs l LEFT JOIN af_admin_users u ON u.id=l.admin_user_id WHERE {$where}");$st->execute($params);$pg=afAdminPagination((int)$st->fetchColumn(),30);$st=$pdo->prepare("SELECT l.*,u.nome admin_nome,u.email admin_email FROM af_admin_logs l LEFT JOIN af_admin_users u ON u.id=l.admin_user_id WHERE {$where} ORDER BY l.id DESC LIMIT {$pg['per_page']} OFFSET {$pg['offset']}");$st->execute($params);$items=$st->fetchAll()?:[];
$access=$pdo->query("SELECT a.*,u.nome admin_nome,u.email admin_email FROM af_admin_access_logs a LEFT JOIN af_admin_users u ON u.id=a.admin_user_id ORDER BY a.id DESC LIMIT 40")->fetchAll()?:[];
$pageTitle='Admin • Logs';$showNav=false;$bodyClass='admin-body';$adminPage='logs';require __DIR__.'/../includes/header.php';?>
<div class="admin-shell"><?php require __DIR__.'/../includes/admin_nav.php';?><main class="admin-main"><header class="admin-topbar"><div><small>AUDITORIA ADMINISTRATIVA</small><h1>Logs</h1></div><span class="admin-count"><?=$pg['total']?> ações</span></header>
<form class="admin-filters" method="get"><input name="q" value="<?=esc($q)?>" placeholder="Ação, entidade ou administrador"><button>Pesquisar</button></form>
<section class="admin-panel"><header><div><small>OPERAÇÕES</small><h2>Ações no sistema</h2></div></header><div class="admin-table-wrap"><table class="admin-table"><thead><tr><th>Data</th><th>Administrador</th><th>Ação</th><th>Entidade</th><th>Detalhes</th></tr></thead><tbody><?php foreach($items as $l):?><tr><td><?=date('d/m/Y H:i:s',strtotime($l['criado_em']))?></td><td><b><?=esc($l['admin_nome']??'Sistema')?></b><small><?=esc($l['admin_email']??'')?></small></td><td><span class="admin-pill"><?=esc($l['acao'])?></span></td><td><?=esc($l['entidade_tipo']??'-')?> <small><?=esc($l['entidade_id']??'')?></small></td><td><code class="admin-log-json"><?=esc($l['detalhes']??'{}')?></code></td></tr><?php endforeach;?></tbody></table></div></section><?php include __DIR__.'/../includes/admin_pagination.php';?>
<section class="admin-panel"><header><div><small>AUTENTICAÇÃO</small><h2>Acessos administrativos recentes</h2></div></header><div class="admin-table-wrap"><table class="admin-table"><thead><tr><th>Data</th><th>Conta</th><th>Evento</th><th>Resultado</th><th>Detalhes</th></tr></thead><tbody><?php foreach($access as $a):?><tr><td><?=date('d/m/Y H:i:s',strtotime($a['criado_em']))?></td><td><b><?=esc($a['admin_nome']??'Não identificado')?></b><small><?=esc($a['admin_email']??'')?></small></td><td><span class="admin-pill"><?=esc($a['evento'])?></span></td><td><?=$a['sucesso']?'<span class="admin-pill status-normal">Sucesso</span>':'<span class="admin-pill priority-alta">Falha</span>'?></td><td><code class="admin-log-json"><?=esc($a['detalhes']??'{}')?></code></td></tr><?php endforeach;?></tbody></table></div></section>
</main></div><script src="<?=SITE_URL?>/assets/js/admin.js?v=14.1"></script><?php require __DIR__.'/../includes/footer.php';
