hrms-mgt/src/modules/15_development/components/DialogRecordPerson.vue
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 81b0ba60df fix bug
2024-09-25 17:13:44 +07:00

488 lines
16 KiB
Vue

<script setup lang="ts">
import { ref, reactive, watch } from "vue";
import { useQuasar } from "quasar";
import { useRoute } from "vue-router";
import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
/**
* importType
*/
import type { DataOption } from "@/modules/15_development/interface/index/Main";
import type { ResPrefix } from "@/modules/15_development/interface/response/Main";
/**
* importComponents
*/
import DialogHeader from "@/components/DialogHeader.vue";
const $q = useQuasar();
const route = useRoute();
const {
dialogConfirm,
success,
messageError,
showLoader,
hideLoader,
date2Thai,
calculateDurationYmd,
} = useCounterMixin();
/**
* props
*/
const modal = defineModel<boolean>("modal", { required: true }); //รับ ตัวแปร popup
const tab = defineModel<string>("tab", { required: true }); //รับข้อมูล tab
const props = defineProps({
fetchData: { type: Function, required: true },
});
const projectId = ref<string>(route.params.id.toString()); // id route
const formData = reactive({
type: "", //ประเภท
citizenId: "", // เลข ปชช
prefix: "", //คำนำหน้า
firstName: "", //ชื่อ
lastName: "", //นามสกุล
position: "", //ตำแหน่ง
org: "", //หน่วยงาน
dateStart: null, //วันที่เริ่ม
dateEnd: null, //วันที่สิ้นสุด
trainingDays: "", //จำนวนวันที่ อบรม
commandNumber: "", //เลขที่คำสั่ง
commandDate: null, //ลงวันที่
});
/** Option*/
const typeOpsMain = ref<DataOption[]>([
{
id: "OFFICER",
name: "ขรก.กทม. สามัญ",
},
{
id: "EMPLOYEE",
name: "ลูกจ้างประจำ",
},
]);
const typeOps = ref<DataOption[]>(typeOpsMain.value); // ตัวเลือกประเภท
const prefixOpsMain = ref<DataOption[]>([]); //ตัวเลือกคำนำหน้า หลัก
const prefixOps = ref<DataOption[]>([]); //ตัวเลือกคำนำหน้า
/**
* function บันทึกข้อมูลการเพิ่มราชชื่อ
*/
function onSubmit() {
dialogConfirm($q, () => {
showLoader();
http
.post(config.API.devUploadUser + projectId.value, formData)
.then(async (res) => {
await props?.fetchData();
tab.value = res.data.result ? "IN" : "OUT";
await success($q, "บันทึกข้อมูลสำเร็จ");
onCloseDialog();
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
});
}
/**
* function ค้นหาข้อมูล Option
* @param val คำค้นหา
* @param update function
* @param refData ประเภท
*/
function filterSelector(val: string, update: Function, refData: string) {
switch (refData) {
case "type":
update(() => {
typeOps.value = typeOpsMain.value.filter(
(v: DataOption) => v.name.indexOf(val) > -1
);
});
break;
case "prefix":
update(() => {
prefixOps.value = prefixOpsMain.value.filter(
(v: DataOption) => v.name.indexOf(val) > -1
);
});
break;
default:
break;
}
}
/**
* function ปิด Dialig และเคลียร์ข้อมูลใน formData
*/
function onCloseDialog() {
modal.value = false;
formData.type = "";
formData.citizenId = "";
formData.prefix = "";
formData.firstName = "";
formData.lastName = "";
formData.position = "";
formData.org = "";
formData.dateStart = null;
formData.dateEnd = null;
formData.trainingDays = "";
formData.commandNumber = "";
formData.commandDate = null;
}
/**
* function เรียกข้อมูลคำนำหน้าชื่อ
*/
function fetchDataPrefix() {
showLoader();
http
.get(config.API.orgPrefix)
.then((res) => {
const option = res.data.result.map((e: ResPrefix) => ({
id: e.id,
name: e.name,
}));
prefixOpsMain.value = option;
prefixOps.value = option;
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* callback function ทำงานเมื่อเลือกวันเริ่มต้นและวันาสิ้นสุด จะคำนวนจำนวนวันที่อบรม
*/
watch(
() => [formData.dateStart, formData.dateEnd],
() => {
if (formData.dateStart && formData.dateEnd) {
formData.trainingDays = calculateDurationYmd(
formData.dateStart,
formData.dateEnd
);
}
}
);
/**
* callback function ทำงานเมื่อ modal = true
*/
watch(
() => modal.value,
() => {
if (modal.value) {
fetchDataPrefix();
}
}
);
</script>
<template>
<q-dialog v-model="modal" persistent>
<q-card style="width: 900px; max-width: 80vw">
<q-form greedy @submit.prevent @validation-success="onSubmit">
<DialogHeader
:tittle="'เพิ่มรายชื่อผู้ผ่านการอบรม'"
:close="onCloseDialog"
/>
<q-separator />
<q-card-section>
<div class="row q-col-gutter-md">
<div class="col-xs-12 col-sm-6 col-md-6">
<q-select
dense
outlined
use-input
lazy-rules
emit-value
map-options
hide-bottom-space
input-debounce="0"
option-label="name"
option-value="id"
v-model="formData.type"
class="inputgreen"
:options="typeOps"
label="ประเภท"
:rules="[(val: string) => !!val || `${'กรุณาเลือกประเภท'}`]"
@filter="(inputValue: string,
doneFn: Function) => filterSelector(inputValue, doneFn, 'type'
)"
>
<template v-slot:no-option>
<q-item>
<q-item-section class="text-italic text-grey">
ไมอม
</q-item-section>
</q-item>
</template>
</q-select>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
<q-input
dense
outlined
lazy-rules
hide-bottom-space
maxlength="13"
mask="#############"
v-model="formData.citizenId"
class="inputgreen"
label="เลขประจำตัวประชาชน"
:rules="[
(val: string) => !!val || `${'กรุณากรอก เลขประจำตัวประชาชน'}`,
(val: string) =>
val.length >= 13 ||
`${'กรุณากรอกเลขประจำตัวประชาชนให้ครบ'}`,
]"
/>
</div>
<div class="col-xs-12 col-sm-4 col-md-4">
<q-select
dense
outlined
use-input
lazy-rules
emit-value
map-options
hide-bottom-space
input-debounce="0"
option-label="name"
option-value="name"
v-model="formData.prefix"
class="inputgreen"
:options="prefixOps"
label="คำนำหน้าชื่อ"
:rules="[(val: string) => !!val || `${'กรุณาเลือกคำนำหน้าชื่อ'}`]"
@filter="(inputValue: string,
doneFn: Function) => filterSelector(inputValue, doneFn, 'prefix'
)"
>
<template v-slot:no-option>
<q-item>
<q-item-section class="text-italic text-grey">
ไมอม
</q-item-section>
</q-item>
</template>
</q-select>
</div>
<div class="col-xs-12 col-sm-4 col-md-4">
<q-input
dense
outlined
lazy-rules
hide-bottom-space
v-model="formData.firstName"
class="inputgreen"
label="ชื่อ"
:rules="[
(val: string) => !!val || `${'กรุณากรอกชื่อ'}`]"
/>
</div>
<div class="col-xs-12 col-sm-4 col-md-4">
<q-input
dense
outlined
lazy-rules
hide-bottom-space
v-model="formData.lastName"
class="inputgreen"
label="นามสกุล"
:rules="[
(val: string) => !!val || `${'กรุณากรอกนามสกุล'}`]"
/>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
<q-input
dense
outlined
lazy-rules
hide-bottom-space
v-model="formData.position"
class="inputgreen"
label="ตำแหน่ง"
/>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
<q-input
dense
outlined
lazy-rules
hide-bottom-space
v-model="formData.org"
class="inputgreen"
label="สังกัด"
/>
</div>
<div class="col-xs-12 col-sm-4 col-md-4">
<datepicker
menu-class-name="modalfix"
v-model="formData.dateStart"
:locale="'th'"
autoApply
:enableTimePicker="false"
week-start="0"
:max-date="formData.dateEnd"
>
<template #year="{ year }">{{ year + 543 }}</template>
<template #year-overlay-value="{ value }">{{
parseInt(value + 543)
}}</template>
<template #trigger>
<q-input
class="inputgreen"
dense
outlined
hide-bottom-space
:model-value="
formData.dateStart ? date2Thai(formData.dateStart) : null
"
:label="`${'วันที่เริ่มต้น'}`"
:rules="[(val: string) => !!val || `${'กรุณาเลือกวันที่เริ่มต้น'}`]"
>
<template v-slot:prepend>
<q-icon
name="event"
class="cursor-pointer"
style="color: var(--q-primary)"
>
</q-icon>
</template>
</q-input>
</template>
</datepicker>
</div>
<div class="col-xs-12 col-sm-4 col-md-4">
<datepicker
menu-class-name="modalfix"
v-model="formData.dateEnd"
:locale="'th'"
autoApply
:enableTimePicker="false"
week-start="0"
:min-date="formData.dateStart"
>
<template #year="{ year }">{{ year + 543 }}</template>
<template #year-overlay-value="{ value }">{{
parseInt(value + 543)
}}</template>
<template #trigger>
<q-input
dense
outlined
hide-bottom-space
class="inputgreen"
:model-value="
formData.dateEnd ? date2Thai(formData.dateEnd) : null
"
:label="`${'วันที่สิ้นสุด'}`"
:rules="[(val: string) => !!val || `${'กรุณาเลือกวันที่สิ้นสุด'}`]"
>
<template v-slot:prepend>
<q-icon
name="event"
class="cursor-pointer"
style="color: var(--q-primary)"
>
</q-icon>
</template>
</q-input>
</template>
</datepicker>
</div>
<div class="col-xs-12 col-sm-4 col-md-4">
<q-input
dense
outlined
lazy-rules
hide-bottom-space
v-model="formData.trainingDays"
class="inputgreen"
label="จำนวนวันที่อบรม"
:rules="[
(val: string) => !!val || `${'กรุณากรอกจำนวนวันที่อบรม'}`]"
/>
</div>
<div class="col-xs-12 col-sm-4 col-md-4">
<q-input
dense
outlined
lazy-rules
hide-bottom-space
v-model="formData.commandNumber"
class="inputgreen"
label="เลขที่คำสั่ง/เลขที่หนังสืออนุมัติ"
:rules="[
(val: string) => !!val || `${'กรุณากรอกเลขที่คำสั่ง/เลขที่หนังสืออนุมัติ'}`]"
/>
</div>
<div class="col-xs-12 col-sm-4 col-md-4">
<datepicker
menu-class-name="modalfix"
v-model="formData.commandDate"
:locale="'th'"
autoApply
:enableTimePicker="false"
week-start="0"
>
<template #year="{ year }">{{ year + 543 }}</template>
<template #year-overlay-value="{ value }">{{
parseInt(value + 543)
}}</template>
<template #trigger>
<q-input
dense
outlined
class="inputgreen"
hide-bottom-space
:model-value="
formData.commandDate
? date2Thai(formData.commandDate)
: null
"
:label="`${'คำสั่งลงวันที่/หนังสืออนุมัติลงวันที่'}`"
:rules="[(val: string) => !!val || `${'กรุณาเลือกคำสั่งลงวันที่/หนังสืออนุมัติลงวันที่'}`]"
>
<template v-slot:prepend>
<q-icon
name="event"
class="cursor-pointer"
style="color: var(--q-primary)"
>
</q-icon>
</template>
</q-input>
</template>
</datepicker>
</div>
</div>
</q-card-section>
<q-separator />
<q-card-actions align="right" class="bg-white text-teal">
<q-btn id="onSubmit" type="submit" label="บันทึก" color="public">
<q-tooltip>นทกขอม</q-tooltip>
</q-btn>
</q-card-actions>
</q-form>
</q-card>
</q-dialog>
</template>
<style lang="scss" scoped></style>