diff --git a/src/modules/14_KPI/components/indicatorByPlan/IndicatorByPlan.vue b/src/modules/14_KPI/components/indicatorByPlan/IndicatorByPlan.vue index 132b65e0b..75d971a54 100644 --- a/src/modules/14_KPI/components/indicatorByPlan/IndicatorByPlan.vue +++ b/src/modules/14_KPI/components/indicatorByPlan/IndicatorByPlan.vue @@ -43,7 +43,7 @@ const planData = reactive({ const filter = ref(""); const filterAgency = ref(""); const route = useRoute(); -const id = ref(route.params.id.toString()); +const id = ref(route.params.id ? route.params.id.toLocaleString() : ""); const year = ref(0); const roundOp = ref([]); @@ -85,7 +85,6 @@ function fetchRoundOption(isId: number | null = null) { : "", })); roundOp.value = list; - console.log(isId); if (isId) { planData.kpiPeriodId = ""; @@ -120,11 +119,26 @@ async function fetchDataTree(id: string) { showLoader(); await http .get(config.API.orgByid(id.toString())) - .then((res) => { + .then(async (res) => { const data = res.data.result; nodeAgency.value = data; - store.treeId = ""; + + if (nodeAgency.value && planData.nodeId) { + const nodeTree = await searchAndReplace( + nodeAgency.value, + planData.nodeId + ); + if (nodeTree) { + expandedAgency.value = []; + const parts = nodeTree?.orgName.split("/"); + for (let i = 1; i < parts.length; i++) { + const arrangedParts = parts[i]; + expandedAgency.value.push(arrangedParts); + } + updateSelectedAgency(nodeTree, true); + } + } }) .catch((err) => { messageError($q, err); @@ -179,6 +193,8 @@ async function fetchDataById(id: any) { planData.orgRevisionId = data.orgRevisionId; planData.strategy = data.strategy; planData.strategyId = data.strategyId; + console.log(data); + fetchRoundOption(); }) .catch((err) => { @@ -194,13 +210,19 @@ function updateSelected(data: any) { planData.strategy = data.level; } -function updateSelectedAgency(data: any) { - if (planData.node === data.orgLevel && planData.nodeId === data.orgTreeId) { +function updateSelectedAgency(data: any, isUpdate: boolean = false) { + if ( + planData.node === data.orgLevel && + planData.nodeId === data.orgTreeId && + !isUpdate + ) { planData.node = null; planData.nodeId = null; + console.log("1"); } else { planData.node = data.orgLevel; planData.nodeId = data.orgTreeId; + console.log("2"); } planData.orgRevisionId = data.orgRevisionId; } @@ -234,16 +256,37 @@ async function editData(id: string) { }); } +/** + * function หาหน่วยงานที่รับผิดชอบ + * @param orgTreeData ข้อมูล nodeTree + * @param treeId tree ID + */ +async function searchAndReplace(orgTreeData: any, treeId: string | null) { + if (orgTreeData) { + for (let orgTree of orgTreeData) { + if (orgTree.orgTreeId === treeId) { + return orgTree; + } + let foundOrg: any = await searchAndReplace(orgTree.children, treeId); + if (foundOrg) { + return foundOrg; + } + } + return false; + } +} + onMounted(async () => { // fetchData(); - await fetchTree(); + await fetchOrganizationActive(); - if (id.value !== undefined) { + if (id.value) { editStatus.value = true; fetchDataById(id.value); } setTimeout(async () => { store.activeId && (await fetchDataTree(store.activeId)); + await fetchTree(); }, 200); }); @@ -520,7 +563,7 @@ onMounted(async () => { style="height: 350px; overflow: scroll" dense :nodes="nodeAgency" - node-key="orgTreeId" + node-key="orgTreeName" label-key="labelName" selected-color="primary" :filter="filterAgency"