diff --git a/src/modules/14_KPI/components/indicatorByPlan/IndicatorByPlan.vue b/src/modules/14_KPI/components/indicatorByPlan/IndicatorByPlan.vue index 91968d13c..75d971a54 100644 --- a/src/modules/14_KPI/components/indicatorByPlan/IndicatorByPlan.vue +++ b/src/modules/14_KPI/components/indicatorByPlan/IndicatorByPlan.vue @@ -20,8 +20,7 @@ const { } = mixin; const planData = reactive({ - year: 0, //ปีงบประมาณ - round: "", //รอบการประเมิน(เมษา->APR, ตุลา->OCT) + kpiPeriodId: "", //รอบการประเมิน(เมษา->APR, ตุลา->OCT) including: "", //รหัสตัวชี้วัด includingName: "", //ชื่อตัวชี้วัด target: "", //ค่าเป้าหมาย @@ -44,12 +43,10 @@ const planData = reactive({ const filter = ref(""); const filterAgency = ref(""); const route = useRoute(); -const id = ref(route.params.id); +const id = ref(route.params.id ? route.params.id.toLocaleString() : ""); +const year = ref(0); -const roundOp = ref([ - { id: "APR", name: "เมษายน" }, - { id: "OCT", name: "ตุลาคม" }, -]); +const roundOp = ref([]); const notFound = ref("ไม่พบข้อมูลที่ค้นหา"); const noData = ref("ไม่มีข้อมูล"); const expandedPlan = ref>([]); @@ -68,6 +65,40 @@ async function onSubmit() { ); } +const inputRef = ref(null); +function fetchRoundOption(isId: number | null = null) { + showLoader(); + http + .get( + config.API.kpiPeriod + + `?page=${1}&pageSize=${10}&keyword=${""}&year=${year.value}` + ) + .then((res) => { + const data = res.data.result.data; + const list = data.map((e: any) => ({ + id: e.id, + name: + e.durationKPI === "OCT" + ? "รอบตุลาคม" + : e.durationKPI === "APR" + ? "รอบเมษายน" + : "", + })); + roundOp.value = list; + + if (isId) { + planData.kpiPeriodId = ""; + inputRef.value.resetValidation(); + } + }) + .catch((err) => { + messageError($q, err); + }) + .finally(() => { + hideLoader(); + }); +} + function fetchTree() { showLoader(); http @@ -88,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); @@ -126,9 +172,10 @@ async function fetchDataById(id: any) { .get(config.API.kpiPlanById(id)) .then(async (res) => { const data = res.data.result; + year.value = data.year; // planData.value = data; - planData.year = data.year; - planData.round = data.round; + // planData.year = data.year; + planData.kpiPeriodId = data.kpiPeriodId; planData.including = data.including; planData.includingName = data.includingName; planData.target = data.target; @@ -146,6 +193,9 @@ async function fetchDataById(id: any) { planData.orgRevisionId = data.orgRevisionId; planData.strategy = data.strategy; planData.strategyId = data.strategyId; + console.log(data); + + fetchRoundOption(); }) .catch((err) => { messageError($q, err); @@ -159,13 +209,20 @@ function updateSelected(data: any) { planData.strategyId = data.id; 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; } @@ -184,11 +241,12 @@ async function addData() { }); } -async function editData(id: any) { +async function editData(id: string) { await http .put(config.API.kpiPlanById(id), planData) .then(() => { success($q, "บันทึกข้อมูลสำเร็จ"); + fetchDataById(id); }) .catch((err) => { messageError($q, err); @@ -198,16 +256,37 @@ async function editData(id: any) { }); } +/** + * 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); }); @@ -219,12 +298,12 @@ onMounted(async () => {