{% extends "base.html" %}
{% block title %}Permissions{% endblock %}
{% block page_title %}<i class="bi bi-shield-lock-fill me-2"></i>Permissions{% endblock %}
{% block content %}
<form method="get" class="row g-2 align-items-end mb-3">
    <div class="col-md-3">
        <input type="text" name="player" class="form-control form-control-sm" placeholder="Target player…" value="{{ player }}">
    </div>
    <div class="col-md-2">
        <select name="plugin" class="form-select form-select-sm">
            <option value="">All Plugins</option>
            {% for pl in plugins %}<option {{ 'selected' if pl == plugin_filter }}>{{ pl }}</option>{% endfor %}
        </select>
    </div>
    <div class="col-md-3">
        <input type="text" name="type" class="form-control form-control-sm" placeholder="Event type (e.g. luckperms_permission_set)…" value="{{ etype }}">
    </div>
    <div class="col-auto">
        <button class="btn btn-success btn-sm">Filter</button>
        <a href="{{ url_for('perms') }}" class="btn btn-outline-secondary btn-sm ms-1">Reset</a>
    </div>
</form>

<div class="card">
    <div class="card-header">{{ total }} permission events</div>
    <div class="card-body p-0">
        <div class="table-responsive">
        <table class="table table-sm table-hover mb-0">
            <thead class="table-dark">
                <tr>
                    <th>Time</th>
                    <th>Plugin</th>
                    <th>Event Type</th>
                    <th>Target Player</th>
                    <th>Actor</th>
                    <th>Target Type</th>
                    <th>Target ID</th>
                    <th>Action</th>
                    <th>Server</th>
                </tr>
            </thead>
            <tbody>
            {% for r in rows %}
            {% set badge_colors = {
                'luckperms_permission_set':    'success',
                'luckperms_permission_unset':  'danger',
                'luckperms_parent_add':        'primary',
                'luckperms_parent_remove':     'warning',
                'luckperms_meta_set':          'info',
                'luckperms_meta_unset':        'secondary',
                'luckperms_group_create':      'light',
                'luckperms_group_delete':      'dark',
            } %}
            <tr>
                <td class="small text-muted text-nowrap">{{ r.timestamp | fmt_dt }}</td>
                <td><span class="badge bg-secondary">{{ r.plugin_name or '—' }}</span></td>
                <td><span class="badge bg-{{ badge_colors.get(r.event_type, 'secondary') }} text-wrap text-start" style="font-size:.7rem;">{{ r.event_type }}</span></td>
                <td class="small fw-semibold">{{ r.player_name or '—' }}</td>
                <td class="small">{{ r.actor_name or '—' }}</td>
                <td class="small text-muted">{{ r.target_type or '—' }}</td>
                <td class="small text-muted text-truncate" style="max-width:120px;" title="{{ r.target_id }}">{{ r.target_id or '—' }}</td>
                <td class="small text-truncate" style="max-width:200px;" title="{{ r.action }}">{{ r.action or '—' }}</td>
                <td><span class="badge bg-dark">{{ r.server_name or '—' }}</span></td>
            </tr>
            {% else %}
            <tr><td colspan="9" class="text-center text-muted py-4">No permission events found</td></tr>
            {% endfor %}
            </tbody>
        </table>
        </div>
    </div>
</div>
{% include "_pagination.html" %}
{% endblock %}
