{% extends "base.html" %}
{% block title %}Bad Areas — {{ config.BBS_NAME }}{% endblock %}

{% block content %}
<div class="container-fluid my-3">
    <div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
        <div>
            <h2 class="mb-1"><i class="bi bi-exclamation-triangle"></i> Unknown Echo Areas</h2>
            <p class="text-muted mb-0">
                Echomail tags received from peers that don't match any of our
                subscribed areas. Subscribe to one to start carrying it, or
                dismiss to ignore.
            </p>
        </div>
        <div>
            <a href="{{ url_for('echomail_admin.dashboard') }}" class="btn btn-secondary btn-sm">
                <i class="bi bi-arrow-left"></i> Echomail Dashboard
            </a>
            {% if rows %}
            <form method="post"
                  action="{{ url_for('echomail_admin.bad_areas_clear_all') }}"
                  class="d-inline"
                  onsubmit="return confirm('Clear all bad-area entries?');">
                {% if csrf_token is defined %}
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                {% endif %}
                <button class="btn btn-outline-danger btn-sm">
                    <i class="bi bi-trash"></i> Clear All
                </button>
            </form>
            {% endif %}
        </div>
    </div>

    {% if not rows %}
        <div class="alert alert-success">
            No unknown areas pending review.
        </div>
    {% else %}
        <table class="table table-sm table-hover">
            <thead>
                <tr>
                    <th>Network</th>
                    <th>Tag</th>
                    <th>Hits</th>
                    <th>Last Seen</th>
                    <th>Sample From</th>
                    <th>Sample Subject</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                {% for row in rows %}
                <tr>
                    <td>{{ networks[row.network_id].name if row.network_id in networks else '?' }}</td>
                    <td><code>{{ row.tag }}</code></td>
                    <td>{{ row.count }}</td>
                    <td>{{ row.last_seen_at.strftime('%Y-%m-%d %H:%M') if row.last_seen_at else '' }}</td>
                    <td><small>{{ row.sample_from or '' }}</small></td>
                    <td><small>{{ (row.sample_subject or '')[:60] }}</small></td>
                    <td class="text-end">
                        <form method="post"
                              action="{{ url_for('echomail_admin.bad_area_subscribe', bad_id=row.id) }}"
                              class="d-inline">
                            {% if csrf_token is defined %}
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            {% endif %}
                            <button class="btn btn-success btn-sm">
                                <i class="bi bi-plus-circle"></i> Subscribe
                            </button>
                        </form>
                        <form method="post"
                              action="{{ url_for('echomail_admin.bad_area_dismiss', bad_id=row.id) }}"
                              class="d-inline">
                            {% if csrf_token is defined %}
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            {% endif %}
                            <button class="btn btn-outline-secondary btn-sm">
                                <i class="bi bi-x"></i> Dismiss
                            </button>
                        </form>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    {% endif %}
</div>
{% endblock %}
