Files
Backend-Api/ServiceHost/wwwroot/AssetsClient/pages/Employees/js/CreateEmployeeModal.js
2025-05-25 19:24:29 +03:30

579 lines
18 KiB
JavaScript

var currentUrl = window.location.href;
$(document).ready(function () {
if (currentUrl.includes("/Client#showmodal=/Client/Company/Employees/EmployeeList?handler=CreateEmployee")) {
$(".modal-xxl").css('max-width', '550px');
}
$(".SaveFullData").click(function () {
saveFullData();
});
//checkAndDisableButtons();
//$(document).on('click', '.stepBtn', function () {
// checkAndDisableButtons();
//});
if (currentUrl.includes("/Client/Company/RollCall/EmployeeUploadPicture")) {
$(".employeePart").remove();
$("#nextStep4").addClass("disable");
$("#ModalContent").css("height", "730px");
$(".stepStatus").css("width", "195px");
}
$(".select2Option").select2({
language: "fa",
dir: "rtl",
dropdownParent: $('#MainModal'),
templateResult: function (data, container) {
if (data.element) {
$(container).addClass($(data.element).attr("class"));
}
return data.text;
}
});
if ($(window).width() < 992) {
$('.desktopNotif').hide();
$('.mobileNotif').show();
} else {
$('.desktopNotif').show();
$('.mobileNotif').hide();
}
$(".cancelButton").click(function () {
$("#MainModal").modal("hide");
});
});
function checkPicture() {
let currentUrl = window.location.href; // Get the full URL
let hasImgInDemoResult1 = $("#demoResult1 img").length > 0;
let hasImgInDemoResult2 = $("#demoResult2 img").length > 0;
//var hasImgInDemoResult1Empty = hasImgInDemoResult1.attr("src") === "";
//var hasImgInDemoResult2Empty = hasImgInDemoResult2.attr("src") === "";
//const img1 = document.getElementById('pic1');
//const img2 = document.getElementById('pic2');
//const detection1 = await faceapi.detectSingleFace(img1).withFaceLandmarks().withFaceDescriptor();
//const detection2 = await faceapi.detectSingleFace(img2).withFaceLandmarks().withFaceDescriptor();
if ((hasImgInDemoResult2 && !hasImgInDemoResult1) || (!hasImgInDemoResult2 && hasImgInDemoResult1)) {
$("#prevStep4").addClass("disable");
$("#nextStep4").addClass("disable");
} else {
$("#prevStep4").removeClass("disable");
$("#nextStep4").removeClass("disable");
}
//if (detection1 && detection2) {
// const distance = faceapi.euclideanDistance(detection1.descriptor, detection2.descriptor);
// if (distance > 0.45) {
// $('.alert-msg').show();
// $('.alert-msg p').text('چهره اول با چهره دوم مطابقت ندارد');
// setTimeout(function () {
// $('.alert-msg').hide();
// $('.alert-msg p').text('');
// }, 4000);
// $("#prevStep4").addClass("disable");
// $("#nextStep4").addClass("disable");
// $("#save4").addClass("disable").prop("disabled", true);
// return false;
// }
//}
if (hasImgInDemoResult2 && hasImgInDemoResult1) {
if (currentUrl.includes("/Client/Company/Employees/EmployeeList")) {
$("#demoResult1 , #demoResult2").removeClass("cantPass");
$("#demoResult1 , #demoResult2").addClass("canPass");
//$("#prevStep4").removeClass("disable");
//$("#nextStep4").removeClass("disable");
$("#save4").removeClass("disable");
} else {
$("#demoResult1 , #demoResult2").removeClass("cantPass");
$("#demoResult1 , #demoResult2").addClass("canPass");
$("#save4").removeClass("disable");
$("#save4").prop("disabled",false);
}
return true;
} else if (!hasImgInDemoResult2 && !hasImgInDemoResult1) {
if (currentUrl.includes("/Client/Company/Employees/EmployeeList")) {
$("#demoResult1 , #demoResult2").removeClass("cantPass");
$("#demoResult1 , #demoResult2").addClass("canPass");
//$("#prevStep4").removeClass("disable");
//$("#nextStep4").removeClass("disable");
$("#save4").removeClass("disable");
} else {
$("#demoResult1 , #demoResult2").removeClass("cantPass");
$("#demoResult1 , #demoResult2").addClass("canPass");
$("#save4").removeClass("disable");
}
return true;
}
else {
$("#demoResult1 , #demoResult2").addClass("cantPass");
$("#demoResult1 , #demoResult2").removeClass("canPass");
//$("#prevStep4").addClass("disable");
//$("#nextStep4").addClass("disable");
$("#save4").addClass("disable");
return false;
}
}
$(document).ready(function () {
class ModalState {
constructor(modal) {
this.modal = modal;
}
next() {
throw new Error("next() باید در هر مرحله پیاده‌سازی شود");
}
prev() {
throw new Error("prev() باید در هر مرحله پیاده‌سازی شود");
}
save() {
console.log(this.modal.formData);
saveFullData(this.modal.formData);
}
render() {
throw new Error("render() باید در هر مرحله پیاده‌سازی شود");
}
}
class Step1 extends ModalState {
render() {
$(".step").hide();
$("#step1").show();
$(".stepStatus").removeClass("active");
$("#step-1").addClass("active");
checkStepStatus();
}
next() {
if (goToStep2) {
this.modal.setState(this.modal.step2);
}
}
}
class Step2 extends ModalState {
render() {
$(".step").hide();
$("#step2").show();
$(".stepStatus").removeClass("active");
$("#step-2").addClass("active");
checkStepStatus();
}
next() {
if (checkInputs()) {
this.modal.setState(this.modal.step3);
}
}
}
class Step3 extends ModalState {
render() {
$(".step").hide();
$("#step3").show();
$(".stepStatus").removeClass("active");
$("#step-3").addClass("active");
checkStepStatus();
}
next() {
var inputsDateTime = $('.dateTime');
var inputsDateTimeRs = $('.dateTimeRS');
var inputsDateTimeIrregular = $('.dateTimeIrregular');
var allowToNextStep3 = true;
var selectedOption = $("input[name='Command.CreateCustomizeEmployeeSettings.WorkshopShiftStatus']:checked").val();
if (selectedOption === "Regular") {
inputsDateTime.each(function () {
if ($(this).val() === '') {
allowToNextStep3 = false;
showAlert('ابتدا ساعت شروع و پایان را وارد نمائید.', $(this));
}
});
} else if (selectedOption === "Rotating") {
inputsDateTimeRs.each(function () {
if ($(this).val() === '') {
allowToNextStep3 = false;
showAlert('ابتدا ساعت شروع و پایان را وارد نمائید.', $(this));
}
});
} else if (selectedOption === "Irregular") {
var selectedOptionWorkshopIrregularShifts = $("input[name='Command.CreateCustomizeEmployeeSettings.IrregularShift.WorkshopIrregularShifts']:checked").val();
if (!selectedOptionWorkshopIrregularShifts) {
showAlert('لطفاً یکی از گزینه‌های شیفت کاری را انتخاب کنید.', $('.outlineDate'));
allowToNextStep3 = false;
} else {
inputsDateTimeIrregular.each(function () {
if ($(this).val() === '') {
allowToNextStep3 = false;
showAlert('ابتدا ساعت شروع و پایان را وارد نمائید.', $(this));
}
});
}
}
// This should be checked the hasCustomizeCheckoutService for step4 / TempCheckout
if (hasCustomizeCheckoutService==='True') {
console.log(hasCustomizeCheckoutService);
if (allowToNextStep3) {
this.modal.setState(this.modal.step4);
}
} else {
if (allowToNextStep3) {
this.modal.setState(this.modal.step5);
}
}
}
prev() {
var inputsDateTime = $('.dateTime');
var inputsDateTimeRs = $('.dateTimeRS');
var inputsDateTimeIrregular = $('.dateTimeIrregular');
var allowToPrevStep3 = true;
var selectedOption = $("input[name='Command.CreateCustomizeEmployeeSettings.WorkshopShiftStatus']:checked").val();
if (selectedOption === "Regular") {
inputsDateTime.each(function () {
if ($(this).val() === '') {
allowToPrevStep3 = false;
showAlert('ابتدا ساعت شروع و پایان را وارد نمائید.', $(this));
}
});
} else if (selectedOption === "Rotating") {
inputsDateTimeRs.each(function () {
if ($(this).val() === '') {
allowToPrevStep3 = false;
showAlert('ابتدا ساعت شروع و پایان را وارد نمائید.', $(this));
}
});
} else if (selectedOption === "Irregular") {
var selectedOptionWorkshopIrregularShifts = $("input[name='Command.CreateCustomizeEmployeeSettings.IrregularShift.WorkshopIrregularShifts']:checked").val();
if (!selectedOptionWorkshopIrregularShifts) {
showAlert('لطفاً یکی از گزینه‌های شیفت کاری را انتخاب کنید.', $('.outlineDate'));
allowToPrevStep3 = false;
} else {
inputsDateTimeIrregular.each(function () {
if ($(this).val() === '') {
allowToPrevStep3 = false;
showAlert('ابتدا ساعت شروع و پایان را وارد نمائید.', $(this));
}
});
}
}
if (allowToPrevStep3) {
this.modal.setState(this.modal.step2);
}
}
}
class Step4 extends ModalState {
render() {
$(".step").hide();
$("#step4").show();
$(".stepStatus").removeClass("active");
$("#step-4").addClass("active");
checkStepStatus();
}
next() {
if (checkStepTempCheckout()) {
this.modal.setState(this.modal.step5);
}
}
prev() {
//if (checkStepTempCheckout()) {
this.modal.setState(this.modal.step3);
//}
}
}
class Step5 extends ModalState {
render() {
$(".step").hide();
$("#step5").show();
$(".stepStatus").removeClass("active");
$("#step-5").addClass("active");
checkStepStatus();
checkPicture();
}
next() {
if (checkPicture()) {
this.modal.setState(this.modal.step6);
}
}
prev() {
// This should be checked the hasCustomizeCheckoutService for step4 / TempCheckout
if (hasCustomizeCheckoutService === 'True') {
if (checkPicture()) {
this.modal.setState(this.modal.step4);
}
} else {
if (checkPicture()) {
this.modal.setState(this.modal.step3);
}
}
}
}
class Step6 extends ModalState {
render() {
$(".step").hide();
$("#step6").show();
$(".stepStatus").removeClass("active");
$("#step-6").addClass("active");
checkStepStatus();
}
next() {
this.modal.setState(this.modal.step7);
}
prev() {
this.modal.setState(this.modal.step5);
}
}
class Step7 extends ModalState {
render() {
$(".step").hide();
$("#step7").show();
$(".stepStatus").removeClass("active");
$("#step-7").addClass("active");
checkStepStatus();
checkStep5();
}
prev() {
this.modal.setState(this.modal.step6);
}
}
class Modal {
constructor() {
this.step1 = new Step1(this);
this.step2 = new Step2(this);
this.step3 = new Step3(this);
this.step4 = new Step4(this);
this.step5 = new Step5(this);
this.step6 = new Step6(this);
this.step7 = new Step7(this);
this.currentState = this.step1;
this.formData = {};
this.initEventListeners();
this.currentState.render();
}
setState(state) {
this.currentState = state;
this.currentState.render();
}
initEventListeners() {
$("#nextStep1").on("click", () => this.currentState.next());
$("#nextStep2").on("click", () => this.currentState.next());
$("#nextStep3").on("click", () => this.currentState.next());
$("#nextStep4").on("click", () => this.currentState.next());
$("#nextStep5").on("click", () => this.currentState.next());
$("#prevStep2").on("click", () => this.currentState.prev());
$("#prevStep3").on("click", () => this.currentState.prev());
$("#prevStep4").on("click", () => this.currentState.prev());
$("#prevStep5").on("click", () => this.currentState.prev());
$("#prevStep6").on("click", () => this.currentState.prev());
}
}
const modal = new Modal();
});
function saveFullData() {
//var hasbreaktime = "false";
//if ($('#BreakTime').prop('checked')) {
// hasbreaktime = $("#BreakTime").prop("checked") ? "true" : "false";
//} else if ($('#BreakTimeFirst').prop('checked')) {
// hasbreaktime = $("#BreakTimeFirst").prop("checked") ? "true" : "false";
//}
//var breaktimevalue = $("#TimeOnly").val();
//var FridayWork = "Default";
//if ($('#Friday1').prop('checked')) {
// FridayWork = "Default";
//} else if ($('#Friday2').prop('checked')) {
// FridayWork = "WorkInFriday";
//}
//var HolidayWork = "Default";
//if ($('#HolidayWork1').prop('checked')) {
// HolidayWork = "Default";
//} else if ($('#HolidayWork2').prop('checked')) {
// HolidayWork = "WorkInHolidays";
//}
//command.push({ name: 'Command.CreateCustomizeEmployeeSettings.BreakTime.HasBreakTimeValue', value: hasbreaktime });
//command.push({ name: 'Command.CreateCustomizeEmployeeSettings.BreakTime.BreakTimeValue', value: breaktimevalue });
//command.push({ name: 'Command.CreateCustomizeEmployeeSettings.FridayWork', value: FridayWork });
//command.push({ name: 'Command.CreateCustomizeEmployeeSettings.HolidayWork', value: HolidayWork });
var formData = new FormData();
var command = $('#create-form').serializeArray();
command.forEach(function (item) {
formData.append(item.name, item.value);
});
formData.append("Command.CreateCustomizeEmployeeSettings.BreakTime.HasBreakTimeValue", $('#BreakTime').prop('checked') || $('#BreakTimeFirst').prop('checked') ? "true" : "false");
formData.append("Command.CreateCustomizeEmployeeSettings.BreakTime.BreakTimeValue", $("#TimeOnly").val());
formData.append("Command.CreateCustomizeEmployeeSettings.FridayWork", $('#Friday1').prop('checked') ? "Default" : "WorkInFriday");
formData.append("Command.CreateCustomizeEmployeeSettings.HolidayWork", $('#HolidayWork1').prop('checked') ? "Default" : "WorkInHolidays");
formData.append("Command.CreateCustomizeEmployeeSettings.Salary", $('#personnelSalary').val());
formData.append("Command.CreateCustomizeEmployeeSettings.LeavePermittedDays", $('#leavePermittedDays').val());
let pic1 = $("#pic1").attr('src');
let pic2 = $("#pic2").attr('src');
if (pic1) formData.append("Command.RollCallUploadEmployeePicture.Picture1", pic1);
if (pic2) formData.append("Command.RollCallUploadEmployeePicture.Picture2", pic2);
if (currentUrl.includes("/Client/Company/Employees/EmployeeList") || currentUrl.includes("/Client#showmodal=/Client?handler=CreateEmployee")) {
employeeBankInfos.forEach((item, index) => {
formData.append(`Command.EmployeeBankInfos[${index}].BankId`, item.BankId);
formData.append(`Command.EmployeeBankInfos[${index}].BankAccountNumber`, item.BankAccountNumber);
formData.append(`Command.EmployeeBankInfos[${index}].CardNumber`, item.CardNumber);
formData.append(`Command.EmployeeBankInfos[${index}].ShebaNumber`, item.ShebaNumber);
});
pictures.forEach((item, index) => {
formData.append(`Command.EmployeeDocumentItems[${index}].Label`, item.Label);
formData.append(`Command.EmployeeDocumentItems[${index}].PictureFile`, item.PictureFile);
});
}
let saveURL = '';
if (currentUrl.includes("/Client#showmodal=/Client?handler=CreateEmployee")) {
saveURL = saveHomeUrl;
} else if (currentUrl.includes("/Client/Company/Employees/EmployeeList")) {
saveURL = saveEmployeeURL;
} else {
saveURL = saveUrlRollCall;
}
$.ajax({
url: saveURL,
method: "POST",
data: formData,
processData: false,
contentType: false,
headers: { 'RequestVerificationToken': antiForgeryToken },
success: (response) => {
if (response.success) {
if (currentUrl.includes("/Client#showmodal=/Client?handler=CreateEmployee")) {
// do it something
// it's for dashboard home
} else if (currentUrl.includes("/Client/Company/Employees/EmployeeList")) {
$('#loadEmployeesAjax').html('');
loadEmployee('', '');
} else {
pageIndexMain = 0;
searchName = $('#personnelSearch').val();
$('#filterRemove').removeClass('disable');
$('#ajaxDataLoad').html('');
$('#loadingSkeleton').css('display', 'contents');
loadDataAjax();
}
$("#MainModal").modal("hide");
$('.alert-success-msg').show();
$('.alert-success-msg p').text(response.message);
setTimeout(function () {
$('.alert-success-msg').hide();
$('.alert-success-msg p').text('');
}, 1000);
} else {
$('.alert-msg').show();
$('.alert-msg p').text(response.message);
setTimeout(function () {
$('.alert-msg').hide();
$('.alert-msg p').text('');
}, 1000);
}
},
error: (err) => {
$('.alert-msg').show();
$('.alert-msg p').text(err.message);
setTimeout(function () {
$('.alert-msg').hide();
$('.alert-msg p').text(err.message);
}, 4000);
}
});
}
function checkStepStatus() {
$('.stepStatus').each(function () {
if ($(this).hasClass("active")) {
$('.navbarBtn').animate({
scrollLeft: $(this).offset().left - $('.navbarBtn').offset().left + $('.navbarBtn').scrollLeft()
}, 500);
}
});
}
//function checkAndDisableButtons() {
// if ($('.SaveFullData.disable').length > 0) {
// $('.SaveFullData').addClass('disable');
// } else {
// $('.SaveFullData').removeClass('disable');
// }
//}
function validateField(selector, message) {
$(selector).addClass("errored");
$('.alert-msg').show();
$('.alert-msg p').text(message);
setTimeout(function () {
$('.alert-msg').hide();
$('.alert-msg p').text("");
$(selector).removeClass("errored");
}, 3500);
}