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

{% block title %}Form Templates - {{ block.super }}{% endblock %}

{% block customCSS %}
<style>
    /* ================================================= */
    /* UI Variables (Matching Standard Theme) */
    /* ================================================= */
    :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 (Consistent) --- */
    .btn-main-action {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
        font-weight: 600;
        transition: all 0.2s ease;
        display: inline-flex;
        align-items: center;
        gap: 0.4rem;
        padding: 0.5rem 1rem;
    }
    .btn-main-action:hover {
        background-color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal-dark);
        transform: none; /* Removed translate to match other lists */
        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);
    }
    /* Primary Outline for View/Preview */
    .btn-outline-primary {
        color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
    }
    .btn-outline-primary:hover {
        background-color: var(--kaauh-teal);
        color: white;
    }

    /* --- Card and Layout Styles (Consistent) --- */
    .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:not(.no-hover):hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 16px rgba(0,0,0,0.1) !important;
    }
    .card.no-hover:hover {
        transform: none;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
    }
    
    /* Card Header (For Search/Filter Card) */
    .card-header {
        font-weight: 600;
        padding: 1.25rem;
        border-bottom: 1px solid var(--kaauh-border);
        background-color: var(--kaauh-gray-light); 
    }

    /* Stats Theming */
    .stat-value {
        font-size: 1.5rem;
        font-weight: 800;
        color: var(--kaauh-teal-dark);
    }
    .stat-label {
        font-size: 0.85rem;
        color: var(--kaauh-primary-text);
        font-weight: 500;
    }

    /* Table Styling (Consistent) */
    .table-view .table thead th {
        background-color: var(--kaauh-teal-dark);
        color: white;
        font-weight: 600;
        border-color: var(--kaauh-border);
        text-transform: uppercase;
        font-size: 0.8rem;
        letter-spacing: 0.5px;
        padding: 1rem;
    }
    .table-view .table tbody td {
        vertical-align: middle;
        padding: 1rem;
        border-color: var(--kaauh-border);
    }
    .table-view .table tbody tr:hover {
        background-color: var(--kaauh-gray-light);
    }

    /* Pagination Styling (Consistent) */
    .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;
    }

    /* Empty State Icon Color */
    .empty-state i, .text-center i.fa-3x {
        color: var(--kaauh-teal-dark) !important;
    }

    /* Filter Buttons Container */
    .filter-buttons {
        display: flex;
        gap: 0.5rem;
    }

