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

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

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

    /* Person Card Specifics */
    .person-card .card-title {
        color: var(--kaauh-teal-dark);
        font-weight: 600;
        font-size: 1.15rem;
    }
    .person-card .card-text i {
        color: var(--kaauh-teal);
        width: 1.25rem;
    }

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

    /* Table Styling */
    .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 Link Styling */
    .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;
    }

    /* Profile Image Styling */
    .profile-image-small {
        width: 40px;
        height: 40px;
        object-fit: cover;
        border-radius: 50%;
        border: 2px solid var(--kaauh-border);
    }

    .profile-image-medium {
        width: 60px;
        height: 60px;
        object-fit: cover;
        border-radius: 50%;
        border: 3px solid var(--kaauh-teal);
    }

    /* Filter & Search Layout */
    .filter-buttons {
        display: flex;
        gap: 0.5rem;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <!-- Header -->
    <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-user-friends me-2"></i> {% trans "Applicants List" %}
        </h1>
            <a href="{% url 'person_create' %}" class="btn btn-main-action">
                <i class="fas fa-plus me-1"></i> {% trans "Add New Applicant" %}
            </a>
    </div>

    <!-- Search and Filters -->
    <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">
                    <form method="GET" class="row g-3 align-items-end h-100">
                        {% if request.GET.q %}<input type="hidden" name="q" value="{{ request.GET.q }}">{% endif %}

                        <div class="col-md-4">
                            <label for="nationality_filter" class="form-label small text-muted">{% trans "Filter by Nationality" %}</label>
                            <select name="nationality" id="nationality_filter" class="form-select form-select-sm">
                                <option value="">{% trans "All Nationalities" %}</option>
                                {% for nationality in nationalities %}
                                <option value="{{ nationality }}" {% if request.GET.nationality == nationality %}selected{% endif %}>{{ nationality }}</option>
                                {% endfor %}
                            </select>
                        </div>

                        <div class="col-md-4">
                            <label for="gender_filter" class="form-label small text-muted">{% trans "Filter by Gender" %}</label>
                            <select name="gender" id="gender_filter" class="form-select form-select-sm">
                                <option value="">{% trans "All Genders" %}</option>
                                <option value="M" {% if request.GET.gender == 'M' %}selected{% endif %}>{% trans "Male" %}</option>
                                <option value="F" {% if request.GET.gender == 'F' %}selected{% endif %}>{% trans "Female" %}</option>
                            </select>
                        </div>

                        <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 Filter" %}
                                </button>
                                {% if request.GET.q or request.GET.nationality or request.GET.gender %}
                                    <a href="{% url 'person_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 people_list %}
        <div id="person-list">
            <!-- View Switcher -->
            {% include "includes/_list_view_switcher.html" with list_id="person-list" %}

            <!-- Table View (Default) -->
            <div class="table-view">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead>
                            <tr>
                                <th scope="col">{% trans "Photo" %}</th>
                                <th scope="col">{% trans "Name" %}</th>
                                <th scope="col">{% trans "Email" %}</th>
                                <th scope="col">{% trans "Phone" %}</th>
                                <th scope="col">{% trans "Nationality" %}</th>
                                <th scope="col">{% trans "Gender" %}</th>
                                <th scope="col">{% trans "Agency" %}</th>
                                <th scope="col">{% trans "Created" %}</th>
                                <th scope="col" class="text-end">{% trans "Actions" %}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for person in people_list %}
                                <tr>
                                    <td>
                                        {% if person.profile_image %}
                                            <img src="{{ person.profile_image.url }}" alt="{{ person.get_full_name }}"
                                                 class="profile-image-small">
                                        {% else %}
                                            <div class="profile-image-small d-flex align-items-center justify-content-center bg-light">
                                                <i class="fas fa-user text-muted"></i>
                                            </div>
                                        {% endif %}
                                    </td>
                                    <td class="fw-medium">
                                        <a href="{% url 'person_detail' person.slug %}"
                                           class="text-decoration-none link-secondary">
                                            {{ person.full_name }}
                                        </a>
                                    </td>
                                    <td>{{ person.email|default:"" }}</td>
                                    <td>{{ person.phone|default:"" }}</td>
                                    <td>
                                        {% if person.nationality %}
                                            <span class="badge bg-primary-theme">{{ person.nationality }}</span>
                                        {% else %}
                                            <span class="text-muted"></span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if person.gender %}
                                            <span class="badge bg-primary-theme">
                                                {% if person.gender == 'M' %}{% trans "Male" %}{% else %}{% trans "Female" %}{% endif %}
                                            </span>
                                        {% else %}
                                            <span class="text-muted"></span>
                                        {% endif %}
                                    </td>
                                    <td><span class="badge bg-primary-theme">{{ person.agency.name|default:"" }}</span></td>
                                    <td>{{ person.created_at|date:"d-m-Y" }}</td>
                                    <td class="text-end">
                                        <div class="btn-group btn-group-sm" role="group">
                                            <a href="{% url 'person_detail' person.slug %}"
                                               class="btn btn-outline-primary" title="{% trans 'View' %}">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            {% if user.is_staff %}
                                                <a href="{% url 'person_update' person.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 'person_delete' person.slug %}"
                                                        data-item-name="{{ person.get_full_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">
                {% for person in people_list %}
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card person-card h-100 shadow-sm">
                            <div class="card-body d-flex flex-column">
                                <div class="d-flex align-items-start mb-3">
                                    <div class="me-3">
                                        {% if person.profile_image %}
                                            <img src="{{ person.profile_image.url }}" alt="{{ person.get_full_name }}"
                                                 class="profile-image-medium">
                                        {% else %}
                                            <div class="profile-image-medium d-flex align-items-center justify-content-center bg-light">
                                                <i class="fas fa-user text-muted fa-2x"></i>
                                            </div>
                                        {% endif %}
                                    </div>
                                    <div class="flex-grow-1">
                                        <h5 class="card-title mb-1">
                                            <a href="{% url 'person_detail' person.slug %}"
                                               class="text-decoration-none text-primary-theme">
                                                {{ person.get_full_name }}
                                            </a>
                                        </h5>
                                        <p class="text-muted small mb-2">{{ person.email|default:"" }}</p>
                                    </div>
                                </div>

                                <div class="card-text text-muted small">
                                    {% if person.phone %}
                                        <div class="mb-2">
                                            <i class="fas fa-phone me-2"></i>{{ person.phone }}
                                        </div>
                                    {% endif %}
                                    {% if person.nationality %}
                                        <div class="mb-2">
                                            <i class="fas fa-globe me-2"></i>
                                            <span class="badge bg-primary-theme">{{ person.nationality }}</span>
                                        </div>
                                    {% endif %}
                                    {% if person.gender %}
                                        <div class="mb-2">
                                            <i class="fas fa-venus-mars me-2"></i>
                                            <span class="badge bg-primary-theme">
                                                {% if person.gender == 'M' %}{% trans "Male" %}{% else %}{% trans "Female" %}{% endif %}
                                            </span>
                                        </div>
                                    {% endif %}
                                    {% if person.date_of_birth %}
                                        <div class="mb-2">
                                            <i class="fas fa-birthday-cake me-2"></i>{{ person.date_of_birth }}
                                        </div>
                                    {% endif %}
                                </div>

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

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