refactor: add from Function Employee

This commit is contained in:
Net 2024-08-05 16:53:19 +07:00
parent 2e1bbabac3
commit 555e06672a

View file

@ -1,8 +1,12 @@
import { ref, watch } from 'vue';
import { defineStore } from 'pinia';
import { CustomerCreate } from 'src/stores/customer/types';
import { Employee, EmployeeCreate } from 'src/stores/employee/types';
import useMyBranch from 'src/stores/my-branch';
import useCustomerStore from 'src/stores/customer';
import useEmployeeStore from 'src/stores/employee';
import useFlowStore from 'src/stores/flow';
export const useCustomerForm = defineStore('form-customer', () => {
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL;
@ -209,5 +213,224 @@ export const useCustomerForm = defineStore('form-customer', () => {
});
export const useEmployeeForm = defineStore('form-employee', () => {
return {};
const customerStore = useCustomerStore();
const employeeStore = useEmployeeStore();
const flowStore = useFlowStore();
const branchStore = useMyBranch();
const state = ref<{
dialogType: 'info' | 'create' | 'edit';
drawerModal: boolean;
currentEmployeeCode: string;
currentEmployee: Employee | null;
profileUrl: string;
isEmployeeEdit: boolean;
profileSubmit: boolean;
formDataEmployeeSameAddr: boolean;
infoEmployeePersonCard: {
id: string;
img: string;
name: string;
male: boolean;
female: boolean;
badge: string;
disabled: boolean;
}[];
formDataEmployeeOwner:
| {
id: string;
address: string;
addressEN: string;
provinceId: string;
districtId: string;
subDistrictId: string;
zipCode: string;
}
| undefined;
}>({
drawerModal: false,
dialogType: 'info',
currentEmployeeCode: '',
currentEmployee: null,
profileUrl: '',
isEmployeeEdit: false,
profileSubmit: false,
formDataEmployeeSameAddr: false,
infoEmployeePersonCard: [],
formDataEmployeeOwner: undefined,
});
let defaultFormData: EmployeeCreate = {
code: '',
image: null,
customerBranchId: '',
nrcNo: '',
dateOfBirth: null,
gender: '',
nationality: '',
firstName: '',
firstNameEN: '',
lastName: '',
lastNameEN: '',
addressEN: '',
address: '',
zipCode: '',
passportType: '',
passportNumber: '',
passportIssueDate: null,
passportExpiryDate: null,
passportIssuingCountry: '',
passportIssuingPlace: '',
previousPassportReference: '',
visaType: '',
visaNumber: '',
visaIssueDate: null,
visaExpiryDate: null,
visaIssuingPlace: '',
visaStayUntilDate: null,
tm6Number: '',
entryDate: null,
workerStatus: '',
subDistrictId: '',
districtId: '',
provinceId: '',
employeeWork: [
{
workEndDate: null,
workPermitExpireDate: null,
workPermitIssuDate: null,
workPermitNo: '',
workplace: '',
jobType: '',
positionName: '',
ownerName: '',
remark: '',
},
],
employeeCheckup: [
{
coverageExpireDate: null,
coverageStartDate: null,
insuranceCompany: '',
medicalBenefitScheme: '',
remark: '',
hospitalName: '',
provinceId: '',
checkupResult: '',
checkupType: '',
},
],
employeeOtherInfo: {
citizenId: '',
fatherFirstName: '',
fatherLastName: '',
fatherFirstNameEN: '',
fatherLastNameEN: '',
fatherBirthPlace: '',
motherFirstName: '',
motherLastName: '',
motherFirstNameEN: '',
motherLastNameEN: '',
motherBirthPlace: '',
},
};
let resetEmployeeData = structuredClone(defaultFormData);
const currentFromDataEmployee = ref<EmployeeCreate>(
structuredClone(defaultFormData),
);
function resetFormDataEmployee(cb?: (...args: any[]) => unknown) {
currentFromDataEmployee.value = structuredClone(resetEmployeeData);
cb?.();
}
async function assignFormDataEmployee(id: string) {
const res = await employeeStore.fetchById(id);
if (res) {
state.value.currentEmployee = res;
const {
province,
district,
subDistrict,
createdBy,
updatedBy,
profileImageUrl,
...playlond
} = res;
currentFromDataEmployee.value = {
...playlond,
provinceId: province?.id,
districtId: district?.id,
subDistrictId: subDistrict?.id,
employeeCheckup: structuredClone(playlond.employeeCheckup),
employeeOtherInfo: structuredClone(playlond.employeeOtherInfo),
employeeWork: structuredClone(playlond.employeeWork),
image: null,
};
const foundBranch = await customerStore.fetchListCustomeBranchById(
playlond.customerBranchId,
);
state.value.currentEmployeeCode = playlond.code;
state.value.profileUrl = profileImageUrl || '';
profileImageUrl
? (state.value.profileSubmit = true)
: (state.value.profileSubmit = false);
state.value.isEmployeeEdit = true;
state.value.formDataEmployeeOwner = { ...foundBranch };
if (foundBranch.address === playlond.address) {
state.value.formDataEmployeeSameAddr = true;
} else {
state.value.formDataEmployeeSameAddr = false;
}
if (state.value.infoEmployeePersonCard) {
state.value.infoEmployeePersonCard[0].img = profileImageUrl || '';
}
flowStore.rotate();
}
}
async function employeeFilterOwnerBranch(
val: string,
update: (...args: unknown[]) => void,
) {
update(async () => {
const result = await customerStore.fetchListCustomeBranch({
includeCustomer: true,
query: val,
pageSize: 30,
});
if (result) employeeStore.ownerOption = result.result;
});
}
return {
state,
currentFromDataEmployee,
resetEmployeeData,
resetFormDataEmployee,
assignFormDataEmployee,
employeeFilterOwnerBranch,
};
});