{% extends 'base.html' %}
{% load static i18n crispy_forms_tags %}
{% load partials %}

{% block title %}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;
    }

    /* --- Table Styles --- */
    .table-responsive {
        border-radius: 0.5rem;
        overflow: hidden;
    }
    .table {
        margin-bottom: 0;
    }
    .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.8rem;
        letter-spacing: 0.5px;
        padding: 1rem;
    }
    .table tbody td {
        padding: 1rem;
        vertical-align: middle;
        border-color: var(--kaauh-border);
    }
    .table tbody tr {
        transition: background-color 0.2s;
    }
    .table tbody tr:hover {
        background-color: var(--kaauh-gray-light);
    }

    /* --- 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);
    }
</style>
{% endblock %}

{% block content %}
<div class="container py-4">
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{% url 'dashboard' %}">{% trans "Dashboard" %}</a></li>
            <li class="breadcrumb-item"><a href="{% url 'form_templates_list' %}">{% trans "Form Templates" %}</a></li>
            <li class="breadcrumb-item active">{% trans "Submissions" %}</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-file-alt me-2"></i>
                    {% trans "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_templates_list' %}" class="btn btn-outline-light btn-sm">
                <i class="fas fa-arrow-left me-1"></i> {% trans "Back to Templates" %}
            </a>
        </div>
        <div class="card-body">
            {% if page_obj.object_list %}
                <div id="form-template-submissions-list">
                    {# View Switcher #}
                    {% include "includes/_list_view_switcher.html" with list_id="form-template-submissions-list" %}

                    {# Table View (Default) #}
                    <div class="table-view active">
                        <div class="table-responsive mb-4">
                            <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>
                                        <th scope="col" class="text-end">{% trans "Actions" %}</th>
                                    </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>
                                        <td class="text-end">
                                            <a href="{% url 'form_submission_details' template_id=template.id submission_id=submission.id  %}" class="btn btn-sm btn-outline-primary">
                                                <i class="fas fa-eye me-1"></i> {% trans "View Details" %}
                                            </a>
                                        </td>                     
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>

                    {# Card View #}
                    <div class="card-view">
                        <div class="row g-4">
                            {% for submission in page_obj %}
                            <div class="col-lg-4 col-md-6">
                                <div class="card h-100">
                                    <div class="card-header">
                                        <h3 class="h5 mb-2">{% trans "Submission" %} #{{ submission.id }}</h3>
                                        <small class="text-white-50">{{ template.name }}</small>
                                    </div>
                                    <div class="card-body">
                                        <p class="card-text">
                                            <strong>{% trans "Applicant Name" %}:</strong> {{ submission.applicant_name|default:"N/A" }}<br>
                                            <strong>{% trans "Applicant Email" %}:</strong> {{ submission.applicant_email|default:"N/A" }}<br>
                                            <strong>{% trans "Submitted At" %}:</strong> {{ submission.submitted_at|date:"M d, Y H:i" }}
                                        </p>
                                    </div>
                                    <div class="card-footer">
                                        <a href="{% url 'form_submission_details' template_id=template.id submission_id=submission.id %}" class="btn btn-sm btn-outline-primary w-100">
                                            <i class="fas fa-eye me-1"></i> {% trans "View Details" %}
                                        </a>
                                    </div>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>

                <!-- Pagination -->
                {% if page_obj.has_other_pages %}
                    <div class="d-flex flex-column flex-md-row justify-content-between align-items-center">
                        <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_templates_list' %}" class="btn btn-main-action">
                        <i class="fas fa-arrow-left me-1"></i> {% trans "Back to Templates" %}
                    </a>
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
