diff --git a/public/userarea/partsTable.js b/public/userarea/partsTable.js
index f6fc697..a4bba99 100644
--- a/public/userarea/partsTable.js
+++ b/public/userarea/partsTable.js
@@ -9,6 +9,7 @@ $(document).ready(function () {
let quotations = [];
let partsExtraField = null; // {field_id, field_label} oppure null
let extraFieldOptions = []; // [{id,label}]
+ let isLoadingPartsRecord = false;
// --- ROW ID helpers: niente più cache impazzita di jQuery .data() ---
function getPartId($row) {
@@ -509,21 +510,175 @@ $(document).ready(function () {
// MODAL HANDLING
// ===================
function loadParts(iddatadb, idquotations, callback = null) {
+ isLoadingPartsRecord = true;
+ unsavedChanges = false;
+
+ // Store current modal context
+ $("#partsModal").data("iddatadb", iddatadb || null);
+ $("#partsModal").data("idquotations", idquotations || null);
+
+ // Store the visible record list from the main grid
+ if (Array.isArray(window.visibleIddatadbList)) {
+ $("#partsModal").data(
+ "visible-iddatadb-list",
+ window.visibleIddatadbList,
+ );
+ }
+
+ updatePartsRecordHeader(iddatadb);
+
+ const finishLoading = function () {
+ unsavedChanges = false;
+ isLoadingPartsRecord = false;
+
+ if (callback) callback();
+ };
+
if (iddatadb) {
loadMacroMatrici();
initializeGlobalSelect2();
loadPartsExtraField(iddatadb, function () {
loadPhoto(iddatadb, idquotations);
- loadExistingParts(iddatadb, idquotations, callback);
+ loadExistingParts(iddatadb, idquotations, finishLoading);
});
} else {
loadPartsExtraField(iddatadb, function () {
loadPhoto(iddatadb, idquotations);
- loadExistingParts(iddatadb, idquotations, callback);
+ loadExistingParts(iddatadb, idquotations, finishLoading);
});
}
}
+ // ===================
+ // PARTS MODAL RECORD NAVIGATION
+ // ===================
+
+ function getVisiblePartsRecordList() {
+ const listFromModal = $("#partsModal").data("visible-iddatadb-list");
+
+ if (Array.isArray(listFromModal) && listFromModal.length > 0) {
+ return listFromModal.map((v) => parseInt(v, 10)).filter(Boolean);
+ }
+
+ if (
+ Array.isArray(window.visibleIddatadbList) &&
+ window.visibleIddatadbList.length > 0
+ ) {
+ return window.visibleIddatadbList
+ .map((v) => parseInt(v, 10))
+ .filter(Boolean);
+ }
+
+ if (Array.isArray(window.gridData) && window.gridData.length > 0) {
+ return window.gridData
+ .map((row) => parseInt(row.iddatadb, 10))
+ .filter(Boolean);
+ }
+
+ return [];
+ }
+
+ function getGridRecordById(iddatadb) {
+ if (!Array.isArray(window.gridData)) return null;
+
+ return (
+ window.gridData.find((row) => {
+ return parseInt(row.iddatadb, 10) === parseInt(iddatadb, 10);
+ }) || null
+ );
+ }
+
+ function getRecordHeaderLabel(iddatadb) {
+ const record = getGridRecordById(iddatadb);
+
+ if (!record) {
+ return iddatadb ? "#" + iddatadb : "";
+ }
+
+ // Prefer main field value if available
+ if (record.mainFieldValue) {
+ return record.mainFieldValue;
+ }
+
+ // Fallbacks
+ if (record.importreferencecode) {
+ return record.importreferencecode;
+ }
+
+ if (record.filename_import) {
+ return record.filename_import;
+ }
+
+ return "#" + iddatadb;
+ }
+
+ function updatePartsRecordHeader(iddatadb) {
+ const list = getVisiblePartsRecordList();
+ const currentId = parseInt(iddatadb, 10);
+ const currentIndex = list.indexOf(currentId);
+
+ $("#trfHeader").text(getRecordHeaderLabel(currentId));
+
+ if (list.length <= 1 || currentIndex === -1) {
+ $("#partsRecordCounter").text("-");
+ $("#prevPartsRecordBtn").prop("disabled", true);
+ $("#nextPartsRecordBtn").prop("disabled", true);
+ return;
+ }
+
+ $("#partsRecordCounter").text(currentIndex + 1 + " / " + list.length);
+
+ $("#prevPartsRecordBtn").prop("disabled", currentIndex <= 0);
+ $("#nextPartsRecordBtn").prop(
+ "disabled",
+ currentIndex >= list.length - 1,
+ );
+ }
+
+ function goToAdjacentPartsRecord(direction) {
+ const list = getVisiblePartsRecordList();
+ const currentId = parseInt($("#partsModal").data("iddatadb"), 10);
+ const currentIndex = list.indexOf(currentId);
+
+ if (currentIndex === -1) return;
+
+ const nextIndex = currentIndex + direction;
+
+ if (nextIndex < 0 || nextIndex >= list.length) return;
+
+ if (
+ !isLoadingPartsRecord &&
+ unsavedChanges &&
+ !confirm(
+ "Hai modifiche non salvate. Vuoi cambiare record senza salvare?",
+ )
+ ) {
+ return;
+ }
+
+ const nextIddatadb = list[nextIndex];
+
+ // Reset local modal state before loading the next record
+ partMatrice = {};
+ unsavedChanges = false;
+ $("#partsTableBody").empty();
+ $("#photoSelectorContainer").empty().hide();
+ $("#samplePhoto").attr("src", "");
+ $(".temp-alert").remove();
+
+ loadParts(nextIddatadb, null);
+ }
+
+ $(document).on("click", "#prevPartsRecordBtn", function (e) {
+ e.preventDefault();
+ goToAdjacentPartsRecord(-1);
+ });
+
+ $(document).on("click", "#nextPartsRecordBtn", function (e) {
+ e.preventDefault();
+ goToAdjacentPartsRecord(1);
+ });
+
// EVENTO PER APRIRE IL SECONDO MODALE
$(document).on("click", "#openAnnotationsBtn", function () {
console.log("Clic su Apri Annotazioni...");
@@ -1090,7 +1245,10 @@ $(document).ready(function () {
initializeExtraFieldSelect2($newRow);
updateRowButtons();
- markUnsaved();
+
+ if (!isLoadingPartsRecord) {
+ markUnsaved();
+ }
}
// ===================
@@ -2142,6 +2300,8 @@ $(document).ready(function () {
});
function markUnsaved() {
+ if (isLoadingPartsRecord) return;
+
if (!unsavedChanges) {
unsavedChanges = true;
}