document.addEventListener("DOMContentLoaded", function () {
    document.getElementById("formAtencion").addEventListener("submit", function (event) {
        event.preventDefault(); // Evita recargar la p¨¢gina al enviar el formulario

        let formData = new FormData(this);

        fetch("guardar_atencion.php", {
            method: "POST",
            body: formData
        })
        .then(response => response.json())
        .then(data => {
            // ”9è7 Mostrar Toastify
            Toastify({
                text: data.message,
                duration: 3000,
                close: true,
                gravity: "top",
                position: "right",
                style: {
                    background: data.status === "success" ? "linear-gradient(to right, #00b09b, #96c93d)" : "linear-gradient(to right, #ff5f6d, #ffc371)",
                    borderRadius: "5px",
                    textTransform: "uppercase"
                }
            }).showToast();

            // ”9è7 Si la inserci¨®n fue exitosa, cerrar el modal y recargar la p¨¢gina
            if (data.status === "success") {
                let modalElement = document.getElementById("modalAgregar");
                let modal = bootstrap.Modal.getOrCreateInstance(modalElement);
                modal.hide();
                document.getElementById("formAtencion").reset();

                // ”9è7 Recargar la p¨¢gina despu¨¦s de 2 segundos
                setTimeout(() => {
                    window.location.href = "index.php";
                }, 2000);
            }
        })
        .catch(error => console.error("Error:", error));
    });
});
