<!DOCTYPE html>
<html lang="pt-BR">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Auditoria de acessos</title>
  <style>
    body{font-family:system-ui,Segoe UI,Arial,sans-serif;margin:0;background:#f5f6f8;color:#222}
    header{background:#1f2937;color:#fff;padding:14px 24px;display:flex;justify-content:space-between;align-items:center}
    header a{color:#cbd5e1;text-decoration:none;font-size:14px}
    main{padding:20px 24px}
    form{display:flex;gap:10px;flex-wrap:wrap;align-items:end;margin-bottom:16px}
    label{font-size:12px;color:#555;display:flex;flex-direction:column;gap:4px}
    input,select{padding:7px 9px;border:1px solid #ccd;border-radius:6px;font-size:14px}
    button{padding:8px 16px;border:0;border-radius:6px;background:#2563eb;color:#fff;cursor:pointer}
    table{width:100%;border-collapse:collapse;background:#fff;border-radius:8px;overflow:hidden;font-size:13px}
    th,td{padding:8px 10px;border-bottom:1px solid #eee;text-align:left;vertical-align:top}
    th{background:#eef0f4;font-size:12px;text-transform:uppercase;letter-spacing:.04em;color:#555}
    .ok{color:#15803d}.falha{color:#b45309}.negado{color:#b91c1c;font-weight:600}
    .vazio{padding:30px;text-align:center;color:#777}
  </style>
</head>
<body>
  <header>
    <strong>🔒 Auditoria de acessos</strong>
    <a href="/painel/kanban">← Voltar ao sistema</a>
  </header>
  <main>
    <form method="get" action="/admin/auditoria">
      <label>Ação
        <select name="acao">
          <option value="">Todas</option>
          <% acoes.forEach(function(a){ %><option value="<%= a %>" <%= filtros.acao === a ? 'selected' : '' %>><%= a %></option><% }) %>
        </select>
      </label>
      <label>Resultado
        <select name="resultado">
          <option value="">Todos</option>
          <% ['ok','falha','negado'].forEach(function(r){ %><option value="<%= r %>" <%= filtros.resultado === r ? 'selected' : '' %>><%= r %></option><% }) %>
        </select>
      </label>
      <label>ID do usuário <input type="number" name="usuario" value="<%= filtros.usuario || '' %>" style="width:110px"></label>
      <label>De <input type="date" name="de" value="<%= filtros.de || '' %>"></label>
      <label>Até <input type="date" name="ate" value="<%= filtros.ate || '' %>"></label>
      <button type="submit">Filtrar</button>
    </form>

    <% if (!registros.length) { %>
      <div class="vazio">Nenhum registro encontrado.</div>
    <% } else { %>
    <table>
      <thead><tr><th>Quando</th><th>Usuário</th><th>Ação</th><th>Recurso</th><th>Detalhe</th><th>Resultado</th><th>IP</th></tr></thead>
      <tbody>
      <% registros.forEach(function(r){ %>
        <tr>
          <td><%= new Date(r.createdAt).toLocaleString('pt-BR', { timeZone: 'America/Sao_Paulo' }) %></td>
          <td><%= r.funcionario_nome || '—' %><%= r.funcionario_id ? ' (#' + r.funcionario_id + ')' : '' %></td>
          <td><%= r.acao %></td>
          <td><%= r.recurso || '' %><%= r.recurso_id ? ' #' + r.recurso_id : '' %></td>
          <td><%= r.detalhe || '' %></td>
          <td class="<%= r.resultado %>"><%= r.resultado %></td>
          <td><%= r.ip || '' %></td>
        </tr>
      <% }) %>
      </tbody>
    </table>
    <p style="color:#777;font-size:12px">Mostrando os 300 registros mais recentes do filtro.</p>
    <% } %>
  </main>
</body>
</html>
