AlkantarClanX12
| Current Path : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/lib/ |
| Current File : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/lib/edit-used-finance.js |
jQuery(document).ready(function ($) {
function generateOptionsHTML(options) {
let html = "";
options.forEach(function (option) {
html += "Model => " + option.model + " | ";
html += "Start Year => " + option.start_year + " | ";
html += "End Year => " + option.end_year + " | ";
html += "Term => " + option.term + " | ";
html += "Value => " + option.value + " | ";
html += "isCPO => " + (option.iscpo ? "Yes" : "No") + " ";
});
return html;
}
function handleAjaxResponse(response) {
if (response.success) {
$("#response-message").html(
"<p class='msg'>" + response.data.message + "</p>"
);
if (response.data.reload) {
location.reload();
} else if (response.data.options) {
let optionsHTML = generateOptionsHTML(response.data.options);
$("#options-container").html(optionsHTML);
}
} else {
$("#response-message").html(
"<p class='msg-er'>Error: " + response.data + "</p>"
);
}
}
$("#save-options").on("click", function () {
var option1 = $("#model").val();
var option2 = $("#start_year").val();
var option3 = $("#end_year").val();
var option4 = $("#term").val();
var option5 = $("#value").val();
var option6 = $("#iscpo").prop("checked"); // Capturar como booleano
var missingFields = [];
var valueError = [];
if (option1 === "") {
missingFields.push("Model");
}
if (option2 === "" || option2 == 0) {
missingFields.push("Start Year");
}
if (option3 === "") {
missingFields.push("End Year");
}
if (option4 === "" || option4 == 0) {
missingFields.push("Term");
}
if (option5 === "" || option5 == 0) {
missingFields.push("Value");
}
if (option5 >= 50) {
valueError.push("Value");
}
if (missingFields.length > 0) {
alert(
"The following fields are required and cannot be empty or zero: " +
missingFields.join(", ")
);
return;
}
if (valueError.length > 0) {
alert(
"value must be less than 50: "
);
return;
}
$.ajax({
url: editUsedFinance.ajax_url,
method: "POST",
data: {
action: "save_custom_finance_options",
option1: option1,
option2: option2,
option3: option3,
option4: option4,
option5: option5,
option6: option6,
nonce: editUsedFinance.nonce,
},
success: handleAjaxResponse,
error: function (xhr, status, error) {
$("#response-message").html("<p>Error: " + error + "</p>");
},
});
});
$(document).on("click", "#update-entries", function (e) {
e.preventDefault();
var formData = $("#update-entries-form").serializeArray();
var transformedData = [];
// Group form data by entry index
var groupedData = {};
formData.forEach(function (item) {
var matches = item.name.match(/entries\[(\d+)\]\[(\w+)\]/);
if (matches) {
var index = matches[1];
var key = matches[2];
if (!groupedData[index]) groupedData[index] = {};
groupedData[index][key] = item.value;
}
});
// Transform grouped data into desired format
for (var index in groupedData) {
var entry = groupedData[index];
var value = parseFloat(entry.value);
// Validación para asegurar que el valor no sea mayor a 50
if (value > 50) {
alert("El valor no puede ser mayor a 50.");
return; // Detiene el proceso si el valor es mayor a 50
}
transformedData.push({
models: entry.models,
startyear: parseInt(entry.startyear),
endyear: parseInt(entry.endyear),
term: parseInt(entry.term),
value: value,
isCPO: entry.isCPO === "1",
});
}
// console.log(transformedData);
$.ajax({
url: editUsedFinance.ajax_url,
method: "POST",
data: {
action: "update_custom_finance_options",
entries: transformedData,
nonce: editUsedFinance.nonce,
},
success: handleAjaxResponse,
error: function (xhr, status, error) {
$("#response-message").html("<p>Error: " + error + "</p>");
},
});
});
$(".delete-entry").on("click", function () {
var index = $(this).data("index");
// console.log(index);
if (confirm("Are you sure you want to delete this entry?")) {
$.ajax({
url: editUsedFinance.ajax_url,
method: "POST",
data: {
action: "delete_finance_entry",
nonce: editUsedFinance.nonce,
index: index,
},
success: handleAjaxResponse,
});
}
});
$("#delete-selected").on("click", function () {
var selectedEntries = [];
$(".select-entry:checked").each(function () {
selectedEntries.push(
$(this).closest("tr").find(".delete-entry").data("index")
);
});
console.log(selectedEntries);
if (selectedEntries.length === 0) {
alert("Please select at least one entry to delete.");
return;
}
if (confirm("Are you sure you want to delete the selected entries?")) {
$.ajax({
url: editUsedFinance.ajax_url,
method: "POST",
data: {
action: "delete_selected_entries",
entries: selectedEntries,
nonce: editUsedFinance.nonce,
},
success: handleAjaxResponse,
error: function (xhr, status, error) {
console.log(xhr.responseText); // Log the full error response
alert("Error: " + error);
},
});
}
});
$("#updateGoogleSheet").on("click", function () {
// Deshabilitar el botón al iniciar la solicitud AJAX
$(this).prop("disabled", true);
$.ajax({
url: editUsedFinance.ajax_url,
method: "POST",
data: {
action: "set_data_google_sheet",
nonce: editUsedFinance.nonce,
},
success: function(response) {
// console.log(response); // Verifica la respuesta del servidor
if (response.success) {
$("#google-sheet-update-result").html("<p class='msg'>" + response.data.message + "</p>");
} else {
$("#google-sheet-update-result").html("<p class='msg-er'>Error: " + response.data.message + "</p>");
}
// Aplica la animación de fadeOut después de que el mensaje aparezca
$("#google-sheet-update-result .msg, #google-sheet-update-result .msg-er").fadeIn(1000, function() {
setTimeout(function() {
$("#google-sheet-update-result .msg, #google-sheet-update-result .msg-er").fadeOut(1000);
}, 2000); // Espera 2 segundos antes de comenzar el fadeOut
});
},
error: function (xhr, status, error) {
$("#google-sheet-update-result").html("<p class='msg-er'>Error: " + error + "</p>");
// console.log(xhr.responseText); // Verifica el error si ocurre
},
complete: function() {
// Rehabilitar el botón después de que la solicitud haya terminado
$("#updateGoogleSheet").prop("disabled", false);
}
});
});
});