diff --git a/src/pages/02_personnel-management/MainPage.vue b/src/pages/02_personnel-management/MainPage.vue index 3b5a3aae..939837f4 100644 --- a/src/pages/02_personnel-management/MainPage.vue +++ b/src/pages/02_personnel-management/MainPage.vue @@ -5,7 +5,12 @@ import { storeToRefs } from 'pinia'; import useUserStore from 'stores/user'; import useBranchStore from 'src/stores/branch'; -import { UserCreate, UserTypeStats, UserOption } from 'src/stores/user/types'; +import { + UserCreate, + UserTypeStats, + UserOption, + RoleData, +} from 'src/stores/user/types'; import { BranchUserStats } from 'src/stores/branch/types'; import PersonCard from 'components/home/PersonCard.vue'; @@ -53,6 +58,7 @@ const defaultFormData = { responsibleArea: '', }; +const urlProfile = ref(); const isEdit = ref(false); const modal = ref(false); const status = ref(false); @@ -62,6 +68,10 @@ const selectorLabel = ref(''); const hqId = ref(''); const brId = ref(''); const username = ref(''); +const age = ref(); +const formDialogRef = ref(); +const userStats = ref(); +const typeStats = ref(); const formData = ref({ provinceId: null, districtId: null, @@ -93,9 +103,6 @@ const formData = ref({ birthDate: null, responsibleArea: '', }); -const userStats = ref(); -const typeStats = ref(); -const age = ref(); const profileFile = ref(undefined); const inputFile = document.createElement('input'); @@ -120,6 +127,7 @@ const genderOpts = [ const userOption = ref({ hqOpts: [], brOpts: [], + roleOpts: [], }); const selectorList = [ @@ -139,6 +147,16 @@ async function createKeycloak() { return res.data; } +async function fetchRoleOption() { + const res = await api.get('/keycloak/role'); + res.data.map((item) => { + userOption.value.roleOpts.push({ + label: item.name, + value: item.id, + }); + }); +} + async function fetchHqOption() { if (userOption.value.hqOpts.length === 0) { const res = await branchStore.fetchList({ pageSize: 999, filter: 'head' }); @@ -169,6 +187,7 @@ async function fetchBrOption(id: string) { async function openDialog(id?: string) { await fetchHqOption(); + await fetchRoleOption(); modal.value = true; if (id && userData.value) { const foundUser = userData.value.result.find((user) => user.id === id); @@ -196,7 +215,11 @@ async function openDialog(id?: string) { hqId.value = foundUser.branch[0].headOfficeId as string; brId.value = foundUser.branch[0].id; code.value = foundUser.code; + urlProfile.value = foundUser.profileImageUrl; isEdit.value = true; + await fetchBrOption(hqId.value); + await formDialogRef.value.fetchSubDistrict(formData.value.districtId); + await formDialogRef.value.fetchDistrict(formData.value.provinceId); } } } @@ -204,31 +227,28 @@ async function openDialog(id?: string) { function onClose() { modal.value = false; Object.assign(formData.value, defaultFormData); + mapUserType(selectorLabel.value); + isEdit.value = false; hqId.value = ''; brId.value = ''; username.value = ''; userId.value = ''; code.value = ''; - mapUserType(selectorLabel.value); + urlProfile.value = ''; } async function onSubmit() { - console.log('enter'); formData.value.profileImage = profileFile.value as File; if (isEdit.value === true && userId.value) { - console.log('edit'); - await userStore.editById(userId.value, formData.value); + const formDataEdit = { ...formData.value }; + delete formDataEdit.keycloakId; + await userStore.editById(userId.value, formDataEdit); } else { - console.log('post'); formData.value.keycloakId = await createKeycloak(); - if (formData.value.keycloakId !== '') { - console.log('keycloakOk'); + if (formData.value.keycloakId) { const result = await userStore.create(formData.value); - console.log('Result after create:', result); if (result) { - console.log('hi3'); - console.log(result.id); - // await branchStore.addUser(result.id, userId.value); + await branchStore.addUser(brId.value, result.id); } } } @@ -253,6 +273,27 @@ function mapUserType(label: string) { } } +function calculateAge(birthDate: Date | null) { + if (!birthDate) return null; + const birthDateTimeStamp = new Date(birthDate).getTime(); + const now = new Date(); + const diff = now.getTime() - birthDateTimeStamp; + + const ageDate = new Date(diff); + const years = ageDate.getUTCFullYear() - 1970; + const months = ageDate.getUTCMonth(); + const days = ageDate.getUTCDate() - 1; + + age.value = `${years} ปี ${months} เดือน ${days} วัน`; +} + +async function selectHq(id: string) { + if (!id) return; + brId.value = ''; + userOption.value.brOpts = []; + await fetchBrOption(id); +} + onMounted(async () => { await userStore.fetchList({ includeBranch: true }); typeStats.value = await userStore.typeStats(); @@ -274,11 +315,11 @@ watch( ); watch( - () => hqId.value, - async (id) => { - fetchBrOption(id); - brId.value = ''; - userOption.value.brOpts = []; + () => formData.value.birthDate, + (birthDate) => { + if (birthDate) { + calculateAge(birthDate); + } }, ); @@ -377,6 +418,7 @@ watch(
+
+ สถานะผู้ใช้งาน
- @@ -669,7 +723,7 @@ watch(
- +