hrms-admin/src/modules/01_metadata/components/position/DialogFormExecutive.vue
2024-09-05 16:44:08 +07:00

175 lines
6.1 KiB
Vue

<script setup lang="ts">
import { ref, reactive, watch } from "vue";
import { useQuasar } from "quasar";
import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import type { RowListForm } from "@/modules/01_metadata/interface/request/position/index";
import DialogHeader from "@/components/DialogHeader.vue";
const $q = useQuasar();
const { dialogConfirm, showLoader, success, hideLoader, messageError } =
useCounterMixin();
/**
* props
*/
const modal = defineModel<boolean>("executive", { required: true }); // popup
const formData = defineModel<any>("formData", { required: true }); // ข้อมูลที่ต้องการแก้ไข
const isEdit = defineModel<any>("edit", { required: true }); // สถานะการแก้ไข
const props = defineProps({
getData: Function,
});
const isReadonly = ref<boolean>(false); // อ่านได้อย่างเดียว
// ตัวแปรตำแหน่งทางการบริหาร
const formExecutive = reactive<RowListForm>({
id: "",
posExecutiveName: "",
posExecutivePriority: null,
});
/**
* ยืนยันการบันทึกข้อมูลรายการตำแหน่งทางการบริหาร
*
* ุ ถ้า isEdit เป็น false จะทำการเพิ่มข้อมูลรายการตำแหน่งทางการบริหาร ถ้าไม่จะทำการแก้ไขข้อมูล
* เมื่อบันทึกข้อมูลเสร็จจะเรียก function props.getData() เพื่อดึงข้อมูลรายการตำแหน่งทางการบริหาร
*
*/
function onSubmit() {
dialogConfirm($q, async () => {
showLoader();
const body = {
posExecutiveName: formExecutive.posExecutiveName,
posExecutivePriority: formExecutive.posExecutivePriority, //สายงาน
};
// กำหนด Path API
const path = isEdit.value
? config.API.orgPosExecutive + `/${formExecutive.id}`
: config.API.orgPosExecutive;
// กำหนด method
const method = isEdit.value ? "put" : "post";
await http[method](path, body)
.then(async () => {
await props.getData?.();
success($q, "บันทึกข้อมูลสำเร็จ");
close();
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
});
}
/**
* ปิด popup ฟอร์มข้อมูลรายการตำแหน่งทางการบริหาร
*
* และเคลียร formExecutive ข้อมูลรายการตำแหน่งทางการบริหารให้เป็นค่าว่าง
*/
function close() {
modal.value = false;
formExecutive.id = "";
formExecutive.posExecutiveName = "";
formExecutive.posExecutivePriority = null;
isEdit.value = false;
}
/**
* ส่งค่า css ออกไปตามเงื่อนไข
* @param val true/false
*/
function inputEdit(val: boolean) {
return {
"full-width cursor-pointer inputgreen ": val,
"full-width cursor-pointer inputgreen": !val,
};
}
/**
* เมื่อค่า modal เป็น true จะทำการรีเซ็ตค่าของฟอร์ม formExecutive
*
* และเมื่อ isEdit เป็น true จะกำหนดข้อมูลลงในฟอร์ม formExecutive
*/
watch(
() => modal.value,
async () => {
if (modal.value) {
const dataList = await formData.value;
formExecutive.id = "";
formExecutive.posExecutiveName = "";
formExecutive.posExecutivePriority = null;
if (isEdit.value == true) {
formExecutive.id = dataList.id;
formExecutive.posExecutiveName = dataList.posExecutiveName;
formExecutive.posExecutivePriority = dataList.posExecutivePriority;
}
}
}
);
</script>
<template>
<q-dialog v-model="modal" persistent>
<q-card style="min-width: 350px">
<q-form greedy @submit.prevent @validation-success="onSubmit">
<DialogHeader
:tittle="
isEdit ? `แก้ไขตำแหน่งทางการบริหาร` : 'เพิ่มตำแหน่งทางการบริหาร'
"
:close="close"
/>
<q-separator />
<q-card-section>
<div class="row q-col-gutter-sm col-12">
<div class="col-12">
<q-input
ref="posExecutiveNameRef"
v-model="formExecutive.posExecutiveName"
:class="inputEdit(isReadonly)"
dense
outlined
for="#posExecutiveName"
label="ชื่อตำแหน่งทางการบริหาร"
lazy-rules
hide-bottom-space
:rules="[
(val:string) => !!val || `${'กรุณากรอกชื่อตำแหน่งทางการบริหาร'}`,
]"
/>
</div>
<div class="col-12">
<q-input
ref="posExecutivePriorityRef"
v-model="formExecutive.posExecutivePriority"
:class="inputEdit(isReadonly)"
dense
outlined
type="number"
for="#posExecutivePriority"
label="ลำดับความสำคัญ"
lazy-rules
hide-bottom-space
:rules="[(val:string) => !!val || `${'กรุณากรอกลำดับความสำคัญ'}`]"
/>
</div>
</div>
</q-card-section>
<q-separator />
<q-card-actions align="right">
<q-btn type="submit" :label="`บันทึก`" color="public" />
</q-card-actions>
</q-form>
</q-card>
</q-dialog>
</template>