lubelog/wwwroot/js/gasrecord.js
2024-01-07 07:52:45 -07:00

123 lines
4.1 KiB
JavaScript

function showAddGasRecordModal() {
$.get('/Vehicle/GetAddGasRecordPartialView', function (data) {
if (data) {
$("#gasRecordModalContent").html(data);
//initiate datepicker
$('#gasRecordDate').datepicker({
endDate: "+0d",
format: getShortDatePattern().pattern
});
$('#gasRecordModal').modal('show');
}
});
}
function showEditGasRecordModal(gasRecordId) {
$.get(`/Vehicle/GetGasRecordForEditById?gasRecordId=${gasRecordId}`, function (data) {
if (data) {
$("#gasRecordModalContent").html(data);
//initiate datepicker
$('#gasRecordDate').datepicker({
endDate: "+0d",
format: getShortDatePattern().pattern
});
$('#gasRecordModal').modal('show');
}
});
}
function hideAddGasRecordModal() {
$('#gasRecordModal').modal('hide');
}
function deleteGasRecord(gasRecordId) {
$("#workAroundInput").show();
Swal.fire({
title: "Confirm Deletion?",
text: "Deleted Gas Records cannot be restored.",
showCancelButton: true,
confirmButtonText: "Delete",
confirmButtonColor: "#dc3545"
}).then((result) => {
if (result.isConfirmed) {
$.post(`/Vehicle/DeleteGasRecordById?gasRecordId=${gasRecordId}`, function (data) {
if (data) {
hideAddGasRecordModal();
successToast("Gas Record deleted");
var vehicleId = GetVehicleId().vehicleId;
getVehicleGasRecords(vehicleId);
} else {
errorToast("An error has occurred, please try again later.");
}
});
} else {
$("#workAroundInput").hide();
}
});
}
function saveGasRecordToVehicle(isEdit) {
//get values
var formValues = getAndValidateGasRecordValues();
//validate
if (formValues.hasError) {
errorToast("Please check the form data");
return;
}
//save to db.
$.post('/Vehicle/SaveGasRecordToVehicleId', { gasRecord: formValues }, function (data) {
if (data) {
successToast(isEdit ? "Gas Record Updated" : "Gas Record Added.");
hideAddGasRecordModal();
getVehicleGasRecords(formValues.vehicleId);
} else {
errorToast("An error has occurred, please try again later.");
}
})
}
function getAndValidateGasRecordValues() {
var gasDate = $("#gasRecordDate").val();
var gasMileage = $("#gasRecordMileage").val();
var gasGallons = $("#gasRecordGallons").val();
var gasCost = $("#gasRecordCost").val();
var gasIsFillToFull = $("#gasIsFillToFull").is(":checked");
var vehicleId = GetVehicleId().vehicleId;
var gasRecordId = getGasRecordModelData().id;
//validation
var hasError = false;
if (gasDate.trim() == '') { //eliminates whitespace.
hasError = true;
$("#gasRecordDate").addClass("is-invalid");
} else {
$("#gasRecordDate").removeClass("is-invalid");
}
if (gasMileage.trim() == '' || parseInt(gasMileage) < 0) {
hasError = true;
$("#gasRecordMileage").addClass("is-invalid");
} else {
$("#gasRecordMileage").removeClass("is-invalid");
}
if (gasGallons.trim() == '' || parseInt(gasGallons) < 0) {
hasError = true;
$("#gasRecordGallons").addClass("is-invalid");
} else {
$("#gasRecordGallons").removeClass("is-invalid");
}
if (gasCost.trim() == '') {
hasError = true;
$("#gasRecordCost").addClass("is-invalid");
} else {
$("#gasRecordCost").removeClass("is-invalid");
}
return {
id: gasRecordId,
hasError: hasError,
vehicleId: vehicleId,
date: gasDate,
mileage: gasMileage,
gallons: gasGallons,
cost: gasCost,
files: uploadedFiles,
isFillToFull: gasIsFillToFull
}
}
function deleteGasRecordFile(fileLocation, event) {
event.parentElement.remove();
uploadedFiles = uploadedFiles.filter(x => x.location != fileLocation);
}