hrms-mgt/src/modules/14_KPI/views/01_kpiRound.vue
2026-03-25 16:55:15 +07:00

653 lines
21 KiB
Vue

<script setup lang="ts">
import { ref, reactive, onMounted, watch } from "vue";
import { useQuasar, type QTableProps } from "quasar";
import { useCounterMixin } from "@/stores/mixin";
import { checkPermission } from "@/utils/permissions";
import http from "@/plugins/http";
import config from "@/app.config";
import { calculateFiscalYear } from "@/utils/function";
import { usePagination } from "@/composables/usePagination";
/** importType*/
import type { DataOption } from "@/modules/14_KPI/interface/index/Main";
import type { FormRound } from "@/modules/14_KPI/interface/request/Main";
import type { ResRound } from "@/modules/14_KPI/interface/response/Main";
/** importComponents*/
import DialogHeader from "@/components/DialogHeader.vue";
/** use*/
const $q = useQuasar();
const {
showLoader,
hideLoader,
success,
messageError,
date2Thai,
dialogConfirm,
dialogRemove,
} = useCounterMixin();
const { pagination, params, onRequest, checkAndUpdatePage } = usePagination(
"",
fetchList
);
/** Table*/
const rows = ref<ResRound[]>([]); //รายการรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
const columns = ref<QTableProps["columns"]>([
{
name: "year",
align: "left",
label: "ปีงบประมาณ",
sortable: true,
field: "year",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
format: (val) => (val ? val + 543 : "-"),
},
{
name: "durationKPI",
align: "left",
label: "รอบการประเมิน",
sortable: true,
field: "durationKPI",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
format: (val) => connvertName(val),
},
{
name: "startDate",
align: "left",
label: "วันเริ่มต้น",
sortable: true,
field: "startDate",
format: (val) => date2Thai(val),
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "endDate",
align: "left",
label: "วันสิ้นสุด",
sortable: true,
field: "endDate",
format: (val) => date2Thai(val),
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "isActive",
align: "left",
label: "สถานะ",
sortable: true,
field: "isActive",
format: (val) => (val ? "เปิด" : "ปิดแล้ว"),
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
]);
const visibleColumns = ref<string[]>([
"year",
"durationKPI",
"startDate",
"endDate",
"isActive",
]);
const modalDialog = ref<boolean>(false); //modal เพิ่มรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
const isStatusEdit = ref<boolean>(false); //status การแก้ไขข้อมูล
const fiscalyear = ref<number>(calculateFiscalYear(new Date())); //ปีงบประมาณปัจจุบัน
const keyword = ref<string>(""); //คำค้นหา
//ฟร์อมข้อมูลเพิ่มรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
const formData = reactive<FormRound>({
durationKPI: "", //รอบการประเมิน
year: null, //ปีงบประมาณ
startDate: null, //วันเริ่มต้น
endDate: null, //วันสิ้นสุด
});
const roundOp = ref<DataOption[]>([
{ id: "APR", name: "รอบที่ 1 เมษายน" },
{
id: "OCT",
name: "รอบที่ 2 ตุลาคม",
},
]); //Option รอบการประเมิน
/** ฟังก์ชัน fetch ข้อมูลรายการรอบการประเมินผลการปฏิบัติหน้าที่ราชการ*/
async function fetchList() {
showLoader();
await http
.get(config.API.kpiPeriod + "/admin", {
params: {
...params.value,
year: fiscalyear.value,
keyword: keyword.value.trim(),
},
})
.then(async (res) => {
const result = await res.data.result;
pagination.value.rowsNumber = result.total;
rows.value = result.data;
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* ฟังก์ชัน เปิด Dialog เพิ่มรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
* @param status เพิ่ม,แก้ไข
*/
function onClickAddOrView(status: boolean = false) {
isStatusEdit.value = status;
modalDialog.value = true;
}
/**
* ฟังก์ชันปิด popup เพิ่มรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
* ปิดแล้วเคลียร์ฟร๋อมข้อมูลการเพิ่มรอบ
*/
function closeDialog() {
modalDialog.value = false;
formData.durationKPI = "";
formData.startDate = null;
formData.endDate = null;
formData.year = null;
}
/** ฟังก์ชัน Clear วันสิ้นสุด*/
function changeDateStart() {
if (formData?.startDate !== null && formData?.endDate !== null) {
const startDate = new Date(formData.startDate);
const endDate = new Date(formData?.endDate);
if (startDate > endDate) {
formData.endDate = null;
}
}
}
/** ฟังก์ชันบันทึกข้อมูลการเพิ่มรอบการประเมิน*/
function onSubmit() {
dialogConfirm($q, async () => {
showLoader();
try {
const url = isStatusEdit.value
? config.API.kpiPeriodById("12")
: config.API.kpiPeriod;
const method = isStatusEdit.value ? "put" : "post";
await http[method](url, {
...formData,
startDate: formData.startDate,
endDate: formData.endDate,
});
await fetchList();
await success($q, "บันทึกข้อมูลสำเร็จ");
closeDialog();
} catch (e) {
messageError($q, e);
} finally {
hideLoader();
}
});
}
/**
* ฟังก์ชันเลือกเมนูแก้ไขรอบ
* @param action ปิดรอบ,ลบรอบ
* @param id รอบการประเมิน
*/
function onClickAction(action: string, id: string) {
switch (action) {
case "open":
onOpenRounde(id);
break;
case "close":
onCloseRounde(id);
break;
case "delete":
onDeleteRound(id);
break;
default:
break;
}
}
/**
* ฟังก์ชันยืนยันการเปิดรอบ
* @param id รอบการประเมินที่จ้องการเปิดรอบ
*/
function onOpenRounde(id: string) {
dialogConfirm(
$q,
async () => {
showLoader();
await http
.get(config.API.kpiPeriod + `/open/${id}`)
.then(async () => {
await fetchList();
await success($q, "เปิดรอบสำเร็จ");
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
},
"ยืนยันการเปิดรอบ",
"ต้องการยืนยันการเปิดรอบนี้หรือไม่ ?"
);
}
/**
* ฟังก์ชันยืนยันการปิดรอบ
* @param id รอบการประเมินที่จ้องการปิดรอบ
*/
function onCloseRounde(id: string) {
dialogConfirm(
$q,
async () => {
showLoader();
await http
.get(config.API.kpiPeriod + `/close/${id}`)
.then(async () => {
await fetchList();
await success($q, "ปิดรอบสำเร็จ");
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
},
"ยืนยันการปิดรอบ",
"ต้องการยืนยันการปิดรอบนี้หรือไม่ ?"
);
}
/**
* ฟังก์ชันยืนยันการลบข้อมูลรายการรอบ
* @param id รอบการประเมิน
*/
function onDeleteRound(id: string) {
dialogRemove($q, () => {
showLoader();
http
.delete(config.API.kpiPeriodById(id))
.then(async () => {
await checkAndUpdatePage(rows.value.length);
await fetchList();
await success($q, "ลบข้อมูลสำเร็จ");
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
});
}
/**
* ฟังก์ชันเปลี่ยนชื่อรอบการประเมิน
* @param val id อบการประเมิน
* @return ชื่อรอบการประเมิน
*/
function connvertName(val: string) {
//หาข้อมูลรอบการประเมิน
const findData = roundOp.value.find((e: DataOption) => e.id === val);
return findData?.name;
}
/** function อัพเดทปีงบประมาณ และ fetch รายการรอบการประเมินผลการปฏิบัติหน้าที่ราชการ*/
function handleUpdateYear() {
pagination.value.page = 1;
fetchList();
}
/** callback function เช็ต วันเริ่มต้น และวันสิ้นสุด รอบการประเมินผลการปฏิบัติหน้าที่ราชการ*/
watch(
[() => formData.durationKPI, () => formData.year],
([newDurationKPI, newYear], [oldDurationKPI, oldYear]) => {
if (newDurationKPI && newYear) {
if (newDurationKPI === "APR") {
formData.startDate = new Date(`${Number(newYear) - 1}-10-01`);
formData.endDate = new Date(`${newYear}-03-31`);
} else if (newDurationKPI === "OCT") {
formData.startDate = new Date(`${newYear}-04-01`);
formData.endDate = new Date(`${newYear}-09-30`);
}
}
}
);
/** hookLifecycle*/
onMounted(async () => {
await fetchList();
});
</script>
<template>
<div class="toptitle text-dark col-12 row items-center">
รายการรอบการประเมนผลการปฏหนาทราชการ
</div>
<q-card flat bordered class="col-12 q-pa-md">
<div class="row q-col-gutter-sm">
<div class="row col-12">
<datepicker
v-model="fiscalyear"
:locale="'th'"
autoApply
year-picker
:enableTimePicker="false"
@update:model-value="handleUpdateYear"
>
<template #year="{ year }">{{ year + 543 }}</template>
<template #year-overlay-value="{ value }">{{
parseInt(value + 543)
}}</template>
<template #trigger>
<q-input
dense
outlined
:model-value="
fiscalyear === 0 ? 'ทั้งหมด' : Number(fiscalyear) + 543
"
:label="`${'ปีงบประมาณ'}`"
>
<template v-slot:prepend>
<q-icon
name="event"
class="cursor-pointer"
style="color: var(--q-primary)"
>
</q-icon>
</template>
<template v-if="fiscalyear" v-slot:append>
<q-icon
name="cancel"
@click.stop.prevent="
(fiscalyear = 0), (pagination.page = 1), fetchList()
"
class="cursor-pointer"
/>
</template>
</q-input>
</template>
</datepicker>
<q-btn
v-if="checkPermission($route)?.attrIsCreate"
flat
round
icon="add"
color="primary"
@click="onClickAddOrView()"
>
<q-tooltip>เพิ่ม</q-tooltip>
</q-btn>
<q-space />
<q-select
v-model="visibleColumns"
multiple
outlined
dense
options-dense
:display-value="$q.lang.table.columns"
emit-value
map-options
:options="columns"
option-value="name"
style="min-width: 140px"
/>
</div>
<div class="col-12">
<p-table
for="table"
ref="table"
:columns="columns"
:rows="rows"
row-key="subject"
flat
bordered
dense
class="custom-header-table"
:visible-columns="visibleColumns"
:rows-per-page-options="[10, 25, 50, 100]"
v-model:pagination="pagination"
@request="onRequest"
>
<template v-slot:header="props">
<q-tr :props="props">
<q-th auto-width />
<q-th v-for="col in props.cols" :key="col.name" :props="props">
<span class="text-weight-medium">{{ col.label }}</span>
</q-th>
</q-tr>
</template>
<template v-slot:body="props">
<q-tr :props="props">
<q-td>
<q-btn
v-if="checkPermission($route)?.attrIsUpdate !== false"
flat
round
dense
:disable="!props.row.isActive"
:color="props.row.isActive ? 'orange' : 'grey'"
icon="mdi-close"
@click="onClickAction('close', props.row.id)"
>
<q-tooltip>ปิดรอบ</q-tooltip>
</q-btn>
<q-btn
v-if="checkPermission($route)?.attrIsDelete !== false"
clickable
v-close-popup
@click="onClickAction('delete', props.row.id)"
flat
round
dense
color="red"
icon="mdi-delete"
>
<q-tooltip>ลบรอบ</q-tooltip>
</q-btn>
</q-td>
<q-td v-for="col in props.cols" :key="col.name" :props="props">
<div class="table_ellipsis">
{{ col.value ? col.value : "-" }}
</div>
</q-td>
</q-tr>
</template>
</p-table>
</div>
</div>
</q-card>
<q-dialog v-model="modalDialog" persistent>
<q-card style="width: 350px">
<q-form greedy @submit.prevent @validation-success="onSubmit">
<DialogHeader
:tittle="'เพิ่มรอบการประเมินผลการปฏิบัติหน้าที่ราชการ'"
:close="closeDialog"
/>
<q-separator />
<q-card-section>
<div class="row col-12 q-col-gutter-md">
<div class="col-12">
<q-select
dense
outlined
hide-bottom-space
lazy-rules
v-model="formData.durationKPI"
:options="roundOp"
option-label="name"
option-value="id"
emit-value
map-options
input-class="text-red"
label="รอบการประเมิน"
class="inputgreen"
:rules="[
(val:string) =>
!!val || `${'กรุณาเลือกรอบการประเมิน'}`,
]"
/>
</div>
<div class="col-12">
<datepicker
menu-class-name="modalfix"
v-model="formData.year"
:locale="'th'"
autoApply
year-picker
:enableTimePicker="false"
>
<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.year === null
? null
: Number(formData.year) + 543
"
: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-12">
<datepicker
menu-class-name="modalfix"
v-model="formData.startDate"
:locale="'th'"
autoApply
:enableTimePicker="false"
week-start="0"
@update:model-value="changeDateStart()"
>
<template #year="{ year }">{{ year + 543 }}</template>
<template #year-overlay-value="{ value }">{{
parseInt(value + 543)
}}</template>
<template #trigger>
<q-input
dense
outlined
:model-value="
formData.startDate ? date2Thai(formData.startDate) : null
"
:label="`${'วันเริ่มต้น'}`"
hide-bottom-space
class="inputgreen"
: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-12">
<datepicker
menu-class-name="modalfix"
v-model="formData.endDate"
:locale="'th'"
autoApply
:enableTimePicker="false"
week-start="0"
:min-date="formData.startDate"
>
<template #year="{ year }">{{ year + 543 }}</template>
<template #year-overlay-value="{ value }">{{
parseInt(value + 543)
}}</template>
<template #trigger>
<q-input
dense
outlined
:model-value="
formData.endDate ? date2Thai(formData.endDate) : null
"
:label="`${'วันสิ้นสุด'}`"
class="inputgreen"
hide-bottom-space
: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 unelevated label="บันทึก" type="submit" color="public">
<q-tooltip>นทกขอม</q-tooltip>
</q-btn>
</q-card-actions>
</q-form>
</q-card>
</q-dialog>
</template>
<style scoped></style>