{% extends "base.html" %}
{% load static i18n %}

{% block title %}Applications - {{ block.super }}{% endblock %}

{% block customCSS %}
<style>
    /* UI Variables for the KAAT-S Theme (Consistent with Reference) */
    :root {
        --kaauh-teal: #00636e;
        --kaauh-teal-dark: #004a53;
        --kaauh-border: #eaeff3;
        --kaauh-primary-text: #343a40;
        --kaauh-gray-light: #f8f9fa; /* Added for hover/background consistency */
        --kaauh-success: #28a745;
        --kaauh-danger: #dc3545;
    }
    
    body {
        font-family: 'Inter', sans-serif;
    }

    /* Primary Color Overrides */
    .text-primary-theme { color: var(--kaauh-teal) !important; }
    .bg-primary-theme { background-color: var(--kaauh-teal) !important; }
    .text-success { color: var(--kaauh-success) !important; }
    .text-danger { color: var(--kaauh-danger) !important; }
    .text-info { color: #17a2b8 !important; }

    /* Enhanced Card Styling (Consistent) */
    .card {
        border: 1px solid var(--kaauh-border);
        border-radius: 0.75rem;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
        transition: transform 0.2s, box-shadow 0.2s;
        background-color: white;
    }
    .card:not(.no-hover):hover { /* Use no-hover class for main structure cards */
        transform: translateY(-2px);
        box-shadow: 0 6px 16px rgba(0,0,0,0.1);
    }
    .card.no-hover:hover {
        transform: none;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
    }

    /* Main Action Button Style (Teal Theme) */
     .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.25rem;
    }

    .btn-main-action:hover {
        background-color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal-dark);
        box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    }

    /* Secondary Button Style (For Edit/Outline - Consistent) */
    .btn-outline-secondary {
        color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal);
        transition: all 0.2s ease;
    }
    .btn-outline-secondary:hover {
        background-color: var(--kaauh-teal-dark);
        color: white;
        border-color: var(--kaauh-teal-dark);
    }

    /* Card Specifics (Adapted from Job Card to Candidate Card) */
    .candidate-card .card-title {
        color: var(--kaauh-teal-dark);
        font-weight: 600;
        font-size: 1.15rem;
    }
    .candidate-card .card-text i {
        color: var(--kaauh-teal);
        width: 1.25rem;
        margin-right: 0.25rem; /* Space for icon */
    }

    /* Table & Card Badge Styling (Unified) */
    .badge {
        font-weight: 600;
        padding: 0.4em 0.7em;
        border-radius: 0.3rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    /* Status Badge Mapping (Using standard Bootstrap names where possible) */
    .bg-primary { background-color: var(--kaauh-teal) !important; color: white !important;} /* Main job/stage badge */
    .bg-success { background-color: #28a745 !important; color: white !important;}
    .bg-warning { background-color: #ffc107 !important; color: #343a40 !important;}

    /* Table Styling (Consistent with Reference) */
    .table-view .table thead th {
        background-color: var(--kaauh-teal-dark); /* Dark header background */
        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 Link 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;
    }

    /* Filter & Search Layout Adjustments */
    .filter-buttons {
        display: flex;
        gap: 0.5rem;
    }


/* ------------------------------------------- */
/* Spinner Styling for Resume Parsing */
/* ------------------------------------------- */
.kaats-spinner {
    animation: kaats-spinner-rotate 1.5s linear infinite;
    width: 20px; 
    height: 20px;
    display: inline-block; 
    vertical-align: middle;
}

.kaats-spinner .path {
    stroke: var(--kaauh-teal, #00636e);
    stroke-linecap: round;
}

.kaats-spinner circle {
    stroke: var(--kaauh-border, #e9ecef);
    fill: none;
    stroke-width: 5; 
}

@keyframes kaats-spinner-rotate {
    100% {
        transform: rotate(360deg);
    }
}

@keyframes kaats-spinner-dash {
    0% {
        stroke-dasharray: 1, 150;
        stroke-dashoffset: 0;
    }
    50% {
        stroke-dasharray: 90, 150;
        stroke-dashoffset: -35;
    }
    100% {
        stroke-dasharray: 90, 150;
        stroke-dashoffset: -124;
    }
}

</style>
{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 style="color: var(--kaauh-teal-dark); font-weight: 700;">
            <i class="fas fa-users me-2"></i> {% trans "Applications List" %}
        </h1>
            <a href="{% url 'candidate_create' %}" class="btn btn-main-action">
                <i class="fas fa-plus me-1"></i> {% trans "Add New Application" %}
            </a>
    </div>

    <div class="card mb-4 shadow-sm no-hover">
    <div class="card-body">
        <div class="row g-4">
            <div class="col-md-6">
                <label for="search" class="form-label small text-muted">{% trans "Search by Name or Email" %}</label>
                <div class="input-group input-group-lg">
                    <form method="get" action="" class="w-100">
                        {% include 'includes/search_form.html' %}
                    </form>
                </div>
            </div>

            <div class="col-md-6">
                {% url 'candidate_list' as candidate_list_url %}

                <form method="GET" class="row g-3 align-items-end h-100">
                    {% if search_query %}<input type="hidden" name="q" value="{{ search_query }}">{% endif %}

                        <div class="col-md-4">
                            <label for="job_filter" class="form-label small text-muted">{% trans "Filter by Job" %}</label>
                            <div class="d-flex gap-2">
                                <select name="job" id="job_filter" class="form-select form-select-sm">
                                    <option value="">{% trans "All Jobs" %}</option>
                                    {% for job in available_jobs %}
                                    <option value="{{ job.slug }}" {% if job_filter == job.slug %}selected{% endif %}>{{ job.title }}</option>
                                    {% endfor %}
                            </select>
                            
                            </div>
                        </div>

                        <div class="col-md-4">
                            <label for="stage_filter" class="form-label small text-muted">{% trans "Filter by Stages" %}</label>
                            <div class="d-flex gap-2">
                               
                            <select name="stage" id="stage_filter" class="form-select form-select-sm">
                                <option value="">{% trans "All Stages" %}</option>
                                    <option value="Applied" {% if stage_filter == 'Applied' %}selected{% endif %}>{% trans "Applied" %}</option>
                                    <option value="Exam" {% if stage_filter == 'Exam' %}selected{% endif %}>{% trans "Exam" %}</option>
                                    <option value="Interview" {% if stage_filter == 'Interview' %}selected{% endif %}>{% trans "Interview" %}</option>
                                    <option value="Offer" {% if stage_filter == 'Offer' %}selected{% endif %}>{% trans "Offer" %}</option>
                                </select>
                            </div>
                        </div>

                    {# Buttons Group (pushed to the right/bottom) #}
                    <div class="col-md-4 d-flex ">
                        <div class="filter-buttons">
                            <button type="submit" class="btn btn-main-action btn-sm">
                                <i class="fas fa-filter me-1"></i> {% trans "Apply Filters" %}
                            </button>
                            {% if job_filter or stage_filter or search_query %}
                                <a href="{% url 'candidate_list' %}" class="btn btn-outline-secondary btn-sm">
                                    <i class="fas fa-times me-1"></i> {% trans "Clear" %}
                                </a>
                            {% endif %}
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

    {% if applications %}
        <div id="candidate-list">
            {# View Switcher - list_id must match the container ID #}
            {% include "includes/_list_view_switcher.html" with list_id="candidate-list" %}

            {# Table View (Default for Desktop) #}
            <div class="table-view active d-none d-lg-block">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead>
                            <tr>
                                <th scope="col" >{% trans "Name" %}</th>
                                <th scope="col">{% trans "Email" %}</th>
                                <th scope="col">{% trans "Job" %}</th>
                                <th scope="col" >{% trans "Major" %}</th>
                                <th scope="col" >{% trans "Stage" %}</th>
                                <th scope="col">{% trans "Hiring Source" %}</th>
                                <th scope="col" >{% trans "created At" %}</th>
                                <th scope="col"  class="text-end">{% trans "Actions" %}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for candidate in applications %}
                                <tr>
                                    <td class="fw-medium"><a href="{% url 'candidate_detail' candidate.slug %}" class="text-decoration-none link-secondary">{{ candidate.name }}<a></td>
                                    <td>{{ candidate.email }}</td>
                                    <td> <span class="badge bg-primary"><a href="{% url 'job_detail' candidate.job.slug %}" class="text-decoration-none text-white">{{ candidate.job.title }}</a></span></td>
                                    <td>
                                    {% if candidate.is_resume_parsed %}
                                        {% if candidate.professional_category != 'Uncategorized' %}
                                            <span class="badge bg-primary">
                                                {{ candidate.professional_category }}
                                            </span>
                                        {% endif %}
                                    {% else %}
                                        <a href="{% url 'candidate_list' %}" class="text-decoration-none d-flex align-items-center gap-2">
                                            <svg class="kaats-spinner" viewBox="0 0 50 50" style="width: 25px; height: 25px;">
                                                <circle cx="25" cy="25" r="20"></circle>
                                                <circle class="path" cx="25" cy="25" r="20" fill="none" stroke-width="5"
                                                    style="animation: kaats-spinner-dash 1.5s ease-in-out infinite;"></circle>
                                            </svg>
                                        </a>
                                    {% endif %}
                                    </td>
                                    <td>
                                        <span class="badge bg-primary">
                                            {{ candidate.stage }}
                                        </span>
                                    </td>
                                    <td>
                                        {% if candidate.hiring_agency and candidate.hiring_source == 'Agency' %}
                                            <a href="{% url 'agency_detail' candidate.hiring_agency.slug %}" class="text-decoration-none">
                                                <span class="badge bg-primary">
                                                    <i class="fas fa-building"></i> {{ candidate.hiring_agency.name }}
                                                </span>
                                            </a>
                                        {% else %}
                                            <span class="badge bg-primary">{{ candidate.hiring_source }}</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ candidate.created_at|date:"d-m-Y" }}</td>
                                    <td class="text-end">
                                        <div class="btn-group btn-group-sm" role="group">
                                            <a href="{% url 'candidate_detail' candidate.slug %}" class="btn btn-outline-secondary" title="{% trans 'View' %}">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            {% if user.is_staff %}
                                                <a href="{% url 'candidate_update' candidate.slug %}" class="btn btn-outline-secondary" title="{% trans 'Edit' %}">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <button type="button" class="btn btn-outline-danger" title="{% trans 'Delete' %}"
                                                        data-bs-toggle="modal" data-bs-target="#deleteModal"
                                                        data-delete-url="{% url 'candidate_delete' candidate.slug %}"
                                                        data-item-name="{{ candidate.name }}">
                                                    <i class="fas fa-trash-alt"></i>
                                                </button>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>

            {# Card View (Default for Mobile) #}
            <div class="card-view row g-4 d-lg-none">
                {% for candidate in applications %}
                    <div class="col-md-6 col-sm-12">
                        <div class="card candidate-card h-100 shadow-sm">
                            <div class="card-body d-flex flex-column">
                                <div class="d-flex justify-content-between align-items-start mb-3">
                                    <h5 class="card-title flex-grow-1 me-3">
                                        <a href="{% url 'candidate_detail' candidate.slug %}" class="text-decoration-none text-primary-theme ">{{ candidate.name }}</a>
                                    </h5>
                                    <span class="badge bg-primary">{{ candidate.stage }}</span>
                                </div>

                                <p class="card-text text-muted small flex-grow-1">
                                    <span class="d-block mb-1"><i class="fas fa-envelope"></i> {{ candidate.email }}</span>
                                    <span class="d-block mb-1"><i class="fas fa-phone-alt"></i> {{ candidate.phone|default:"N/A" }}</span>
                                    <span class="d-block mb-1"><i class="fas fa-calendar-alt"></i> {{ candidate.created_at|date:"d-m-Y" }}</span>
                                    <span class="d-block mt-2"><i class="fas fa-briefcase"></i> 
                                        <span class="badge bg-primary"><a href="{% url 'job_detail' candidate.job.slug %}" class="text-decoration-none text-white">{{ candidate.job.title }}</a></span>
                                    </span>
                                    {% if candidate.hiring_agency %}
                                        <span class="d-block mt-2"><i class="fas fa-building"></i> 
                                            <a href="{% url 'agency_detail' candidate.hiring_agency.slug %}" class="text-decoration-none">
                                                <span class="badge bg-info">{{ candidate.hiring_agency.name }}</span>
                                            </a>
                                        </span>
                                    {% endif %}
                                </p>

                                <div class="mt-auto pt-3 border-top">
                                   <div class="d-flex gap-2">
                                        <a href="{% url 'candidate_detail' candidate.slug %}" class="btn btn-sm btn-main-action">
                                            <i class="fas fa-eye"></i> {% trans "View" %}
                                        </a>
                                        {% if user.is_staff %}
                                            <a href="{% url 'candidate_update' candidate.slug %}" class="btn btn-sm btn-outline-secondary">
                                                <i class="fas fa-edit"></i> {% trans "Edit" %}
                                            </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="{% url 'candidate_delete' candidate.slug %}"
                                                    data-item-name="{{ candidate.name }}">
                                                <i class="fas fa-trash-alt"></i>
                                            </button>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>

        {# Pagination (Standardized to Reference) #}
        {% include "includes/paginator.html" %}
    {% else %}
        <div class="text-center py-5 card shadow-sm">
            <div class="card-body">
                <i class="fas fa-users fa-3x mb-3" style="color: var(--kaauh-teal-dark);"></i>
                <h3>{% trans "No application found" %}</h3>
                <p class="text-muted">{% trans "Create your first application." %}</p>
                {% if user.is_staff %}
                    <a href="{% url 'candidate_create' %}" class="btn btn-main-action mt-3">
                        <i class="fas fa-plus me-1"></i> {% trans "Add Application" %}
                    </a>
                {% endif %}
            </div>
        </div>
    {% endif %}
</div>
{% endblock %}