(function () { function escapeHtml(str) { return String(str || "") .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } function valueOrDash(v) { return v === null || v === undefined || String(v).trim() === "" ? "—" : escapeHtml(v); } function renderReadonlyField(label, value) { return `
${escapeHtml(label)}
${valueOrDash(value)}
`; } window.loadMatriceWorksheets = function (idmatrice, matriceNome, endpoint) { endpoint = endpoint || "ajax/worksheet-linked-data.php"; $("#wl_idmatrice").val(idmatrice); $("#wl_matrice_name").html( `${escapeHtml(matriceNome || "")}`, ); $("#worksheetsListContainer").html( '
Caricamento fogli di lavoro...
', ); fetch(endpoint, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: "action=get_matrice_worksheets&idmatrice=" + encodeURIComponent(idmatrice), }) .then(async (r) => { const text = await r.text(); try { return JSON.parse(text); } catch (e) { console.error("Risposta non JSON worksheets:", text); throw new Error("Risposta non JSON, vedi console"); } }) .then((data) => { if (!data.success) { $("#worksheetsListContainer").html( '
Errore nel caricamento dei fogli
', ); return; } const rows = data.worksheets || []; if (!rows.length) { $("#worksheetsListContainer").html( '
Nessun foglio di lavoro collegato a questo profilo
', ); return; } let html = `
`; rows.forEach((r) => { const statusBadge = r.worksheet_status === "inactive" ? `Inattivo` : `Attivo`; html += ` `; }); html += `
Foglio Rev. Stato Data foglio Cliente Codice profilo Mescole Azioni
${escapeHtml(r.worksheet_number_label || "—")} ${escapeHtml(r.revision_label || "0")} ${statusBadge} ${escapeHtml(r.worksheet_date_it || "—")} ${escapeHtml(r.customer_name || "—")} ${escapeHtml(r.profile_type_code || "—")} ${escapeHtml(String(r.mix_count || 0))} Apri pagina
`; $("#worksheetsListContainer").html(html); }) .catch(() => { $("#worksheetsListContainer").html( '
Errore nel caricamento dei fogli
', ); }); }; let wsDisegniList = []; function wsFileExt(name) { const p = String(name || "").split("."); return p.length > 1 ? p.pop().toLowerCase() : ""; } function wsIsImage(ext) { return ["jpg", "jpeg", "png", "gif", "bmp", "webp"].includes(ext); } function showWorksheetDisegno(index) { const f = wsDisegniList[index]; if (!f) return; $(".ws-disegno-item").removeClass("active"); $(`.ws-disegno-item[data-index="${index}"]`).addClass("active"); const name = f.file_name || ""; const path = f.file_url || f.file_path || ""; const ext = wsFileExt(name); let viewer; if (ext === "pdf") { viewer = ``; } else if (wsIsImage(ext)) { viewer = `
${escapeHtml(name)}
`; } else { viewer = `
Anteprima non disponibile per questo formato: usa "Apri in nuova scheda"
`; } $("#wsDisegnoToolbar").html(`
${escapeHtml(name)}
${f.description ? `
${escapeHtml(f.description)}
` : ""}
Apri in nuova scheda `); $("#wsDisegnoViewer").html(viewer); } function loadWorksheetDisegni(idmatrice) { wsDisegniList = []; const $c = $("#wsDisegniContainer"); if (!idmatrice) { $("#wsDisegniCount").text("0"); $c.html( '
Nessun profilo collegato a questo foglio.
', ); return; } fetch("get_matrice_attachments.php?id=" + encodeURIComponent(idmatrice)) .then((r) => r.json()) .then((data) => { if (!data.success) throw new Error( data.message || "Errore caricamento allegati", ); wsDisegniList = (data.attachments || []).filter( (a) => parseInt(a.is_disegno, 10) === 1, ); $("#wsDisegniCount").text(wsDisegniList.length); if (!wsDisegniList.length) { $c.html( '
Nessun disegno tecnico per questo profilo. ' + "Si possono segnare dalla gestione matrici (graffetta → pulsante compasso).
", ); return; } let listHtml = ""; if (wsDisegniList.length > 1) { listHtml = '
' + wsDisegniList .map( (f, i) => ``, ) .join("") + "
"; } $c.html(` ${listHtml}
`); // Se l'utente ha già aperto il tab mentre caricava, mostro subito il primo if ($('.ws-tab[data-tab="disegni"]').hasClass("active")) { showWorksheetDisegno(0); } }) .catch((err) => { $("#wsDisegniCount").text("!"); $c.html( '
Errore nel caricamento dei disegni: ' + escapeHtml(err.message || "") + "
", ); }); } window.loadWorksheetDetail = function (worksheetId, endpoint) { endpoint = endpoint || "ajax/worksheet-linked-data.php"; $("#worksheetDetailContainer").html( '
Caricamento dettaglio foglio...
', ); fetch(endpoint, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: "action=get_worksheet_detail&worksheet_id=" + encodeURIComponent(worksheetId), }) .then(async (r) => { const text = await r.text(); try { return JSON.parse(text); } catch (e) { console.error("Risposta non JSON worksheet detail:", text); throw new Error("Risposta non JSON, vedi console"); } }) .then((data) => { if (!data.success) { console.error("Errore worksheets:", data); $("#worksheetsListContainer").html( '
Errore nel caricamento dei fogli: ' + escapeHtml( data.message || "nessun dettaglio restituito", ) + "
", ); return; } const ws = data.worksheet || {}; const mixRows = data.mix_rows || []; let mixHtml = `
Nessuna mescola associata
`; if (mixRows.length) { mixHtml = `
`; mixRows.forEach((r) => { const nomeMescola = r.mescola_uscita ? `${escapeHtml(r.mescola_nome || "—")}
${escapeHtml(r.mescola_uscita)}
` : escapeHtml(r.mescola_nome || "—"); mixHtml += ` `; }); mixHtml += `
Pos Mescola Peso g/m Densità Durezza Lubr. Note lubr.
${escapeHtml(String(r.mix_position || ""))} ${nomeMescola} ${valueOrDash(r.mix_weight_g_m)} ${valueOrDash(r.required_density)} ${valueOrDash(r.required_hardness_shore_a)} ${valueOrDash(r.lubrication_type)} ${valueOrDash(r.lubrication_notes)}
`; } const headerHtml = `

${escapeHtml(ws.worksheet_number_label || "—")} / Rev. ${escapeHtml(ws.revision_label || "0")}

Stato: ${escapeHtml(ws.worksheet_status_label || "Attivo")} ${ws.matrice_nome ? " • Profilo: " + escapeHtml(ws.matrice_nome) : ""} ${ws.matrice_cliente ? " • Cliente matrice: " + escapeHtml(ws.matrice_cliente) : ""}
Apri foglio completo
`; const html = `
Dati principali
${renderReadonlyField("Foglio", ws.worksheet_number_label)} ${renderReadonlyField("Revisione", ws.revision_label)} ${renderReadonlyField("Stato", ws.worksheet_status_label)} ${renderReadonlyField("Data foglio", ws.worksheet_date_it)} ${renderReadonlyField("Cliente override", ws.customer_name)} ${renderReadonlyField("Codice profilo", ws.profile_type_code)} ${renderReadonlyField("Marchiatura", ws.marking)} ${renderReadonlyField("Approvato da", ws.approved_by)} ${renderReadonlyField("Creato il", ws.created_at_it)} ${renderReadonlyField("Ultimo aggiornamento", ws.updated_at_it)}
Controlli produzione
${renderReadonlyField("Taglio", ws.control_frequency_cut)} ${renderReadonlyField("Disegno", ws.control_frequency_drawing)} ${renderReadonlyField("Dima", ws.control_frequency_jig)} ${renderReadonlyField("Modalità dima", ws.control_mode_jig)}

Impostazione misure controllo produzione
${valueOrDash(ws.prod_control_measure_settings)}
Packaging / Confezionamento
${renderReadonlyField("Codice confezione", ws.requested_package_code)} ${renderReadonlyField("Metri per confezione", ws.meters_per_package)} ${renderReadonlyField("Tolleranza metri/conf.", ws.meters_per_package_tolerance)} ${renderReadonlyField("Scatola tipo", ws.box_type)} ${renderReadonlyField("Conf./pezzi per scatola", ws.packages_or_pieces_per_box)} ${renderReadonlyField("Metri per scatola", ws.meters_per_box)} ${renderReadonlyField("Bancale tipo", ws.pallet_type)} ${renderReadonlyField("Scatole/conf. per bancale", ws.boxes_or_packages_per_pallet)}

Note metri / confezione
${valueOrDash(ws.meters_per_package_notes)}
Velocità e note
${renderReadonlyField("Vel. prevista kg/h", ws.speed_expected_kg_h)} ${renderReadonlyField("Vel. effettiva kg/h", ws.speed_actual_kg_h)} ${renderReadonlyField("Vel. prevista m/h", ws.speed_expected_m_h)} ${renderReadonlyField("Vel. effettiva m/h", ws.speed_actual_m_h)}

Note
${valueOrDash(ws.notes)}
Mescole associate al foglio
${mixHtml}
`; const fullHtml = ` ${headerHtml}
${html}
Caricamento disegni tecnici...
`; $("#worksheetDetailContainer").html(fullHtml); loadWorksheetDisegni(ws.idmatrice); }) .catch((err) => { console.error("Catch loadMatriceWorksheets:", err); $("#worksheetsListContainer").html( '
Errore nel caricamento dei fogli: ' + escapeHtml(err.message || "errore JavaScript/fetch") + "
", ); }); }; $(document).on("click", ".worksheets, .show-worksheets", function () { const idmatrice = $(this).data("id"); const nome = $(this).data("nome") || ""; const endpoint = $(this).data("endpoint") || "ajax/worksheet-linked-data.php"; loadMatriceWorksheets(idmatrice, nome, endpoint); new bootstrap.Modal( document.getElementById("worksheetsListModal"), ).show(); }); $(document).on("click", ".ws-tab", function () { const tab = $(this).data("tab"); $(".ws-tab").removeClass("active"); $(this).addClass("active"); $(".ws-tab-pane").removeClass("active"); $(`.ws-tab-pane[data-pane="${tab}"]`).addClass("active"); // Carico il PDF solo la prima volta che si apre il tab if ( tab === "disegni" && wsDisegniList.length && $("#wsDisegnoViewer").is(":empty") ) { showWorksheetDisegno(0); } }); $(document).on("click", ".ws-disegno-item", function () { showWorksheetDisegno(parseInt($(this).data("index"), 10)); }); $(document).on("click", ".open-worksheet-detail", function () { const worksheetId = $(this).data("id"); const endpoint = $(this).data("endpoint") || "ajax/worksheet-linked-data.php"; loadWorksheetDetail(worksheetId, endpoint); new bootstrap.Modal( document.getElementById("worksheetDetailModal"), ).show(); }); })();