{% extends 'admin/base.html' %}
{% block title %}{{ t('audit_log.title') }}{% endblock %}
{% block content %}

<div class="page-header">
    <h2>🛡️ {{ t('audit_log.title') }}</h2>
    <p>{{ t('audit_log.subtitle') }}</p>
</div>

<!-- Filter -->
<div class="filter-bar">
    <form method="GET">
        <select name="action" onchange="this.form.submit()">
            <option value="">{{ t('audit_log.all_actions') }}</option>
            {% for action in action_list %}
            <option value="{{ action }}" {% if action_filter == action %}selected{% endif %}>{{ t('audit_log.action_' + action) }}</option>
            {% endfor %}
        </select>
    </form>
    {% if action_filter %}
    <a href="{{ url_for('admin_audit_log') }}">{{ t('audit_log.clear_filter') }}</a>
    {% endif %}
    <div class="flex-1"></div>
    <span class="text-sm text-muted">{{ logs.total }} {{ t('audit_log.entries') }}</span>
</div>

{% if logs.items %}
<table class="log-table">
    <thead>
        <tr>
            <th>{{ t('audit_log.time') }}</th>
            <th>{{ t('audit_log.user') }}</th>
            <th>{{ t('audit_log.action') }}</th>
            <th>{{ t('audit_log.details') }}</th>
            <th>{{ t('audit_log.ip') }}</th>
        </tr>
    </thead>
    <tbody>
        {% for log in logs.items %}
        <tr>
            <td class="time-cell">{{ log.created_at|format_datetime }}</td>
            <td>{{ log.username or '—' }}</td>
            <td><span class="badge badge-{{ log.action }}">{{ t('audit_log.action_' + log.action) }}</span></td>
            <td>{{ (log.details|translate_detail) or '—' }}</td>
            <td class="ip-cell">{{ log.ip_address or '—' }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

<div class="pagination-footer">
{% if logs.pages > 1 %}
<div class="pagination">
    {% if logs.has_prev %}
    <a href="{{ url_for('admin_audit_log', page=logs.prev_num, action=action_filter, per_page=per_page) }}">{{ t('pagination.prev') }}</a>
    {% endif %}
    {% for p in logs.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=2) %}
        {% if p %}
            {% if p == logs.page %}
                <span class="current">{{ p }}</span>
            {% else %}
                <a href="{{ url_for('admin_audit_log', page=p, action=action_filter, per_page=per_page) }}">{{ p }}</a>
            {% endif %}
        {% else %}
            <span>…</span>
        {% endif %}
    {% endfor %}
    {% if logs.has_next %}
    <a href="{{ url_for('admin_audit_log', page=logs.next_num, action=action_filter, per_page=per_page) }}">{{ t('pagination.next') }}</a>
    {% endif %}
</div>
{% endif %}
    <div class="per-page-selector">
        <span>{{ t('pagination.per_page') }}</span>
        <select onchange="var u=new URLSearchParams(location.search);u.set('per_page',this.value);u.delete('page');location.search=u">
            {% for pp in [20, 50, 100] %}
            <option value="{{ pp }}" {% if per_page == pp %}selected{% endif %}>{{ pp }}</option>
            {% endfor %}
        </select>
        <span>{{ t('pagination.per_page_suffix') }}</span>
    </div>
</div>

{% else %}
<div class="empty-state">
    <p class="empty-state-icon">📋</p>
    <h3>{{ t('audit_log.no_entries') }}</h3>
    <p>{{ t('audit_log.no_entries_text') }}</p>
</div>
{% endif %}

{% endblock %}
