<?php
/* ============================================================================
   WBMCZ ADMIN — ACTIVITY LOG
   Every action taken inside the admin panel: who, what, where, when, from
   which IP. Filterable, searchable, exportable.
   ============================================================================ */
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/Language.php';
require_once __DIR__ . '/../includes/activity.php';

if (!isLoggedIn() || !isAdmin()) { redirect('login.php'); }

/* Only a staff account that was given this module may open the page */
require_once __DIR__ . '/../includes/rbac.php';
rbac_guard('manage_staff_roles');

$lang         = new Language($db);
$current_lang = $lang->getCurrentLanguage();
$user_id      = $_SESSION['user_id'];

activity_ensure_schema($db, true);

$user_data = $db->select("SELECT * FROM users WHERE id = ?", [$user_id]);
$user      = !empty($user_data) ? $user_data[0] : null;
if (!$user) { session_destroy(); redirect('login.php'); }

$flash = $_SESSION['flash_success'] ?? '';
unset($_SESSION['flash_success']);

/* ---------------------------------------------------------------------------
   CLEAR OLD ENTRIES
   ------------------------------------------------------------------------ */
if (isset($_POST['purge_logs'])) {
    $days = max(1, (int)($_POST['purge_days'] ?? 90));
    try {
        $db->insert("DELETE FROM activity_logs WHERE created_at < DATE_SUB(NOW(), INTERVAL ? DAY)", [$days]);
        activity_log($db, 'delete', 'Activity Log', "Purged activity older than {$days} days");
        $_SESSION['flash_success'] = "Activity older than {$days} days has been cleared.";
    } catch (\Throwable $e) {
        $_SESSION['flash_success'] = "Nothing to clear.";
    }
    redirect('activity_log.php');
}

/* ---------------------------------------------------------------------------
   FILTERS
   ------------------------------------------------------------------------ */
$f_q      = trim((string)($_GET['q']      ?? ''));
$f_action = trim((string)($_GET['action'] ?? ''));
$f_module = trim((string)($_GET['module'] ?? ''));
$f_user   = (int)($_GET['uid'] ?? 0);
$f_range  = trim((string)($_GET['range']  ?? '7'));
$page     = max(1, (int)($_GET['p'] ?? 1));
$per      = 60;

$where  = ['1'];
$params = [];

if ($f_q !== '') {
    $where[]  = "(a.description LIKE ? OR a.user_name LIKE ? OR a.url LIKE ? OR a.ip_address LIKE ?)";
    $like     = "%$f_q%";
    array_push($params, $like, $like, $like, $like);
}
if ($f_action !== '') { $where[] = "a.action = ?"; $params[] = $f_action; }
if ($f_module !== '') { $where[] = "a.module = ?"; $params[] = $f_module; }
if ($f_user   >  0)   { $where[] = "a.user_id = ?"; $params[] = $f_user; }

switch ($f_range) {
    case 'today': $where[] = "DATE(a.created_at) = CURDATE()"; break;
    case '7':     $where[] = "a.created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)"; break;
    case '30':    $where[] = "a.created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)"; break;
    case 'all':
    default:      break;
}
$where_sql = implode(' AND ', $where);

/* ---------------------------------------------------------------------------
   DATA
   ------------------------------------------------------------------------ */
$total = 0;
try {
    $r = $db->select("SELECT COUNT(*) n FROM activity_logs a WHERE $where_sql", $params);
    $total = !empty($r) ? (int)$r[0]['n'] : 0;
} catch (\Throwable $e) {}

$pages  = max(1, (int)ceil($total / $per));
$page   = min($page, $pages);
$offset = ($page - 1) * $per;

$logs = [];
try {
    $logs = $db->select(
        "SELECT a.* FROM activity_logs a WHERE $where_sql ORDER BY a.created_at DESC, a.id DESC LIMIT $per OFFSET $offset",
        $params
    );
} catch (\Throwable $e) {}

