369 lines
14 KiB
JavaScript
369 lines
14 KiB
JavaScript
$(document).ready(function () {
|
||
if (!hasPermission_80212) {
|
||
$('#sendListSection').hide();
|
||
}
|
||
|
||
var $inputDebtAmount = $('#Debt_Amount');
|
||
var formatted = formatNumber($inputDebtAmount.val());
|
||
$inputDebtAmount.val(formatted);
|
||
|
||
$(".date").each(function () {
|
||
const input = this;
|
||
|
||
input.addEventListener("input", function () {
|
||
this.value = convertPersianNumbersToEnglish(this.value);
|
||
});
|
||
|
||
new JalaliDateInput(input);
|
||
});
|
||
|
||
$('#Debt_Amount').on('input', function () {
|
||
const formatted = formatNumber($(this).val());
|
||
$(this).val(formatted);
|
||
});
|
||
|
||
function checkCardCompletion($card) {
|
||
const $status = $card.find('.card-action__status');
|
||
const $icon = $card.find('.card-action__status-icon-svg');
|
||
|
||
const isWrittenVerbal = $card.find('.WrittenVerbal').length > 0;
|
||
|
||
if (isWrittenVerbal) {
|
||
const isWritten = $card.find('#Written').is(':checked');
|
||
const isVerbal = $card.find('#Verbal').is(':checked');
|
||
const description = $card.find('#description').val()?.trim() || '';
|
||
const verbalValid = isVerbal && description.length >= 15;
|
||
const writtenValid = isWritten;
|
||
|
||
if (writtenValid || verbalValid) {
|
||
$status.addClass('card-action__status-success');
|
||
$icon.show();
|
||
} else {
|
||
$status.removeClass('card-action__status-success');
|
||
$icon.hide();
|
||
}
|
||
|
||
return;
|
||
}
|
||
|
||
const isChecked = $card.find('.card-action__inspection-type input[type="checkbox"]:checked').length > 0;
|
||
|
||
const $dateInput = $card.find('.card-action__input.date');
|
||
const dateFilled = $dateInput.length > 0 ? $dateInput.val().trim().length === 10 : true;
|
||
|
||
const $priceInput = $card.find('.card-action__price input');
|
||
const priceFilled = $priceInput.length > 0 ? $priceInput.val().trim() !== '' : true;
|
||
|
||
const $fileInput = $card.find('.card-action__upload input[type="file"]');
|
||
const $mediaId = $card.find('.card-action__upload input.mediaIds');
|
||
|
||
let fileSelected = true;
|
||
if ($fileInput.length > 0 && $mediaId.length > 0) {
|
||
const mediaIdVal = parseInt($mediaId.val() || "0");
|
||
if (mediaIdVal === 0) {
|
||
fileSelected = $fileInput[0].files.length > 0;
|
||
}
|
||
}
|
||
|
||
if (isChecked && dateFilled && priceFilled && fileSelected) {
|
||
$status.addClass('card-action__status-success');
|
||
$icon.show();
|
||
} else {
|
||
$status.removeClass('card-action__status-success');
|
||
$icon.hide();
|
||
}
|
||
}
|
||
|
||
$('.card-action__inspection-type input[type="checkbox"]').on('change', function () {
|
||
const $group = $(this).closest('.card-action__inspection-type');
|
||
const $checkboxes = $group.find('input[type="checkbox"]');
|
||
const $card = $(this).closest('.card-action');
|
||
|
||
if ($(this).is(':checked')) {
|
||
$checkboxes.not(this).prop('checked', false);
|
||
}
|
||
|
||
checkCardCompletion($card);
|
||
});
|
||
|
||
$('.card-action__input').on('input', function () {
|
||
const $card = $(this).closest('.card-action');
|
||
checkCardCompletion($card);
|
||
});
|
||
|
||
$('#description').on('input', function () {
|
||
const $card = $(this).closest('.card-action');
|
||
checkCardCompletion($card);
|
||
});
|
||
|
||
$(".card-action").each(function () {
|
||
const $card = $(this).closest('.card-action');
|
||
checkCardCompletion($card);
|
||
});
|
||
|
||
$(document).on('click', '.remove-file-btn', function () {
|
||
const container = $(this).closest('.card-action__upload');
|
||
const type = $(this).data('type');
|
||
|
||
$(this).hide();
|
||
|
||
const chooseBtn = container.find('.choose-file-btn[data-type="' + type + '"]');
|
||
if (chooseBtn.length === 0) {
|
||
container.append('<button type="button" class="card-action__upload-btn choose-file-btn" data-type="' + type + '">انتخاب فایل</button>');
|
||
} else {
|
||
chooseBtn.show();
|
||
}
|
||
|
||
container.find('input[type="file"][data-type="' + type + '"]').val('');
|
||
|
||
const $card = $(this).closest('.card-action');
|
||
checkCardCompletion($card);
|
||
});
|
||
|
||
$(document).on('click', '.choose-file-btn', function () {
|
||
const type = $(this).data('type');
|
||
$(this).siblings('input[type="file"][data-type="' + type + '"]').click();
|
||
});
|
||
|
||
$(document).on('change', 'input[type="file"]', function () {
|
||
const container = $(this).closest('.card-action__upload');
|
||
const file = this.files[0];
|
||
const type = $(this).data('type');
|
||
|
||
if (file) {
|
||
container.find('.choose-file-btn[data-type="' + type + '"]').hide();
|
||
|
||
const removeBtn = container.find('.remove-file-btn[data-type="' + type + '"]');
|
||
if (removeBtn.length === 0) {
|
||
container.append('<button type="button" class="card-action__upload-btn remove-file-btn" data-type="' + type + '">حذف فایل</button>');
|
||
} else {
|
||
removeBtn.show();
|
||
}
|
||
}
|
||
|
||
const $card = $(this).closest('.card-action');
|
||
checkCardCompletion($card);
|
||
});
|
||
|
||
$('.WrittenVerbal').on('change', function () {
|
||
const $card = $(this).closest('.card-action');
|
||
const $descriptionSection = $card.find('#descriptionSection');
|
||
const $description = $card.find('#description');
|
||
|
||
const isWritten = $card.find('#Written').is(':checked');
|
||
const isVerbal = $card.find('#Verbal').is(':checked');
|
||
|
||
if (isVerbal) {
|
||
$descriptionSection.removeClass('disable');
|
||
} else {
|
||
$descriptionSection.addClass('disable');
|
||
|
||
$description.val('');
|
||
}
|
||
|
||
checkCardCompletion($card);
|
||
});
|
||
$('.WrittenVerbal').each(function () {
|
||
$(this).trigger('change');
|
||
});
|
||
});
|
||
|
||
function formatNumber(input) {
|
||
let raw = convertPersianNumbersToEnglish(input).replace(/[^0-9]/g, '');
|
||
if (raw === '' || parseInt(raw) === 0) return '';
|
||
return raw.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||
}
|
||
|
||
async function SaveData() {
|
||
const form = document.querySelector("#create-form");
|
||
const formData = new FormData(form);
|
||
|
||
//var checkboxesInspection = form.querySelectorAll('input[name="Inspection.Type"]:checked');
|
||
//var lastInspectionDate = $('#last-inspection-date');
|
||
//var inspectionFileMediaId = $('#Inspection_InspectionFileMediaId');
|
||
//var inspectionFile = $('input[name="Inspection.InspectionFile"]');
|
||
|
||
//if (checkboxesInspection.length === 0) {
|
||
// if (lastInspectionDate.val() !== "") {
|
||
// $.Notification.autoHideNotify('error', 'top right', "خطا", "لطفا نوع بازرسی را انتخاب کنید.");
|
||
// return false;
|
||
// }
|
||
|
||
// if (inspectionFile.get(0).files.length !== 0) {
|
||
// $.Notification.autoHideNotify('error', 'top right', "خطا", "لطفا نوع بازرسی را انتخاب کنید.");
|
||
// return false;
|
||
// }
|
||
|
||
// formData.append("Inspection.Type", "0");
|
||
//} else {
|
||
// if (lastInspectionDate.val() === "") {
|
||
// errorInput(lastInspectionDate);
|
||
// $.Notification.autoHideNotify('error', 'top right', "لطفا تاریخ آخرین بازرسی را وارد کنید.");
|
||
// return false;
|
||
// }
|
||
|
||
// if (inspectionFileMediaId.val() === 0) {
|
||
// if (inspectionFile.get(0).files.length === 0) {
|
||
// errorInput($('#card-inspection'));
|
||
// $.Notification.autoHideNotify('error', 'top right', "لطفا فایل بازرسی را انتخاب کنید.");
|
||
// return false;
|
||
// }
|
||
// }
|
||
//}
|
||
|
||
//var checkboxesDebt = form.querySelectorAll('input[name="Debt.Type"]:checked');
|
||
//var debtAmount = $('#Debt_Amount');
|
||
//var debtDate = $('#Debt_DebtDate');
|
||
//var debtFileMediaId = $('#Debt_DebtFileMediaId');
|
||
//var debtFile = $('input[name="Debt.DebtFile"]');
|
||
|
||
//if (checkboxesDebt.length === 0) {
|
||
// if (debtDate.val() !== "") {
|
||
// $.Notification.autoHideNotify('error', 'top right', "خطا", "لطفا نوع بدهی را انتخاب کنید.");
|
||
// return false;
|
||
// }
|
||
|
||
// if (debtAmount.val() !== "") {
|
||
// $.Notification.autoHideNotify('error', 'top right', "خطا", "لطفا نوع بدهی را انتخاب کنید.");
|
||
// return false;
|
||
// }
|
||
|
||
// if (debtFile.get(0).files.length !== 0) {
|
||
// $.Notification.autoHideNotify('error', 'top right', "خطا", "لطفا نوع بدهی را انتخاب کنید.");
|
||
// return false;
|
||
// }
|
||
|
||
// formData.append("Debt.Type", "0");
|
||
//} else {
|
||
// if (debtDate.val() === "") {
|
||
// errorInput(debtDate);
|
||
// $.Notification.autoHideNotify('error', 'top right', "خطا", "لطفا تاریخ بدهی را وارد کنید.");
|
||
// return false;
|
||
// }
|
||
|
||
// if (debtAmount.val() === "") {
|
||
// errorInput(debtAmount);
|
||
// $.Notification.autoHideNotify('error', 'top right', "خطا", "لطفا مبلغ بدهی را وارد کنید.");
|
||
// return false;
|
||
// }
|
||
|
||
// if (debtFileMediaId.val() === 0) {
|
||
// if (debtFile.get(0).files.length === 0) {
|
||
// errorInput($('#card-debt'));
|
||
// $.Notification.autoHideNotify('error', 'top right', "خطا", "لطفا فایل بدهی را انتخاب کنید.");
|
||
// return false;
|
||
// }
|
||
// }
|
||
//}
|
||
|
||
function validateSection({ typeCheckboxes, dateInput, amountInput, fileInput, fileMediaIdInput, sectionName, typeFieldName, defaultTypeValue = "0", sectionCard }) {
|
||
const isTypeChecked = typeCheckboxes.length > 0;
|
||
const hasDate = dateInput.val() !== "";
|
||
const hasAmount = amountInput ? amountInput.val() !== "" : false;
|
||
const hasFile = fileInput.get(0).files.length !== 0;
|
||
const hasMediaId = fileMediaIdInput.val() !== "0";
|
||
|
||
if (!isTypeChecked) {
|
||
if (hasDate || hasAmount || hasFile) {
|
||
$.Notification.autoHideNotify('error', 'top right', "خطا", `لطفا نوع ${sectionName} را انتخاب کنید.`);
|
||
return false;
|
||
}
|
||
formData.append(typeFieldName, defaultTypeValue);
|
||
} else {
|
||
if (!hasDate) {
|
||
errorInput(dateInput);
|
||
$.Notification.autoHideNotify('error', 'top right', `لطفا تاریخ ${sectionName} را وارد کنید.`);
|
||
return false;
|
||
}
|
||
|
||
if (amountInput && !hasAmount) {
|
||
errorInput(amountInput);
|
||
$.Notification.autoHideNotify('error', 'top right', `لطفا مبلغ ${sectionName} را وارد کنید.`);
|
||
return false;
|
||
}
|
||
|
||
if (!hasMediaId && !hasFile) {
|
||
errorInput(sectionCard);
|
||
$.Notification.autoHideNotify('error', 'top right', `لطفا فایل ${sectionName} را انتخاب کنید.`);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
var isInspectionValid = validateSection({
|
||
typeCheckboxes: form.querySelectorAll('input[name="Inspection.Type"]:checked'),
|
||
dateInput: $('#last-inspection-date'),
|
||
amountInput: null,
|
||
fileInput: $('input[name="Inspection.InspectionFile"]'),
|
||
fileMediaIdInput: $('#Inspection_InspectionFileMediaId'),
|
||
sectionName: 'بازرسی',
|
||
typeFieldName: 'Inspection.Type',
|
||
sectionCard: $('#card-inspection')
|
||
});
|
||
|
||
// بدهی
|
||
var isDebtValid = validateSection({
|
||
typeCheckboxes: form.querySelectorAll('input[name="Debt.Type"]:checked'),
|
||
dateInput: $('#Debt_DebtDate'),
|
||
amountInput: $('#Debt_Amount'),
|
||
fileInput: $('input[name="Debt.DebtFile"]'),
|
||
fileMediaIdInput: $('#Debt_DebtFileMediaId'),
|
||
sectionName: 'بدهی',
|
||
typeFieldName: 'Debt.Type',
|
||
sectionCard: $('#card-debt')
|
||
});
|
||
|
||
if (!isInspectionValid || !isDebtValid) return false;
|
||
|
||
var checkboxesApproval = form.querySelectorAll('input[name="Approval.ApprovalStatus"]:checked');
|
||
if (checkboxesApproval.length === 0) {
|
||
formData.append("Approval.ApprovalStatus", "0");
|
||
} else {
|
||
if ($('input[name="Approval.ApprovalStatus"]').val() === "1") {
|
||
const description = $('#description').val()?.trim() || '';
|
||
if (description.length < 15) {
|
||
$.Notification.autoHideNotify('error', 'top right', "خطا", "توضیحات باید حداقل ۱۵ کاراکتر باشد.");
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
|
||
const confirmSentList = form.querySelector('input[name="ConfirmSentList"]');
|
||
formData.append("ConfirmSentList", confirmSentList.checked ? "true" : "false");
|
||
|
||
//const formData = new FormData();
|
||
//formData.append("file", $('#fileInput')[0].files[0]);
|
||
//formData.append("title", "test");
|
||
|
||
try {
|
||
var response = await ajaxService.post(saveOperationsModal, formData, true);
|
||
if (response.success) {
|
||
$.Notification.autoHideNotify('success', 'top right', "موفق", response.message);
|
||
|
||
// Reload Function
|
||
pageIndexJs = 0;
|
||
hasMoreData = true;
|
||
var $activeTab = $('.tab-bar__tab--active');
|
||
var activeValue = $activeTab.val();
|
||
$('#load-data-html').html('');
|
||
loadGetTabCounts();
|
||
loadSearchNew(activeValue);
|
||
$("#MainModal").modal('hide');
|
||
} else {
|
||
$.Notification.autoHideNotify('error', 'top right', "خطا", response.message);
|
||
}
|
||
|
||
} catch (e) {
|
||
console.error(e);
|
||
}
|
||
}
|
||
|
||
function errorInput($el) {
|
||
$el.addClass('error-input');
|
||
|
||
setTimeout(() => {
|
||
$el.removeClass('error-input');
|
||
}, 2000);
|
||
} |