hrms-mgt/src/modules/18_command/components/DialogCreateCommand.vue
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 f3bd67c93f fix:label q-input year
2025-10-09 17:02:11 +07:00

597 lines
20 KiB
Vue

<script setup lang="ts">
import { ref, watch } from "vue";
import { useQuasar } from "quasar";
import http from "@/plugins/http";
import config from "@/app.config";
import { useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";
import { useCommandMainStore } from "@/modules/18_command/store/Main";
import { usePagination } from "@/composables/usePagination";
import type { QTableProps } from "quasar";
import type {
ListCommand,
DataOrder,
Pagination,
TabOptions,
} from "@/modules/18_command/interface/index/Main";
import type { DataListCommand } from "@/modules/18_command/interface/response/Main";
import DialogHeader from "@/components/DialogHeader.vue";
const storeCommand = useCommandMainStore();
const router = useRouter();
const $q = useQuasar();
const mixin = useCounterMixin();
const { showLoader, messageError, dialogConfirm, hideLoader, date2Thai } =
mixin;
const { pagination, params, onRequest } = usePagination("", getListCommandDraf);
/** props*/
const modal = defineModel<boolean>("modal", { required: true });
const props = defineProps({
commandTypeCode: String, // ไอดีประเภทคำสั่ง
persons: Array, // ไอดีคนที่เลือกออกคำสั่งส่งมาเป็น array
notPerson: {
type: Boolean,
default: false,
},
fetchData: {
type: Function,
default: () => {},
},
});
const commandOp = ref<ListCommand[]>([]); // ประเภทคำสั่ง
const commandType = ref<string>(""); //ประเภทคำสั่ง
const commandNo = ref<string>(""); //คำสั่งเลขที่
const commandCode = ref<string>(""); //รหัสคำสั่ง
const commandYear = ref<number>(new Date().getFullYear()); //ปี
const commandVolume = ref<string>(""); //เล่มที่
const commandChapter = ref<string>(""); //ตอนที่
const rows = ref<DataListCommand[]>([]); // รายการคำสั่ง
const selected = ref<DataOrder[]>([]); // id คำสั่งที่เลือก
const filter = ref<string>(""); // คำค้นหา
const visibleColumns = ref<string[]>([
"commandNo",
"issue",
"commandExcecuteDate",
"commandAffectDate",
"createdFullName",
"assignFullName",
]);
const columns = ref<QTableProps["columns"]>([
{
name: "commandNo",
align: "left",
label: "เลขที่คำสั่ง",
sortable: true,
field: "commandNo",
format(val, row) {
return val ? `${val}/${row.commandYear + 543}` : "-";
},
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "issue",
align: "left",
label: "ชื่อคำสั่ง",
sortable: true,
field: "issue",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "commandAffectDate",
align: "left",
label: "วันที่ลงนาม",
sortable: true,
field: "commandAffectDate",
format(val) {
return val ? date2Thai(val) : "-";
},
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "commandExcecuteDate",
align: "left",
label: "วันที่คำสั่งมีผล",
sortable: true,
field: "commandExcecuteDate",
format(val) {
return val ? date2Thai(val) : "-";
},
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "createdFullName",
align: "left",
label: "ผู้สร้าง",
sortable: true,
field: "createdFullName",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "assignFullName",
align: "left",
label: "ผู้ลงนาม",
sortable: false,
field: "assignFullName",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
]);
const selectCreate = ref<string | null>("NEW"); //ประเภทการออกคำสั่ง สร้างใหม่/คำสั่งแบบร่าง
/** ฟังก์ชันเรียกข้อมูลรายการคำสั่ง*/
async function getListCommandDraf() {
showLoader();
await http
.get(config.API.commandList, {
params: {
...params.value,
year: commandYear.value,
keyword: filter.value.trim(),
status: "DRAFT",
commandTypeId: commandType.value,
},
})
.then(async (res) => {
const result = res.data.result;
pagination.value.rowsNumber = result.total;
rows.value = result.data;
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* ฟังก์ชันสร้างคำสั่งใหม่
* @param isRedirect บันทึกและไปยังหน้าคำสั่ง เป็น true บันทึกและเลือกรายชื่อต่อ เป็น false
*/
function createCommand(isRedirect: boolean) {
dialogConfirm($q, async () => {
showLoader();
const data = props?.persons?.map((e: any) => ({
refId: e.id,
prefix: e.prefix,
firstName: e.firstName,
lastName: e.lastName,
citizenId: e.citizenId,
profileId: e.profileId,
rootId: e.rootId,
position: e.position,
posType: e.posType,
posLevel: e.posLevel,
...(e.remarkVertical ? { remarkVertical: e.remarkVertical } : {}),
...(e.remarkHorizontal ? { remarkHorizontal: e.remarkHorizontal } : {}),
}));
const body = {
commandYear: commandYear.value,
commandNo:
commandCode.value !== "C-PM-47"
? commandNo.value
: `${commandVolume.value}/${commandChapter.value}`,
commandTypeId: commandType.value,
persons: !props.notPerson ? data : [],
};
await http
.post(config.API.command + `/person`, body)
.then(async (res) => {
const id = await res.data.result;
modal.value = false;
if (isRedirect) {
router.push(`/command/edit/${id}`);
} else {
clearValue();
await props.fetchData(); // เรียกฟังก์ชัน fetchData ที่ส่งมาจาก props เพื่อรีเฟรชข้อมูล
}
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {
hideLoader();
});
});
}
/**
* ฟังก์ชันเพิ่มคนเข้าไปในคำสั่งที่เป็นแบบร่าง
* @param isRedirect บันทึกและไปยังหน้าคำสั่ง เป็น true บันทึกและเลือกรายชื่อต่อ เป็น false
*/
function addPersonalToCommand(isRedirect: boolean) {
dialogConfirm($q, async () => {
const data = props?.persons?.map((e: any) => ({
refId: e.id,
prefix: e.prefix,
firstName: e.firstName,
lastName: e.lastName,
citizenId: e.citizenId,
profileId: e.profileId,
rootId: e.rootId,
position: e.position,
posType: e.posType,
posLevel: e.posLevel,
...(e.remarkVertical ? { remarkVertical: e.remarkVertical } : {}),
...(e.remarkHorizontal ? { remarkHorizontal: e.remarkHorizontal } : {}),
}));
const body = {
commandId: selected.value[0].id,
persons: data,
};
showLoader();
await http
.post(config.API.command + `/person`, body)
.then(async (res) => {
const id = await res.data.result;
modal.value = false;
if (isRedirect) {
router.push(`/command/edit/${id}`);
} else {
modal.value = false;
await props.fetchData(); // เรียกฟังก์ชัน fetchData ที่ส่งมาจาก props เพื่อรีเฟรชข้อมูล
}
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {
hideLoader();
});
});
}
/**
* ฟังก์ชันบันทึกข้อมูล
* @param isRedirect บันทึกและไปยังหน้าคำสั่ง เป็น true บันทึกและเลือกรายชื่อต่อ เป็น false
*/
function onSubmit(isRedirect: boolean) {
if (selectCreate.value === "NEW") {
createCommand(isRedirect);
} else {
addPersonalToCommand(isRedirect);
}
}
/** ฟังก์ชันปิด popup */
function closeModal() {
modal.value = false;
clearValue();
}
/** ฟังก์ชันกำหน้ค่าเป็น Defult*/
function clearValue() {
commandNo.value = "";
commandYear.value = new Date().getFullYear();
selectCreate.value = "NEW";
selected.value = [];
filter.value = "";
commandVolume.value = "";
commandChapter.value = "";
}
/** ฟังก์ชันดึงข้อมูลคำสั่ง */
async function fetchCommandType() {
const data = await storeCommand.getCommandTypes(); // ยิงไป get ที่ store
// filter เฉพาะ code ของคำสั่งที่เลือก
commandOp.value = data.filter(
(v: ListCommand) => v.code == props.commandTypeCode
);
commandType.value = commandOp.value[0].id;
commandCode.value = commandOp.value[0].code;
}
const isHold = ref<boolean>(true); // true คือแสดงปุ่มบันทึกและเลือกรายชื่อต่อ / false คือซ่อนปุ่มบันทึกและเลือกรายชื่อต่อ
// tab สร้างคำสั่งใหม่ และเลือกคำสั่งที่เป็นแบบร่าง
const tabOptions = ref<TabOptions[]>([
{ label: "สร้างคำสั่งใหม่", value: "NEW" },
{ label: "เลือกคำสั่งที่เป็นแบบร่าง", value: "DRAF" },
]);
/** ฟังก์ชั่นเช็คการแสดงผล ตรวจสอบว่าเป็นประเภทคำสั่งที่เป็นแบบเลือกได้รายการเดียวไหม ถ้าเป็นแบบรายการเดียวจะซ่อนเลือกคำสั่งที่เป็นแบบร่าง*/
async function displayTab() {
isHold.value =
(props.commandTypeCode !== "C-PM-10" &&
props.commandTypeCode !== "C-PM-11" &&
props.commandTypeCode !== "C-PM-12") ??
false;
tabOptions.value = isHold.value
? [
{ label: "สร้างคำสั่งใหม่", value: "NEW" },
{ label: "เลือกคำสั่งที่เป็นแบบร่าง", value: "DRAF" },
]
: [{ label: "สร้างคำสั่งใหม่", value: "NEW" }];
}
/** ดูการเปลี่ยนแปลงของ modal*/
watch(modal, () => {
if (modal.value && props.persons?.length !== 0) {
displayTab();
fetchCommandType();
}
});
/** ดูการเปลี่ยนแปลงของประเภทการออกคำสั่ง*/
watch(
() => selectCreate.value,
async () => {
if (selectCreate.value == "DRAF") {
getListCommandDraf();
}
}
);
</script>
<template>
<q-dialog v-model="modal" persistent>
<q-card style="min-width: 50vw">
<DialogHeader :tittle="'สร้าง/เลือกคำสั่ง'" :close="closeModal" />
<q-separator />
<q-card-section style="max-height: 50vh" class="scroll">
<div class="row q-mb-md">
<div class="col-12 q-gutter-md">
<q-btn-toggle
v-model="selectCreate"
toggle-color="primary"
color="white"
text-color="black"
no-caps
:options="tabOptions"
/>
</div>
</div>
<q-separator />
<div v-if="selectCreate == 'NEW'" class="row q-mt-sm q-col-gutter-sm">
<div class="col-12 q-col-gutter-sm">
<q-select
class="inputgreen"
v-model="commandType"
:label="`${'ประเภทคำสั่ง'}`"
option-label="name"
:options="commandOp"
option-value="id"
dense
emit-value
map-options
lazy-rules
use-input
hide-bottom-space
outlined
readonly
>
<template v-slot:no-option>
<q-item>
<q-item-section class="text-grey">
ไม่มีข้อมูล
</q-item-section>
</q-item>
</template>
</q-select>
</div>
<div class="row col-12 q-col-gutter-sm">
<div class="col-6" v-if="commandCode !== 'C-PM-47'">
<q-input
class="inputgreen"
outlined
dense
v-model="commandNo"
:label="`${'คำสั่งเลขที่'}`"
/>
</div>
<label
class="col-1 flex justify-center items-center text-bold"
v-if="commandCode !== 'C-PM-47'"
>/
</label>
<div class="col-4" v-if="commandCode === 'C-PM-47'">
<q-input
class="inputgreen"
outlined
dense
v-model="commandVolume"
:label="`${'เล่มที่'}`"
/>
</div>
<div class="col-4" v-if="commandCode === 'C-PM-47'">
<q-input
class="inputgreen"
outlined
dense
v-model="commandChapter"
:label="`${'ตอนที่'}`"
/>
</div>
<div :class="commandCode === 'C-PM-47' ? 'col-4' : 'col-5'">
<datepicker
menu-class-name="modalfix"
v-model="commandYear"
:locale="'th'"
autoApply
year-picker
:enableTimePicker="false"
class="inputgreen"
>
<template #year="{ year }">{{ year + 543 }}</template>
<template #year-overlay-value="{ value }">{{
parseInt(value + 543)
}}</template>
<template #trigger>
<q-input
dense
outlined
hide-bottom-space
:model-value="
commandYear == null ? null : commandYear + 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>
</div>
<div v-else class="row q-mt-sm">
<q-toolbar style="padding: 0">
<datepicker
menu-class-name="modalfix"
v-model="commandYear"
:locale="'th'"
autoApply
year-picker
:enableTimePicker="false"
class="inputgreen"
@update:model-value="getListCommandDraf()"
>
<template #year="{ year }">{{ year + 543 }}</template>
<template #year-overlay-value="{ value }">{{
parseInt(value + 543)
}}</template>
<template #trigger>
<q-input
dense
outlined
hide-bottom-space
:model-value="commandYear == null ? null : commandYear + 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>
<q-space />
<q-input
borderless
outlined
dense
v-model="filter"
placeholder="ค้นหา"
style="width: 200px; max-width: auto"
@keydown.enter.prevent="getListCommandDraf()"
>
<template v-slot:append>
<q-icon v-if="filter == ''" name="search" />
<q-icon
v-if="filter !== ''"
name="clear"
class="cursor-pointer"
@click="filter = ''"
/>
</template>
</q-input>
<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"
class="gt-xs q-ml-sm"
/>
</q-toolbar>
<div class="col-12">
<p-table
:columns="columns"
:rows="rows"
row-key="id"
:visible-columns="visibleColumns"
selection="single"
v-model:selected="selected"
:rows-per-page-options="[10, 25, 50, 100]"
v-model:pagination="pagination"
@request="onRequest"
>
<template v-slot:body="props">
<q-tr :props="props" class="cursor-pointer">
<q-td>
<q-checkbox
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 ?? "-" }}
</div>
</q-td>
</q-tr>
</template>
</p-table>
</div>
</div>
</q-card-section>
<q-separator />
<q-card-actions align="right">
<q-btn
v-if="isHold"
label="บันทึกและเลือกรายชื่อต่อ"
@click="() => onSubmit(false)"
:disable="
selectCreate == 'NEW' ? commandType == '' : selected.length == 0
"
color="blue"
>
<q-tooltip>นทกและเลอกรายชอต</q-tooltip>
</q-btn>
<q-btn
label="บันทึกและไปยังหน้าคำสั่ง"
@click="() => onSubmit(true)"
:disable="
selectCreate == 'NEW' ? commandType == '' : selected.length == 0
"
color="public"
>
<q-tooltip>นทกและไปยงหนาคำส</q-tooltip>
</q-btn>
</q-card-actions>
</q-card>
</q-dialog>
</template>
<style scoped></style>