/* CSV export of the current view */
if (isset($_GET['export'])) {
    $rows = [];
    try {
        $rows = $db->select("SELECT a.* FROM activity_logs a WHERE $where_sql ORDER BY a.created_at DESC LIMIT 10000", $params);
    } catch (\Throwable $e) {}
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=activity_log_' . date('Ymd_His') . '.csv');
    $out = fopen('php://output', 'w');
    fputcsv($out, ['When', 'User', 'Role', 'Action', 'Module', 'Description', 'Ref', 'Method', 'URL', 'IP']);
    foreach ($rows as $r) {
        fputcsv($out, [
            $r['created_at'], $r['user_name'], $r['user_role'], $r['action'], $r['module'],
            $r['description'],
            trim(($r['reference_type'] ?? '') . ' ' . ($r['reference_id'] ?? '')),
            $r['method'], $r['url'], $r['ip_address'],
        ]);
    }
    fclose($out);
    exit;
}

/* Distinct values for the filter dropdowns */
$all_actions = [];
$all_modules = [];
$all_actors  = [];
try { $all_actions = $db->select("SELECT DISTINCT action FROM activity_logs ORDER BY action"); } catch (\Throwable $e) {}
try { $all_modules = $db->select("SELECT DISTINCT module FROM activity_logs WHERE module IS NOT NULL ORDER BY module"); } catch (\Throwable $e) {}
try { $all_actors  = $db->select("SELECT user_id, MAX(user_name) nm, COUNT(*) n FROM activity_logs WHERE user_id IS NOT NULL GROUP BY user_id ORDER BY n DESC LIMIT 40"); } catch (\Throwable $e) {}

/* Headline stats */
$stats = ['today' => 0, 'week' => 0, 'actors' => 0, 'writes' => 0];
try {
    $r = $db->select("SELECT
            SUM(DATE(created_at)=CURDATE()) td,
            SUM(created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)) wk,
            COUNT(DISTINCT user_id) ac,
            SUM(action <> 'view') wr
        FROM activity_logs");
    if (!empty($r)) {
        $stats['today']  = (int)$r[0]['td'];
        $stats['week']   = (int)$r[0]['wk'];
        $stats['actors'] = (int)$r[0]['ac'];
        $stats['writes'] = (int)$r[0]['wr'];
    }
} catch (\Throwable $e) {}

$raw_logo     = getSetting('company_logo', 'assets/images/logo.png');
$company_logo = (strpos($raw_logo, 'http') === 0) ? $raw_logo : '../' . ltrim($raw_logo, '/');
$company_name = getSetting('company_name', 'WBMCZ');

