diff --git a/src/components/home/PersonCard.vue b/src/components/home/PersonCard.vue index e5d576db..8bb8258e 100644 --- a/src/components/home/PersonCard.vue +++ b/src/components/home/PersonCard.vue @@ -60,7 +60,11 @@ const status = ref(false); - + (structuredClone(defaultFormData)); +const formData = ref({ + provinceId: null, + districtId: null, + subDistrictId: null, + telephoneNo: '', + email: '', + zipCode: '', + gender: '', + addressEN: '', + address: '', + trainingPlace: null, + importNationality: null, + sourceNationality: null, + licenseExpireDate: null, + licenseIssueDate: null, + licenseNo: null, + discountCondition: '', + retireDate: null, + startDate: null, + registrationNo: null, + lastNameEN: '', + lastName: '', + firstNameEN: '', + firstName: '', + userRole: '', + userType: '', + keycloakId: '', + profileImage: null, + birthDate: null, + responsibleArea: '', +}); const userStats = ref(); const typeStats = ref(); const age = ref(); -const confirmData = ref({ - modal: false, - title: '', - message: '', - icon: '', - action: () => {}, -}); - const userTypeOpts = [ { label: 'พนักงาน', value: 'USER' }, { label: 'พนักงานส่งเอกสาร', value: 'MESSENGER' }, @@ -83,10 +108,12 @@ const genderOpts = [ { label: 'ชาย', value: 'male' }, { label: 'หญิง', value: 'female' }, ]; -const hqOpts = ref([ - { label: '0000000001', value: '1' }, - { label: '0000000002', value: '2' }, -]); + +const userOption = ref({ + hqOpts: [], + brOpts: [], +}); + const brOpts = ref([ { label: '0000000001-1', value: '1' }, { label: '0000000001-2', value: '2' }, @@ -109,13 +136,91 @@ async function createKeycloak() { return res.data; } -// function selectFile() { -// const fileInput = this.$refs.file; -// fileInput.pickFiles(); -// } +function selectFile() { + // const fileInput = this.$refs.file; + // fileInput.pickFiles(); +} -function openDialog() { +async function fetchHqOption() { + if (userOption.value.hqOpts.length === 0) { + const res = await branchStore.fetchList({ pageSize: 999, filter: 'head' }); + if (res) { + res.result.map((item) => { + userOption.value.hqOpts.push({ + label: item.code, + value: item.id, + }); + }); + } + } +} + +async function fetchBrOption(id: string) { + const res = await branchStore.fetchById(id, { + includeSubBranch: true, + }); + if (res) { + res.branch.map((item) => { + userOption.value.brOpts.push({ + label: item.code, + value: item.id, + }); + }); + } +} + +async function openDialog(id?: string) { + await fetchHqOption(); modal.value = true; + if (id && userData.value) { + const foundUser = userData.value.result.find((user) => user.id === id); + if (foundUser) { + formData.value.provinceId = foundUser.provinceId; + formData.value.districtId = foundUser.districtId; + formData.value.subDistrictId = foundUser.subDistrictId; + formData.value.telephoneNo = foundUser.telephoneNo; + formData.value.email = foundUser.email; + formData.value.zipCode = foundUser.zipCode; + formData.value.gender = foundUser.gender; + formData.value.addressEN = foundUser.addressEN; + formData.value.address = foundUser.address; + formData.value.trainingPlace = foundUser.trainingPlace; + formData.value.importNationality = foundUser.importNationality; + formData.value.sourceNationality = foundUser.sourceNationality; + formData.value.licenseExpireDate = foundUser.licenseExpireDate + ? new Date(foundUser.licenseExpireDate) + : null; + formData.value.licenseIssueDate = foundUser.licenseIssueDate + ? new Date(foundUser.licenseIssueDate) + : null; + formData.value.licenseNo = foundUser.licenseNo; + formData.value.discountCondition = foundUser.discountCondition; + formData.value.retireDate = foundUser.retireDate + ? new Date(foundUser.retireDate) + : null; + formData.value.startDate = foundUser.startDate + ? new Date(foundUser.startDate) + : null; + formData.value.registrationNo = foundUser.registrationNo; + formData.value.lastNameEN = foundUser.lastNameEN; + formData.value.lastName = foundUser.lastName; + formData.value.firstNameEN = foundUser.firstNameEN; + formData.value.firstName = foundUser.firstName; + formData.value.userRole = foundUser.userRole; + formData.value.userType = foundUser.userType; + formData.value.keycloakId = foundUser.keycloakId; + formData.value.profileImage = new File([], foundUser.profileImageUrl); + formData.value.birthDate = foundUser.birthDate + ? new Date(foundUser.birthDate) + : null; + formData.value.responsibleArea = foundUser.responsibleArea; + userId.value = foundUser.id; + hqId.value = foundUser.branch[0].id; + brId.value = foundUser.branch; + code.value = foundUser.code; + isEdit.value = true; + } + } } function onClose() { @@ -125,17 +230,14 @@ function onClose() { brId.value = ''; username.value = ''; userId.value = ''; + code.value = ''; mapUserType(selectorLabel.value); } -function submitConfirm() { - confirmData.value.modal = true; -} - -async function onSubmit(id?: string) { +async function onSubmit() { try { - if (id) { - await userStore.editById(id, formData.value); + if (isEdit.value === true && userId.value) { + await userStore.editById(userId.value, formData.value); } else { formData.value.keycloakId = await createKeycloak(); await userStore.create(formData.value); @@ -164,7 +266,7 @@ function mapUserType(label: string) { } onMounted(async () => { - await userStore.fetchList(); + await userStore.fetchList({ includeBranch: true }); typeStats.value = await userStore.typeStats(); if ((await branchStore.userStats(formData.value.userType)) !== false) { userStats.value = await branchStore.userStats(formData.value.userType); @@ -180,6 +282,15 @@ watch( } }, ); + +watch( + () => hqId.value, + async (id) => { + fetchBrOption(id); + brId.value = ''; + userOption.value.brOpts = []; + }, +);