import { ref, watch } from 'vue'; import { defineStore } from 'pinia'; import { CustomerCreate } from 'src/stores/customer/types'; import useMyBranch from 'src/stores/my-branch'; import useCustomerStore from 'src/stores/customer'; export const useCustomerForm = defineStore('form-customer', () => { const customerStore = useCustomerStore(); const branchStore = useMyBranch(); let defaultFormData: CustomerCreate = { status: 'CREATED', personName: '', customerType: 'CORP', customerName: '', customerNameEN: '', taxNo: '', registeredBranchId: branchStore.currentMyBranch?.id || '', customerBranch: [], image: null, }; let resetFormData = structuredClone(defaultFormData); const currentFormData = ref(structuredClone(defaultFormData)); const state = ref<{ dialogType: 'info' | 'create' | 'edit'; dialogOpen: boolean; dialogModal: boolean; branchIndex: number; saveMode: 'customer' | 'branch'; customerImageUrl: string; imageDialog: boolean; imageEdit: boolean; editCustomerId?: string; editCustomerBranchId?: string; }>({ dialogType: 'info', dialogOpen: false, dialogModal: false, imageDialog: false, branchIndex: 0, imageEdit: false, saveMode: 'customer', customerImageUrl: '', editCustomerId: '', editCustomerBranchId: '', }); watch( currentFormData, (v) => (defaultFormData.customerType = v.customerType), ); function isFormDataDifferent() { return ( JSON.stringify(resetFormData) !== JSON.stringify(currentFormData.value) ); } function resetForm(clean = false) { if (clean) { defaultFormData.customerType = currentFormData.value.customerType; currentFormData.value = structuredClone(defaultFormData); currentFormData.value.registeredBranchId = branchStore.currentMyBranch?.id || ''; return; } if (!resetFormData.registeredBranchId) { resetFormData.registeredBranchId = branchStore.currentMyBranch?.id || ''; } currentFormData.value = structuredClone(resetFormData); } async function assignFormData(id?: string) { if (!id) { resetFormData = structuredClone(defaultFormData); return; } const data = await customerStore.fetchById(id); if (!data) return; resetFormData.registeredBranchId = data.registeredBranchId; resetFormData.status = data.status; resetFormData.customerType = data.customerType; resetFormData.customerName = data.customerName; resetFormData.customerNameEN = data.customerNameEN; resetFormData.personName = data.personName; resetFormData.taxNo = data.taxNo; resetFormData.image = null; resetFormData.customerBranch = data.branch.map((v) => ({ id: v.id, code: v.code, branchNo: v.branchNo, address: v.address, addressEN: v.addressEN, provinceId: v.province.id, districtId: v.district.id, subDistrictId: v.subDistrict.id, zipCode: v.zipCode, email: v.email, telephoneNo: v.telephoneNo, name: v.name, status: undefined, taxNo: v.taxNo, nameEN: v.nameEN, legalPersonNo: v.legalPersonNo, registerName: v.registerName, registerDate: new Date(v.registerDate), authorizedCapital: v.authorizedCapital, employmentOffice: v.employmentOffice, bussinessType: v.bussinessType, bussinessTypeEN: v.bussinessTypeEN, jobPosition: v.jobPosition, jobPositionEN: v.jobPositionEN, jobDescription: v.jobDescription, saleEmployee: v.saleEmployee, payDate: new Date(v.payDate), wageRate: v.wageRate, })); currentFormData.value = structuredClone(resetFormData); } function addCurrentCustomerBranch() { currentFormData.value.customerBranch?.push({ id: '', code: '', branchNo: 1, address: '', addressEN: '', provinceId: '', districtId: '', subDistrictId: '', zipCode: '', email: '', telephoneNo: '', name: '', status: 'CREATED', taxNo: '', nameEN: '', legalPersonNo: '', registerName: '', registerDate: new Date(), authorizedCapital: '', employmentOffice: '', bussinessType: '', bussinessTypeEN: '', jobPosition: '', jobPositionEN: '', jobDescription: '', saleEmployee: '', payDate: new Date(), wageRate: 0, }); } async function submitForm() { if (state.value.saveMode === 'customer') await submitFormCustomer(); } async function submitFormCustomer() { if (state.value.dialogType === 'info') return; if (state.value.dialogType === 'create') { return await customerStore.create(currentFormData.value); } if (!state.value.editCustomerId) { throw new Error( 'Form mode is set to edit but no ID is provided. Make sure to set customer ID.', ); } await customerStore.editById(state.value.editCustomerId, { ...currentFormData.value, image: currentFormData.value.image || undefined, customerBranch: undefined, }); } return { state, resetFormData, currentFormData, isFormDataDifferent, resetForm, assignFormData, submitForm, addCurrentCustomerBranch, }; }); export const useEmployeeForm = defineStore('form-employee', () => { return {}; });