<?php
require_once '../../config.php';

/* validar sesión */
if (!$_settings->is_logged_in()) {
    header("Location: /admin/login.php");
    exit;
}

/* validar permiso */
$_settings->require_permission('reports', 'ver');

/* obtener bitácora */
$qry = $conn->query("
    SELECT 
        created_at,
        user_email,
        role,
        modulo,
        accion,
        descripcion,
        ip
    FROM audit_log
    ORDER BY created_at DESC
    LIMIT 300
");
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Bitácora del Sistema</title>
    <style>
        body{ font-family: Arial; background:#f4f4f4 }
        h2{ color:#b30000 }
        table{ background:#fff; border-collapse: collapse; width:100% }
        th,td{ padding:8px 10px; border:1px solid #ddd; font-size:13px }
        th{ background:#3a3a3a; color:#fff }
        tr:nth-child(even){ background:#f9f9f9 }
    </style>
</head>
<body>

<h2>🧾 Bitácora / Auditoría del Sistema</h2>

<table>
    <tr>
        <th>Fecha</th>
        <th>Usuario</th>
        <th>Rol</th>
        <th>Módulo</th>
        <th>Acción</th>
        <th>Descripción</th>
        <th>IP</th>
    </tr>

    <?php while($row = $qry->fetch_assoc()): ?>
    <tr>
        <td><?= $row['created_at'] ?></td>
        <td><?= $row['user_email'] ?></td>
        <td><?= $row['role'] ?></td>
        <td><?= $row['modulo'] ?></td>
        <td><?= $row['accion'] ?></td>
        <td><?= $row['descripcion'] ?></td>
        <td><?= $row['ip'] ?></td>
    </tr>
    <?php endwhile; ?>
</table>

</body>
</html>