/* Presentation helpers */
function act_tone($a) {
    $m = [
        'view'    => ['fa-eye',            'bg-slate-100 text-slate-600'],
        'create'  => ['fa-plus',           'bg-emerald-100 text-emerald-700'],
        'add'     => ['fa-plus',           'bg-emerald-100 text-emerald-700'],
        'update'  => ['fa-pen',            'bg-sky-100 text-sky-700'],
        'edit'    => ['fa-pen',            'bg-sky-100 text-sky-700'],
        'save'    => ['fa-floppy-disk',    'bg-sky-100 text-sky-700'],
        'set'     => ['fa-sliders',        'bg-sky-100 text-sky-700'],
        'delete'  => ['fa-trash',          'bg-rose-100 text-rose-700'],
        'remove'  => ['fa-trash',          'bg-rose-100 text-rose-700'],
        'approve' => ['fa-check',          'bg-emerald-100 text-emerald-700'],
        'reject'  => ['fa-xmark',          'bg-rose-100 text-rose-700'],
        'block'   => ['fa-ban',            'bg-rose-100 text-rose-700'],
        'login'   => ['fa-right-to-bracket','bg-violet-100 text-violet-700'],
        'logout'  => ['fa-right-from-bracket','bg-violet-100 text-violet-700'],
        'pay'     => ['fa-money-bill-transfer', 'bg-lime-100 text-lime-700'],
        'export'  => ['fa-file-csv',       'bg-amber-100 text-amber-700'],
        'send'    => ['fa-paper-plane',    'bg-teal-100 text-teal-700'],
        'reset'   => ['fa-key',            'bg-amber-100 text-amber-700'],
    ];
    return $m[strtolower($a)] ?? ['fa-circle-dot', 'bg-slate-100 text-slate-600'];
}
function act_ago($ts) {
    $d = time() - strtotime($ts);
    if ($d < 60)    return 'just now';
    if ($d < 3600)  return floor($d / 60) . 'm ago';
    if ($d < 86400) return floor($d / 3600) . 'h ago';
    if ($d < 604800) return floor($d / 86400) . 'd ago';
    return date('d M Y', strtotime($ts));
}
function act_role_pill($r) {
    $m = [
        'admin'    => 'bg-rose-50 text-rose-600 ring-rose-200',
        'employee' => 'bg-teal-50 text-teal-600 ring-teal-200',
        'seller'   => 'bg-amber-50 text-amber-600 ring-amber-200',
        'customer' => 'bg-sky-50 text-sky-600 ring-sky-200',
    ];
    return $m[strtolower((string)$r)] ?? 'bg-slate-50 text-slate-500 ring-slate-200';
}
function act_qs($over = []) {
    $q = array_merge($_GET, $over);
    unset($q['export']);
    return '?' . http_build_query(array_filter($q, function ($v) { return $v !== '' && $v !== null; }));
}
?>
<!DOCTYPE html>
<html lang="<?php echo $current_lang; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Activity Log - <?php echo htmlspecialchars($company_name); ?> Admin</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Inter', sans-serif; }
        [x-cloak] { display: none !important; }
        .hide-scrollbar::-webkit-scrollbar { display: none; }
        .tl-row:hover .tl-line { background: #34d399; }
    </style>
</head>
<body class="bg-gray-50" x-data="{ sidebarOpen: window.innerWidth >= 768, purge: false }">

<div class="flex h-screen overflow-hidden">

    <?php include __DIR__ . '/includes/sidebar.php'; ?>

    <div class="flex-1 flex flex-col min-w-0 bg-gray-100 overflow-hidden">

        <header class="h-20 bg-white text-slate-800 flex items-center justify-between px-4 lg:px-8 shadow-sm border-b border-gray-200 z-40 relative flex-shrink-0">
            <div class="flex items-center min-w-0">
                <button @click="sidebarOpen = !sidebarOpen" class="p-2 mr-3 text-slate-500 hover:text-emerald-600 rounded-lg hover:bg-gray-100 transition-colors"><i class="fas fa-bars text-xl"></i></button>
                <div class="min-w-0">
                    <h2 class="text-xl font-bold truncate">Activity Log</h2>
                    <p class="text-[11px] text-slate-400 hidden sm:block">Full audit trail of everything done in the admin panel</p>
                </div>
            </div>
            <div class="flex items-center gap-2">
                <a href="<?php echo act_qs(['export' => 1]); ?>&export=1" class="hidden sm:inline-flex items-center gap-2 px-4 py-2 rounded-xl bg-white ring-1 ring-slate-200 text-xs font-bold text-slate-600 hover:bg-slate-50 transition-colors">
                    <i class="fas fa-file-csv"></i> Export
                </a>
                <button @click="purge = true" class="inline-flex items-center gap-2 px-4 py-2 rounded-xl bg-rose-50 ring-1 ring-rose-200 text-xs font-bold text-rose-600 hover:bg-rose-100 transition-colors">
                    <i class="fas fa-broom"></i> Clear old
                </button>
            </div>
        </header>

        <main class="flex-1 overflow-y-auto p-4 md:p-6 lg:p-8">

            <?php if ($flash): ?>
            <div class="mb-6 bg-emerald-50 border border-emerald-200 text-emerald-700 px-4 py-3 rounded-xl flex items-center shadow-sm">
                <i class="fas fa-check-circle mr-2"></i><span class="font-medium"><?php echo htmlspecialchars($flash); ?></span>
            </div>
            <?php endif; ?>

            <!-- ================= STATS ================= -->
            <div class="grid grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
                <?php
                $cards = [
                    ['Today',        $stats['today'],  'fa-calendar-day',       'from-emerald-500 to-teal-600'],
                    ['Last 7 days',  $stats['week'],   'fa-calendar-week',      'from-sky-500 to-blue-600'],
                    ['Active staff', $stats['actors'], 'fa-user-group',         'from-violet-500 to-purple-600'],
                    ['Changes made', $stats['writes'], 'fa-wand-magic-sparkles','from-amber-500 to-orange-600'],
                ];
                foreach ($cards as $c): ?>
                <div class="bg-white rounded-2xl border border-slate-200 p-5 shadow-sm relative overflow-hidden">
                    <div class="absolute -right-6 -top-6 w-24 h-24 rounded-full bg-gradient-to-br <?php echo $c[3]; ?> opacity-10"></div>
                    <div class="w-10 h-10 rounded-xl bg-gradient-to-br <?php echo $c[3]; ?> text-white grid place-items-center mb-3 shadow-lg">
                        <i class="fas <?php echo $c[2]; ?>"></i>
                    </div>
                    <p class="text-[11px] font-bold uppercase tracking-widest text-slate-400"><?php echo $c[0]; ?></p>
                    <p class="text-2xl font-extrabold text-slate-800"><?php echo number_format($c[1]); ?></p>
                </div>
                <?php endforeach; ?>
            </div>

            <!-- ================= FILTERS ================= -->
            <form method="GET" class="bg-white rounded-2xl border border-slate-200 p-4 md:p-5 shadow-sm mb-6">
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-5 gap-3">
                    <div class="lg:col-span-2 relative">
                        <i class="fas fa-magnifying-glass absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-300 text-xs"></i>
                        <input type="text" name="q" value="<?php echo htmlspecialchars($f_q); ?>" placeholder="Search description, user, IP…"
                               class="w-full pl-9 pr-3 py-2.5 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-emerald-400 focus:border-emerald-400 outline-none transition-all">
                    </div>
                    <select name="action" class="px-3 py-2.5 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-emerald-400 outline-none">
                        <option value="">All actions</option>
                        <?php foreach ($all_actions as $a): ?>
                            <option value="<?php echo htmlspecialchars($a['action']); ?>" <?php echo $f_action === $a['action'] ? 'selected' : ''; ?>><?php echo htmlspecialchars(ucfirst($a['action'])); ?></option>
                        <?php endforeach; ?>
                    </select>
                    <select name="module" class="px-3 py-2.5 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-emerald-400 outline-none">
                        <option value="">All modules</option>
                        <?php foreach ($all_modules as $m): ?>
                            <option value="<?php echo htmlspecialchars($m['module']); ?>" <?php echo $f_module === $m['module'] ? 'selected' : ''; ?>><?php echo htmlspecialchars($m['module']); ?></option>
                        <?php endforeach; ?>
                    </select>
                    <select name="uid" class="px-3 py-2.5 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-emerald-400 outline-none">
                        <option value="">Everyone</option>
                        <?php foreach ($all_actors as $a): ?>
                            <option value="<?php echo (int)$a['user_id']; ?>" <?php echo $f_user === (int)$a['user_id'] ? 'selected' : ''; ?>>
                                <?php echo htmlspecialchars($a['nm'] ?: ('User #' . $a['user_id'])); ?> (<?php echo (int)$a['n']; ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="flex flex-wrap items-center gap-2 mt-4">
                    <?php foreach (['today' => 'Today', '7' => 'Last 7 days', '30' => 'Last 30 days', 'all' => 'All time'] as $k => $lbl):
                        $on = ($f_range === $k); ?>
                        <label class="cursor-pointer">
                            <input type="radio" name="range" value="<?php echo $k; ?>" class="sr-only" <?php echo $on ? 'checked' : ''; ?> onchange="this.form.submit()">
                            <span class="inline-block px-4 py-2 rounded-xl text-xs font-bold ring-1 transition-all <?php echo $on ? 'bg-slate-900 text-white ring-slate-900 shadow-lg shadow-slate-900/20' : 'bg-white text-slate-600 ring-slate-200 hover:bg-slate-50'; ?>"><?php echo $lbl; ?></span>
                        </label>
                    <?php endforeach; ?>

                    <div class="ml-auto flex items-center gap-2">
                        <span class="text-xs text-slate-400 font-medium"><?php echo number_format($total); ?> entries</span>
                        <button type="submit" class="px-5 py-2.5 rounded-xl bg-emerald-600 hover:bg-emerald-700 text-white text-xs font-bold shadow-lg shadow-emerald-600/25 transition-all">
                            <i class="fas fa-filter mr-1"></i> Apply
                        </button>
                        <a href="activity_log.php" class="px-4 py-2.5 rounded-xl bg-white ring-1 ring-slate-200 text-slate-500 text-xs font-bold hover:bg-slate-50 transition-colors">Reset</a>
                    </div>
                </div>
            </form>

            <!-- ================= TIMELINE ================= -->
            <div class="bg-white rounded-2xl border border-slate-200 shadow-sm overflow-hidden">
                <?php if (empty($logs)): ?>
                    <div class="p-16 text-center">
                        <div class="w-16 h-16 rounded-2xl bg-slate-100 grid place-items-center mx-auto mb-4 text-slate-300 text-2xl"><i class="fas fa-clock-rotate-left"></i></div>
                        <p class="text-slate-500 font-semibold">No activity recorded for these filters.</p>
                        <p class="text-slate-400 text-sm mt-1">New admin actions will start appearing here immediately.</p>
                    </div>
                <?php else: ?>
                    <div class="divide-y divide-slate-100">
                        <?php $lastDay = ''; foreach ($logs as $l):
                            $day = date('Y-m-d', strtotime($l['created_at']));
                            if ($day !== $lastDay):
                                $lastDay = $day; ?>
                                <div class="px-5 py-2.5 bg-slate-50/80 sticky top-0 z-10 backdrop-blur">
                                    <p class="text-[11px] font-extrabold uppercase tracking-widest text-slate-400">
                                        <?php echo date('l, d M Y', strtotime($l['created_at'])); ?>
                                    </p>
                                </div>
                            <?php endif;
                            list($ico, $tone) = act_tone($l['action']); ?>
                            <div class="tl-row flex items-start gap-4 px-5 py-3.5 hover:bg-slate-50/70 transition-colors">
                                <div class="flex flex-col items-center flex-shrink-0 pt-0.5">
                                    <span class="w-9 h-9 rounded-xl grid place-items-center text-xs <?php echo $tone; ?>">
                                        <i class="fas <?php echo $ico; ?>"></i>
                                    </span>
                                    <span class="tl-line w-px flex-1 bg-slate-200 mt-1 transition-colors"></span>
                                </div>

                                <div class="min-w-0 flex-1">
                                    <div class="flex flex-wrap items-center gap-x-2 gap-y-1">
                                        <span class="font-bold text-slate-800 text-sm"><?php echo htmlspecialchars($l['user_name'] ?: 'System'); ?></span>
                                        <?php if (!empty($l['user_role'])): ?>
                                            <span class="px-2 py-0.5 rounded-full text-[10px] font-bold ring-1 <?php echo act_role_pill($l['user_role']); ?>"><?php echo htmlspecialchars(ucfirst($l['user_role'])); ?></span>
                                        <?php endif; ?>
                                        <span class="text-slate-400 text-[11px]">&bull;</span>
                                        <span class="text-[11px] font-semibold text-slate-400"><?php echo htmlspecialchars($l['module'] ?: '—'); ?></span>
                                    </div>
                                    <p class="text-sm text-slate-600 mt-0.5"><?php echo htmlspecialchars($l['description'] ?: ucfirst($l['action'])); ?></p>
                                    <div class="flex flex-wrap items-center gap-x-3 gap-y-1 mt-1.5 text-[11px] text-slate-400">
                                        <span><i class="fas fa-clock mr-1"></i><?php echo date('h:i A', strtotime($l['created_at'])); ?></span>
                                        <span><i class="fas fa-location-dot mr-1"></i><?php echo htmlspecialchars($l['ip_address'] ?: '—'); ?></span>
                                        <?php if (!empty($l['reference_id'])): ?>
                                            <span class="font-mono"><i class="fas fa-hashtag mr-0.5"></i><?php echo htmlspecialchars(trim(($l['reference_type'] ?? '') . ' ' . $l['reference_id'])); ?></span>
                                        <?php endif; ?>
                                        <span class="hidden md:inline truncate max-w-[22rem] font-mono opacity-70"><?php echo htmlspecialchars($l['url'] ?? ''); ?></span>
                                    </div>
                                </div>

                                <span class="text-[11px] text-slate-400 whitespace-nowrap flex-shrink-0 pt-1"><?php echo act_ago($l['created_at']); ?></span>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>

            <!-- ================= PAGINATION ================= -->
            <?php if ($pages > 1): ?>
            <div class="flex items-center justify-center gap-1.5 mt-6 flex-wrap">
                <?php
                $win = 2;
                $from = max(1, $page - $win);
                $to   = min($pages, $page + $win);
                if ($page > 1) echo '<a href="' . act_qs(['p' => $page - 1]) . '" class="px-3 py-2 rounded-lg bg-white ring-1 ring-slate-200 text-xs font-bold text-slate-500 hover:bg-slate-50"><i class="fas fa-chevron-left"></i></a>';
                if ($from > 1) echo '<a href="' . act_qs(['p' => 1]) . '" class="px-3.5 py-2 rounded-lg bg-white ring-1 ring-slate-200 text-xs font-bold text-slate-500 hover:bg-slate-50">1</a><span class="px-1 text-slate-300">…</span>';
                for ($i = $from; $i <= $to; $i++) {
                    $on = ($i === $page);
                    echo '<a href="' . act_qs(['p' => $i]) . '" class="px-3.5 py-2 rounded-lg text-xs font-bold ring-1 transition-all '
                       . ($on ? 'bg-slate-900 text-white ring-slate-900' : 'bg-white text-slate-500 ring-slate-200 hover:bg-slate-50') . '">' . $i . '</a>';
                }
                if ($to < $pages) echo '<span class="px-1 text-slate-300">…</span><a href="' . act_qs(['p' => $pages]) . '" class="px-3.5 py-2 rounded-lg bg-white ring-1 ring-slate-200 text-xs font-bold text-slate-500 hover:bg-slate-50">' . $pages . '</a>';
                if ($page < $pages) echo '<a href="' . act_qs(['p' => $page + 1]) . '" class="px-3 py-2 rounded-lg bg-white ring-1 ring-slate-200 text-xs font-bold text-slate-500 hover:bg-slate-50"><i class="fas fa-chevron-right"></i></a>';
                ?>
            </div>
            <?php endif; ?>

            <div class="h-10"></div>
        </main>
    </div>
</div>

<!-- ================= PURGE MODAL ================= -->
<div x-show="purge" x-cloak class="fixed inset-0 z-[100] flex items-center justify-center p-4">
    <div class="absolute inset-0 bg-slate-900/60 backdrop-blur-sm" @click="purge = false"></div>
    <div class="relative bg-white rounded-2xl shadow-2xl w-full max-w-md p-6" x-transition>
        <div class="w-12 h-12 rounded-xl bg-rose-50 text-rose-600 grid place-items-center mb-4 text-xl"><i class="fas fa-broom"></i></div>
        <h3 class="text-lg font-extrabold text-slate-800">Clear old activity</h3>
        <p class="text-sm text-slate-500 mt-1 mb-5">Entries older than the chosen period will be permanently deleted. Recent activity is kept.</p>
        <form method="POST" class="space-y-4">
            <select name="purge_days" class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm font-semibold">
                <option value="30">Older than 30 days</option>
                <option value="90" selected>Older than 90 days</option>
                <option value="180">Older than 180 days</option>
                <option value="365">Older than 1 year</option>
            </select>
            <div class="flex gap-2">
                <button type="button" @click="purge = false" class="flex-1 px-4 py-3 rounded-xl bg-slate-100 text-slate-600 font-bold text-sm hover:bg-slate-200 transition-colors">Cancel</button>
                <button type="submit" name="purge_logs" value="1" class="flex-1 px-4 py-3 rounded-xl bg-rose-600 hover:bg-rose-700 text-white font-bold text-sm shadow-lg shadow-rose-600/25 transition-all">Delete</button>
            </div>
        </form>
    </div>
</div>

</body>
</html>
