{% load partials %}
<!-- Stage Update Modal -->
<div class="modal fade" id="stageUpdateModal" tabindex="-1" aria-labelledby="stageUpdateModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-top">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="stageUpdateModalLabel">
                    <i class="fas fa-exchange-alt me-2"></i>Update Application Stage
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            {% partialdef stage-modal %}
                <div class="modal-body">
                    <!-- Success/Error Messages Container -->
                    <div id="stageUpdateMessages"></div>

                    <!-- Current Stage Display -->
                    <div class="alert alert-info mb-4">
                        <div class="d-flex align-items-center">
                            <i class="fas fa-info-circle me-2"></i>
                            <strong>Current Stage:</strong>
                            <span class="badge bg-primary ms-2" id="currentStageBadge" data-text="$stage"></span>
                        </div>
                    </div>

                    <!-- Stage Progress Visualization -->
                    <div class="mb-4" >
                        <h6 class="text-muted mb-3">Application Pipeline</h6>
                        <div class="progress-stages">
                            <div class="d-flex justify-content-between align-items-center mb-2">
                                <div class="stage-item {% if candidate.stage == 'Applied' %}active{% endif %}" data-stage="Applied">
                                    <div class="stage-icon">
                                        <i class="fas fa-user-check"></i>
                                    </div>
                                    <div class="stage-label">Applied</div>
                                </div>


                                <div data-class="{active: $stage == 'Exam', completed: $stage == 'Interview' || $stage == 'Offer'}" class="stage-connector"></div>

                                <div data-class="{active: $stage == 'Exam', completed: $stage == 'Offer' || $stage == 'Interview'}" class="stage-item" data-stage="Exam">
                                    <div class="stage-icon">
                                        <i class="fas fa-clipboard-check"></i>
                                    </div>
                                    <div class="stage-label">Exam</div>
                                </div>
                                <div data-class="{active: $stage == 'Interview', completed: $stage == 'Offer'}" class="stage-connector"></div>

                                <div data-class="{active: $stage == 'Interview', completed: $stage == 'Offer'}" class="stage-item" data-stage="Interview">
                                    <div class="stage-icon">
                                        <i class="fas fa-comments"></i>
                                    </div>
                                    <div class="stage-label">Interview</div>
                                </div>
                                <div data-class="{active: $stage == 'Offer'}" class="stage-connector"></div>
                                <div data-class="{active: $stage == 'Offer', completed: $stage == 'Offer'}" class="stage-item" data-stage="Offer">
                                    <div class="stage-icon">
                                        <i class="fas fa-handshake"></i>
                                    </div>
                                    <div class="stage-label">Offer</div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Form Container -->
                    <div id="stageUpdateFormContainer" data-show="$stage != 'Offer'">
                        {% comment %} <form id="stageUpdateForm" hx-post="{% url 'candidate_update_stage' candidate.slug %}" hx-target="#stageUpdateFormContainer" hx-swap="outerHTML" hx-indicator="#loadingIndicator"> {% endcomment %}
                        {% url 'candidate_update_stage' candidate.slug as stage_update_url %}
                            <form id="stageUpdateForm" data-on-submit="@post('{{ stage_update_url }}',{
                            contentType: 'form',
                            headers: {'X-CSRFToken': '{{ csrf_token }}'}})" data-indicator-fetching>
                            {% csrf_token %}

                            <!-- Stage Selection -->
                            <div class="mb-3">
                                <label for="id_stage" class="form-label">
                                    <i class="fas fa-arrow-right me-1"></i>Move to Stage
                                </label>
                                {% partialdef id-stage %}
                                <select data-attr-disabled="$fetching" id="id_stage" name="stage" class="form-select" onchange="updateStageInfo(this.value, '{{ candidate.stage }}')">
                                    {% for value, label in stage_form.stage.field.choices %}
                                        <option value="{{ value }}">{{ label }}</option>
                                    {% endfor %}
                                </select>
                                {% endpartialdef %}
                                {% partial id-stage %}
                            </div>

                            <!-- Available Stages Info -->
                            <div class="alert alert-light border" id="availableStagesInfo">
                                <small class="text-muted">
                                    <i class="fas fa-lightbulb me-1"></i>
                                    <span id="availableStagesText">Select a stage above to see transition information.</span>
                                </small>
                            </div>


                            <!-- Form Actions -->
                            <div class="d-flex justify-content-between mt-4">
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                                    <i class="fas fa-times me-1"></i>Cancel
                                </button>

                                <button data-show="$stage != 'Offer'" type="submit" class="btn btn-primary" id="stageUpdateSubmit" data-attr-disabled="$fetching">
                                    <i class="fas fa-save me-1"></i>
                                    <span data-show="$fetching" class="spinner-border spinner-border-sm ms-2" role="status" aria-hidden="true"></span>
                                    <span class="ms-2" data-text="$fetching ? 'Updating...' : 'Update'"></span>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            {% endpartialdef %}
            {% partial stage-modal %}

        </div>
    </div>
</div>

<script>
// Update stage information when selection changes
function updateStageInfo(selectedValue, currentStage) {
    const availableStagesText = document.getElementById('availableStagesText');

    if (selectedValue && selectedValue !== currentStage) {
        const stageLabels = {
            'Applied': 'Applied',
            'Exam': 'Exam',
            'Interview': 'Interview',
            'Offer': 'Offer'
        };

        if (availableStagesText) {
            availableStagesText.textContent = `Moving from "${currentStage}" to "${stageLabels[selectedValue]}". This transition is allowed.`;
            availableStagesText.parentElement.parentElement.classList.remove('alert-warning', 'alert-light');
            availableStagesText.parentElement.parentElement.classList.add('alert-success');
        }
    } else if (selectedValue === currentStage) {
        if (availableStagesText) {
            availableStagesText.textContent = 'No stage change selected. The candidate is already in this stage.';
            availableStagesText.parentElement.parentElement.classList.remove('alert-success', 'alert-warning');
            availableStagesText.parentElement.parentElement.classList.add('alert-info');
        }
    } else {
        if (availableStagesText) {
            availableStagesText.textContent = 'Select a stage above to see transition information.';
            availableStagesText.parentElement.parentElement.classList.remove('alert-success', 'alert-warning');
            availableStagesText.parentElement.parentElement.classList.add('alert-light');
        }
    }
}
</script>

<style>
.progress-stages .stage-item {
    text-align: center;
    min-width: 80px;
}

.progress-stages .stage-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 8px;
    font-size: 16px;
    transition: all 0.3s ease;
    background-color: #f8f9fa;
    border: 2px solid #dee2e6;
    color: #6c757d;
}

.progress-stages .stage-item.active .stage-icon {
    background-color: #0d6efd;
    color: white;
    border: 2px solid #0d6efd;
}

.progress-stages .stage-item.completed .stage-icon {
    background-color: #198754;
    color: white;
    border: 2px solid #198754;
}

.progress-stages .stage-label {
    font-size: 12px;
    font-weight: 500;
    color: #6c757d;
}

.progress-stages .stage-item.active .stage-label {
    color: #0d6efd;
    font-weight: 600;
}

.progress-stages .stage-connector {
    flex: 1;
    height: 2px;
    background-color: #dee2e6;
    margin: 0 10px;
    position: relative;
    top: -20px;
}

.progress-stages .stage-connector.active {
    background-color: #198754;
}

.progress-stages .stage-item.completed .stage-label {
    color: #198754;
    font-weight: 600;
}

.htmx-indicator {
    display: none;
    text-align: center;
    margin: 10px 0;
}

.htmx-request .htmx-indicator {
    display: block;
}
</style>

