{% extends 'base.html' %}
{% load static i18n form_filters %}


{% block title %}All Submissions for {{ template.name }} - ATS{% endblock %}

{% block customCSS %}
<style>
    /* ================================================= */
    /* UI Variables (Matching Form Templates List) */
    /* ================================================= */
    :root {
        --kaauh-teal: #00636e;
        --kaauh-teal-dark: #004a53;
        --kaauh-border: #eaeff3;
        --kaauh-primary-text: #343a40;
        --kaauh-gray-light: #f8f9fa;
    }

    /* --- Typography and Color Overrides --- */
    .text-primary { color: var(--kaauh-teal) !important; }

    /* --- Button Base Styles (Matching Form Templates List) --- */
    .btn-main-action {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
        font-weight: 600;
        padding: 0.375rem 0.75rem;
        border-radius: 0.5rem;
        transition: all 0.2s ease;
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
    }
    .btn-main-action:hover {
        background-color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal-dark);
        transform: translateY(-1px);
        box-shadow: 0 4px 8px rgba(0,0,0,0.15);
        color: white;
    }

    /* Secondary Button Style (for Edit/Preview) */
    .btn-outline-secondary {
        color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal);
    }
    .btn-outline-secondary:hover {
        background-color: var(--kaauh-teal-dark);
        color: white;
        border-color: var(--kaauh-teal-dark);
    }

    /* Size Utilities (matching Bootstrap convention) */
    .btn-lg {
        padding: 0.75rem 1.5rem;
        font-size: 1.1rem;
    }
    .btn-sm {
        font-size: 0.8rem;
        padding: 0.3rem 0.6rem;
    }

    /* --- Card and Layout Styles (Matching Form Templates List) --- */
    .card {
        border: 1px solid var(--kaauh-border);
        border-radius: 0.75rem;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
        background-color: white;
        transition: transform 0.2s, box-shadow 0.2s;
    }

    .card-header {
        background-color: var(--kaauh-teal-dark) !important;
        border-bottom: 1px solid var(--kaauh-border);
        color: white !important;
        font-weight: 600;
        padding: 1rem 1.25rem;
        border-radius: 0.75rem 0.75rem 0 0;
    }

    .card-header h1 {
        color: white !important;
        font-weight: 700;
        font-size: 1.5rem;
    }
    .card-header .fas {
        color: white !important;
    }
    .card-header .small {
        color: rgba(255, 255, 255, 0.7) !important;
    }

    .card-body {
        padding: 1.25rem;
    }

    /* --- Compact Table Styles --- */
    .table-responsive {
        border-radius: 0.5rem;
        overflow: auto;
        max-height: 70vh;
        display: flex;
        flex-direction: column;
    }
    .table {
        margin-bottom: 0;
        min-width: max-content;
    }
    .table thead {
        position: sticky;
        top: 0;
        z-index: 10;
    }
    .table thead th {
        background-color: var(--kaauh-teal-dark);
        color: white;
        border-color: var(--kaauh-border);
        font-weight: 600;
        text-transform: uppercase;
        font-size: 0.7rem;
        letter-spacing: 0.3px;
        padding: 0.5rem 0.75rem;
        white-space: nowrap;
    }
    .table tbody td {
        padding: 0.5rem 0.75rem;
        vertical-align: middle;
        border-color: var(--kaauh-border);
        font-size: 0.9rem;
    }
    .table tbody tr {
        transition: background-color 0.2s;
    }
    .table tbody tr:hover {
        background-color: var(--kaauh-gray-light);
    }
    /* Compact form elements */
    .file-response {
        display: flex;
        align-items: center;
        gap: 0.25rem;
    }
    .badge-response {
        margin: 0.05rem;
        font-size: 0.75rem;
        padding: 0.2rem 0.4rem;
    }
    .response-value p {
        margin: 0;
        font-size: 0.85rem;
    }
    .btn-sm {
        font-size: 0.75rem;
        padding: 0.2rem 0.4rem;
    }

    /* --- Pagination Styling (Matching Form Templates List) --- */
    .pagination .page-item .page-link {
        color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-border);
    }
    .pagination .page-item.active .page-link {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
    }
    .pagination .page-item:hover .page-link:not(.active) {
        background-color: #e9ecef;
    }
    .pagination-info {
        color: var(--kaauh-primary-text);
        font-size: 0.9rem;
    }

    /* --- Empty State Theming --- */
    .empty-state {
        text-align: center;
        padding: 3rem 1rem;
        color: var(--kaauh-primary-text);
        border: 2px dashed var(--kaauh-border);
        border-radius: 0.75rem;
        background-color: var(--kaauh-gray-light);
    }
    .empty-state i {
        font-size: 3.5rem;
        margin-bottom: 1rem;
        color: var(--kaauh-teal-dark);
    }
    .empty-state .btn-main-action .fas {
        color: white !important;
    }

    /* --- Breadcrumb --- */
    .breadcrumb {
        background-color: transparent;
        padding: 0;
        margin-bottom: 1rem;
    }
    .breadcrumb-item a {
        color: var(--kaauh-teal-dark);
        text-decoration: none;
    }
    .breadcrumb-item a:hover {
        text-decoration: underline;
    }
    .breadcrumb-item.active {
        color: var(--kaauh-primary-text);
    }

    /* --- Response Value Styling --- */
    .response-value {
        word-break: break-word;
        max-width: 200px;
    }
    .file-response {
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }
    .badge-response {
        margin: 0.1rem;
    }
