259 lines
9.4 KiB
JavaScript
259 lines
9.4 KiB
JavaScript
/**
|
|
* modals_gridData.js — Photos, Parts, Tested Component handlers for gridData pages
|
|
*/
|
|
(function () {
|
|
"use strict";
|
|
|
|
// ── Photos — use photos.js loadPopupContent (exported to window) ──
|
|
$(document).on("click", ".photos-btn", function () {
|
|
const iddatadb = $(this).data("iddatadb") || null;
|
|
const idquotations = $(this).data("idquotations") || null;
|
|
const modal = document.getElementById("photosModal");
|
|
if (!modal) return;
|
|
|
|
modal.style.display = "block";
|
|
|
|
if (typeof window.loadPopupContent === "function") {
|
|
window.loadPopupContent(iddatadb, idquotations);
|
|
}
|
|
});
|
|
|
|
// Close photos modal
|
|
$(document).on("click", ".close-btn", function () {
|
|
const modal = document.getElementById("photosModal");
|
|
if (modal) modal.style.display = "none";
|
|
});
|
|
|
|
// Close on backdrop click
|
|
$(document).on("click", "#photosModal", function (e) {
|
|
if (e.target === this) {
|
|
this.style.display = "none";
|
|
}
|
|
});
|
|
|
|
// ── Parts (matching import_edit2.php behavior) ─────────────────────
|
|
$(document).on("click", ".parts-btn", function () {
|
|
const iddatadb = $(this).data("iddatadb") || null;
|
|
const idquotations = $(this).data("idquotations") || null;
|
|
|
|
$.ajax({
|
|
url: "modal_partsTable.php",
|
|
method: "GET",
|
|
data: { iddatadb: iddatadb },
|
|
success: function (response) {
|
|
$("#partsModalContainer").html(response);
|
|
const modalElement = document.getElementById("partsModal");
|
|
if (!modalElement) return;
|
|
|
|
$("#trfHeader").text(iddatadb || idquotations || "");
|
|
|
|
const visibleIddatadbList = Array.isArray(window.gridData)
|
|
? window.gridData
|
|
.map((r) => parseInt(r.iddatadb, 10))
|
|
.filter((v) => !isNaN(v) && v > 0)
|
|
: [];
|
|
|
|
$("#partsModal")
|
|
.data("iddatadb", iddatadb)
|
|
.data("idquotations", idquotations)
|
|
.data("visible-iddatadb-list", visibleIddatadbList);
|
|
|
|
let modal = bootstrap.Modal.getInstance(modalElement);
|
|
if (!modal)
|
|
modal = new bootstrap.Modal(modalElement, {
|
|
backdrop: true,
|
|
keyboard: true,
|
|
focus: true,
|
|
});
|
|
modal.show();
|
|
|
|
if (typeof window.loadParts === "function") {
|
|
window.loadParts(iddatadb, idquotations);
|
|
}
|
|
},
|
|
error: function (xhr, status, error) {
|
|
console.error("Error loading parts:", error);
|
|
},
|
|
});
|
|
});
|
|
|
|
$(document).on("hidden.bs.modal", "#partsModal", function () {
|
|
const modalElement = document.getElementById("partsModal");
|
|
if (modalElement) {
|
|
const modal = bootstrap.Modal.getInstance(modalElement);
|
|
if (modal) modal.dispose();
|
|
}
|
|
$("#partsModalContainer").empty();
|
|
$(".modal-backdrop").remove();
|
|
$("body").removeClass("modal-open").css("padding-right", "");
|
|
});
|
|
|
|
// ── Tested Component quick add ───────────────────────────────────────
|
|
$(document).on("click", ".add-part-btn", async function () {
|
|
const iddatadb = $(this).data("iddatadb") || null;
|
|
const rowIndex = parseInt($(this).data("row"));
|
|
const row = window.gridData?.[rowIndex];
|
|
const id = iddatadb || (row ? row.iddatadb : null);
|
|
if (!id) return;
|
|
|
|
const $cell = $(this).closest(".grid-cell, div");
|
|
const $input = $cell.find("input");
|
|
const raw = ($input.val() || "").trim();
|
|
const parts = raw
|
|
.split("|")
|
|
.map((s) => s.trim())
|
|
.filter((s) => s.length > 0);
|
|
const uniqueParts = [...new Set(parts)];
|
|
|
|
if (!uniqueParts.length) {
|
|
alert("Insert a description first.");
|
|
$input.focus();
|
|
return;
|
|
}
|
|
|
|
try {
|
|
for (const p of uniqueParts) {
|
|
const formData = new FormData();
|
|
formData.append("iddatadb", id);
|
|
formData.append("part_description", p);
|
|
await fetch("add_part_quick.php", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
}
|
|
alert(`Added ${uniqueParts.length} part(s).`);
|
|
$input.val("");
|
|
} catch (e) {
|
|
alert("Error: " + e.message);
|
|
}
|
|
});
|
|
|
|
// ── Delete row ───────────────────────────────────────────────────────
|
|
let deleteIddatadb = null;
|
|
let deleteRowIndex = null;
|
|
|
|
$(document).on("click", ".delete-btn", function () {
|
|
deleteIddatadb = $(this).data("iddatadb");
|
|
deleteRowIndex = parseInt($(this).data("row"));
|
|
const modalEl = document.getElementById("deleteConfirmModal");
|
|
if (modalEl) {
|
|
document.getElementById("deleteIddatadbText").textContent =
|
|
deleteIddatadb;
|
|
new bootstrap.Modal(modalEl).show();
|
|
}
|
|
});
|
|
|
|
$(document).on("click", "#deleteConfirmBtn", async function () {
|
|
const modalEl = document.getElementById("deleteConfirmModal");
|
|
const modal = bootstrap.Modal.getInstance(modalEl);
|
|
if (modal) modal.hide();
|
|
|
|
if (!deleteIddatadb) return;
|
|
|
|
try {
|
|
const resp = await fetch("delete_record.php", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ id: deleteIddatadb }),
|
|
});
|
|
const result = await resp.json();
|
|
|
|
if (result.success) {
|
|
// Remove from gridData
|
|
const idx = window.gridData.findIndex(
|
|
(r) => r.iddatadb === deleteIddatadb,
|
|
);
|
|
if (idx >= 0) window.gridData.splice(idx, 1);
|
|
|
|
// Re-render
|
|
const gr = window.gridRenderer;
|
|
if (gr) gr.renderVisibleRows();
|
|
} else {
|
|
alert("Error: " + result.message);
|
|
}
|
|
} catch (e) {
|
|
alert("Error: " + e.message);
|
|
}
|
|
|
|
deleteIddatadb = null;
|
|
deleteRowIndex = null;
|
|
});
|
|
|
|
// ── Add new row ──────────────────────────────────────────────────────
|
|
$(document).on("click", "#addRowBtn", async function () {
|
|
const btn = this;
|
|
btn.disabled = true;
|
|
|
|
try {
|
|
const templateId = window.gridMeta?.templateId;
|
|
if (!templateId) {
|
|
alert("Template ID missing");
|
|
return;
|
|
}
|
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
const importref = urlParams.get("importref") || "";
|
|
const resp = await fetch("add_record.php", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
template_id: templateId,
|
|
importreferencecode: importref,
|
|
}),
|
|
});
|
|
const result = await resp.json();
|
|
|
|
if (result.success && result.iddatadb) {
|
|
// Build new row object
|
|
const newRow = {
|
|
iddatadb: result.iddatadb,
|
|
status: "i",
|
|
idclient: window.gridMeta?.defaultIdclient || "",
|
|
cliente_fornitore_id: null,
|
|
commessaweb: null,
|
|
user_name: result.user_name || "",
|
|
importreferencecode: result.importreferencecode || "",
|
|
filename_import: "",
|
|
importdate: new Date()
|
|
.toISOString()
|
|
.slice(0, 19)
|
|
.replace("T", " "),
|
|
fixedFields: {},
|
|
details: {},
|
|
mainFieldValue: "",
|
|
_dirty: false,
|
|
};
|
|
|
|
// Add to beginning of gridData
|
|
window.gridData.unshift(newRow);
|
|
|
|
// Re-render
|
|
const gr = window.gridRenderer;
|
|
if (gr) gr.renderVisibleRows();
|
|
|
|
// Highlight new row briefly
|
|
const newGridRow = document.querySelector(
|
|
`.grid-row[data-id="${result.iddatadb}"]`,
|
|
);
|
|
if (newGridRow) {
|
|
newGridRow.classList.add("row-just-created");
|
|
newGridRow.scrollIntoView({
|
|
behavior: "smooth",
|
|
block: "center",
|
|
});
|
|
setTimeout(
|
|
() => newGridRow.classList.remove("row-just-created"),
|
|
4000,
|
|
);
|
|
}
|
|
} else {
|
|
alert("Error: " + (result.message || "Unknown error"));
|
|
}
|
|
} catch (e) {
|
|
alert("Error: " + e.message);
|
|
} finally {
|
|
btn.disabled = false;
|
|
}
|
|
});
|
|
})();
|