<?php

$pageTitle = 'Activity Logs - PET SPA';
$activeAdminPage = 'logs';
require BASE_PATH . '/app/views/layouts/header.php';
require BASE_PATH . '/app/views/layouts/navbar.php';
$types = ['user', 'product', 'post', 'gallery', 'review', 'system_setting'];
?>

<main class="admin-area"><div class="container-fluid"><div class="row g-4">
    <div class="col-lg-3 col-xl-2"><?php require BASE_PATH . '/app/views/layouts/admin-sidebar.php'; ?></div>
    <div class="col-lg-9 col-xl-10">
        <h1 class="h3 mb-1">Activity Logs</h1><p class="text-muted mb-3"><?= number_format($totalLogs) ?> log.</p>
        <form method="GET" class="card card-body mb-3"><input type="hidden" name="url" value="admin/activity-logs"><div class="row g-2"><div class="col-md-5"><input class="form-control" name="search" value="<?= htmlspecialchars($search) ?>" placeholder="Tìm action"></div><div class="col-md-3"><select class="form-select" name="entity_type"><option value="">Tất cả entity</option><?php foreach ($types as $type): ?><option value="<?= $type ?>" <?= $entityType === $type ? 'selected' : '' ?>><?= htmlspecialchars($type) ?></option><?php endforeach; ?></select></div><div class="col-md-2"><input class="form-control" name="actor_id" min="1" type="number" value="<?= $actorId > 0 ? $actorId : '' ?>" placeholder="Actor ID"></div><div class="col-md-2"><button class="btn btn-dark w-100">Lọc</button></div></div></form>
        <div class="card"><div class="table-responsive"><table class="table table-hover align-middle mb-0"><thead><tr><th>Thời gian</th><th>Actor</th><th>Action</th><th>Entity</th><th>Chi tiết</th></tr></thead><tbody>
            <?php foreach ($logs as $log): ?>
                <?php $details = json_decode((string) $log['details_json'], true); $detailsText = is_array($details) ? json_encode($details, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : ''; ?>
                <tr><td><?= htmlspecialchars($log['created_at']) ?></td><td><?= htmlspecialchars($log['actor_name'] ?? 'System') ?><div class="small text-muted">#<?= (int) ($log['actor_id'] ?? 0) ?></div></td><td><?= htmlspecialchars($log['action']) ?></td><td><?= htmlspecialchars(($log['entity_type'] ?? '-') . '#' . ($log['entity_id'] ?? '-')) ?></td><td><code class="small"><?= htmlspecialchars($detailsText) ?></code></td></tr>
            <?php endforeach; ?>
            <?php if ($logs === []): ?><tr><td colspan="5" class="text-center text-muted py-4">Không có log phù hợp.</td></tr><?php endif; ?>
        </tbody></table></div></div>
        <?php if ($totalPages > 1): ?><nav class="mt-3"><ul class="pagination"><?php for ($number = 1; $number <= $totalPages; $number++): ?><li class="page-item <?= $number === $page ? 'active' : '' ?>"><a class="page-link" href="<?= BASE_URL ?>/?url=admin/activity-logs&<?= htmlspecialchars(http_build_query(['search' => $search, 'entity_type' => $entityType, 'actor_id' => $actorId, 'page' => $number])) ?>"><?= $number ?></a></li><?php endfor; ?></ul></nav><?php endif; ?>
    </div>
</div></div></main>

<?php require BASE_PATH . '/app/views/layouts/footer.php'; ?>
