{% extends "base.html" %}
{% import "partials/nav.html" as nav %}
{% import "partials/icons.html" as icons %}
{% block title %}Queue — Quartermaster{% endblock %}
{% block nav %}{% call nav::nav("queue", user, csrf_token, fika_installed) %}{% endcall %}{% endblock %}
{% block content %}
<div class="flex-between">
    <h1>Pending Operations</h1>
    {% if user.role.can_manage_queue() && !ops.is_empty() %}
    <form method="post" action="/queue/apply"
          onsubmit="return confirm('Apply all pending operations?')">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <button type="submit" class="btn btn-success">{% call icons::play() %}{% endcall %} Apply All</button>
    </form>
    {% endif %}
</div>

{% if ops.is_empty() %}
<div class="card">
    <div class="empty-state">
        {% call icons::check() %}{% endcall %}
        <p>All clear — no pending operations.</p>
    </div>
</div>
{% else %}
<div class="card">
    <table>
        <thead>
            <tr>
                <th>Action</th>
                <th>Mod</th>
                <th>Queued</th>
                <th>By</th>
                {% if user.role.can_manage_queue() %}<th>Actions</th>{% endif %}
            </tr>
        </thead>
        <tbody>
            {% for op in &ops %}
            <tr>
                <td>
                    {% if op.action == "install" %}<span class="badge badge-success">install</span>
                    {% else if op.action == "update" %}<span class="badge badge-warning">update</span>
                    {% else if op.action == "remove" %}<span class="badge badge-danger">remove</span>
                    {% else %}<span class="badge badge-muted">{{ op.action }}</span>
                    {% endif %}
                </td>
                <td>{{ op.mod_name }}</td>
                <td class="text-muted text-sm">{{ op.queued_at }}</td>
                <td class="text-muted text-sm">{{ op.queued_by.as_deref().unwrap_or("-") }}</td>
                {% if user.role.can_manage_queue() %}
                <td>
                    <form method="post" action="/queue/{{ op.id }}/cancel" style="display:inline"
                          onsubmit="return confirm('Cancel this operation?')">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                        <button type="submit" class="btn btn-sm btn-outline">{% call icons::x() %}{% endcall %} Cancel</button>
                    </form>
                </td>
                {% endif %}
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% endif %}
{% endblock %}