</style>
{% endblock %}

{% block content %}
<div class="container py-4">
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{% url 'dashboard' %}" class="text-secondary text-decoration-none">{% trans "Dashboard" %}</a></li>
            <li class="breadcrumb-item"><a href="{% url 'form_templates_list' %}" class="text-secondary text-decoration-none">{% trans "Form Templates" %}</a></li>
            <li class="breadcrumb-item"><a href="{% url 'form_template_submissions_list' template.slug %}" class="text-secondary text-decoration-none">{% trans "Submissions" %}</a></li>
            <li class="breadcrumb-item active" style="
                            color: #F43B5E; 
                            font-weight: 600;
                        ">{% trans "All Submissions Table" %}</li>
        </ol>
    </nav>

    <div class="card shadow-sm">
        <div class="card-header d-flex justify-content-between align-items-center">
            <div>
                <h1 class="h3 mb-1 d-flex align-items-center">
                    <i class="fas fa-table me-2"></i>
                    {% trans "All Submissions for" %}: <span class="text-white ms-2">{{ template.name }}</span>
                </h1>
                <small class="text-white-50">Template ID: #{{ template.id }}</small>
            </div>
            <a href="{% url 'form_template_submissions_list' template.slug %}" class="btn btn-outline-light btn-sm">
                <i class="fas fa-arrow-left me-1"></i> {% trans "Back to Submissions" %}
            </a>
        </div>
        <div class="card-body">
            {% if page_obj.object_list %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th scope="col">{% trans "Submission ID" %}</th>
                                <th scope="col">{% trans "Applicant Name" %}</th>
                                <th scope="col">{% trans "Applicant Email" %}</th>
                                <th scope="col">{% trans "Submitted At" %}</th>
                                {% for field in fields %}
                                    <th scope="col">{{ field.label }}</th>
                                {% endfor %}
                            </tr>
                        </thead>
                        <tbody>
                            {% for submission in page_obj %}
                            <tr>
                                <td class="fw-medium">{{ submission.id }}</td>
                                <td>{{ submission.applicant_name|default:"N/A" }}</td>
                                <td>{{ submission.applicant_email|default:"N/A" }}</td>
                                <td>{{ submission.submitted_at|date:"M d, Y H:i" }}</td>
                                {% for field in fields %}
                                    {% get_field_response_for_submission submission field as response %}
                                    <td class="response-value">
                                        {% if response %}
                                            {% if response.uploaded_file %}
                                                <div class="file-response">

                                                    <a href="{{ response.uploaded_file.url }}" class="btn btn-sm btn-outline-primary" target="_blank" title="Download File">
                                                        <i class="fas fa-download"></i>
                                                    </a>
                                                </div>
                                            {% elif response.value %}
                                                {% if response.field.field_type == 'checkbox' and response.value|length > 0 %}
                                                    <div>
                                                        {% for val in response.value|to_list %}
                                                            <span class="badge bg-secondary badge-response">{{ val }}</span>
                                                        {% endfor %}
                                                    </div>
                                                {% elif response.field.field_type == 'radio' or response.field.field_type == 'select' %}
                                                    <span class="badge bg-info">{{ response.value }}</span>
                                                {% else %}
                                                    <p class="mb-0">{{ response.value|linebreaksbr|truncatewords:10 }}</p>
                                                {% endif %}
                                            {% else %}
                                                <span class="text-muted">Not provided</span>
                                            {% endif %}
                                        {% else %}
                                            <span class="text-muted">Not provided</span>
                                        {% endif %}
                                    </td>
                                {% endfor %}
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                {% if page_obj.has_other_pages %}
                    <div class="d-flex flex-column flex-md-row justify-content-between align-items-center mt-4">
                        <div class="pagination-info mb-3 mb-md-0">
                            {% blocktrans with start=page_obj.start_index end=page_obj.end_index total=page_obj.paginator.count %}
                                Showing {{ start }} to {{ end }} of {{ total }} results.
                            {% endblocktrans %}
                        </div>
                        <nav aria-label="Page navigation">
                            <ul class="pagination pagination-sm mb-0">
                                {% if page_obj.has_previous %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page=1" aria-label="First">
                                            <span aria-hidden="true">&laquo;</span>
                                        </a>
                                    </li>
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
                                            <span aria-hidden="true">&lsaquo;</span>
                                        </a>
                                    </li>
                                {% endif %}

                                <li class="page-item active">
                                    <span class="page-link">
                                        {% trans "Page" %} {{ page_obj.number }} {% trans "of" %} {{ page_obj.paginator.num_pages }}
                                    </span>
                                </li>

                                {% if page_obj.has_next %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
                                            <span aria-hidden="true">&rsaquo;</span>
                                        </a>
                                    </li>
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}" aria-label="Last">
                                            <span aria-hidden="true">&raquo;</span>
                                        </a>
                                    </li>
                                {% endif %}
                            </ul>
                        </nav>
                    </div>
                {% endif %}
            {% else %}
                <div class="empty-state">
                    <i class="fas fa-inbox"></i>
                    <h3 class="h5 mb-3">{% trans "No Submissions Found" %}</h3>
                    <p class="text-muted mb-4">
                        {% trans "There are no submissions for this form template yet." %}
                    </p>
                    <a href="{% url 'form_template_submissions_list' template.slug %}" class="btn btn-main-action">
                        <i class="fas fa-arrow-left me-1"></i> {% trans "Back to Submissions" %}
                    </a>
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
