(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
', ); }); }; 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 html = `

${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
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}
`; $("#worksheetDetailContainer").html(html); }) .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", ".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(); }); })();