diff --git a/src/modules/02_organization/components/StructureOrgMain.vue b/src/modules/02_organization/components/StructureOrgMain.vue index d45b753b9..4a6ae377f 100644 --- a/src/modules/02_organization/components/StructureOrgMain.vue +++ b/src/modules/02_organization/components/StructureOrgMain.vue @@ -18,6 +18,8 @@ import { exportChartToPNG, } from "@/plugins/exportChart"; +import type { DataOption } from "@/modules/02_organization/interface/index/Main"; + const mixin = useCounterMixin(); const store = useOrganizational(); const storeOrg = useStructStore(); @@ -31,11 +33,15 @@ const dataSourceLock = ref(); // ข้อมูลตั้งต้นขอ const chartRef = ref(); // อ้างอิงไปที่ตัว chart const scrollContainer = ref(null); +const agencyId = ref(""); // id ของหน่วยงานที่เลือก +const agencyData = ref([]); // ข้อมูลรายการหน่วยงาน +const agencyOp = ref([]); // ตัวเลือกหน่วยงาน + /** fetch ข้อมูล Chart โครงสร้าง*/ -async function fetchOrgChart() { +async function fetchOrgChart(agencyId: string = "root") { if (rootOrgID.value) { showLoader(); - let urlRequest = config.API.orgChart(rootOrgID.value); + let urlRequest = config.API.orgChart(rootOrgID.value) + `/${agencyId}`; await http .get(urlRequest) .then(async (response) => { @@ -135,6 +141,51 @@ async function savePDF() { }, 500); } +/** ฟังก์ชันเลื่อน scroll ไปที่กึ่งกลาง*/ +async function scrollToCenter() { + const container = scrollContainer.value; + if (container) { + container.scrollLeft = (container.scrollWidth - container.clientWidth) / 2; + } +} + +async function fetchAgencyData(id: string) { + if (!id) return; // ถ้าไม่มี id ให้ return ออกไป + + agencyId.value = ""; + try { + const data = await storeOrg.fetchDataTree(id); + console.log(data); + + agencyData.value = data.data.map((item: any) => ({ + id: item.orgTreeId, + name: item.orgName, + })); + agencyOp.value = agencyData.value; + } catch (err) { + messageError($q, err); + } +} + +function onSelectAgency(val: string) { + const id = val || "root"; // ถ้าไม่มีการเลือกหน่วยงาน ให้ใช้ root + fetchOrgChart(id); +} + +/** + * ฟังก์ชันต้นหาข้อมูลของ Option ขอสถานะ + * @param val ค่าที่ต้องการฟิลเตอร์ + * @param update อัพเดทค่า + * @param refData ดาต้าที่ต้องการฟิลเตอร์ + */ +function filterOption(val: string, update: Function) { + update(() => { + agencyOp.value = agencyData.value.filter( + (v: DataOption) => v.name.indexOf(val) > -1 + ); + }); +} + watch( [() => store.typeOrganizational, () => store.historyId], ([new1], [old1]) => { @@ -149,18 +200,13 @@ watch( ? store.draftId : store.historyId; - fetchOrgChart(); + if (rootOrgID.value) { + fetchAgencyData(rootOrgID.value); + fetchOrgChart(); + } } ); -/** ฟังก์ชันเลื่อน scroll ไปที่กึ่งกลาง*/ -async function scrollToCenter() { - const container = scrollContainer.value; - if (container) { - container.scrollLeft = (container.scrollWidth - container.clientWidth) / 2; - } -} - onMounted(async () => { rootOrgID.value = store.typeOrganizational === "current" @@ -171,6 +217,7 @@ onMounted(async () => { await fetchOrgChart(); await scrollToCenter(); + await fetchAgencyData(rootOrgID.value); }); onBeforeMount(() => { @@ -182,7 +229,7 @@ onBeforeMount(() => {
-
+
ดาวน์โหลด PNG @@ -195,6 +242,33 @@ onBeforeMount(() => { > ดาวน์โหลด PDF + + + + +
{ const dataSource = ref(); + const cachedData = ref>({}); + + /** + * ฟังก์ชันดึงข้อมูลโครงสร้าง + * @param keyId id โครงสร้าง + */ + async function fetchDataTree(keyId: string) { + // ตรวจสอบว่ามีข้อมูลใน cache แล้วหรือไม่ + if (cachedData.value[keyId]) { + return cachedData.value[keyId]; + } + + // ถ้าไม่มีข้อมูลใน cache หรือ forceRefresh เป็น true ให้ดึงข้อมูลจาก API + try { + const res = await http.get(config.API.orgByid(keyId)); + const result = res.data.result; + + // เก็บข้อมูลใน cache + cachedData.value[keyId] = result; + + return result; + } catch (error) { + console.error("Error fetching data tree:", error); + return []; + } + } + return { dataSource, + fetchDataTree, }; }); diff --git a/src/modules/17_acting/views/main.vue b/src/modules/17_acting/views/main.vue index f3c10d52a..7b2a402e0 100644 --- a/src/modules/17_acting/views/main.vue +++ b/src/modules/17_acting/views/main.vue @@ -395,7 +395,7 @@ onMounted(async () => { `${col.posNo} ${ col.fullNameCurrentHolder ? " (" + col.fullNameCurrentHolder + ")" - : "" + : "ว่าง" }` }}