{% extends "admin_base.html" %}
{% block admin_content %}
<div class="admin-toolbar">
    <h2>Devii tasks</h2>
    <span class="admin-count">{{ items|length }} {{ state }}</span>
</div>

<p class="admin-text-muted">
    Members may create {{ limits['member_create_24h'] }} tasks and execute
    {{ limits['member_runs_24h'] }} runs per rolling 24 hours; administrators
    {{ limits['admin_create_24h'] }} and {{ limits['admin_runs_24h'] }}. A run over the quota is
    postponed until a slot frees, never dropped. At most {{ limits['max_concurrent'] }} tasks run at
    the same time across all owners, one at a time per owner, and an owner may hold
    {{ limits['max_per_owner'] }} active tasks. A task stops itself after {{ limits['max_failures'] }}
    consecutive failures, or when its owner has been inactive for {{ limits['idle_days'] }} days.
    Only administrators may schedule new work from inside a running task.
</p>

<nav class="admin-tabs" aria-label="Task states">
    <div class="overflow-tabs-strip">
        {% for tab in tabs %}
        <a href="/admin/devii-tasks?state={{ tab['key'] }}" class="admin-tab {% if tab['active'] %}active{% endif %}">{{ tab['label'] }}</a>
        {% endfor %}
    </div>
</nav>

<table class="admin-table">
    <caption class="sr-only">Scheduled Devii tasks with disable and delete actions</caption>
    <thead>
        <tr>
            <th scope="col">Task</th>
            <th scope="col">Owner</th>
            <th scope="col">Schedule</th>
            <th scope="col">Runs</th>
            <th scope="col">Next run</th>
            <th scope="col">Expires</th>
            <th scope="col" class="admin-table-actions">Actions</th>
        </tr>
    </thead>
    <tbody>
        {% for item in items %}
        <tr>
            <td>
                {{ item['label'] }}
                <small class="admin-text-muted">{{ item['status'] }}</small>
                {% if item['last_error'] %}
                <small class="admin-text-muted">{{ item['last_error'][:120] }}</small>
                {% endif %}
            </td>
            <td>
                <a href="/profile/{{ item['owner'] }}">{{ item['owner'] }}</a>
                <small class="admin-text-muted">{{ 'admin' if item['owner_is_admin'] else 'member' }}</small>
                {% if item['quota'] %}
                <small class="admin-text-muted">
                    24h: {{ item['quota']['runs_used'] }}/{{ item['quota']['runs_limit'] }} runs,
                    {{ item['quota']['creates_used'] }}/{{ item['quota']['creates_limit'] }} created
                </small>
                {% endif %}
            </td>
            <td>{{ item['schedule'] }}</td>
            <td>
                {{ item['run_count'] }}{% if item['max_runs'] %} / {{ item['max_runs'] }}{% endif %}
                {% if item['failure_count'] %}
                <small class="admin-text-muted">{{ item['failure_count'] }} failed</small>
                {% endif %}
            </td>
            <td>{% if item['next_run_at'] %}{{ local_dt(item['next_run_at']) }}{% else %}-{% endif %}</td>
            <td>{% if item['expires_at'] %}{{ local_dt(item['expires_at']) }}{% else %}-{% endif %}</td>
            <td class="admin-table-actions">
                {% if item['enabled'] %}
                <form method="POST" action="/admin/devii-tasks/{{ item['uid'] }}/disable" class="admin-inline-form">
                    <button type="submit" class="admin-btn admin-btn-sm" aria-label="Disable {{ item['label'] }}">Disable</button>
                </form>
                {% endif %}
                <form method="POST" action="/admin/devii-tasks/{{ item['uid'] }}/delete" class="admin-inline-form">
                    <button type="submit" class="admin-btn admin-btn-sm" data-confirm-danger data-confirm="Delete this scheduled task? It moves to the trash." aria-label="Delete {{ item['label'] }}">Delete</button>
                </form>
            </td>
        </tr>
        {% else %}
        <tr>
            <td colspan="7" class="empty-state">No tasks.</td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% endblock %}
