{% extends "base.html" %}
{% block title %}Last Callers Admin{% endblock %}
{% block content %}
<div class="bbs-header">
    <h1><i class="bi bi-people"></i> Last Callers</h1>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-12">
        <div class="card p-3">
            <h6 class="mb-2">InterBBS Sharing</h6>
            <form method="post" action="{{ url_for('lastcallers_admin.settings') }}">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <div class="d-flex gap-2 align-items-center flex-wrap">
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="interbbs_enabled"
                               id="interbbs_enabled" {{ 'checked' if interbbs_enabled }}>
                        <label class="form-check-label" for="interbbs_enabled">
                            Enable InterBBS Last Callers — share recent-caller entries with other ANetBBS installs over echomail
                        </label>
                    </div>
                    <div class="d-flex align-items-center gap-2">
                        <label for="interbbs_network_id" class="form-label mb-0">Network:</label>
                        <select class="form-select form-select-sm w-auto" name="interbbs_network_id" id="interbbs_network_id">
                            <option value="">— pick a network —</option>
                            {% for net in networks %}
                            <option value="{{ net.id }}" {{ 'selected' if interbbs_network_id|string == net.id|string }}>{{ net.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="form-text mt-1 mb-3">BinkP networks only — QWK areas are identified by conference number, not by name, so a QWK network can never carry the InterBBS Last Callers data area. Only service (telnet/ssh/web/...) and login time are shared — IP addresses are never relayed across BBS boundaries.</div>

                <hr>
                <h6 class="mb-2">Display</h6>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="hide_sysop"
                           id="hide_sysop" {{ 'checked' if hide_sysop }}>
                    <label class="form-check-label" for="hide_sysop">
                        Hide sysop logins from the Last Callers displays
                    </label>
                </div>
                <div class="form-text mt-1 mb-3">Keeps a frequently-logged-in sysop from flooding the user-facing Last Callers list (terminal screen, terminal inline block, and the web one-liners page) — this admin list below always shows everything regardless of this setting. Also keeps sysop logins from being relayed to InterBBS Last Callers above, so testing heavily on your own account doesn't flood every other BBS on the network with your logins too.</div>

                <div class="d-flex align-items-center gap-2 mb-1">
                    <label for="display_count" class="form-label mb-0">Rows to show:</label>
                    <select class="form-select form-select-sm w-auto" name="display_count" id="display_count">
                        {% for n in [5, 10, 15, 20, 25, 30, 50, 100] %}
                        <option value="{{ n }}" {{ 'selected' if display_count == n }}>{{ n }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="form-text mt-1 mb-3">How many entries the terminal Last Callers screen shows. Does not affect this admin list, or the web one-liners page's "Last 10" preview.</div>

                <button type="submit" class="btn btn-sm btn-primary">Apply</button>
            </form>
        </div>
    </div>
</div>

{% if pagination.items %}
<div class="table-responsive">
<table class="table table-sm table-hover align-middle">
    <thead>
        <tr>
            <th>#</th>
            <th>User</th>
            <th>Service</th>
            <th>Source</th>
            <th>When</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        {% for c in pagination.items %}
        <tr>
            <td>{{ c.id }}</td>
            <td>{{ c.username or '?' }}</td>
            <td>{{ c.service or '?' }}</td>
            <td>{% if c.origin_bbs %}<span class="badge bg-info text-dark" title="Imported via InterBBS Last Callers">{{ c.origin_bbs }}</span>{% else %}<span class="text-muted small">local</span>{% endif %}</td>
            <td><small>{{ c.started_at|eastern('%Y-%m-%d %H:%M', '?') }}</small></td>
            <td>{% if not c.origin_bbs %}<a href="{{ url_for('admin.caller_log_detail', caller_log_id=c.id) }}"
                   class="btn btn-sm btn-outline-primary">View Activity</a>{% endif %}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>
</div>

{% if pagination.pages > 1 %}
<nav>
    <ul class="pagination pagination-sm">
        {% for p in pagination.iter_pages() %}
            {% if p %}
            <li class="page-item {{ 'active' if p == pagination.page }}">
                <a class="page-link" href="{{ url_for('lastcallers_admin.index', page=p) }}">{{ p }}</a>
            </li>
            {% else %}
            <li class="page-item disabled"><span class="page-link">…</span></li>
            {% endif %}
        {% endfor %}
    </ul>
</nav>
{% endif %}

{% else %}
<p class="text-muted">No callers logged yet.</p>
{% endif %}
{% endblock %}
