{% extends "base.html.j2" %}

{% block page_title %}Attached Files{% endblock %}

{% block header_subtitle %}
<p class="text-muted">Manage files attached to <em>{{custom['exam_name']}}</em></p>
{% endblock %}

{% block form_section %}
<!-- No form on this page -->
{% endblock %}

{% block content %}
<div class="mb-2">
    <a href="{{url_for('exams.exam_constructor')}}" class="btn btn-secondary">&larr; Back to Constructor</a>
</div>

<div class="files-container">
    <!-- Local Files -->
    <div class="files-section">
        <div class="card">
            <h2 class="text-left">Available Files</h2>
            <p class="text-muted" style="font-size: 13px;">Select files from your local cache to attach to this exam.</p>
            
            {% if local_files %}
            <ul class="file-list">
                {% for name in local_files %}
                <li class="file-row {% if name in attached_files %}attached{% endif %}">
                    <span class="file-name">
                        {{ name }}
                        {% if name in attached_files %}<span class="attached-badge">attached</span>{% endif %}
                    </span>
                    <span class="file-actions">
                        {% if name not in attached_files %}
                        <form method="post" action="{{url_for('files.attach_to_exam')}}" style="display: inline;">
                            <input type="hidden" name="filename" value="{{ name }}">
                            <input type="hidden" name="exam-name" value="{{ custom['exam_name'] }}">
                            <input type="hidden" name="exam-id" value="{{ custom['exam_id'] }}">
                            <button class="btn btn-primary btn-sm" type="submit">Attach</button>
                        </form>
                        {% endif %}
                    </span>
                </li>
                {% endfor %}
            </ul>
            {% else %}
            <div class="empty-state" style="margin-top: 16px;">
                <p class="text-muted">No files in local cache.</p>
                <a href="{{url_for('files.files_page')}}" class="btn btn-secondary btn-sm mt-1">Upload Files</a>
            </div>
            {% endif %}
        </div>
    </div>
    
    <!-- Attached Files -->
    <div class="files-section">
        <div class="card">
            <h2 class="text-left">Attached to {{custom['exam_name']}}</h2>
            
            {% if attached_files %}
            <ul class="file-list">
                {% for name in attached_files %}
                <li class="file-row">
                    <span class="file-name">{{ name }}</span>
                    <span class="file-actions">
                        <form method="post" action="{{url_for('files.remove_attached')}}" style="display: inline;">
                            <input type="hidden" name="filename" value="{{ name }}">
                            <input type="hidden" name="exam-name" value="{{ custom['exam_name'] }}">
                            <input type="hidden" name="exam-id" value="{{ custom['exam_id'] }}">
                            <button class="btn btn-danger btn-sm" type="submit">Remove</button>
                        </form>
                    </span>
                </li>
                {% endfor %}
            </ul>
            {% else %}
            <div class="empty-state" style="margin-top: 16px;">
                <div class="empty-state-icon">📎</div>
                <p class="text-muted">No files attached to this exam yet.</p>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock content %}
