{% extends "base.html" %}
{% block title %}Hold Queue — {{ config.BBS_NAME }}{% endblock %}

{% block content %}
<div class="bbs-header d-flex justify-content-between align-items-center flex-wrap gap-2">
    <div>
        <h1><i class="bi bi-clock-history"></i> Hold Queue</h1>
        <p>Outbound echomail queued for delivery to downstream BinkP nodes.</p>
    </div>
    <a href="{{ url_for('hub_admin.index') }}" class="btn btn-secondary">
        <i class="bi bi-arrow-left"></i> Hub Overview
    </a>
</div>

<!-- Filters -->
<form method="GET" class="card mb-3">
    <div class="card-body d-flex gap-2 flex-wrap align-items-end">
        <div>
            <label class="form-label">Status</label>
            <select name="status" class="form-select form-select-sm">
                <option value="pending" {% if status=='pending' %}selected{% endif %}>Pending</option>
                <option value="sent" {% if status=='sent' %}selected{% endif %}>Sent</option>
                <option value="failed" {% if status=='failed' %}selected{% endif %}>Failed</option>
            </select>
        </div>
        <div>
            <label class="form-label">Node</label>
            <select name="node_id" class="form-select form-select-sm">
                <option value="">All nodes</option>
                {% for n in nodes %}
                <option value="{{ n.id }}" {% if node_id==n.id %}selected{% endif %}>{{ n.ftn_address }} — {{ n.name }}</option>
                {% endfor %}
            </select>
        </div>
        <button type="submit" class="btn btn-primary btn-sm">Filter</button>
    </div>
</form>

<div class="card">
    <div class="card-body p-0">
        {% if entries.items %}
        <table class="table table-striped table-sm mb-0">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Node</th>
                    <th>Message</th>
                    <th>Area</th>
                    <th>Status</th>
                    <th>Queued</th>
                    <th>Sent</th>
                </tr>
            </thead>
            <tbody>
                {% for entry in entries.items %}
                <tr>
                    <td style="font-size:0.8rem;">{{ entry.id }}</td>
                    <td>
                        <a href="{{ url_for('hub_admin.binkp_node_detail', node_id=entry.node_id) }}">
                            {{ entry.node.ftn_address if entry.node else '?' }}
                        </a>
                    </td>
                    <td style="font-size:0.8rem;">
                        {% if entry.message %}{{ entry.message.subject[:40] }}{% else %}—{% endif %}
                    </td>
                    <td>
                        {% if entry.message and entry.message.area %}<code>{{ entry.message.area.tag }}</code>{% else %}—{% endif %}
                    </td>
                    <td>
                        {% if entry.status == 'sent' %}<span class="badge bg-success">sent</span>
                        {% elif entry.status == 'pending' %}<span class="badge bg-warning text-dark">pending</span>
                        {% else %}<span class="badge bg-danger">{{ entry.status }}</span>{% endif %}
                    </td>
                    <td style="font-size:0.8rem;">{{ entry.queued_at|eastern('%m-%d %H:%M') }}</td>
                    <td style="font-size:0.8rem;">
                        {% if entry.sent_at %}{{ entry.sent_at|eastern('%m-%d %H:%M') }}{% else %}—{% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>

        <!-- Pagination -->
        {% if entries.pages > 1 %}
        <nav class="d-flex justify-content-center mt-3 mb-2">
            <ul class="pagination pagination-sm">
                {% if entries.has_prev %}
                <li class="page-item"><a class="page-link" href="?page={{ entries.prev_num }}&status={{ status }}&node_id={{ node_id or '' }}">«</a></li>
                {% endif %}
                {% for p in entries.iter_pages() %}
                {% if p %}
                <li class="page-item {% if p==entries.page %}active{% endif %}">
                    <a class="page-link" href="?page={{ p }}&status={{ status }}&node_id={{ node_id or '' }}">{{ p }}</a>
                </li>
                {% else %}
                <li class="page-item disabled"><span class="page-link">…</span></li>
                {% endif %}
                {% endfor %}
                {% if entries.has_next %}
                <li class="page-item"><a class="page-link" href="?page={{ entries.next_num }}&status={{ status }}&node_id={{ node_id or '' }}">»</a></li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}

        {% else %}
        <div class="text-center p-5 text-muted">No hold queue entries matching the filter.</div>
        {% endif %}
    </div>
</div>
{% endblock %}
