{% extends "base.html" %}

{% block title %}File Areas - Admin{% endblock %}

{% block content %}
<div class="bbs-header">
    <h1><i class="bi bi-folder-symlink"></i> File Areas (Admin)</h1>
</div>

<div class="d-flex justify-content-end mb-3">
    <a href="{{ url_for('admin.file_areas_bulk_import') }}" class="btn btn-primary btn-sm">
        <i class="bi bi-upload"></i> Bulk Import (Backbone File)
    </a>
</div>

{% set binkp_nets = networks|selectattr('network_type', 'equalto', 'binkp')|list %}
{% if binkp_nets %}
<details class="card mb-3" style="border-color:#006600;">
    <summary class="card-header" style="background:#001100; cursor:pointer;">
        <i class="bi bi-cloud-arrow-up"></i> FileFix — Subscribe/Unsubscribe to All File Areas (per Network)
    </summary>
    <div class="card-body">
        <p class="text-muted small mb-2">
            Sends a FileFix <code>+ALL</code> netmail to the hub asking it
            to feed every file echo it carries, and flips the local
            <em>subscribed</em> flag on every <code>FileArea</code> row for
            this network. Run <strong>Bulk Import</strong> first if your
            local list is empty.
        </p>
        {% for net in binkp_nets %}
        <div class="row g-2 mb-2 align-items-center">
            <div class="col-md-3"><strong>{{ net.name }}</strong>
                <div class="text-muted" style="font-size:0.75rem;">{{ net.hub_address or '(no hub)' }}</div>
            </div>
            <div class="col-md-9 d-flex gap-2">
                <form method="post"
                      action="{{ url_for('admin.file_areas_subscribe_all', network_id=net.id) }}"
                      onsubmit="return confirm('Subscribe to ALL file areas on {{ net.name }}? Sends FileFix +ALL to {{ net.hub_address }} and marks every local FileArea row subscribed.');">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button class="btn btn-success btn-sm">
                        <i class="bi bi-bell"></i> Subscribe to ALL
                    </button>
                </form>
                <form method="post"
                      action="{{ url_for('admin.file_areas_unsubscribe_all', network_id=net.id) }}"
                      onsubmit="return confirm('Unsubscribe from ALL file areas on {{ net.name }}? Sends FileFix -ALL and clears every local subscribed flag.');">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button class="btn btn-warning btn-sm">
                        <i class="bi bi-bell-slash"></i> Unsubscribe from ALL
                    </button>
                </form>
            </div>
        </div>
        {% endfor %}
    </div>
</details>
{% endif %}

<div class="card mb-3">
    <div class="card-header"><i class="bi bi-plus-square"></i> Create Area</div>
    <div class="card-body">
        <form method="post" class="row g-2">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <input type="hidden" name="action" value="create">
            <div class="col-md-3">
                <label class="form-label">Tag</label>
                <input type="text" name="tag" class="form-control" required
                       placeholder="FILES.GAMES">
            </div>
            <div class="col-md-3">
                <label class="form-label">Name</label>
                <input type="text" name="name" class="form-control">
            </div>
            <div class="col-md-3">
                <label class="form-label">Storage Path</label>
                <input type="text" name="storage_path" class="form-control"
                       placeholder="/var/lib/anetbbs/file_areas/games">
            </div>
            <div class="col-md-2">
                <label class="form-label">Network</label>
                <select name="network_id" class="form-select">
                    <option value="">— local —</option>
                    {% for n in networks %}<option value="{{ n.id }}">{{ n.name }}</option>{% endfor %}
                </select>
            </div>
            <div class="col-md-1 d-flex align-items-end">
                <button class="btn btn-primary w-100">Add</button>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body p-0">
    {% if areas %}
        {% for fa in areas %}
        <form method="post" class="border-bottom p-2">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <input type="hidden" name="action" value="update">
            <input type="hidden" name="area_id" value="{{ fa.id }}">
            <div class="row g-2 align-items-end">
                <div class="col-md-2"><strong><code>{{ fa.tag }}</code></strong></div>
                <div class="col-md-3">
                    <label class="form-label small mb-0">Name</label>
                    <input type="text" name="name" class="form-control form-control-sm" value="{{ fa.name or '' }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label small mb-0">Storage</label>
                    <input type="text" name="storage_path" class="form-control form-control-sm" value="{{ fa.storage_path or '' }}">
                </div>
                <div class="col-md-1">
                    <label class="form-label small mb-0">Upload</label>
                    <select name="upload_permission" class="form-select form-select-sm">
                        <option value="users" {% if fa.upload_permission == 'users' %}selected{% endif %}>users</option>
                        <option value="sysop" {% if fa.upload_permission == 'sysop' %}selected{% endif %}>sysop</option>
                        <option value="none"  {% if fa.upload_permission == 'none' %}selected{% endif %}>none</option>
                    </select>
                </div>
                <div class="col-md-1">
                    <label class="form-label small mb-0">Password</label>
                    <input type="text" name="password" class="form-control form-control-sm" value="{{ fa.password or '' }}">
                </div>
                <div class="col-md-2 d-flex flex-wrap">
                    <div class="form-check me-2"><input type="checkbox" name="is_active" {% if fa.is_active %}checked{% endif %} class="form-check-input"><label class="form-check-label small">Active</label></div>
                    <div class="form-check me-2"><input type="checkbox" name="is_subscribed" {% if fa.is_subscribed %}checked{% endif %} class="form-check-input"><label class="form-check-label small">Subscribed</label></div>
                    <div class="form-check"><input type="checkbox" name="is_sysop_only" {% if fa.is_sysop_only %}checked{% endif %} class="form-check-input"><label class="form-check-label small">Sysop-only</label></div>
                </div>
            </div>
            {# Nodelist auto-import. When checked, TICs landing in this area
               are unwrapped and the nodelist text is imported into the
               Nodelist table for /nodelist/ browsing — tagged by domain. #}
            <div class="row g-2 mt-2">
                <div class="col-md-3">
                    <div class="form-check"><input type="checkbox" name="is_nodelist_source" {% if fa.is_nodelist_source %}checked{% endif %} class="form-check-input"><label class="form-check-label small">Nodelist source (auto-import inbound TICs)</label></div>
                </div>
                <div class="col-md-3">
                    <label class="form-label small mb-0">Nodelist domain</label>
                    <input type="text" name="nodelist_domain" class="form-control form-control-sm" value="{{ fa.nodelist_domain or '' }}" placeholder="e.g. tqwnet">
                </div>
            </div>
            <div class="mt-2">
                <button class="btn btn-primary btn-sm">Save</button>
                <button class="btn btn-outline-danger btn-sm" name="action" value="delete"
                        onclick="return confirm('Delete area {{ fa.tag }}? Files on disk are NOT removed.')">Delete</button>
                <a href="{{ url_for('file_areas.view_area', area_id=fa.id) }}" class="btn btn-outline-secondary btn-sm">Browse</a>
            </div>
        </form>
        {% endfor %}
    {% else %}
        <div class="p-3 text-muted">No file areas configured. Create one above or wait for the first inbound TIC.</div>
    {% endif %}
    </div>
</div>
{% endblock %}
