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

{% block title %}{{ job.title }} - Applicants{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="h3 mb-1">
            <a href="{% url 'job_detail' job.slug %}" class="text-decoration-none">
                <i class="fas fa-arrow-left me-2"></i>
            </a>
            Applicants for "{{ job.title }}"
        </h1>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="{% url 'dashboard' %}">Dashboard</a></li>
                <li class="breadcrumb-item"><a href="{% url 'job_list' %}">Jobs</a></li>
                <li class="breadcrumb-item"><a href="{% url 'job_detail' job.slug %}">{{ job.title }}</a></li>
                <li class="breadcrumb-item active" aria-current="page">Applicants</li>
            </ol>
        </nav>
    </div>
    <div>
        <a href="{% url 'candidate_create_for_job' job.slug %}" class="btn btn-success">
            <i class="fas fa-user-plus"></i> Add New Applicant
        </a>
    </div>
</div>

<!-- Job Summary Card -->
<div class="card mb-4">
    <div class="card-body">
        <div class="row">
            <div class="col-md-8">
                <h5 class="card-title mb-3">{{ job.title }}</h5>
                <div class="row">
                    <div class="col-md-6">
                        <small class="text-muted">Department:</small>
                        <div>{{ job.department|default:"Not specified" }}</div>
                    </div>
                    <div class="col-md-6">
                        <small class="text-muted">Location:</small>
                        <div>{{ job.get_location_display }}</div>
                    </div>
                    <div class="col-md-6">
                        <small class="text-muted">Job Type:</small>
                        <div>{{ job.get_job_type_display }}</div>
                    </div>
                    <div class="col-md-6">
                        <small class="text-muted">Workplace:</small>
                        <div>{{ job.get_workplace_type_display }}</div>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="text-end">
                    <span class="badge bg-{{ job.status|lower }} status-badge">
                        {{ job.get_status_display }}
                    </span>
                    {% if candidates %}
                        <div class="mt-2">
                            <small class="text-muted">Total Applicants:</small>
                            <h4 class="text-primary mb-0">{{ candidates.count }}</h4>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Search and Filters -->
<div class="card mb-4">
    <div class="card-body">
        <form method="get" class="row g-3">
            <div class="col-md-6">
                <label for="search" class="form-label">Search Applicants</label>
                <div class="input-group">
                    <span class="input-group-text"><i class="fas fa-search"></i></span>
                    <input type="text" class="form-control" id="search" name="search"
                           placeholder="Search by name, email, phone, or stage..."
                           value="{{ search_query }}">
                </div>
            </div>
            <div class="col-md-3 d-flex align-items-end">
                <button type="submit" class="btn btn-primary w-100">
                    <i class="fas fa-filter"></i> Search
                </button>
            </div>
            <div class="col-md-3 d-flex align-items-end">
                <a href="{% url 'job_candidates_list' job.slug %}" class="btn btn-outline-secondary w-100">
                    <i class="fas fa-times"></i> Clear
                </a>
            </div>
        </form>
    </div>
</div>

<!-- Candidates -->
{% if candidates %}
    <div id="job-candidates-list">
        {# View Switcher #}
        {% include "includes/_list_view_switcher.html" with list_id="job-candidates-list" %}

        {# Table View (Default) #}
        <div class="table-view active">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">Applicants ({{ candidates.count }})</h5>
                        <div class="d-flex gap-2">
                            <select class="form-select form-select-sm" style="width: auto;" onchange="window.location.href='?stage='+this.value+'&search={{ search_query }}'">
                                <option value="">All Stages</option>
                                <option value="Applied" {% if request.GET.stage == 'Applied' %}selected{% endif %}>Applied</option>
                                <option value="Interview" {% if request.GET.stage == 'Interview' %}selected{% endif %}>Interview</option>
                                <option value="Offer" {% if request.GET.stage == 'Offer' %}selected{% endif %}>Offer</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th scope="col">
                                    <input type="checkbox" class="form-check-input" id="selectAll">
                                </th>
                                <th scope="col">Name</th>
                                <th scope="col">Email</th>
                                <th scope="col">Phone</th>
                                <th scope="col">Stage</th>
                                <th scope="col">Applied Date</th>
                                <th scope="col" class="text-center">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for candidate in candidates %}
                                <tr>
                                    <td>
                                        <input type="checkbox" class="form-check-input candidate-checkbox" value="{{ candidate.slug }}">
                                    </td>
                                    <td>
                                        <div>
                                            <strong>{{ candidate.first_name }} {{ candidate.last_name }}</strong>
                                        </div>
                                    </td>
                                    <td>{{ candidate.email }}</td>
                                    <td>{{ candidate.phone|default:"-" }}</td>
                                    <td>
                                        <span class="badge bg-{% if candidate.stage == 'Applied' %}primary{% elif candidate.stage == 'Interview' %}info{% elif candidate.stage == 'Offer' %}success{% else %}secondary{% endif %}">
                                            {{ candidate.stage }}
                                        </span>
                                    </td>
                                    <td>{{ candidate.created_at|date:"M d, Y" }}</td>
                                    <td class="text-center">
                                        <div class="btn-group" role="group">
                                            <a href="{% url 'candidate_detail' candidate.slug %}" class="btn btn-outline-primary btn-sm" title="View">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            {% if user.is_staff %}
                                                <a href="{% url 'candidate_update' candidate.slug %}" class="btn btn-outline-secondary btn-sm" title="Edit">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <button type="button" class="btn btn-outline-danger btn-sm" title="Delete"
                                                        data-bs-toggle="deleteModal"
                                                        data-delete-url="{% url 'candidate_delete' candidate.slug %}"
                                                        data-item-name="{{ candidate.first_name }} {{ candidate.last_name }}">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- Bulk Actions -->
                <div class="card-footer d-flex justify-content-between align-items-center">
                    <div class="d-flex align-items-center">
                        <span class="me-3">Selected: <span id="selectedCount">0</span></span>
                        <button class="btn btn-sm btn-outline-primary me-2" onclick="bulkAction('interview')"
                                {% if not user.is_staff %}disabled{% endif %}>
                            <i class="fas fa-comments"></i> Mark as Interview
                        </button>
                        <button class="btn btn-sm btn-success" onclick="bulkAction('offer')"
                                {% if not user.is_staff %}disabled{% endif %}>
                            <i class="fas fa-handshake"></i> Mark as Offer
                        </button>
                    </div>

                    <!-- Pagination -->
                    {% if is_paginated %}
                        <nav aria-label="Page navigation">
                            <ul class="pagination pagination-sm mb-0">
                                {% if page_obj.has_previous %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}" aria-label="Previous">
                                            <span aria-hidden="true">&laquo;</span>
                                        </a>
                                    </li>
                                {% endif %}

                                {% for num in page_obj.paginator.page_range %}
                                    {% if page_obj.number == num %}
                                        <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ num }}{% if search_query %}&search={{ search_query }}{% endif %}">{{ num }}</a>
                                        </li>
                                    {% endif %}
                                {% endfor %}

                                {% if page_obj.has_next %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}" aria-label="Next">
                                            <span aria-hidden="true">&raquo;</span>
                                        </a>
                                    </li>
                                {% endif %}
                            </ul>
                        </nav>
                    {% endif %}
                </div>
            </div>
        </div>

        {# Card View #}
        <div class="card-view">
            <div class="row g-4">
                {% for candidate in candidates %}
                    <div class="col-md-6 col-lg-4">
                        <div class="card h-100">
                            <div class="card-header">
                                <h5 class="h5 mb-1">{{ candidate.first_name }} {{ candidate.last_name }}</h5>
                                <small class="text-white-50">{{ candidate.email }}</small>
                            </div>
                            <div class="card-body">
                                <p class="card-text">
                                    <strong>{% trans "Phone" %}:</strong> {{ candidate.phone|default:"N/A" }}<br>
                                    <strong>{% trans "Stage" %}:</strong> <span class="badge bg-{% if candidate.stage == 'Applied' %}primary{% elif candidate.stage == 'Interview' %}info{% elif candidate.stage == 'Offer' %}success{% else %}secondary{% endif %}">{{ candidate.stage }}</span><br>
                                    <strong>{% trans "Applied Date" %}:</strong> {{ candidate.created_at|date:"M d, Y" }}
                                </p>
                            </div>
                            <div class="card-footer">
                                <div class="d-flex gap-2">
                                    <a href="{% url 'candidate_detail' candidate.slug %}" class="btn btn-sm btn-outline-primary w-100">
                                        <i class="fas fa-eye"></i> {% trans "View" %}
                                    </a>
                                    {% if user.is_staff %}
                                        <div class="btn-group w-100" role="group">
                                            <a href="{% url 'candidate_update' candidate.slug %}" class="btn btn-sm btn-outline-secondary" title="Edit">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <button type="button" class="btn btn-outline-danger btn-sm" title="Delete"
                                                    data-bs-toggle="deleteModal"
                                                    data-delete-url="{% url 'candidate_delete' candidate.slug %}"
                                                    data-item-name="{{ candidate.first_name }} {{ candidate.last_name }}">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>
    </div>
{% else %}
    <div class="text-center py-5">
        <i class="fas fa-user-slash fa-3x text-muted mb-3"></i>
        <h4 class="text-muted">No applicants found</h4>
        <p class="text-muted">There are no candidates who have applied for this position yet.</p>
        <a href="{% url 'candidate_create_for_job' job.slug %}" class="btn btn-primary">
            <i class="fas fa-user-plus"></i> Add First Applicant
        </a>
    </div>
{% endif %}

<!-- Delete Modal -->
{% include "includes/delete_modal.html" %}
{% endblock %}

{% block extra_js %}
<script>
// Select all checkbox functionality
document.getElementById('selectAll').addEventListener('change', function() {
    const checkboxes = document.querySelectorAll('.candidate-checkbox');
    checkboxes.forEach(checkbox => {
        checkbox.checked = this.checked;
    });
    updateSelectedCount();
});

// Individual checkbox change
document.querySelectorAll('.candidate-checkbox').forEach(checkbox => {
    checkbox.addEventListener('change', updateSelectedCount);
});

function updateSelectedCount() {
    const checkedBoxes = document.querySelectorAll('.candidate-checkbox:checked');
    document.getElementById('selectedCount').textContent = checkedBoxes.length;
}

function bulkAction(stage) {
    const selectedCandidates = Array.from(document.querySelectorAll('.candidate-checkbox:checked'))
        .map(cb => cb.value);

    if (selectedCandidates.length === 0) {
        alert('Please select at least one candidate.');
        return;
    }

    if (confirm(`Mark ${selectedCandidates.length} candidate(s) as ${stage}?`)) {
        // Here you would typically make an AJAX request to update the candidates
        console.log('Updating candidates:', selectedCandidates, 'to stage:', stage);
        // For now, just show a message
        alert(`Bulk update functionality would mark ${selectedCandidates.length} candidates as ${stage}`);
    }
}
</script>
{% endblock %}
