<?php ob_start(); ?>

<?php admin_content_header(__('admin.activity_log.title'), 'activity', []); ?>

<!-- Filters -->
<div style="display:flex;gap:1rem;margin-bottom:1rem;flex-wrap:wrap;">
    <form method="GET" action="<?= admin_url('activity-log') ?>" style="display:flex;gap:0.5rem;flex-wrap:wrap;">
        <select name="action" onchange="this.form.submit()" style="min-width:140px;">
            <option value=""><?= __('admin.activity_log.all_actions') ?></option>
            <?php foreach ($actions as $a): ?>
            <option value="<?= e($a) ?>" <?= ($filterAction ?? '') === $a ? 'selected' : '' ?>><?= e(ucfirst($a)) ?></option>
            <?php endforeach; ?>
        </select>
        <select name="type" onchange="this.form.submit()" style="min-width:140px;">
            <option value=""><?= __('admin.activity_log.all_types') ?></option>
            <?php foreach ($types as $t): ?>
            <option value="<?= e($t) ?>" <?= ($filterType ?? '') === $t ? 'selected' : '' ?>><?= e($t) ?></option>
            <?php endforeach; ?>
        </select>
        <?php if ($filterAction || $filterType): ?>
        <a href="<?= admin_url('activity-log') ?>" class="btn btn-sm btn-ghost"><?= icon('x', 14) ?> <?= __('admin.common.reset') ?></a>
        <?php endif; ?>
    </form>
</div>

<div class="table-responsive">
<table class="data-table">
    <thead>
        <tr>
            <th><?= __('admin.activity_log.timestamp') ?></th>
            <th><?= __('admin.activity_log.user') ?></th>
            <th><?= __('admin.activity_log.action') ?></th>
            <th><?= __('admin.activity_log.resource') ?></th>
            <th><?= __('admin.activity_log.name') ?></th>
            <th><?= __('admin.activity_log.ip') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($paginated['items'])): ?>
        <?php admin_empty_state(__('admin.activity_log.no_entries'), 'activity', '', '', true, 6); ?>
        <?php else: ?>
        <?php foreach ($paginated['items'] as $log): ?>
        <tr>
            <td style="white-space:nowrap;">
                <time datetime="<?= e($log['created_at']) ?>">
                    <?= e(date('d.m.Y H:i', strtotime($log['created_at']))) ?>
                </time>
            </td>
            <td><?= e($log['user_name'] ?? 'System') ?></td>
            <td>
                <?php
                $badgeClass = match($log['action']) {
                    'delete' => 'badge-danger',
                    'create' => 'badge-success',
                    'login'  => 'badge-info',
                    default  => 'badge-default',
                };
                ?>
                <span class="badge <?= $badgeClass ?>"><?= e(ucfirst($log['action'])) ?></span>
            </td>
            <td><?= e($log['resource_type']) ?></td>
            <td><?= e($log['resource_name'] ?? '–') ?></td>
            <td><code style="font-size:0.85em;"><?= e($log['ip_address'] ?? '') ?></code></td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>
</div>

<?php if (($paginated['pages'] ?? 1) > 1): ?>
<div class="pagination" style="margin-top:1.5rem;display:flex;gap:0.25rem;justify-content:center;">
    <?php for ($p = 1; $p <= $paginated['pages']; $p++): ?>
    <?php
    $qs = http_build_query(array_filter([
        'page'   => $p > 1 ? $p : null,
        'action' => $filterAction,
        'type'   => $filterType,
    ]));
    ?>
    <a href="<?= admin_url('activity-log') ?><?= $qs ? '?' . $qs : '' ?>"
       class="btn btn-sm <?= $p === $paginated['page'] ? 'btn-primary' : 'btn-secondary' ?>">
        <?= $p ?>
    </a>
    <?php endfor; ?>
</div>
<?php endif; ?>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
