diff --git a/src/modules/02_organization/components/DialogFormPosition.vue b/src/modules/02_organization/components/DialogFormPosition.vue index d023d8730..091e22d13 100644 --- a/src/modules/02_organization/components/DialogFormPosition.vue +++ b/src/modules/02_organization/components/DialogFormPosition.vue @@ -54,6 +54,7 @@ const props = defineProps({ fetchDataTable: Function, getSummary: Function, shortName: { type: String, required: true }, + orgShortName: { type: String, required: true }, dataTree: { type: Object, default: () => [] }, }); @@ -61,6 +62,8 @@ const isReadonly = ref(false); // อ่านได้อย่าง const isEdit = ref(false); //การแก้ไข const modalAdd = ref(false); //เพิ่ม แก้้ไขตำแหน่ง const isPosition = ref(false); // แสดงการเพิ่มตำแหน่ง +const orgLevel = ref(0); +const orgId = ref(""); //Id หน่วยงาน const dataCopy = ref(); //ข้อมูลที่จ้องการคัดลอก const search = ref(""); //คำค้นหา @@ -78,7 +81,7 @@ const optionFilter = ref([ //ฟอร์มอัตรากำลัง const formData = reactive({ - shortName: props.shortName, + shortName: "", prefixNo: "", positionNo: "", suffixNo: "", @@ -224,6 +227,8 @@ async function fetchPosition(id: string) { formData.suffixNo = data.posMasterNoSuffix; formData.reason = data.reason; formData.isDirector = data.isDirector; + orgLevel.value = data.orgLevel; + orgId.value = data.orgId; //กำหนดค่า Id หน่วยงาน // formData.isStaff = data.isStaff; formData.positionSign = data.positionSign; rows.value = data.positions; @@ -260,6 +265,7 @@ function onSubmit() { isSpecial: e.isSpecial, positionIsSelected: e.positionIsSelected, })); + const body = { posMasterNoPrefix: formData.prefixNo, //*Prefix นำหน้าเลขที่ตำแหน่ง เป็น Optional (ไม่ใช่อักษรย่อของหน่วยงาน/ส่วนราชการ) posMasterNo: Number(formData.positionNo), //*เลขที่ตำแหน่ง เป็นตัวเลข @@ -269,56 +275,109 @@ function onSubmit() { // isStaff: formData.isStaff ? formData.isStaff : false, isStaff: false, positionSign: formData.positionSign, - orgRootId: props.orgLevel === 0 ? props.treeId : null, //Id สำนัก - orgChild1Id: props.orgLevel === 1 ? props.treeId : null, - orgChild2Id: props.orgLevel === 2 ? props.treeId : null, - orgChild3Id: props.orgLevel === 3 ? props.treeId : null, - orgChild4Id: props.orgLevel === 4 ? props.treeId : null, + orgRootId: getOrgIdByLevel(0), // Id สำนัก + orgChild1Id: getOrgIdByLevel(1), + orgChild2Id: getOrgIdByLevel(2), + orgChild3Id: getOrgIdByLevel(3), + orgChild4Id: getOrgIdByLevel(4), + // orgRootId: + // props.actionType === "ADD" + // ? props.orgLevel === 0 + // ? props.treeId + // : null + // : orgLevel.value === 0 + // ? props.treeId + // : null, //Id สำนัก + // orgChild1Id: + // props.actionType === "ADD" + // ? props.orgLevel === 1 + // ? props.treeId + // : null + // : orgLevel.value === 1 + // ? orgId.value + // : null, + // orgChild2Id: + // props.actionType === "ADD" + // ? props.orgLevel === 2 + // ? props.treeId + // : null + // : orgLevel.value === 2 + // ? orgId.value + // : null, + // orgChild3Id: + // props.actionType === "ADD" + // ? props.orgLevel === 3 + // ? props.treeId + // : null + // : orgLevel.value === 3 + // ? orgId.value + // : null, + // orgChild4Id: + // props.actionType === "ADD" + // ? props.orgLevel === 4 + // ? props.treeId + // : null + // : orgLevel.value === 4 + // ? orgId.value + // : null, positions: positionsData, }; showLoader(); - props.actionType === "ADD" || props.actionType === "COPY" - ? await http - .post(config.API.orgPosMaster, body) - .then(async () => { - await props.fetchDataTable?.( - reqMaster.value.id, - reqMaster.value.type, - false - ); - await props.getSummary?.(); - await success($q, "เพิ่มข้อมูลสำเร็จ"); - close(); - }) - .catch((err) => { - messageError($q, err); - }) - .finally(() => { - hideLoader(); - }) - : props.rowId && - (await http - .put(config.API.orgPosMasterById(props.rowId), body) - .then(async () => { - await props.fetchDataTable?.( - reqMaster.value.id, - reqMaster.value.type, - false - ); - await success($q, "แก้ไขข้อมูลสำเร็จ"); - close(); - }) - .catch((err) => { - messageError($q, err); - }) - .finally(() => { - hideLoader(); - })); + try { + const isAddOrCopy = + props.actionType === "ADD" || props.actionType === "COPY"; + + if (isAddOrCopy) { + await http.post(config.API.orgPosMaster, body); + await Promise.all([ + props.fetchDataTable?.( + reqMaster.value.id, + reqMaster.value.type, + false + ), + props.getSummary?.(), + ]); + await success($q, "เพิ่มข้อมูลสำเร็จ"); + } else if (props.rowId) { + await http.put(config.API.orgPosMasterById(props.rowId), body); + await props.fetchDataTable?.( + reqMaster.value.id, + reqMaster.value.type, + false + ); + await success($q, "แก้ไขข้อมูลสำเร็จ"); + } + + close(); + } catch (error) { + messageError($q, error); + } finally { + hideLoader(); + } }); } } +/** + * ฟังก์ชันสร้าง organization ID สำหรับแต่ละ level + * @param level ระดับของ organization (0-4) + * @returns ID ที่เหมาะสมสำหรับ level นั้น + */ +function getOrgIdByLevel(level: number): string | null { + const isAdd = props.actionType === "ADD"; + + if (isAdd) { + return props.orgLevel === level ? props.treeId ?? null : null; + } else { + return orgLevel.value === level + ? level === 0 + ? props.treeId ?? null + : orgId.value + : null; + } +} + /** * ฟังก์ชันต้นหาตำแหน่งที่ต้องการเพิ่ม */ @@ -441,7 +500,6 @@ async function clearFormPositionSelect() { /** * ฟังก์ชัน ปิด popup - * * และกำหนดตัวแปรทั้งหมดให้เป็นค่า Default */ function close() { @@ -472,14 +530,15 @@ function inputEdit(val: boolean) { } /** - * ดูการเปลี่ยยนแปลง props.modal - * + * ดูการเปลี่ยนแปลง props.modal * เมื่อ props.modal เป็น true */ watch( () => props.modal, async () => { if (props.modal === true) { + formData.shortName = + props.actionType === "ADD" ? props.shortName : props.orgShortName; if (props.actionType !== "ADD" && props.rowId) { fetchPosition(props.rowId); } @@ -491,9 +550,8 @@ watch( () => formData.isDirector, (newData, oldData) => { if (newData === true) { - formData.positionSign = !formData.positionSign - ? '' + ? "" : formData.positionSign; } else { formData.positionSign = ""; diff --git a/src/modules/02_organization/components/TableMain.vue b/src/modules/02_organization/components/TableMain.vue index 7f32cc047..ccd1a60a5 100644 --- a/src/modules/02_organization/components/TableMain.vue +++ b/src/modules/02_organization/components/TableMain.vue @@ -71,6 +71,8 @@ const personalId = ref(""); //ตัวแปร id ข้อมูล const modalSelectPerson = ref(false); //เลือกคนครอล const rowId = ref(""); //id ที่ต้องการกระทำ const actionType = ref(""); //ประเภทการกระทำ +const orgShortName = ref(""); //ชื่อย่อหน่วยงาน +const orgRootIdMain = ref(""); //Id สำนัก /** ListMenu Table*/ const listMenu = ref([ { @@ -285,7 +287,12 @@ const dialogPosition = ref(false); //ตำแหน่ง * @param type ประเภท * @param id id */ -function onClickPosition(type: string, id: string) { +function onClickPosition( + type: string, + id: string, + data: PosMaster2 = {} as PosMaster2 +) { + orgShortName.value = type === "EDIT" ? data?.orgShortname : ""; rowId.value = id ? id : ""; actionType.value = type; dialogPosition.value = !dialogPosition.value; @@ -296,7 +303,12 @@ function onClickPosition(type: string, id: string) { * @param type ประเภทการกระทำ * @param id id ที่ต้องการกระทำ */ -function onClickCopyPosition(type: string, id: string) { +function onClickCopyPosition( + type: string, + id: string, + data: PosMaster2 = {} as PosMaster2 +) { + orgShortName.value = data?.orgShortname; rowId.value = id ? id : ""; actionType.value = type; dialogPosition.value = !dialogPosition.value; @@ -723,7 +735,7 @@ watch( v-close-popup @click=" item.type === 'EDIT' - ? onClickPosition('EDIT', props.row.id) + ? onClickPosition('EDIT', props.row.id, props.row) : item.type === 'DEL' ? onClickDelete(props.row.id) : item.type === 'MOVE' @@ -733,7 +745,7 @@ watch( : item.type === 'INHERIT' ? onClickInherit(props.row.id) : item.type === 'COPY' - ? onClickCopyPosition('COPY', props.row.id) + ? onClickCopyPosition('COPY', props.row.id, props.row) : item.type === 'CONDITION' ? onClickCodition(props.row) : null @@ -962,6 +974,7 @@ watch( :modal="dialogPosition" :data-tree="mainTree" :shortName="shortName" + :orgShortName="orgShortName" :close="onClickPosition" :orgLevel="orgLevel" :treeId="treeId"