diff --git a/src/pages/01_branch-management/MainPage.vue b/src/pages/01_branch-management/MainPage.vue index e2f91bae..3a924501 100644 --- a/src/pages/01_branch-management/MainPage.vue +++ b/src/pages/01_branch-management/MainPage.vue @@ -4,6 +4,7 @@ import { ref, onMounted } from 'vue'; import { Icon } from '@iconify/vue'; import useBranchStore from 'stores/branch'; +import useBranchContactStore from 'stores/branch-contact'; import AppBox from 'components/app/AppBox.vue'; import BtnAddComponent from 'components/01_branch-management/BtnAddComponent.vue'; import TooltipComponent from 'components/TooltipComponent.vue'; @@ -13,13 +14,23 @@ import DeatailsBranchDrawerComponent from 'src/components/01_branch-management/D import FormDialog from 'src/components/FormDialog.vue'; import TableCardComponent from 'src/components/01_branch-management/TableCardComponent.vue'; -import { BranchWithChildren, Branch } from 'src/stores/branch/types'; +import { BranchContactCreate } from 'src/stores/branch-contact/types'; + +import { + BranchWithChildren, + Branch, + BranchCreate, +} from 'src/stores/branch/types'; const branchStore = useBranchStore(); +const branchContactStore = useBranchContactStore(); + const { data: branchData } = storeToRefs(branchStore); const modal = ref(false); +const currentBranchIdEdit = ref(''); +const currentBranchContactIdEdit = ref(''); const showCurrentBranch = ref(); const shape = ref(false); @@ -40,39 +51,35 @@ const optionsFilter: string[] = [ 'สถานะ', ]; -const formData = ref({ - hqId: '', - branchId: '', - tel: '', - gender: '', +const formDataContact = ref({ + lineId: '', + telephoneNo: '', + qrCodeImage: new File([], 'ชื่อไฟล์.png', { type: 'image/png' }), +}); + +const formType = ref<'edit' | 'create' | 'delete'>('create'); +const typeBranch = ref<'headOffice' | 'subBranch'>('headOffice'); +const branchContact = ref(); +const inputCode = ref(''); +const formData = ref({ + taxNo: '', + nameEN: '', + name: '', + addressEN: '', + address: '', + zipCode: '', email: '', - addressTitle: { - address: '', - province: '', - district: '', - subDistrict: '', - zip: '', - }, - addressENTitle: { - address: '', - province: '', - district: '', - subDistrict: '', - zip: '', - }, + telephoneNo: '', + longitude: '', + latitude: '', + subDistrictId: '', + districtId: '', + provinceId: '', + headOfficeId: null, }); const selected = ref(''); -const headOfficeCode = ref(''); -const headOfficeName = ref(''); -const taxIDNumber = ref(''); -const nameHeadOfficeEN = ref(''); -const inputPhone = ref(''); -const inputIdLine = ref(''); -const inputPhoneHeadOffice = ref(''); -const inputEmailHeadOffice = ref(''); - const expanded = ref([]); const branchStat = ref< @@ -119,6 +126,30 @@ const rowsBranch = ref< }[] >([]); +function clearData() { + formData.value = { + taxNo: '', + nameEN: '', + name: '', + addressEN: '', + address: '', + zipCode: '', + email: '', + telephoneNo: '', + longitude: '', + latitude: '', + subDistrictId: '', + districtId: '', + provinceId: '', + headOfficeId: null, + }; + + formDataContact.value = { + lineId: '', + telephoneNo: '', + qrCodeImage: new File([], 'ชื่อไฟล์.png', { type: 'image/png' }), + }; +} function openDialog() { modal.value = true; } @@ -127,6 +158,7 @@ async function getTree() { const result = await branchStore.fetchList({ tree: true, }); + if (result) { treeData.value = result.result; rowsBranch.value = treeData.value @@ -145,6 +177,175 @@ async function getTree() { } } +function changeSubject( + formType: 'edit' | 'create' | 'delete', + typeBranch: 'headOffice' | 'subBranch', +) { + if (typeBranch === 'headOffice') { + if (formType === 'create') { + return 'เพิ่มสำนักงานใหญ่'; + } + return 'แก้ไขสำนักงานใหญ่'; + } + if (typeBranch === 'subBranch') { + if (formType === 'create') { + return 'เพิ่มสาขา'; + } + return 'แก้ไขสาขา'; + } +} + +function triggerCreateSubBranch(code: string, id: string) { + clearData(); + formType.value = 'create'; + typeBranch.value = 'subBranch'; + inputCode.value = code; + formData.value.headOfficeId = id; + openDialog(); +} + +function triggerEditSubBranch(code: string, id: string) { + clearData(); + formType.value = 'edit'; + typeBranch.value = 'subBranch'; + inputCode.value = code; + formData.value.headOfficeId = id; + currentBranchIdEdit.value = id; + fetchFormEditBranch(id); + fetchFormEditBranchContact(id); + openDialog(); +} + +function triggerDeleteSubBranch(id: string) { + clearData(); + formType.value = 'delete'; + typeBranch.value = 'subBranch'; + formData.value.headOfficeId = id; + openDialog(); +} + +function triggerCreateHeadOffice() { + clearData(); + formType.value = 'create'; + typeBranch.value = 'headOffice'; + openDialog(); +} + +function triggerEditHeadOffice(id: string) { + clearData(); + formType.value = 'edit'; + typeBranch.value = 'headOffice'; + currentBranchIdEdit.value = id; + fetchFormEditBranch(id); + fetchFormEditBranchContact(id); + openDialog(); +} + +function triggerDeleteHeadOffice() { + clearData(); + formType.value = 'delete'; + typeBranch.value = 'headOffice'; + openDialog(); +} + +async function fetchFormEditBranch(id: string) { + const result = await branchStore.fetchById(id); + if (result) { + formData.value = { + taxNo: result.taxNo, + nameEN: result.nameEN, + name: result.name, + addressEN: result.addressEN, + address: result.address, + zipCode: result.zipCode, + email: result.email, + telephoneNo: result.telephoneNo, + longitude: result.longitude, + latitude: result.latitude, + subDistrictId: result.subDistrictId, + districtId: result.districtId, + provinceId: result.provinceId, + headOfficeId: result.headOfficeId, + }; + } +} + +async function fetchFormEditBranchContact(id: string) { + const result = await branchContactStore.fetchList(id); + + if (result) { + const resultFilter = result.result.filter((v) => v.branchId === id); + currentBranchContactIdEdit.value = resultFilter[0].id; + formDataContact.value = { + lineId: resultFilter[0].lineId, + telephoneNo: resultFilter[0].telephoneNo, + qrCodeImage: new File([], 'ชื่อไฟล์.png', { type: 'image/png' }), + }; + } +} + +async function submitForm( + typeSubmit: 'create' | 'edit' | 'delete', + formTypeSubmit: 'headOffice' | 'subBranch', + inputBranchCreate: BranchCreate, + inputBranchContactCreate: BranchContactCreate, +) { + if (formTypeSubmit === 'headOffice') { + if (typeSubmit === 'create') { + const result = await branchStore.create(inputBranchCreate); + if (result) { + await branchContactStore.create(result.id, inputBranchContactCreate); + getTree(); + modal.value = false; + return; + } + } + if (typeSubmit === 'edit') { + await branchStore.editById(currentBranchIdEdit.value, inputBranchCreate); + await branchContactStore.editById( + currentBranchIdEdit.value, + currentBranchContactIdEdit.value, + inputBranchContactCreate, + ); + + getTree(); + modal.value = false; + return; + } + if (typeSubmit === 'delete') { + return; + } + } + + if (formTypeSubmit === 'subBranch') { + if (typeSubmit === 'create') { + const result = await branchStore.create(inputBranchCreate); + if (result) { + await branchContactStore.create(result.id, inputBranchContactCreate); + getTree(); + modal.value = false; + return; + } + } + + if (typeSubmit === 'edit') { + await branchStore.editById(currentBranchIdEdit.value, inputBranchCreate); + await branchContactStore.editById( + currentBranchIdEdit.value, + currentBranchContactIdEdit.value, + inputBranchContactCreate, + ); + getTree(); + modal.value = false; + return; + } + + if (typeSubmit === 'delete') { + return; + } + } +} + async function fetchCard(id: string) { const result = await branchStore.fetchById(id); @@ -241,7 +442,7 @@ onMounted(async () => {
+
@@ -281,7 +482,9 @@ onMounted(async () => { { - + { - + { {{ $t('edit') }} - + + + + + {{ $t('delete') }} + + + + +
+ +
+
+
+
+
+
+ + + + + + + + + + {{ $t('edit') }} + + + {