import { Controller } from "@hotwired/stimulus" export default class extends Controller { static values = { projectCode: String } dragstart(event) { event.dataTransfer.setData("text/plain", event.target.dataset.opportunityId) event.dataTransfer.effectAllowed = "move" } dragover(event) { event.preventDefault() event.currentTarget.classList.add("ring-2", "ring-emerald-400") } dragleave(event) { event.currentTarget.classList.remove("ring-2", "ring-emerald-400") } async drop(event) { event.preventDefault() event.currentTarget.classList.remove("ring-2", "ring-emerald-400") const opportunityId = event.dataTransfer.getData("text/plain") const stage = event.currentTarget.dataset.stage if (!opportunityId || !stage) return if (stage === "lost") { const reason = prompt("Motivo perdita (no_response, not_interested, price, competitor, no_streaming, timing, technical, deferred, other):", "other") if (!reason) return await this.updateStage(opportunityId, stage, reason) } else { await this.updateStage(opportunityId, stage) } } async updateStage(opportunityId, stage, lostReason = null) { const token = document.querySelector("meta[name='csrf-token']").content const body = { pipeline_stage: stage } if (lostReason) body.lost_reason = lostReason const response = await fetch(`/p/${this.projectCodeValue}/opportunities/${opportunityId}/update_stage`, { method: "PATCH", headers: { "Content-Type": "application/json", "X-CSRF-Token": token, "Accept": "application/json" }, body: JSON.stringify(body) }) if (response.ok) { window.location.reload() } else { const data = await response.json().catch(() => ({})) alert(data.errors?.join(", ") || "Impossibile aggiornare lo stage") } } }