210 lines
8.7 KiB
JavaScript
210 lines
8.7 KiB
JavaScript
$(document).ready(function () {
|
|
console.log("parts.js caricato correttamente");
|
|
|
|
$(".parts-btn").on("click", function () {
|
|
console.log("Pulsante Parts cliccato");
|
|
const iddatadb = $(this).data("iddatadb");
|
|
const rowIndex = $(this).data("row");
|
|
const importRef = $("table tbody tr")
|
|
.eq(rowIndex)
|
|
.find("td")
|
|
.eq(1)
|
|
.text();
|
|
const description =
|
|
$("table tbody tr").eq(rowIndex).find("td").eq(2).text() ||
|
|
"Sconosciuto";
|
|
|
|
$("#trfHeader").text(`${iddatadb} - ${importRef} - ${description}`);
|
|
$("#partsModal").data("iddatadb", iddatadb);
|
|
|
|
loadExistingParts(iddatadb);
|
|
|
|
$("#partsModal").modal("show");
|
|
});
|
|
|
|
function addNewRow(nextPartNumber) {
|
|
const newRow = `
|
|
<tr data-part-id="new">
|
|
<td><input type="number" class="form-control form-control-sm part-number" value="${nextPartNumber || 1}" style="width: 80px;"></td>
|
|
<td><input type="text" class="form-control form-control-sm part-description" placeholder="Inserisci descrizione" style="width: 100%;"></td>
|
|
<td>
|
|
<button type="button" class="btn btn-success btn-sm add-row" style="padding: 0.1rem 0.3rem; font-size: 0.8rem;"><i class="fas fa-plus fa-xs"></i></button>
|
|
<button type="button" class="btn btn-danger btn-sm remove-row" style="padding: 0.1rem 0.3rem; font-size: 0.8rem; display: none;"><i class="fas fa-trash fa-xs"></i></button>
|
|
<span class="save-status text-success" style="display: none; margin-left: 5px;"><i class="fas fa-check fa-xs"></i></span>
|
|
<span class="save-loading text-warning" style="display: none; margin-left: 5px;"><i class="fas fa-spinner fa-spin fa-xs"></i></span>
|
|
</td>
|
|
</tr>
|
|
`;
|
|
$("#partsTableBody").append(newRow);
|
|
updateRowButtons();
|
|
}
|
|
|
|
function updateRowButtons() {
|
|
const rowCount = $("#partsTableBody tr").length;
|
|
$("#partsTableBody tr").each(function (index) {
|
|
const $removeBtn = $(this).find(".remove-row");
|
|
if (rowCount > 1) {
|
|
$removeBtn.show();
|
|
} else {
|
|
$removeBtn.hide();
|
|
}
|
|
});
|
|
}
|
|
|
|
$(document).on("click", ".add-row", function (e) {
|
|
e.preventDefault();
|
|
console.log("Pulsante Aggiungi riga cliccato");
|
|
const maxPartNumber = Math.max(
|
|
...$("#partsTableBody tr")
|
|
.map(function () {
|
|
return parseInt($(this).find(".part-number").val()) || 0;
|
|
})
|
|
.get(),
|
|
);
|
|
addNewRow(maxPartNumber + 1);
|
|
});
|
|
|
|
$(document).on("click", ".remove-row", function (e) {
|
|
e.preventDefault();
|
|
console.log("Pulsante Rimuovi riga cliccato");
|
|
const $row = $(this).closest("tr");
|
|
const partId = $row.data("part-id");
|
|
console.log("ID parte da eliminare:", partId);
|
|
|
|
if (partId !== "new") {
|
|
$.ajax({
|
|
url: "delete_part.php",
|
|
method: "POST",
|
|
data: JSON.stringify({ part_id: partId }),
|
|
contentType: "application/json",
|
|
beforeSend: function () {
|
|
console.log(
|
|
"Invio richiesta AJAX a delete_part.php con part_id:",
|
|
partId,
|
|
);
|
|
},
|
|
success: function (response) {
|
|
console.log("Risposta da delete_part.php:", response);
|
|
if (response.success) {
|
|
$row.remove();
|
|
updateRowButtons();
|
|
} else {
|
|
alert("Errore nell'eliminazione: " + response.message);
|
|
}
|
|
},
|
|
error: function (xhr, status, error) {
|
|
console.log("Errore AJAX:", status, error);
|
|
alert(
|
|
"Errore nell'eliminazione: " +
|
|
error +
|
|
". Stato: " +
|
|
xhr.status +
|
|
" - " +
|
|
xhr.responseText,
|
|
);
|
|
},
|
|
});
|
|
} else {
|
|
$row.remove();
|
|
updateRowButtons();
|
|
}
|
|
});
|
|
|
|
$(document).on("blur", ".part-description, .part-number", function () {
|
|
const $input = $(this);
|
|
const $row = $input.closest("tr");
|
|
const partNumber = $row.find(".part-number").val();
|
|
const partDescription = $row.find(".part-description").val().trim();
|
|
const $saveStatus = $row.find(".save-status");
|
|
const $saveLoading = $row.find(".save-loading");
|
|
const iddatadb = $("#partsModal").data("iddatadb");
|
|
|
|
console.log("Evento blur su input:", { partNumber, partDescription });
|
|
|
|
if (partDescription && iddatadb) {
|
|
$saveLoading.show();
|
|
$saveStatus.hide();
|
|
|
|
$.ajax({
|
|
url: "save_parts.php",
|
|
method: "POST",
|
|
data: JSON.stringify({
|
|
iddatadb: iddatadb,
|
|
parts: [
|
|
{
|
|
part_number: partNumber,
|
|
part_description: partDescription,
|
|
},
|
|
],
|
|
}),
|
|
contentType: "application/json",
|
|
success: function (response) {
|
|
if (response.success) {
|
|
$saveLoading.hide();
|
|
$saveStatus.show();
|
|
setTimeout(() => $saveStatus.hide(), 2000);
|
|
} else {
|
|
alert("Errore nel salvataggio: " + response.message);
|
|
$saveLoading.hide();
|
|
}
|
|
},
|
|
error: function (xhr, status, error) {
|
|
alert("Errore nel salvataggio delle parti: " + error);
|
|
$saveLoading.hide();
|
|
},
|
|
});
|
|
}
|
|
});
|
|
|
|
function loadExistingParts(iddatadb) {
|
|
$.ajax({
|
|
url: "load_parts.php",
|
|
method: "GET",
|
|
data: { iddatadb: iddatadb },
|
|
success: function (response) {
|
|
if (response.success) {
|
|
$("#partsTableBody").empty();
|
|
if (response.parts.length > 0) {
|
|
response.parts.forEach((part) => {
|
|
const newRow = `
|
|
<tr data-part-id="${part.id}">
|
|
<td><input type="number" class="form-control form-control-sm part-number" value="${part.part_number}" style="width: 80px;"></td>
|
|
<td><input type="text" class="form-control form-control-sm part-description" value="${part.part_description}" style="width: 100%;"></td>
|
|
<td>
|
|
<button type="button" class="btn btn-success btn-sm add-row" style="padding: 0.1rem 0.3rem; font-size: 0.8rem;"><i class="fas fa-plus fa-xs"></i></button>
|
|
<button type="button" class="btn btn-danger btn-sm remove-row" style="padding: 0.1rem 0.3rem; font-size: 0.8rem;"><i class="fas fa-trash fa-xs"></i></button>
|
|
<span class="save-status text-success" style="display: none; margin-left: 5px;"><i class="fas fa-check fa-xs"></i></span>
|
|
<span class="save-loading text-warning" style="display: none; margin-left: 5px;"><i class="fas fa-spinner fa-spin fa-xs"></i></span>
|
|
</td>
|
|
</tr>
|
|
`;
|
|
$("#partsTableBody").append(newRow);
|
|
});
|
|
} else {
|
|
addNewRow(1);
|
|
}
|
|
updateRowButtons();
|
|
} else {
|
|
alert(
|
|
"Errore nel caricamento delle parti: " +
|
|
response.message,
|
|
);
|
|
addNewRow(1);
|
|
}
|
|
},
|
|
error: function (xhr, status, error) {
|
|
alert("Errore nel caricamento delle parti: " + error);
|
|
addNewRow(1);
|
|
},
|
|
});
|
|
}
|
|
|
|
$(document).on("mouseenter", "tr", function () {
|
|
console.log("Mouse entrato su riga");
|
|
});
|
|
|
|
$(document).on("mouseleave", "tr", function () {
|
|
console.log("Mouse uscito da riga");
|
|
});
|
|
});
|