document.addEventListener("DOMContentLoaded", () => { if (serverErrors.length > 0) mostrarErrores(serverErrors); if (serverSuccess.length > 0) mostrarSuccess(serverSuccess); // FUNCIÓN PARA MOSTRAR ERRORES function mostrarErrores(errores) { // Buscar si hay un toast previo const viejo = document.getElementById("toastErrores"); // Si existe, se elimina (evita duplicados) if (viejo) viejo.remove(); // Crea el contenedor del toast const toast = document.createElement("div"); // Se le asigna ID para identificarlo toast.id = "toastErrores"; // Clases de estilo (Bootstrap + CSS) toast.className = "toast-flotante alert alert-danger"; // Inserta el contenido dinámico (lista de errores) toast.innerHTML = ` Se encontraron errores: `; // Agrega el toast al body (lo hace visible) document.body.appendChild(toast); // Auto-cierre setTimeout(() => { // Agrega clase para animación de salida (CSS) toast.classList.add("toast-out"); // Elimina el elemento después de la animación setTimeout(() => { toast.remove(); // Elimina el DOM }, 350); }, 5000); // Visible durante 5 segundos } }); // FUNCIÓN PARA MOSTRAR SUCCESS function mostrarSuccess(mensajes) { // Elimina toast previo de éxito si existe const viejo = document.getElementById("toastSuccess"); if (viejo) viejo.remove(); // Crea el contenedor del toast const toast = document.createElement("div"); // Se le asigna ID único toast.id = "toastSuccess"; // Estilo toast.className = "toast-flotante alert alert-success"; // Inserta mensajes dinámicos toast.innerHTML = ` Mensajes de éxito: `; // Agrega al DOM document.body.appendChild(toast); // Auto-cierre a los 5 segundos setTimeout(() => { // Animación de salida toast.classList.add("toast-out"); // Eliminación final setTimeout(() => { toast.remove(); }, 350); }, 5000); }