import { dialog } from 'stores/utils'; import { defineStore } from 'pinia'; import { useI18n } from 'vue-i18n'; import { ref } from 'vue'; // NOTE: Import types import { QuotationPayload, EmployeeWorker } from 'src/stores/quotations/types'; // NOTE: Import stores import { useQuotationStore } from 'stores/quotations'; import useEmployeeStore from 'stores/employee'; const DEFAULT_DATA: QuotationPayload = { productServiceList: [], urgent: false, customerBranchId: '', worker: [], payBillDate: new Date(), paySplit: [], paySplitCount: 0, payCondition: 'Full', dueDate: new Date(), documentReceivePoint: '', contactTel: '', contactName: '', workName: '', actorName: '', _count: { worker: 0 }, status: 'CREATED', }; export const useQuotationForm = defineStore('form-quotation', () => { const { t } = useI18n(); const quotationStore = useQuotationStore(); const employeeStore = useEmployeeStore(); const newWorkerList = ref< (EmployeeWorker & { attachment?: { name?: string; group?: string; url?: string; file?: File; _meta?: Record; }[]; })[] >([]); let resetFormData = structuredClone(DEFAULT_DATA); const fileItemNewWorker = ref< { employeeIndex: number; name?: string; group: 'passport' | 'visa' | 'in-country-notice'; url?: string; file?: File; _meta?: Record; }[] >([]); const currentTreeData = ref(); const currentFormData = ref( structuredClone(resetFormData), ); const currentFormState = ref<{ mode: null | 'info' | 'create' | 'edit'; }>({ mode: null, }); function isFormDataDifferent() { const { ...resetData } = resetFormData; const { ...currData } = currentFormData.value; return JSON.stringify(resetData) !== JSON.stringify(currData); } function resetForm(clean = false) { if (clean) { currentFormData.value = structuredClone(DEFAULT_DATA); resetFormData = structuredClone(DEFAULT_DATA); fileItemNewWorker.value = []; newWorkerList.value = []; return; } currentFormData.value = structuredClone(resetFormData); } async function assignFormData(id: string, mode: 'info' | 'edit' = 'info') { const data = await quotationStore.getQuotation(id); if (!data) return; // NOTE: Error should be handled globally by axios instance resetFormData = Object.assign(data, { productServiceList: data.productServiceList.map((v) => ({ ...v, worker: undefined, workerIndex: v.worker.map((a) => data.worker.findIndex((b) => b.employeeId === a.id), ), })), dueDate: new Date(data.dueDate), payBillDate: data.payBillDate ? new Date(data.payBillDate) : undefined, worker: data.worker.map((v) => Object.assign(v.employee, { dateOfBirth: new Date(v.employee.dateOfBirth), }), ), }); currentFormData.value = structuredClone(resetFormData); currentFormState.value.mode = mode; } async function submitQuotation() { if (currentFormState.value.mode === 'create') { const res = await quotationStore.createQuotation(currentFormData.value); if (res !== null) { fileItemNewWorker.value.forEach(async (v) => { if (v.group === undefined) return; await employeeStore.postMeta({ group: v.group, parentId: res.worker[v.employeeIndex].employeeId, meta: v._meta, file: v.file, }); }); } } if (currentFormState.value.mode === 'edit' && currentFormData.value.id) { await quotationStore.editQuotation({ ...currentFormData.value, id: currentFormData.value.id, }); } currentFormState.value.mode = 'info'; } function injectNewEmployee( obj: { data: EmployeeWorker & { attachment?: { name?: string; group?: string; url?: string; file?: File; _meta?: Record; }[]; }; }, callback?: () => void, ) { newWorkerList.value.push({ // passportNo: obj.data.passportNo, wait api add //documentExpireDate: obj.data.documentExpireDate, lastNameEN: obj.data.lastNameEN, lastName: obj.data.lastName, middleNameEN: obj.data.middleNameEN, middleName: obj.data.middleName, firstNameEN: obj.data.firstNameEN, firstName: obj.data.firstName, namePrefix: obj.data.namePrefix, nationality: obj.data.nationality, gender: obj.data.gender, dateOfBirth: obj.data.dateOfBirth, attachment: obj.data.attachment, }); callback?.(); } function dialogDelete(callback: () => void) { dialog({ color: 'negative', icon: 'mdi-alert', title: t('dialog.title.confirmDelete'), actionText: t('general.delete'), persistent: true, message: t('dialog.message.confirmDelete'), action: async () => { callback; }, cancel: () => {}, }); } return { currentFormState, currentFormData, newWorkerList, fileItemNewWorker, isFormDataDifferent, injectNewEmployee, assignFormData, dialogDelete, resetForm, submitQuotation, }; });