{% extends "base.html" %}
{% block title %}Poll Logs — {{ config.BBS_NAME }}{% endblock %}

{% block content %}
<div class="bbs-header">
    <h1><i class="bi bi-journal-text"></i> Poll History</h1>
</div>

<div class="d-flex justify-content-between align-items-center mb-3">
    <a href="{{ url_for('echomail_admin.dashboard') }}" class="btn btn-primary btn-sm">
        <i class="bi bi-arrow-left"></i> Dashboard
    </a>
    <form method="get" class="d-flex gap-2">
        <select name="network_id" class="form-control form-control-sm" style="width:200px;">
            <option value="">All Networks</option>
            {% for net in all_networks %}
            <option value="{{ net.id }}" {% if network_filter == net.id %}selected{% endif %}>{{ net.name }}</option>
            {% endfor %}
        </select>
        <button type="submit" class="btn btn-primary btn-sm">Filter</button>
    </form>
</div>

{% if logs %}
<div class="card">
    <div class="card-body p-0">
        <table class="table table-striped mb-0">
            <thead>
                <tr>
                    <th>Network</th><th>Started</th><th>Completed</th>
                    <th>Type</th><th>Status</th><th>Sent</th><th>Received</th><th>Error</th>
                </tr>
            </thead>
            <tbody>
                {% for log in logs %}
                <tr>
                    <td>{{ log.network.name }}</td>
                    <td style="font-size:0.8rem; white-space:nowrap;">{{ log.started_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                    <td style="font-size:0.8rem; white-space:nowrap;">
                        {{ log.completed_at.strftime('%H:%M:%S') if log.completed_at else '—' }}
                    </td>
                    <td>{{ log.poll_type }}</td>
                    <td>
                        {% if log.status == 'success' %}<span style="color:#00ff00;">✓ success</span>
                        {% elif log.status == 'error' %}<span style="color:#ff0000;">✗ error</span>
                        {% else %}<span style="color:#ffff00;">~ {{ log.status }}</span>{% endif %}
                    </td>
                    <td>{{ log.messages_sent or 0 }}</td>
                    <td>{{ log.messages_received or 0 }}</td>
                    <td style="font-size:0.8rem; color:#ff6600; max-width:300px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;"
                        title="{{ log.error_message or '' }}">
                        {{ log.error_message or '' }}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>

{% if pagination.pages > 1 %}
<nav class="mt-3">
    <ul class="pagination justify-content-center">
        {% if pagination.has_prev %}
        <li class="page-item">
            <a class="page-link" href="{{ url_for('echomail_admin.logs', page=pagination.prev_num, network_id=network_filter or '') }}">‹ Prev</a>
        </li>
        {% endif %}
        {% for p in pagination.iter_pages() %}
            {% if p %}
            <li class="page-item {{ 'active' if p == pagination.page else '' }}">
                <a class="page-link" href="{{ url_for('echomail_admin.logs', page=p, network_id=network_filter or '') }}">{{ p }}</a>
            </li>
            {% else %}
            <li class="page-item disabled"><span class="page-link">…</span></li>
            {% endif %}
        {% endfor %}
        {% if pagination.has_next %}
        <li class="page-item">
            <a class="page-link" href="{{ url_for('echomail_admin.logs', page=pagination.next_num, network_id=network_filter or '') }}">Next ›</a>
        </li>
        {% endif %}
    </ul>
</nav>
{% endif %}

{% else %}
<div class="card">
    <div class="card-body text-center p-4">
        <i class="bi bi-journal-text" style="font-size:3rem;"></i>
        <p class="mt-3">No poll logs yet.</p>
    </div>
</div>
{% endif %}
{% endblock %}
