diff --git a/src/modules/18_command/components/Main/DialogFormCommand.vue b/src/modules/18_command/components/Main/DialogFormCommand.vue index 2ed5152a0..f391a5d04 100644 --- a/src/modules/18_command/components/Main/DialogFormCommand.vue +++ b/src/modules/18_command/components/Main/DialogFormCommand.vue @@ -3,6 +3,8 @@ import { ref, watch } from "vue"; import { useQuasar } from "quasar"; import { useRouter } from "vue-router"; +import { useCounterMixin } from "@/stores/mixin"; +import { useCommandListStore } from "@/modules/18_command/store/ListStore"; import http from "@/plugins/http"; import config from "@/app.config"; @@ -11,44 +13,34 @@ import type { DataCommandType } from "@/modules/18_command/interface/response/Ma import DialogHeader from "@/components/DialogHeader.vue"; -import { useCounterMixin } from "@/stores/mixin"; - const $q = useQuasar(); const router = useRouter(); +const store = useCommandListStore(); const { dialogConfirm, showLoader, hideLoader, messageError, success } = useCounterMixin(); -const modal = defineModel("modal", { required: true }); -const isCopy = defineModel("isCopy", { required: true }); -const commandId = defineModel("commandId", { default: "" }); +/** props*/ +const modal = defineModel("modal", { required: true }); //เปิด,ปิด Popup +const isCopy = defineModel("isCopy", { required: true }); //สถานะทำสำเนา +const commandId = defineModel("commandId", { default: "" }); // id ประเภทคำสั่งที่คำสำเนา -const commandType = ref(""); -const commandNo = ref(""); -const commandYear = ref(new Date().getFullYear()); +const commandType = ref(""); //ประเภทคำสั่ง +const commandNo = ref(""); //คำสั่งเลขที่ +const commandYear = ref(new Date().getFullYear()); //ปี -const listCommand = ref([]); -const commandOp = ref([]); +const listCommand = ref([]); //ข้อมูลรายการประเภทคำสั่ง +const commandOp = ref([]); //ตัวเลือกประเภทคำสั่ง +/** ฟังก์ชันดึงข้อมูลปรเภทคำสั่ง*/ async function fetchCommandType() { - showLoader(); - await http - .get(config.API.commandType) - .then(async (res) => { - const data = await res.data.result; - listCommand.value = data.map((e: DataCommandType) => ({ - id: e.id, - name: e.subtitle ? `${e.name}(${e.subtitle})` : `${e.name}`, - })); - commandOp.value = listCommand.value; - }) - .catch((err) => { - messageError($q, err); - }) - .finally(() => { - hideLoader(); - }); + const data = await store.fetchCommandType(); + if (data) { + listCommand.value = data; + commandOp.value = data; + } } +/** ฟังก์ชันยืนยันการบันทึกข้อมูล */ function onSubmit() { dialogConfirm($q, () => { showLoader(); @@ -77,29 +69,36 @@ function onSubmit() { }); } -function filterSelector(val: string, update: Function, refData: string) { - switch (refData) { - case "OrderTypeOption": - update(() => { - commandType.value = val ? "" : commandType.value; - commandOp.value = listCommand.value.filter( - (v: any) => v.name.indexOf(val) > -1 - ); - }); - break; - - default: - break; - } +/** + * ฟังก์ชันค้นหาข้อมูลรายการประเภทคำสั่ง + * @param val คำที่ค้องการค้นหา + * @param update function จาก quasar + */ +function filterSelector(val: string, update: Function) { + update(() => { + commandType.value = val ? "" : commandType.value; + commandOp.value = listCommand.value.filter( + (v: any) => v.name.indexOf(val) > -1 + ); + }); } +/** + * ฟังก์ชันปิด Popup + * และกำหนด commandNo commandYear เป็นค่า Defult + * */ function onClose() { modal.value = false; commandNo.value = ""; commandYear.value = new Date().getFullYear(); } +/** + * ดูการเปลี่ยนแปลงของ modal + * เมื่อ modal เป็น True และ isCopy เป็น False จะเรียก ฟังก์ชัน fetchCommandType เพื่อดึงข้อมูลปรเภทคำสั่ง + */ watch(modal, () => { + //เช็คค่าของ modal และ isCopy if (modal.value && !isCopy.value) { fetchCommandType(); } @@ -134,8 +133,7 @@ watch(modal, () => { outlined :rules="[(val:string) => !!val || `${'กรุณาเลือกประเภทคำสั่ง'}`]" @filter="(inputValue:any, - doneFn:Function) => filterSelector(inputValue, doneFn,'OrderTypeOption' - ) " + doneFn:Function) => filterSelector(inputValue, doneFn) " >