<!-- 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>
                <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">{{candidate.stage}}</span>
                        </div>
                    </div>

                    <!-- Form Container -->
                    <div id="stageUpdateFormContainer">
                            <form id="stageUpdateForm" action="{% url 'candidate_update_stage' candidate.slug %}" method="post">
                            {% 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>

                                <select id="id_stage" name="stage" class="form-select">
                                    {% for value, label in stage_form.stage.field.choices %}
                                        <option value="{{ value }}">{{ label }}</option>
                                    {% endfor %}
                                </select>
                            </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-outline-secondary" data-bs-dismiss="modal">
                                    <i class="fas fa-times me-1"></i>Cancel
                                </button>

                                <button type="submit" class="btn btn-main-action" id="stageUpdateSubmit">
                                    <i class="fas fa-save me-1"></i>
                                    <span class="ms-2">Update</span>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>


        </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>

