{% extends "base.html" %}

{% block title %}{{ area.tag }} - File Area{% endblock %}

{% block content %}
<div class="bbs-header">
    <h1><i class="bi bi-folder-fill"></i> {{ area.name or area.tag }}</h1>
    <p><code>{{ area.tag }}</code>{% if area.description %} — {{ area.description }}{% endif %}</p>
    <p class="text-muted small">
        Upload: <code>{{ area.upload_permission }}</code>
        {% if area.password %} — password-protected{% endif %}
    </p>
    {% if current_user.is_admin %}
    <p class="text-muted small">Storage: <code>{{ area.storage_path or '(not set)' }}</code></p>
    <form method="post" action="{{ url_for('file_areas.rescan_descriptions', area_id=area.id) }}" class="d-inline">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <button class="btn btn-sm btn-outline-secondary" title="Drop the cached FILE_ID.DIZ descriptions and re-extract from each archive">
            <i class="bi bi-arrow-clockwise"></i> Re-scan descriptions
        </button>
    </form>
    {% endif %}
</div>

<div class="card mb-3">
    <div class="card-body p-0">
    {% if files %}
        <table class="table table-sm table-hover mb-0">
            <thead><tr>
                {% macro sort_hdr(label, col, default_dir) %}
                    {% set active = sort == col or sort == '-' ~ col %}
                    {% if sort == col %}{% set next = '-' ~ col %}{% elif sort == '-' ~ col %}{% set next = col %}{% else %}{% set next = default_dir %}{% endif %}
                    <th><a href="?sort={{ next }}" class="text-decoration-none {{ 'text-info fw-bold' if active else 'text-white-50' }}">
                        {{ label }}{% if sort == col %} ▲{% elif sort == '-' ~ col %} ▼{% endif %}
                    </a></th>
                {% endmacro %}
                {{ sort_hdr('Filename', 'name', 'name') }}
                {{ sort_hdr('Size', 'size', '-size') }}
                {{ sort_hdr('Date', 'date', '-date') }}
                <th class="text-white-50">Description</th>
            </tr></thead>
            <tbody>
            {% for f in files %}
                <tr>
                    <td>
                        {% if f.is_image %}
                        <img src="{{ url_for('file_areas.thumbnail', area_id=area.id, filename=f.name) }}"
                             alt="" style="max-height:48px;max-width:64px;vertical-align:middle;margin-right:8px;border:1px solid #444;">
                        {% endif %}
                        <a href="{{ url_for('file_areas.download', area_id=area.id, filename=f.name) }}">
                            <i class="bi bi-download"></i> {{ f.name }}
                        </a>
                    </td>
                    <td>{{ '%.1f'|format(f.size / 1024) }} KB</td>
                    <td>{{ f.mtime|eastern('%Y-%m-%d') }}</td>
                    <td>
                        {% if f.description %}
                        {% set short_desc = f.description.split('\n')[0][:120] %}
                        <small class="text-muted">{{ short_desc | strip_ansi }}</small>
                        {% if '\n' in f.description or f.description|length > 120 %}
                        <details class="mt-1">
                            <summary class="small text-secondary">full description</summary>
                            <pre class="mt-1 p-2 rounded" style="background:#000;color:#aaaaaa;white-space:pre;overflow-x:auto;overflow-y:auto;max-height:320px;font-family:'Courier New',Courier,monospace;font-size:0.8rem;line-height:1.2;">{{ f.description | ansi_art }}</pre>
                        </details>
                        {% endif %}
                        {% endif %}
                        <details class="mt-1">
                            <summary class="small">share</summary>
                            <form method="post" action="{{ url_for('file_areas.create_share', area_id=area.id, filename=f.name) }}" class="mt-1 d-flex gap-1">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                <input type="text" name="note" class="form-control form-control-sm" placeholder="note (optional)">
                                <input type="number" name="hours_valid" class="form-control form-control-sm" placeholder="hours" min="1" style="max-width:80px;">
                                <input type="number" name="max_downloads" class="form-control form-control-sm" placeholder="max DL" min="1" style="max-width:80px;">
                                <button class="btn btn-sm btn-outline-info">share</button>
                            </form>
                        </details>
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    {% else %}
        <div class="p-3 text-muted">No files in this area yet.</div>
    {% endif %}
    </div>
</div>

{% if pagination and pagination.pages > 1 %}
<nav aria-label="File area pages">
    <ul class="pagination justify-content-center">
        {% if pagination.has_prev %}
        <li class="page-item"><a class="page-link" href="{{ url_for('file_areas.view_area', area_id=area.id, sort=sort, page=pagination.prev_num) }}">‹ Prev</a></li>
        {% else %}
        <li class="page-item disabled"><span class="page-link">‹ Prev</span></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('file_areas.view_area', area_id=area.id, sort=sort, page=p) }}">{{ 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('file_areas.view_area', area_id=area.id, sort=sort, page=pagination.next_num) }}">Next ›</a></li>
        {% else %}
        <li class="page-item disabled"><span class="page-link">Next ›</span></li>
        {% endif %}
    </ul>
    <p class="text-center text-muted small">Page {{ pagination.page }} of {{ pagination.pages }} — {{ pagination.total }} file{{ '' if pagination.total == 1 else 's' }} total</p>
</nav>
{% endif %}

{% set can_upload = (area.upload_permission == 'users') or
                    (area.upload_permission == 'sysop' and current_user.is_admin) %}
{% if can_upload %}
<div class="card">
    <div class="card-header"><i class="bi bi-upload"></i> Upload File</div>
    <div class="card-body">
        <form method="post" action="{{ url_for('file_areas.upload', area_id=area.id) }}"
              enctype="multipart/form-data" class="row g-2 align-items-end">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <div class="col-md-6">
                <label class="form-label">File</label>
                <input type="file" name="file" class="form-control" required>
            </div>
            {% if area.password %}
            <div class="col-md-3">
                <label class="form-label">Area Password</label>
                <input type="password" name="password" class="form-control" required>
            </div>
            {% endif %}
            <div class="col-md-3">
                <button class="btn btn-primary w-100"><i class="bi bi-upload"></i> Upload</button>
            </div>
        </form>
    </div>
</div>
{% endif %}

<div class="mt-3">
    <a href="{{ url_for('file_areas.index') }}" class="btn btn-link">← All file areas</a>
</div>
{% endblock %}
