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

{% block title %}Candidates - {{ 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 */
    }

    /* 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.4rem;
        padding: 0.5rem 1rem;
    }

    .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);
    }
    .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; 
    }

    /* 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;
    }
</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 "Candidate Profiles" %}
        </h1>
        {% if user.is_staff %}
            <a href="{% url 'candidate_create' %}" class="btn btn-main-action">
                <i class="fas fa-plus me-1"></i> {% trans "Add New Candidate" %}
            </a>
        {% endif %}
    </div>

    <div class="card mb-4 shadow-sm no-hover">
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <label for="search" class="form-label small text-muted">{% trans "Search by Name or Email" %}</label>
                    <div class="input-group input-group-lg mb-3">
                        <form method="get" action="" class="w-100">
                            {% include 'includes/search_form.html' %}
                        </form>
                    </div>
                </div>
                <div class="col-md-8">
                    {% url 'candidate_list' as candidate_list_url %}
                    
                    <form method="GET" class="row g-3 align-items-end" >
                        {% if search_query %}<input type="hidden" name="q" value="{{ search_query }}">{% endif %}

                        <div class="col-md-3">
                            <label for="job_filter" class="form-label small text-muted">{% trans "Filter by Job" %}</label>
                            <select name="job" id="job_filter" class="form-select form-select-sm">
                                <option value="">{% trans "All Jobs" %}</option>
                                {% for job in available_jobs %} {# Assuming you pass a context variable 'available_jobs' #}
                                    <option value="{{ job.slug }}" {% if job_filter == job.slug %}selected{% endif %}>{{ job.title }}</option>
                                {% endfor %}
                            </select>
                        </div>

                        <div class="col-md-5">
                            <div class="filter-buttons">
                                <button type="submit" class="btn btn-main-action btn-lg">
                                    <i class="fas fa-filter me-1"></i> {% trans "Apply Filters" %}
                                </button>
                                {% if job_filter or search_query %}
                                    <a href="{% url 'candidate_list' %}" class="btn btn-outline-secondary btn-lg">
                                        <i class="fas fa-times me-1"></i> {% trans "Clear" %}
                                    </a>
                                {% endif %}
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    {% if candidates %}
        <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) #}
            <div class="table-view active">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th scope="col" style="width: 20%;">{% trans "Name" %}</th>
                                <th scope="col" style="width: 20%;">{% trans "Email" %}</th>
                                <th scope="col" style="width: 15%;">{% trans "Phone" %}</th>
                                <th scope="col" style="width: 15%;">{% trans "Job" %}</th>
                                <th scope="col" style="width: 10%;">{% trans "Stage" %}</th>
                                <th scope="col" style="width: 10%;">{% trans "Created" %}</th>
                                <th scope="col" style="width: 10%;" class="text-end">{% trans "Actions" %}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for candidate in candidates %}
                                <tr>
                                    <td class="fw-medium">{{ candidate.name }}</td>
                                    <td>{{ candidate.email }}</td>
                                    <td>{{ candidate.phone }}</td>
                                    <td> <span class="badge bg-primary">{{ candidate.job.title }}</span></td>
                                    <td>
                                        <span class="badge bg-primary">
                                            {{ candidate.stage }}
                                        </span>
                                    </td>
                                    <td>{{ candidate.created_at|date:"M d, 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-primary" 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" {# Updated to standard Bootstrap usage #}
                                                        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 #}
            <div class="card-view row"> {# Added 'row' class for grid structure #}
                {% for candidate in candidates %}
                    <div class="col-md-6 col-lg-4 mb-4"> {# Column wrapper for grid #}
                        <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-2">
                                    <h5 class="card-title flex-grow-1 me-3">{{ candidate.name }}</h5>
                                    <span class="badge bg-primary">{{ candidate.stage }}</span>
                                </div>
                                
                                <p class="card-text text-muted small">
                                    <i class="fas fa-envelope"></i> {{ candidate.email }}<br>
                                    <i class="fas fa-phone-alt"></i> {{ candidate.phone|default:"N/A" }}<br>
                                    <i class="fas fa-briefcase"></i> <span class="badge bg-primary">{{ candidate.job.title }}</span>
                                </p>

                                <div class="mt-auto pt-2 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) #}
        {% if is_paginated %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page=1{% if search_query %}&q={{ search_query }}{% endif %}{% if job_filter %}&job={{ job_filter }}{% endif %}">First</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&q={{ search_query }}{% endif %}{% if job_filter %}&job={{ job_filter }}{% endif %}">Previous</a>
                        </li>
                    {% endif %}

                    <li class="page-item active">
                        <span class="page-link">{{ page_obj.number }} 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 }}{% if search_query %}&q={{ search_query }}{% endif %}{% if job_filter %}&job={{ job_filter }}{% endif %}">Next</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search_query %}&q={{ search_query }}{% endif %}{% if job_filter %}&job={{ job_filter }}{% 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-users fa-3x mb-3" style="color: var(--kaauh-teal-dark);"></i>
                <h3>{% trans "No candidate profiles found" %}</h3>
                <p class="text-muted">{% trans "Create your first candidate profile or adjust your filters." %}</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 Candidate" %}
                    </a>
                {% endif %}
            </div>
        </div>
    {% endif %}
</div>
{% endblock %}