diff --git a/src/modules/05_placement/components/PersonalList/DialogDetail.vue b/src/modules/05_placement/components/PersonalList/DialogDetail.vue index 56cc9b3c4..fca5acdfb 100644 --- a/src/modules/05_placement/components/PersonalList/DialogDetail.vue +++ b/src/modules/05_placement/components/PersonalList/DialogDetail.vue @@ -3,6 +3,7 @@ import { ref, watch } from "vue"; import { useQuasar } from "quasar"; import { useCounterMixin } from "@/stores/mixin"; +import { calculateAge } from "@/utils/function"; import http from "@/plugins/http"; import config from "@/app.config"; @@ -38,6 +39,20 @@ const props = defineProps({ const rows = ref([]); const personalForm = ref(); +const age = ref(""); //อายุ + +// ข้อมูลแปลงจาก ID เป็นชื่อ +const currentProvinceName = ref(""); +const currentDistrictName = ref(""); +const currentSubDistrictName = ref(""); +const registProvinceName = ref(""); +const registDistrictName = ref(""); +const registSubDistrictName = ref(""); + +// Cache ข้อมูล +const provincesCache = ref([]); +const districtsCache = ref>(new Map()); +const subDistrictsCache = ref>(new Map()); /**หัวตาราง */ const columns = ref([ @@ -92,6 +107,12 @@ async function fetchData() { .get(config.API.getDatapersonal(props.personalId)) .then((res) => { personalForm.value = res.data.result; + if (res.data.result.dateOfBirth) { + // กำหนดอายุ ส่งวันเกิดไปคำนวน + age.value = calculateAge(res.data.result.dateOfBirth); + } else { + age.value = null; + } personalForm.value?.education.map((e: Education) => { rows.value.push({ university: e.institute, @@ -107,6 +128,137 @@ async function fetchData() { .finally(() => { hideLoader(); }); + + // แปลง ID เป็นชื่อ + await convertAddressIds(); +} + +/** + * ฟังก์ชันแปลง ID เป็นชื่อจริงของที่อยู่ (แบบ optimize) + */ +async function convertAddressIds() { + if (!personalForm.value) return; + + try { + // โหลดข้อมูลจังหวัดครั้งเดียวถ้ายังไม่มี + await loadProvinces(); + + // แปลงข้อมูลที่อยู่ปัจจุบัน + if (personalForm.value.currentProvinceId) { + currentProvinceName.value = getProvinceNameFromCache( + personalForm.value.currentProvinceId + ); + + if (personalForm.value.currentDistrictId) { + currentDistrictName.value = await getDistrictNameOptimized( + personalForm.value.currentProvinceId, + personalForm.value.currentDistrictId + ); + + if (personalForm.value.currentSubDistrictId) { + currentSubDistrictName.value = await getSubDistrictNameOptimized( + personalForm.value.currentDistrictId, + personalForm.value.currentSubDistrictId + ); + } + } + } + + // แปลงข้อมูลที่อยู่ตามทะเบียน + if (personalForm.value.registProvinceId) { + registProvinceName.value = getProvinceNameFromCache( + personalForm.value.registProvinceId + ); + + if (personalForm.value.registDistrictId) { + registDistrictName.value = await getDistrictNameOptimized( + personalForm.value.registProvinceId, + personalForm.value.registDistrictId + ); + + if (personalForm.value.registSubDistrictId) { + registSubDistrictName.value = await getSubDistrictNameOptimized( + personalForm.value.registDistrictId, + personalForm.value.registSubDistrictId + ); + } + } + } + } catch (error) { + console.error("Error converting address IDs:", error); + } +} + +/** + * โหลดข้อมูลจังหวัดทั้งหมด (ครั้งเดียว) + */ +async function loadProvinces() { + if (provincesCache.value.length === 0) { + try { + const res = await http.get(config.API.province); + provincesCache.value = res.data.result; + } catch (error) { + console.error("Error loading provinces:", error); + } + } +} + +/** + * ดึงชื่อจังหวัดจาก cache + */ +function getProvinceNameFromCache(provinceId: string): string { + const province = provincesCache.value.find( + (p: any) => p.id.toString() === provinceId + ); + return province ? province.name : "-"; +} + +/** + * ดึงชื่ออำเภอแบบ optimize (เรียก API เฉพาะจังหวัดที่ต้องการ) + */ +async function getDistrictNameOptimized( + provinceId: string, + districtId: string +): Promise { + try { + // เช็ค cache ก่อน + if (!districtsCache.value.has(provinceId)) { + const res = await http.get(config.API.listDistrict(provinceId)); + districtsCache.value.set(provinceId, res.data.result.districts); + } + + const districts = districtsCache.value.get(provinceId) || []; + const district = districts.find((d: any) => d.id.toString() === districtId); + return district ? district.name : "-"; + } catch (error) { + console.error("Error loading district:", error); + return "-"; + } +} + +/** + * ดึงชื่อตำบลแบบ optimize (เรียก API เฉพาะอำเภอที่ต้องการ) + */ +async function getSubDistrictNameOptimized( + districtId: string, + subDistrictId: string +): Promise { + try { + // เช็ค cache ก่อน + if (!subDistrictsCache.value.has(districtId)) { + const res = await http.get(config.API.listSubDistrict(districtId)); + subDistrictsCache.value.set(districtId, res.data.result.subDistricts); + } + + const subDistricts = subDistrictsCache.value.get(districtId) || []; + const subDistrict = subDistricts.find( + (sd: any) => sd.id.toString() === subDistrictId + ); + return subDistrict ? subDistrict.name : "-"; + } catch (error) { + console.error("Error loading subdistrict:", error); + return "-"; + } } /** @@ -170,30 +322,58 @@ watch(props, () => {
-
เลขประจำตัวประชาชน
-
วัน/เดือน/ปีเกิด
+
เลขประจำตัวประชาชน
+
วัน/เดือน/ปีเกิด
+
อายุ
+
สัญชาติ
+
หมู่เลือด
-
+
{{ personalForm?.idCard }}
-
+
{{ date2Thai(personalForm?.dateOfBirth) }}
+
+ {{ age ? age : "-" }} +
+
+ {{ + personalForm?.nationality ? personalForm.nationality : "-" + }} +
+
+ {{ + personalForm?.bloodGroup ? personalForm.bloodGroup : "-" + }} +
-
ชื่อ-นามสกุล
-
เพศ
+
ชื่อ-นามสกุล
+
เพศ
+
เชื้อชาติ
+
ศาสนา
+
เบอร์โทรศัพท์
-
+
{{ personalForm?.fullName }}
-
+
{{ personalForm?.gender }}
+
+ {{ personalForm?.race ? personalForm.race : "-" }} +
+
+ {{ personalForm?.religion ? personalForm.religion : "-" }} +
+
+ {{ personalForm?.telephone ? personalForm.telephone : "-" }} +
@@ -203,9 +383,90 @@ watch(props, () => {
ภูมิลำเนา
-
ที่อยู่
-
- {{ personalForm?.registAddress }} + +
+
+ ที่อยู่ปัจจุบัน +
+
+
+
ที่อยู่
+
จังหวัด
+
เขต/อำเภอ
+
แขวง/ตำบล
+
รหัสไปรษณีย์
+
+
+
+ {{ + personalForm?.currentAddress + ? personalForm.currentAddress + : "-" + }} +
+
+ {{ currentProvinceName ? currentProvinceName : "-" }} +
+
+ {{ currentDistrictName ? currentDistrictName : "-" }} +
+
+ {{ + currentSubDistrictName ? currentSubDistrictName : "-" + }} +
+
+ {{ + personalForm?.currentZipCode + ? personalForm.currentZipCode + : "-" + }} +
+
+
+
+ + +
+
+ ที่อยู่ตามทะเบียนบ้าน +
+
+
+
ที่อยู่
+
จังหวัด
+
เขต/อำเภอ
+
แขวง/ตำบล
+
รหัสไปรษณีย์
+
+
+
+ {{ + personalForm?.registAddress + ? personalForm.registAddress + : "-" + }} +
+
+ {{ registProvinceName ? registProvinceName : "-" }} +
+
+ {{ registDistrictName ? registDistrictName : "-" }} +
+
+ {{ + registSubDistrictName ? registSubDistrictName : "-" + }} +
+
+ {{ + personalForm?.registZipCode + ? personalForm.registZipCode + : "-" + }} +
+
+