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

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

    /* 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);
    }
    .btn-outline-secondary:hover {
        background-color: var(--kaauh-teal-dark);
        color: white;
        border-color: var(--kaauh-teal-dark);
    }

    /* Card Specifics */
    .participant-card .card-title {
        color: var(--kaauh-teal-dark);
        font-weight: 600;
        font-size: 1.15rem;
    }
    .participant-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 (Unified to Primary Theme Color) */
    .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 "Participants List" %}
        </h1>
        {% if user.is_staff %}
            <a href="{% url 'participants_create' %}" class="btn btn-main-action">
                <i class="fas fa-plus me-1"></i> {% trans "Add New Participant" %}
            </a>
        {% endif %}
    </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">
                            {# Assuming this includes your search input and submit button #}
                            {% 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 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 Assigned Job" %}</label>
                                <select name="job" id="job_filter" class="form-select form-select-sm">
                                    <option value="">{% trans "All Jobs" %}</option>
                                    {# available_jobs should be passed from the view #}
                                    {% for job in available_jobs %} 
                                    <option value="{{ job.slug }}" {% if job_filter == job.slug %}selected{% endif %}>{{ job.title }}</option>
                                    {% endfor %}
                                </select>
                            </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 search_query %}
                                    <a href="{% url 'participants_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 participants %}
        <div id="participant-list">
            {# View Switcher - list_id must match the container ID #}
            {% include "includes/_list_view_switcher.html" with list_id="participant-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: 15%;">{% trans "Name" %}</th>
                                <th scope="col" style="width: 15%;">{% trans "Email" %}</th>
                                <th scope="col" style="width: 10%;">{% trans "Phone" %}</th>
                               
                                <th scope="col" style="width: 15%;">{% trans "Designation" %}</th>
                                <th scope="col" style="width: 15%;">{% trans "Created At" %}</th>
                                <th scope="col" style="width: 5%;" class="text-end">{% trans "Actions" %}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for participant in participants %}
                                <tr>
                                    <td class="fw-medium"><a href="{% url 'participants_detail' participant.slug %}" class="text-decoration-none link-secondary">{{ participant.name }}<a></td>
                                    <td>{{ participant.email }}</td>
                                    <td>{{ participant.phone|default:"N/A" }}</td>
                                    
                                    <td>{{ participant.designation|default:"N/A" }}</td>
                                    <td>{{ participant.created_at|date:"d-m-Y" }}</td>
                                    <td class="text-end">
                                        <div class="btn-group btn-group-sm" role="group">
                                            <a href="{% url 'participants_detail' participant.slug%}" class="btn btn-outline-secondary" title="{% trans 'View' %}">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            {% if user.is_staff %}
                                                <a href="{% url 'participants_update' participant.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 'participants_delete' participant.slug %}"
                                                        data-item-name="{{ participant.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 participant in participants %}
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card participant-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"><a href="{% url 'participants_detail' participant.slug%}" class="text-decoration-none text-primary-theme ">{{ participant.name }}</a></h5>
                                </div>

                                <p class="card-text text-muted small">
                                    <i class="fas fa-envelope"></i> {{ participant.email }}<br>
                                    <i class="fas fa-phone-alt"></i> {{ participant.phone|default:"N/A" }}<br>
                                    <i class="fas fa-briefcase"></i> {{ participant.designation|default:"N/A" }}
                                </p>
                                
                                

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

        {# Pagination #}
        {% 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 participants found" %}</h3>
                <p class="text-muted">{% trans "Create your first participant record or adjust your filters." %}</p>
                {% if user.is_staff %}
                    <a href="{% url 'participants_create' %}" class="btn btn-main-action mt-3">
                        <i class="fas fa-plus me-1"></i> {% trans "Add Participant" %}
                    </a>
                {% endif %}
            </div>
        </div>
    {% endif %}
</div>


    {# Delete Confirmation Modal #}
    <div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                      <h5 class="modal-title" id="deleteModalLabel">{% trans "Confirm Deletion" %}</h5> 
                      <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <p>{% trans "Are you sure you want to delete" %} <strong id="itemName"></strong>?</p>
                    
                    <p class="text-danger">
                        <i class="fas fa-exclamation-triangle me-1"></i>
                        {% trans "This action cannot be undone." %}
                    </p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
                    <form method="post" id="deleteForm">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-danger">{% trans "Delete" %}</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
 
{% endblock %}



{% block customJS %}
<script>
    // Populate Delete Modal with dynamic data
    var deleteModal = document.getElementById('deleteModal');               

    // We must check if the modal element exists before adding the listener
    if (deleteModal) {
        deleteModal.addEventListener('show.bs.modal', function (event) {
            var button = event.relatedTarget; 
            
            // Get data from the button that triggered the modal
            var deleteUrl = button.getAttribute('data-delete-url'); 
            var itemName = button.getAttribute('data-item-name'); 

            // Get modal elements
            var modalItemName = deleteModal.querySelector('#itemName');
            var deleteForm = deleteModal.querySelector('#deleteForm');

            // Set the dynamic content
            if (modalItemName) {
                modalItemName.textContent = itemName;
            }
            // Set the form action URL
            if (deleteForm) {
                deleteForm.action = deleteUrl;
            }
        });
    }
</script>
{% endblock %}