Dropzone.autoDiscover = false; new Dropzone("#form-invia-candidatura-posizione-aperta", { url: "https://www.orangepix.it/assets/ajax/upload_allegati.php", acceptedFiles: ".pdf,.doc,.docx", maxFilesize: 10, addRemoveLinks: true, dictRemoveFile: "Rimuovi", init: function () { var dz = this; /* ── HELPER: aggiorna stato bottone ─────────────────────────── */ function aggiornaBottone() { var saved = []; try { saved = JSON.parse(localStorage.getItem("dz_candidatura")) || []; } catch(e) {} var btn = document.getElementById("send"); if (!btn) return; if (saved.length > 0) { btn.removeAttribute("disabled"); btn.classList.add("opacity-1"); btn.style.opacity = "1"; } else { btn.setAttribute("disabled", "disabled"); btn.classList.remove("opacity-1"); btn.style.opacity = "0.2"; } } /* ── 1. RICARICA I FILE AL REFRESH ─────────────────────────── */ var saved = []; try { saved = JSON.parse(localStorage.getItem("dz_candidatura")) || []; } catch(e) {} saved.forEach(function (f) { var mock = { name: f.name, size: f.size, serverPath: f.serverPath }; dz.emit("addedfile", mock); dz.emit("complete", mock); dz.files.push(mock); }); // controlla subito al caricamento aggiornaBottone(); /* ── 2. SALVA IN localStorage DOPO UPLOAD OK ───────────────── */ dz.on("success", function (file, response) { var saved = []; try { saved = JSON.parse(localStorage.getItem("dz_candidatura")) || []; } catch(e) {} var serverPath = (response && response.path) ? response.path : null; var exists = saved.some(function(f) { return f.name === file.name && f.size === file.size; }); if (!exists) { saved.push({ name: file.name, size: file.size, serverPath: serverPath }); localStorage.setItem("dz_candidatura", JSON.stringify(saved)); } aggiornaBottone(); }); /* ── 3. RIMUOVI FILE: localStorage + chiamata AJAX ─────────── */ dz.on("removedfile", function (file) { var saved = []; try { saved = JSON.parse(localStorage.getItem("dz_candidatura")) || []; } catch(e) {} saved = saved.filter(function(f) { return !(f.name === file.name && f.size === file.size); }); localStorage.setItem("dz_candidatura", JSON.stringify(saved)); var fd = new FormData(); fd.append("filename", file.name); if (file.serverPath) { fd.append("filepath", file.serverPath); } var xhr = new XMLHttpRequest(); xhr.open("POST", "https://www.orangepix.it/assets/ajax/delete_allegati.php", true); xhr.send(fd); aggiornaBottone(); }); } });