{% extends "allianceauth/base-bs5.html" %}
{% load i18n %}

{% block page_title %}{% translate "Discord Audit Runs" %}{% endblock %}
{% block header_nav_brand %}{% translate "Discord Audit" %}{% endblock %}

{% block content %}
{% include "aa_discord_audit/_subnav.html" with active="runs" %}

<nav aria-label="{% translate 'breadcrumb' %}" class="mb-3">
    <ol class="breadcrumb">
        <li class="breadcrumb-item active" aria-current="page">{% translate "Discord Audit" %}</li>
    </ol>
</nav>

<h2>{% translate "Audit runs" %}</h2>

<div>
    <form method="get" class="mb-3 d-flex align-items-center gap-2 flex-wrap">
        <label for="state-filter" class="form-label mb-0">
            {% translate "State" %}
        </label>
        <select id="state-filter" name="state" class="form-select w-auto">
            <option value="">{% translate "(any)" %}</option>
            {% for state in states %}
                <option value="{{ state.value }}"{% if state.value == selected_state %} selected{% endif %}>
                    {{ state.value }}
                </option>
            {% endfor %}
        </select>

        <label for="from-date" class="form-label mb-0">{% translate "From" %}</label>
        <input id="from-date" type="date" name="from"
               value="{{ selected_from }}" class="form-control w-auto">

        <label for="to-date" class="form-label mb-0">{% translate "To" %}</label>
        <input id="to-date" type="date" name="to"
               value="{{ selected_to }}" class="form-control w-auto">

        <button type="submit" class="btn btn-primary">
            {% translate "Apply" %}
        </button>
    </form>

    <div class="table-responsive">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th scope="col">{% translate "ID" %}</th>
                    <th scope="col">{% translate "State" %}</th>
                    <th scope="col">{% translate "Started" %}</th>
                    <th scope="col">{% translate "Finished" %}</th>
                    <th scope="col" class="text-end">{% translate "Members" %}</th>
                    <th scope="col" class="text-end">{% translate "Findings" %}</th>
                    <th scope="col">{% translate "Dry run" %}</th>
                    <th scope="col"></th>
                </tr>
            </thead>
            <tbody>
                {% for run in page_obj %}
                    <tr>
                        <td>{{ run.pk }}</td>
                        <td>{{ run.state }}</td>
                        <td>{{ run.started_at|date:"Y-m-d H:i" }}</td>
                        <td>
                            {% if run.finished_at %}
                                {{ run.finished_at|date:"Y-m-d H:i" }}
                            {% else %}
                                &mdash;
                            {% endif %}
                        </td>
                        <td class="text-end">{{ run.members_scanned }}</td>
                        <td class="text-end">{{ run.findings_count }}</td>
                        <td>{{ run.dry_run|yesno:_("yes,no") }}</td>
                        <td>
                            <a href="{% url 'aa_discord_audit:run_detail' pk=run.pk %}"
                               class="btn btn-outline-secondary btn-sm">
                                {% translate "Open" %}
                            </a>
                        </td>
                    </tr>
                {% empty %}
                    <tr>
                        <td colspan="8" class="text-center text-muted">
                            {% translate "No audit runs to display." %}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    {% if page_obj.has_other_pages %}
        <nav aria-label="{% translate 'pagination' %}">
            <ul class="pagination">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if selected_state %}&state={{ selected_state }}{% endif %}{% if selected_from %}&from={{ selected_from }}{% endif %}{% if selected_to %}&to={{ selected_to }}{% endif %}">
                            &laquo;
                        </a>
                    </li>
                {% endif %}
                <li class="page-item active" aria-current="page">
                    <span class="page-link">
                        {{ page_obj.number }} / {{ page_obj.paginator.num_pages }}
                    </span>
                </li>
                {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if selected_state %}&state={{ selected_state }}{% endif %}{% if selected_from %}&from={{ selected_from }}{% endif %}{% if selected_to %}&to={{ selected_to }}{% endif %}">
                            &raquo;
                        </a>
                    </li>
                {% endif %}
            </ul>
        </nav>
    {% endif %}
</div>
{% endblock %}
