hrms-mgt/src/modules/07_insignia/components/1_Round/DialogDetail.vue
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2f4b1d198f fix insignia
2025-05-01 11:40:54 +07:00

645 lines
24 KiB
Vue

<script setup lang="ts">
import { computed, reactive, ref, watch } from "vue";
import { useQuasar } from "quasar";
import { useCounterMixin } from "@/stores/mixin";
import http from "@/plugins/http";
import config from "@/app.config";
import type { QTableProps } from "quasar";
import type {
OptionPeriod,
OptionData,
} from "@/modules/07_insignia/interface/index/Main";
import type { FormQuery } from "@/modules/07_insignia/interface/request/Main";
import type { ResponsePosition } from "@/modules/07_insignia/interface/response/Main";
import DialogHeader from "@/components/DialogHeader.vue";
/** use*/
const $q = useQuasar();
const {
date2Thai,
dateToISO,
messageError,
showLoader,
hideLoader,
dialogConfirm,
success,
} = useCounterMixin();
/**
* props
*/
const modal = defineModel<boolean>("modal", { required: true });
const actionType = defineModel<string>("actionType", { required: true });
const roundId = defineModel<string>("roundId", { required: true });
const props = defineProps({
fetchList: { type: Function, required: true },
});
//ชื่อ popup
const title = computed(() => {
return actionType.value === "view"
? "รายละเอียดรอบการเสนอขอพระราชทานเครื่องราชอิสริยาภรณ์"
: actionType.value === "edit"
? "แก้ไขข้อมูล"
: "เพิ่มรอบการเสนอขอพระราชทานเครื่องราชอิสริยาภรณ์";
});
//อ่านอย่างเดียว
const readonly = computed(() => {
return actionType.value === "view" ? true : false;
});
const roundInsig = ref<any>(); //รอบการเสนอขอพระราชทานเครื่องราชฯ
const yearly = ref<number>(new Date().getFullYear()); //ปีที่เสนอ
const dateStart = ref<Date | null>(null); //วันเริ่มต้น
const dateEnd = ref<Date | null>(null); //วันสิ้นสุด
const datelast = ref<number | null>(null); //จำนวนวันแจ้งเตือนก่อนวันสิ้นสุด
const files = ref<any>(); //เอกสารประกอบ
const fileDocDataUpload = ref<File[]>([]); //รายการเอกสารประกอบ
//ตัวเลือกรอบการเสนอขอพระราชทานเครื่องราชฯ
const options = ref<OptionPeriod[]>([
{ label: "รอบการเสนอขอพระราชทานเครื่องราชฯ รอบที่ 1", value: 1 },
{ label: "รอบการเสนอขอพระราชทานเครื่องราชฯ รอบที่ 2", value: 2 },
]);
/** Function เรียกข้อมูลของรอบการเสนอขอ*/
async function fetchData() {
showLoader();
await http
.get(config.API.getRoundInsignia(roundId.value))
.then(async (res) => {
const data = await res.data.result;
roundInsig.value =
options.value.filter((r: OptionPeriod) => r.value == data.period_round)
.length > 0
? options.value.filter(
(r: OptionPeriod) => r.value == data.period_round
)[0]
: null;
yearly.value = data.period_year;
datelast.value = data.period_amount;
dateStart.value = new Date(data.period_start);
dateEnd.value = new Date(data.period_end);
selected.value = rows.value.filter((e: ResponsePosition) =>
data.empPosId.includes(e.id)
);
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {
hideLoader();
});
}
/** Function อัพเดทวันที่เริ่มต้น และ สิ้นสุด*/
function updateDateRange() {
if (roundInsig.value.value == 1) {
dateStart.value = new Date(yearly.value, 9, 1);
dateEnd.value = new Date(yearly.value + 1, 3, 29);
} else if (roundInsig.value.value == 2) {
dateStart.value = new Date(yearly.value + 1, 3, 29);
dateEnd.value = new Date(yearly.value + 1, 4, 29);
}
}
/**
* Function อัพเดทปี
* @param year รับค่าปี คศ
*/
function updateYear(year: number) {
yearly.value = year;
updateDateRange();
}
/**
* Function อัพโหลดไฟล์
* @param files ไฟล์
*/
function fileUploadDoc(files: any) {
files.forEach((file: any) => {
fileDocDataUpload.value.push(file);
});
}
/** Function บันทึกข้อมูล*/
function onSubmit() {
dialogConfirm($q, async () => {
showLoader();
const newEmpPosId = selected.value.map((e) => e.id);
const formData = new FormData();
const name = `รอบการเสนอขอพระราชทานเครื่องราชฯ รอบที่ ${
roundInsig.value.value
} ปี ${yearly.value + 543} `;
formData.append("name", name);
formData.append("year", yearly.value.toString());
formData.append("amount", datelast.value ? datelast.value.toString() : "");
formData.append("round", roundInsig.value.value);
if (dateStart.value !== null) {
formData.append("startDate", dateToISO(dateStart.value));
}
if (dateEnd.value !== null) {
formData.append("endDate", dateToISO(dateEnd.value));
}
formData.append("file", files.value);
if (newEmpPosId.length > 0) {
newEmpPosId.forEach((id) => {
formData.append("empPosId", id);
});
} else {
formData.append("empPosId", '');
}
const url =
actionType.value !== "edit"
? config.API.listRoundInsignia()
: config.API.editRoundInsignia(roundId.value);
const httpMethod = actionType.value !== "edit" ? http.post : http.put;
httpMethod(url, formData)
.then(async () => {
await props.fetchList();
await success($q, "บันทึกข้อมูลสำเร็จ");
onCloseDialog();
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
});
}
/**
* function ปิด popup รอบการเสนอขอพระราชทานเครื่องราชอิสริยาภรณ์
* และกำหนดตัวแปรเป็นค่า Defult
*/
function onCloseDialog() {
modal.value = false;
roundInsig.value = "";
yearly.value = new Date().getFullYear();
dateStart.value = null;
dateEnd.value = null;
datelast.value = null;
files.value = null;
fileDocDataUpload.value = [];
}
/**
* function คืนค่าชื่อคลาสตามค่าที่กำหนด
* @param val ค่ากำหนดว่าจะแสดงคลาสไหน
* @returns ชื่อคลาส
*/
function classInput(val: boolean) {
return {
"full-width inputgreen cursor-pointer": val,
"full-width cursor-pointer": !val,
};
}
// ตัวเลือกค้นหา
const optionFilter = ref<OptionData[]>([
{ id: "positionName", name: "ชื่อตำแหน่ง" },
{ id: "positionType", name: "กลุ่มงาน" },
{ id: "positionLevel", name: "ระดับชั้นงาน" },
]);
const formQuery = reactive<FormQuery>({
keyword: "",
type: "positionName",
});
const rows = ref<ResponsePosition[]>([]);
const selected = ref<ResponsePosition[]>([]); //ราชชื่อที่เลือกส่งไปออกคำสั่ง
const columns = ref<QTableProps["columns"]>([
{
name: "posDictName",
align: "left",
label: "ชื่อตำแหน่ง",
sortable: true,
field: "posDictName",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "posTypeName",
align: "left",
label: "กลุ่มงาน",
sortable: true,
field: "posTypeName",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "posLevelName",
align: "left",
label: "ระดับชั้นงาน",
sortable: true,
field: "posLevelName",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
]);
const visibleColumns = ref<string[]>([
"posDictName",
"posTypeName",
"posLevelName",
]);
const pagination = ref({
sortBy: "desc",
descending: false,
page: 1,
rowsPerPage: 10,
});
/**
* ฟังก์ชันดึงข้อมูลรายการตำแหน่งจาก API
* @param statusType สถานะการส่งประเภทค้นหา (true = ไม่ส่งประเภทค้นหา)
* เก็บข้อมูลรายการตำแหน่งจากไว้ใน rows.value
*/
async function fetchDataPos(statusType: boolean = false) {
rows.value = [];
showLoader();
await http
.get(
config.API.orgEmployeePos +
`?keyword=${formQuery.keyword}&type=${statusType ? "" : formQuery.type}`
)
.then(async (res) => {
rows.value = await res.data.result;
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/** ทำงานเมื่อ modal เป็น true actionType เป็น view*/
watch(
() => modal.value,
async () => {
if (modal.value) {
await fetchDataPos(true);
selected.value = [];
formQuery.keyword = "";
formQuery.type = "positionName";
if (actionType.value !== "") {
await fetchData();
}
}
}
);
</script>
<template>
<q-dialog v-model="modal" persistent>
<q-card style="min-width: 80vw">
<q-form greedy @submit.prevent @validation-success="onSubmit">
<DialogHeader :tittle="title" :close="onCloseDialog" />
<q-separator />
<q-card-section style="padding: 0px">
<div class="col-12 row">
<div class="col-xs-12 col-md-5 row no-wrap">
<div class="col-12">
<div class="col-12 row q-col-gutter-md q-pa-md">
<div class="col-md-9 col-xs-12">
<q-select
:class="classInput(!readonly)"
:readonly="readonly"
dense
outlined
v-model="roundInsig"
:options="options"
option-value="value"
option-label="label"
label="รอบการเสนอขอพระราชทานเครื่องราชฯ"
@update:model-value="updateDateRange"
:rules="[(val:string) => !!val || `${'กรุณาเลือกรอบที่'}`]"
hide-bottom-space
lazy-rules
/>
</div>
<div class="col-md-3 col-xs-12">
<datepicker
:class="classInput(!readonly)"
:readonly="readonly"
menu-class-name="modalfix"
v-model="yearly"
:locale="'th'"
autoApply
year-picker
:enableTimePicker="false"
@update:modelValue="updateYear"
>
<template #year="{ year }">{{ year + 543 }}</template>
<template #year-overlay-value="{ value }">{{
parseInt(value + 543)
}}</template>
<template #trigger>
<q-input
:class="classInput(!readonly)"
:readonly="readonly"
dense
outlined
hide-bottom-space
:model-value="yearly + 543"
:rules="[(val:string) => !!val || `${'กรุณาเลือกปีที่เสนอ'}`]"
:label="`${'ปีที่เสนอ'}`"
>
<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-md-4 col-xs-12">
<datepicker
menu-class-name="modalfix"
v-model="dateStart"
:locale="'th'"
autoApply
borderless
:enableTimePicker="false"
week-start="0"
:class="classInput(!readonly)"
:readonly="readonly"
:max-date="dateEnd"
>
<template #year="{ year }">
{{ year + 543 }}
</template>
<template #year-overlay-value="{ value }">
{{ parseInt(value + 543) }}
</template>
<template #trigger>
<q-input
:class="classInput(!readonly)"
:readonly="readonly"
outlined
dense
:model-value="
dateStart != null ? date2Thai(dateStart) : null
"
:label="`${'วันเริ่มต้น'}`"
:rules="[(val:string) => !!val || `${'กรุณาเลือกวันเริ่มต้น'}`]"
hide-bottom-space
>
<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-md-4 col-xs-12">
<datepicker
menu-class-name="modalfix"
v-model="dateEnd"
:locale="'th'"
autoApply
borderless
:enableTimePicker="false"
week-start="0"
:class="classInput(!readonly)"
:readonly="readonly"
:min-date="dateStart"
>
<template #year="{ year }">
{{ year + 543 }}
</template>
<template #year-overlay-value="{ value }">
{{ parseInt(value + 543) }}
</template>
<template #trigger>
<q-input
outlined
dense
class="col-xs-12 col-sm-4"
:model-value="
dateEnd != null ? date2Thai(dateEnd) : null
"
:label="`${'วันสิ้นสุด'}`"
:rules="[
(val:string) => !!val || `${'กรุณาเลือกวันที่วันสิ้นสุด'}`,
]"
:class="classInput(!readonly)"
:readonly="readonly"
hide-bottom-space
>
<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-md-4 col-xs-12">
<q-input
:class="classInput(!readonly)"
:readonly="readonly"
dense
outlined
v-model="datelast"
label="จำนวนวันแจ้งเตือนก่อนวันสิ้นสุด"
mask="###"
:rules="[
(val:string) =>
!!val || `${'กรุณากรอกจำนวนวันแจ้งเตือนก่อนวันสิ้นสุด'}`,
]"
lazy-rules
hide-bottom-space
/>
</div>
<div class="col-md-12 col-xs-12">
<q-file
:class="classInput(!readonly)"
:readonly="readonly"
outlined
dense
v-model="files"
@added="fileUploadDoc"
label="อัปโหลดเอกสารประกอบ"
hide-bottom-space
lazy-rules
accept=".pdf,.xlsx,.doc"
>
<template v-slot:prepend>
<q-icon name="attach_file" />
</template>
</q-file>
</div>
</div>
</div>
<div class="col-12 row">
<q-separator :vertical="!$q.screen.lt.md" />
</div>
</div>
<div class="col-xs-12 col-md-7 row">
<div class="col-12">
<div class="row col-12 q-col-gutter-sm q-pa-md">
<div class="row col-12 q-col-gutter-sm">
<div class="row q-col-gutter-sm align-items-center">
<div class="col-auto">
<q-select
label="ค้นหาจาก"
v-model="formQuery.type"
:options="optionFilter"
emit-value
dense
map-options
outlined
option-label="name"
option-value="id"
/>
</div>
<div class="col">
<q-input
ref="searchRef"
v-model="formQuery.keyword"
outlined
dense
lazy-rules
label="คำค้น"
hide-bottom-space
@keydown.enter="fetchDataPos()"
>
<template v-slot:append>
<q-icon
v-if="formQuery.keyword"
name="cancel"
@click="formQuery.keyword = ''"
class="cursor-pointer"
></q-icon>
</template>
</q-input>
</div>
<div class="col-auto">
<q-btn
outline
color="primary"
icon="search"
label="ค้นหา"
class="full-width full-height"
@click="fetchDataPos()"
/>
</div>
</div>
<q-space />
<div class="row q-col-gutter-sm align-items-center">
<div class="col-auto">
<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>
</div>
<div class="col-12">
<q-card
bordered
:class="
pagination.rowsPerPage > 10 ? 'col-12 scroll' : 'col-12'
"
:style="pagination.rowsPerPage > 10 ? 'height: 75vh' : ''"
>
<d-table
:columns="columns"
:rows="rows"
row-key="id"
flat
bordered
:paging="true"
dense
:visible-columns="visibleColumns"
selection="multiple"
v-model:selected="selected"
v-model:pagination="pagination"
>
<template v-slot:header-selection="scope">
<q-checkbox
:disable="readonly"
keep-color
color="primary"
dense
v-model="scope.selected"
/>
</template>
<template v-slot:body="props">
<q-tr :props="props">
<q-td auto-width>
<q-checkbox
:disable="readonly"
keep-color
color="primary"
dense
v-model="props.selected"
/>
</q-td>
<q-td
v-for="col in props.cols"
:key="col.name"
:props="props"
>
<div>
{{ col.value ? col.value : "-" }}
</div>
</q-td>
</q-tr>
</template>
</d-table>
</q-card>
</div>
</div>
</div>
</div>
</div>
</q-card-section>
<q-separator />
<q-card-actions align="right" v-if="actionType !== 'view'">
<q-btn label="บันทึก" color="secondary" type="submit" />
</q-card-actions>
</q-form>
</q-card>
</q-dialog>
</template>
<style scoped></style>