document.addEventListener("DOMContentLoaded", function() {
    // Handle modal show event
    var statusModal = document.getElementById("statusModal");
    if (statusModal) {
        statusModal.addEventListener("show.bs.modal", function (event) {
            var button = event.relatedTarget;
            var applicationId = button.getAttribute("data-application-id");
            var currentStatus = button.getAttribute("data-current-status");
            var applicantName = button.getAttribute("data-applicant-name");
            var applicantType = button.getAttribute("data-applicant-type");
            var jobId = button.getAttribute("data-job-id");

            var applicationIdInput = statusModal.querySelector("#application_id");
            var applicantNameSpan = statusModal.querySelector("#applicant_name");
            var statusSelect = statusModal.querySelector("#status");
            var modalJobIdInput = statusModal.querySelector("#modal_job_id");
            var currentStatusDisplay = statusModal.querySelector("#current_status_display");

            if (applicationIdInput) {
                applicationIdInput.value = applicationId;
                console.log("Set application_id to:", applicationId);
            }
            
            // Set the form action dynamically
            var form = statusModal.querySelector("#statusForm");
            if (form) {
                let baseUrl = "/bolsa2/admin/job-applications.php";
                form.action = jobId && jobId !== '0' ? baseUrl + '?id=' + jobId : baseUrl;
                console.log("Set form action to:", form.action);
            }
            
            if (modalJobIdInput) {
                modalJobIdInput.value = jobId || "";
                console.log("Set job_id to:", jobId);
            }
            
            if (applicantNameSpan) {
                var typeLabel = (applicantType === "graduate") ? " (Egresado)" : " (Estudiante)";
                applicantNameSpan.textContent = applicantName + typeLabel;
                console.log("Set applicant name to:", applicantName + typeLabel);
            }

            // Set the current status display with appropriate badge styling
            if (currentStatusDisplay) {
                var statusText = "";
                var badgeClass = "";

                switch (currentStatus) {
                    case "pending":
                        statusText = "Pendiente";
                        badgeClass = "bg-warning";
                        break;
                    case "reviewed":
                        statusText = "Revisado";
                        badgeClass = "bg-info";
                        break;
                    case "accepted":
                        statusText = "Aceptado";
                        badgeClass = "bg-success";
                        break;
                    case "rejected":
                        statusText = "Rechazado";
                        badgeClass = "bg-danger";
                        break;
                    default:
                        statusText = currentStatus;
                        badgeClass = "bg-secondary";
                }

                currentStatusDisplay.textContent = statusText;
                currentStatusDisplay.className = "badge " + badgeClass;
                console.log("Set current status display to:", statusText, "with class:", badgeClass);
            }

            // Set the modal title status badge
            var modalTitleStatus = statusModal.querySelector("#modal_title_status");
            if (modalTitleStatus) {
                var titleStatusText = "";
                var titleBadgeClass = "";

                switch (currentStatus) {
                    case "pending":
                        titleStatusText = "Pendiente";
                        titleBadgeClass = "bg-warning";
                        break;
                    case "reviewed":
                        titleStatusText = "Revisado";
                        titleBadgeClass = "bg-info";
                        break;
                    case "accepted":
                        titleStatusText = "Aceptado";
                        titleBadgeClass = "bg-success";
                        break;
                    case "rejected":
                        titleStatusText = "Rechazado";
                        titleBadgeClass = "bg-danger";
                        break;
                    default:
                        titleStatusText = currentStatus;
                        titleBadgeClass = "bg-secondary";
                }

                modalTitleStatus.textContent = titleStatusText;
                modalTitleStatus.className = "badge " + titleBadgeClass;
                console.log("Set modal title status to:", titleStatusText, "with class:", titleBadgeClass);
            }

            // Set the current status in the dropdown
            if (statusSelect) {
                // Use setTimeout to ensure the modal is fully rendered
                setTimeout(function() {
                    statusSelect.value = currentStatus;
                    console.log("Setting status select to:", currentStatus, "Available options:", Array.from(statusSelect.options).map(opt => opt.value));
                }, 100);
            }
        });

        // Handle modal hide event
        statusModal.addEventListener("hidden.bs.modal", function () {
            // Reset form when modal is closed
            var form = statusModal.querySelector("#statusForm");
            if (form) {
                form.reset();
                var submitBtn = form.querySelector("button[type=submit]");
                if (submitBtn) {
                    submitBtn.disabled = false;
                    submitBtn.innerHTML = "Actualizar Estado";
                }
            }
        });
    }

    // Handle form submission
    var statusForm = document.getElementById("statusForm");
    if (statusForm) {
        statusForm.addEventListener("submit", function(e) {
            // Add loading state
            var submitBtn = statusForm.querySelector("button[type=submit]");
            if (submitBtn) {
                submitBtn.disabled = true;
                submitBtn.innerHTML = "<i class=\"fas fa-spinner fa-spin\"></i> Actualizando...";
            }

            console.log("Form submitted with application_id:", document.getElementById("application_id").value);
            // Let the form submit normally
        });
    }
});