</style>
{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0" style="color: var(--kaauh-teal-dark); font-weight: 700;">
            <i class="fas fa-file-alt me-2"></i>{% trans "Form Templates" %}
        </h1>
        <button type="button" class="btn btn-main-action" data-bs-toggle="modal" data-bs-target="#createTemplateModal">
            <i class="fas fa-plus me-1"></i> {% trans "Create New Template" %}
        </button>
    </div>

    {# Search/Filter Area - Matching Standard Structure #}
    <div class="card mb-4 shadow-sm no-hover">
        <div class="card-body">
            <form method="get" class="row g-3 align-items-end">

                <div class="col-md-6">
                    <label for="searchInput" class="form-label small text-muted">{% trans "Search by Template Name" %}</label>
                    <div class="input-group input-group-lg">
                        <span class="input-group-text"><i class="fas fa-search text-muted"></i></span>
                        <input type="text" name="q" id="searchInput" class="form-control form-control-search"
                            placeholder="{% trans 'Search templates by name...' %}"
                            value="{{ query|default_if_none:'' }}">
                    </div>
                </div>

                <div class="col-md-6">
                    <div class="filter-buttons">
                        <button type="submit" class="btn btn-main-action btn-lg">
                            <i class="fas fa-filter me-1"></i> {% trans "Search" %}
                        </button>

                        {# Show Clear button if search is active #}
                        {% if query %}
                        <a href="{% url 'form_templates_list' %}" class="btn btn-outline-secondary btn-lg">
                            <i class="fas fa-times me-1"></i> {% trans "Clear Search" %}
                        </a>
                        {% endif %}
                    </div>
                </div>

            </form>
        </div>
    </div>

    {% if templates %}
        <div id="form-templates-list">
            {# View Switcher #}
            {% include "includes/_list_view_switcher.html" with list_id="form-templates-list" %}

            {# Card View (Default) #}
            <div class="card-view active row g-4">
                {% for template in templates %}
                <div class="col-lg-4 col-md-6">
                    <div class="card template-card h-100 shadow-sm">
                        <div class="card-body d-flex flex-column">
                            <h5 class="card-title fw-bold" style="color: var(--kaauh-teal-dark);">{{ template.name }}</h5>
                            <span class="text-muted small mb-3">
                                <i class="fas fa-briefcase me-1"></i> {{ template.job|default:"N/A" }}
                            </span>
                            
                            {# Stats #}
                            <div class="row text-center mb-3">
                                <div class="col-6 border-end">
                                    <div class="stat-value">{{ template.get_stage_count }}</div>
                                    <div class="stat-label">{% trans "Stages" %}</div>
                                </div>
                                <div class="col-6">
                                    <div class="stat-value">{{ template.get_field_count }}</div>
                                    <div class="stat-label">{% trans "Fields" %}</div>
                                </div>
                            </div>
                            
                            {# Description #}
                            <p class="card-text small text-muted flex-grow-1">
                                {% if template.description %}
                                    {{ template.description|truncatewords:20 }}
                                {% else %}
                                    <em class="text-muted">{% trans "No description provided" %}</em>
                                {% endif %}
                            </p>

                            {# Action area #}
                            <div class="mt-auto pt-2 border-top">
                                <div class="d-flex gap-2 justify-content-end">

                                    <a href="{% url 'form_wizard' template.id %}" class="btn btn-outline-primary btn-sm" title="{% trans 'Preview' %}">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    <a href="{% url 'form_builder' template.id %}" class="btn btn-outline-secondary btn-sm" title="{% trans 'Edit' %}">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="{% url 'form_template_submissions_list' template.slug %}" class="btn btn-outline-secondary btn-sm" title="{% trans 'Submissions' %}">
                                        <i class="fas fa-file-alt"></i>
                                    </a>
                                    <button type="button" class="btn btn-outline-danger btn-sm" title="{% trans 'Delete' %}"
                                            data-bs-toggle="modal" data-bs-target="#deleteModal"
                                            data-delete-url="#"
                                            data-item-name="{{ template.name }}">
                                        <i class="fas fa-trash-alt"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                        <div class="card-footer bg-light text-muted small">
                            <div class="d-flex justify-content-between">
                                <span><i class="fas fa-calendar-alt me-1"></i> {% trans "Created:" %} {{ template.created_at|date:"M d, Y" }}</span>
                                <span><i class="fas fa-sync-alt me-1"></i> {{ template.updated_at|timesince }} {% trans "ago" %}</span>
                            </div>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>

            {# Table View #}
            <div class="table-view">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead>
                            <tr>
                                <th scope="col" style="width: 30%;">{% trans "Template Name" %}</th>
                                <th scope="col" style="width: 15%;">{% trans "Job" %}</th>
                                <th scope="col" style="width: 8%;">{% trans "Stages" %}</th>
                                <th scope="col" style="width: 8%;">{% trans "Fields" %}</th>
                                <th scope="col" style="width: 15%;">{% trans "Created" %}</th>
                                <th scope="col" style="width: 15%;">{% trans "Last Updated" %}</th>
                                <th scope="col" style="width: 9%;" class="text-end">{% trans "Actions" %}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for template in templates %}
                            <tr>
                                <td class="fw-medium text-primary">{{ template.name }}</td>
                                <td>{{ template.job|default:"N/A" }}</td>
                                <td>{{ template.get_stage_count }}</td>
                                <td>{{ template.get_field_count }}</td>
                                <td>{{ template.created_at|date:"M d, Y" }}</td>
                                <td>{{ template.updated_at|date:"M d, Y" }}</td>
                                <td class="text-end">
                                    <div class="btn-group btn-group-sm" role="group">
                                        <a href="{% url 'form_wizard' template.id %}" class="btn btn-outline-primary" title="{% trans 'Preview' %}">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{% url 'form_builder' template.id %}" class="btn btn-outline-secondary" title="{% trans 'Edit' %}">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <a href="{% url 'form_template_submissions_list' template.slug %}" class="btn btn-outline-secondary" title="{% trans 'Submissions' %}">
                                            <i class="fas fa-file-alt"></i>
                                        </a>
                                        <button type="button" class="btn btn-outline-danger" title="{% trans 'Delete' %}"
                                                data-bs-toggle="modal" data-bs-target="#deleteModal"
                                                data-delete-url="#"
                                                data-item-name="{{ template.name }}">
                                            <i class="fas fa-trash-alt"></i>
                                        </button>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        {# Pagination (Standardized) #}
        {% if templates.has_other_pages %}
        <nav aria-label="Page navigation" class="mt-4">
            <ul class="pagination justify-content-center">
                {% if templates.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page=1{% if query %}&q={{ query }}{% endif %}">First</a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ templates.previous_page_number }}{% if query %}&q={{ query }}{% endif %}">Previous</a>
                    </li>
                {% endif %}

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

                {% if templates.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ templates.next_page_number }}{% if query %}&q={{ query }}{% endif %}">Next</a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ templates.paginator.num_pages }}{% if query %}&q={{ query }}{% endif %}">Last</a>
                    </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}
    {% else %}
        <div class="text-center py-5 card shadow-sm">
            <div class="card-body">
                <i class="fas fa-file-contract fa-3x mb-3" style="color: var(--kaauh-teal-dark);"></i>
                <h3 class="h4 mb-3">{% trans "No Form Templates Found" %}</h3>
                <p class="text-muted">
                    {% if query %}
                        {% blocktrans with query=query %}No templates match your search "{{ query }}".{% endblocktrans %}
                    {% else %}
                        {% trans "You haven't created any form templates yet." %}
                    {% endif %}
                </p>
                <button type="button" class="btn btn-main-action mt-3" data-bs-toggle="modal" data-bs-target="#createTemplateModal">
                    <i class="fas fa-plus me-1"></i> {% trans "Create Your First Template" %}
                </button>
            </div>
        </div>
    {% endif %}

</div>

{% include 'includes/delete_modal.html' %}

<div class="modal fade" id="createTemplateModal" tabindex="-1" aria-labelledby="createTemplateModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-light">
                <h5 class="modal-title" id="createTemplateModalLabel">
                    <i class="fas fa-file-alt me-2"></i>{% trans "Create New Form Template" %}
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                {% url 'create_form_template' as create_form_template_url %}
                <form id="createTemplateForm" method="post" action="{% url 'create_form_template' %}">
                    {% csrf_token %}
                    {{form|crispy}}
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
                <button type="submit" form="createTemplateForm" class="btn btn-main-action">
                    <i class="fas fa-save me-1"></i>{% trans "Create Template" %}
                </button>
            </div>
        </div>
    </div>
</div>
{% endblock %}