<!-- Page Header -->
<div class="page-header">
    <div>
        <h1>Activity Log</h1>
        <p>Full audit trail of all user actions across the system.</p>
    </div>
    <div class="stat-card px-4 py-2" style="gap:12px;">
        <div class="stat-icon" style="background:#fff4ee;color:#f97316;width:36px;height:36px;border-radius:10px;font-size:.95rem;">
            <i class="bi bi-activity"></i>
        </div>
        <div>
            <div class="fw-700"><?= number_format($today) ?></div>
            <div class="stat-label">Actions Today</div>
        </div>
    </div>
</div>

<!-- Filters -->
<div class="card mb-4">
    <div class="card-body py-3">
        <form method="GET" action="<?= url('activity-log') ?>" class="row g-2 align-items-end">
            <div class="col-6 col-md-2">
                <label class="form-label mb-1" style="font-size:.8rem;">Module</label>
                <select name="module" class="form-select form-select-sm">
                    <option value="">All Modules</option>
                    <?php foreach (['auth','category','menu_item','order','voucher','deal','discount','user','settings'] as $m): ?>
                    <option value="<?= $m ?>" <?= $filters['module'] === $m ? 'selected' : '' ?>>
                        <?= ucwords(str_replace('_',' ',$m)) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-6 col-md-2">
                <label class="form-label mb-1" style="font-size:.8rem;">Action</label>
                <select name="action" class="form-select form-select-sm">
                    <option value="">All Actions</option>
                    <?php foreach (['login','logout','created','updated','deleted','toggled','status_changed'] as $a): ?>
                    <option value="<?= $a ?>" <?= $filters['action'] === $a ? 'selected' : '' ?>>
                        <?= ucfirst($a) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-6 col-md-2">
                <label class="form-label mb-1" style="font-size:.8rem;">User</label>
                <select name="user_id" class="form-select form-select-sm">
                    <option value="">All Users</option>
                    <?php foreach ($users as $u): ?>
                    <option value="<?= $u['id'] ?>" <?= $filters['user_id'] == $u['id'] ? 'selected' : '' ?>>
                        <?= e($u['name']) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-6 col-md-2">
                <label class="form-label mb-1" style="font-size:.8rem;">Date From</label>
                <input type="date" name="date_from" class="form-control form-control-sm"
                       value="<?= e($filters['date_from']) ?>">
            </div>
            <div class="col-6 col-md-2">
                <label class="form-label mb-1" style="font-size:.8rem;">Date To</label>
                <input type="date" name="date_to" class="form-control form-control-sm"
                       value="<?= e($filters['date_to']) ?>">
            </div>
            <div class="col-6 col-md-2 d-flex gap-2">
                <button type="submit" class="btn btn-accent btn-sm px-3">
                    <i class="bi bi-funnel me-1"></i> Filter
                </button>
                <a href="<?= url('activity-log') ?>" class="btn btn-outline-secondary btn-sm px-3">
                    <i class="bi bi-x-lg"></i>
                </a>
            </div>
        </form>
    </div>
</div>

<!-- Log Table -->
<div class="card">
    <div class="card-header">
        <i class="bi bi-journal-text text-accent"></i>
        <h6 class="card-title">Audit Trail</h6>
        <span class="ms-auto text-secondary" style="font-size:.8rem;">Last 200 records</span>
    </div>
    <div class="card-body p-0">
        <?php if (empty($logs)): ?>
            <div class="text-center py-5 text-secondary">
                <i class="bi bi-journal-x fs-1 d-block mb-3 opacity-25"></i>
                <p>No activity logs found for the selected filters.</p>
            </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table mb-0" style="font-size:.83rem;">
                <thead>
                    <tr>
                        <th style="width:160px;">Time</th>
                        <th style="width:140px;">User</th>
                        <th style="width:80px;">Role</th>
                        <th style="width:100px;">Action</th>
                        <th style="width:110px;">Module</th>
                        <th>Description</th>
                        <th style="width:110px;">IP Address</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($logs as $log): ?>
                    <tr>
                        <td class="text-secondary" style="white-space:nowrap;">
                            <?= format_datetime($log['created_at']) ?>
                        </td>
                        <td>
                            <span class="fw-600"><?= e($log['user_name'] ?? '—') ?></span>
                        </td>
                        <td>
                            <span class="badge bg-light text-dark border" style="font-size:.7rem;">
                                <?= e(ucwords(str_replace('_',' ', $log['role'] ?? ''))) ?>
                            </span>
                        </td>
                        <td><?= action_badge($log['action']) ?></td>
                        <td>
                            <span style="font-size:.75rem;color:var(--text-secondary);">
                                <?= e(ucwords(str_replace('_',' ',$log['module']))) ?>
                            </span>
                        </td>
                        <td>
                            <?= e($log['description'] ?? '') ?>
                            <?php if ($log['target_name'] && !$log['description']): ?>
                                <span class="text-secondary"> — <?= e($log['target_name']) ?></span>
                            <?php endif; ?>
                        </td>
                        <td class="text-secondary" style="font-size:.75rem;"><?= e($log['ip_address'] ?? '') ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php
function action_badge(string $action): string {
    [$bg, $color, $icon] = match ($action) {
        'created'        => ['#f0fdf4','#16a34a','plus-circle-fill'],
        'updated'        => ['#eff6ff','#2563eb','pencil-fill'],
        'deleted'        => ['#fef2f2','#dc2626','trash-fill'],
        'toggled'        => ['#fdf4ff','#9333ea','toggle-on'],
        'login'          => ['#f0fdf4','#16a34a','box-arrow-in-right'],
        'logout'         => ['#fff7ed','#ea580c','box-arrow-left'],
        'status_changed' => ['#fffbeb','#d97706','arrow-repeat'],
        default          => ['#f4f6fc','#6b7280','circle'],
    };
    return "<span class=\"badge d-inline-flex align-items-center gap-1\" style=\"background:{$bg};color:{$color};font-size:.72rem;\">
                <i class=\"bi bi-{$icon}\"></i>" . ucfirst(str_replace('_',' ',$action)) . "
            </span>";
}
?>
