diff --git a/public/userarea/matrici.php b/public/userarea/matrici.php index 7cf3ca8..1538111 100644 --- a/public/userarea/matrici.php +++ b/public/userarea/matrici.php @@ -1117,28 +1117,48 @@ $("#mm_matrice_name").text(nome); const $sel = $("#mm_mescole"); - $sel.empty().trigger("change"); + $sel.empty(); + + // Placeholder temporaneo mentre carica + const loadingOption = new Option("Caricamento mescole...", "", false, false); + $sel.append(loadingOption).trigger("change"); + + const modalEl = document.getElementById('matriceMescoleModal'); + const modal = new bootstrap.Modal(modalEl); + modal.show(); fetch("get_matrice_mescole.php?id=" + encodeURIComponent(idmatrice)) - .then(r => r.json()) + .then(r => { + if (!r.ok) { + throw new Error("HTTP " + r.status); + } + return r.json(); + }) .then(data => { if (!data.success) { - Swal.fire({ - icon: "error", - title: "Errore", - text: data.message || "Impossibile caricare le mescole" - }); - return; + throw new Error(data.message || "Impossibile caricare le mescole"); } - data.mescole.forEach(m => { + $sel.empty(); + + (data.mescole || []).forEach(m => { const label = m.nomeuscita ? `${m.nome} (${m.nomeuscita})` : m.nome; const opt = new Option(label, m.id, false, false); $sel.append(opt); }); $sel.val((data.selected_ids || []).map(String)).trigger("change"); - new bootstrap.Modal(document.getElementById('matriceMescoleModal')).show(); + }) + .catch(err => { + console.error("Errore caricamento mescole:", err); + + $sel.empty().trigger("change"); + + Swal.fire({ + icon: "error", + title: "Errore", + text: err.message || "Errore durante il caricamento delle mescole" + }); }); });