hrms-mgt/src/modules/18_command/components/DialogCreateCommand.vue

557 lines
16 KiB
Vue
Raw Normal View History

2024-09-24 11:19:41 +07:00
<script setup lang="ts">
import { ref, watch } from "vue";
2024-09-24 11:19:41 +07:00
import { useQuasar } from "quasar";
import http from "@/plugins/http";
import config from "@/app.config";
2024-09-24 11:19:41 +07:00
import { useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";
import { useCommandMainStore } from "@/modules/18_command/store/Main";
import type { QTableProps } from "quasar";
import type { ResponseData } from "@/modules/05_placement/interface/response/Transfer";
import type {
DataCommandType,
DataListCommand,
} from "@/modules/18_command/interface/response/Main";
2024-09-24 11:19:41 +07:00
import DialogHeader from "@/components/DialogHeader.vue";
const storeCommand = useCommandMainStore();
const router = useRouter();
const $q = useQuasar();
const mixin = useCounterMixin();
const {
showLoader,
success,
messageError,
dialogConfirm,
hideLoader,
date2Thai,
} = mixin;
const modal = defineModel<boolean>("modal", { required: true });
const props = defineProps({
commandTypeId: String, // ไอดีประเภทคำสั่ง
personsId: Array, // ไอดีคนที่เลือกออกคำสั่งส่งมาเป็น array
});
const commandType = ref<string>(""); //ประเภทคำสั่ง
const commandOp = ref<DataCommandType[]>([]); //ตัวเลือกคำสั่ง
const listCommand = ref<DataCommandType[]>([]); //ตัวเลือกคำสั่ง
const commandNo = ref<string>("");
const commandYear = ref<number>(new Date().getFullYear());
const rows = ref<DataListCommand[]>([]); // รายการคำสั่ง
const selected = ref<any[]>([]); // id คำสั่งที่เลือก
2024-09-24 11:19:41 +07:00
const filter = ref<string>(""); // คำค้นหา
const total = ref<number>(0);
const totalList = ref<number>(1);
const pagination = ref({
sortBy: "createdAt",
descending: true,
page: 1,
rowsPerPage: 10,
});
const visibleColumns = ref<string[]>([
"commandNo",
"issue",
"commandExcecuteDate",
"commandAffectDate",
"createdFullName",
"assignFullName",
2024-09-24 11:19:41 +07:00
]);
const columns = ref<QTableProps["columns"]>([
2024-09-24 11:19:41 +07:00
{
name: "commandNo",
2024-09-24 11:19:41 +07:00
align: "left",
label: "เลขที่คำสั่ง",
2024-09-24 11:19:41 +07:00
sortable: false,
field: "commandNo",
format(val, row) {
return val ? `${val} / ${row.commandYear + 543}` : "-";
},
2024-09-24 11:19:41 +07:00
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "issue",
2024-09-24 11:19:41 +07:00
align: "left",
label: "ชื่อคำสั่ง",
2024-09-24 11:19:41 +07:00
sortable: true,
field: "issue",
2024-09-24 11:19:41 +07:00
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "commandExcecuteDate",
2024-09-24 11:19:41 +07:00
align: "left",
label: "วันที่ลงนาม",
sortable: false,
field: "commandExcecuteDate",
format(val) {
return val ? date2Thai(val) : "-";
},
2024-09-24 11:19:41 +07:00
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "commandAffectDate",
2024-09-24 11:19:41 +07:00
align: "left",
label: "วันที่คำสั่งมีผล",
sortable: false,
field: "commandAffectDate",
format(val) {
return val ? date2Thai(val) : "-";
},
2024-09-24 11:19:41 +07:00
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "createdFullName",
2024-09-24 11:19:41 +07:00
align: "left",
label: "ผู้สร้าง",
sortable: false,
field: "createdFullName",
2024-09-24 11:19:41 +07:00
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "assignFullName",
2024-09-24 11:19:41 +07:00
align: "left",
label: "ผู้ลงนาม",
sortable: false,
field: "assignFullName",
2024-09-24 11:19:41 +07:00
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
]);
/**
* งกนสรางคำสงใหม
*/
function createCommand() {
dialogConfirm($q, async () => {
// const data = props.personsId;
// const body = {
// commandYear: commandYear.value,
// commandNo: commandNo.value,
// commandTypeId: commandType.value,
// persons: data,
// };
// showLoader();
// await http
// .post(config.API.transferReport, body)
// .then(async (res) => {
// const id = await res.data.result;
// modal.value = false;
router.push("/command/edit/736c178f-9c11-44bc-b6e0-ef47ea06ffe9");
// router.push(`/command/edit/${id}`);
clearValue();
// })
// .catch((e) => {
// messageError($q, e);
// })
// .finally(() => {
// hideLoader();
// });
});
2024-09-24 11:19:41 +07:00
}
/**
* งกนเพมคนเขาไปในคำสงทเปนแบบราง
*/
function addPersonalToCommand() {
dialogConfirm($q, async () => {
const data = props.personsId;
const body = {
commandId: selected.value[0].id,
persons: data,
};
console.log("🚀 ~ dialogConfirm ~ body:", body);
// showLoader();
// await http
// .post(config.API.transferReport, body)
// .then((res) => {
// const id = res.data.result
modal.value = false;
router.push("/command/edit/18477dcd-2f14-4e49-8fca-a446164e8b59");
// router.push("/command/edit/" + id);
// })
// .catch((e) => {
// messageError($q, e);
// })
// .finally(() => {
// hideLoader();
// });
});
2024-09-24 11:19:41 +07:00
}
function onSubmit() {
if (selectCreate.value === "NEW") {
2024-09-24 11:19:41 +07:00
createCommand();
} else {
addPersonalToCommand();
}
}
function filterSelector(val: string, update: Function, refData: string) {
switch (refData) {
case "OrderTypeOption":
update(() => {
commandType.value = val ? "" : commandType.value;
commandOp.value = listCommand.value.filter(
(v: any) => v.name.indexOf(val) > -1
);
});
break;
default:
break;
}
}
/**
* เปลยน tab เเละ clear tab อนหน
* @param type แยกประเภท
*/
function changeTab(type: string) {
clearValue();
selectCreate.value = type;
if (type == "DRAF") {
getListCommandDraf();
2024-09-24 11:19:41 +07:00
}
}
/** ปิด popup */
2024-09-24 11:19:41 +07:00
function closeModal() {
modal.value = false;
clearValue();
}
function clearValue() {
commandType.value = "";
commandNo.value = "";
commandYear.value = new Date().getFullYear();
selectCreate.value = "NEW";
selected.value = []
filter.value = ''
}
const selectCreate = ref<string | null>("NEW");
async function getListCommandDraf() {
showLoader();
await http
.get(
config.API.commandList +
`?page=${pagination.value.page}&pageSize=${pagination.value.rowsPerPage}&year=${commandYear.value}&keyword=${filter.value}&status=DRAFT`
)
.then(async (res) => {
totalList.value = Math.ceil(
res.data.result.total / pagination.value.rowsPerPage
);
total.value = res.data.result.total;
rows.value = res.data.result.data;
console.log("🚀 ~ .then ~ rows.value:", rows.value);
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/** ดึงข้อมูลคำสั่ง */
async function fetchCommandType() {
showLoader();
await http
.get(config.API.commandType)
.then(async (res) => {
const data = await res.data.result;
listCommand.value = data;
commandOp.value = data;
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
2024-09-24 11:19:41 +07:00
}
function updatePagination(newPagination: any) {
pagination.value.page = 1;
pagination.value.rowsPerPage = newPagination.rowsPerPage;
}
watch(
() => pagination.value.rowsPerPage,
async () => {
await getListCommandDraf();
}
);
watch(modal, () => {
if (modal.value && props.personsId?.length !== 0) {
fetchCommandType();
}
});
2024-09-24 11:19:41 +07:00
</script>
<template>
<q-dialog v-model="modal">
<q-card
:style="
selectCreate == 'NEW'
? 'width: 500px;max-width: 80vw;'
: 'width: 1200px;max-width: 80vw;'
"
>
2024-09-24 11:19:41 +07:00
<DialogHeader :tittle="'สร้าง/เลือกคำสั่ง'" :close="closeModal" />
<q-separator />
<q-card-section class="q-pa-sm">
2024-09-24 11:19:41 +07:00
<div class="row q-mb-md">
<div class="col-12 q-gutter-md">
<q-btn
outline
label="สร้างคำสั่งใหม่"
:color="selectCreate == 'NEW' ? 'primary' : 'grey'"
@click="changeTab('NEW')"
2024-09-24 11:19:41 +07:00
/>
<q-btn
outline
label="เลือกคำสั่งที่เป็นแบบร่าง"
:color="selectCreate == 'DRAF' ? 'primary' : 'grey'"
@click="changeTab('DRAF')"
2024-09-24 11:19:41 +07:00
/>
</div>
</div>
<q-separator />
<div v-if="selectCreate == 'NEW'" class="row q-mt-sm q-col-gutter-sm">
<div class="col-12">
<q-select
class="inputgreen"
v-model="commandType"
:label="`${'ประเภทคำสั่ง'}`"
dense
emit-value
map-options
option-label="name"
:options="commandOp"
option-value="id"
lazy-rules
use-input
hide-bottom-space
outlined
:rules="[(val:string) => !!val || `${'กรุณาเลือกประเภทคำสั่ง'}`]"
@filter="(inputValue:any,
doneFn:Function) => filterSelector(inputValue, doneFn,'OrderTypeOption'
) "
>
<template v-slot:no-option>
<q-item>
<q-item-section class="text-grey">
ไมอม
</q-item-section>
</q-item>
</template>
</q-select>
</div>
<div class="col-6">
<q-input
class="inputgreen"
outlined
dense
v-model="commandNo"
:label="`${'คำสั่งเลขที่'}`"
/>
</div>
<label class="col-1 flex justify-center items-center text-bold"
>/</label
>
2024-09-24 11:19:41 +07:00
<div class="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>
2024-09-24 11:19:41 +07:00
</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
2024-09-24 11:19:41 +07:00
dense
outlined
hide-bottom-space
:model-value="commandYear == null ? null : commandYear + 543"
:label="`${'พ.ศ.'}`"
:rules="[(val:string) => !!val || `${'กรุณากรอก พ.ศ.'}`]"
2024-09-24 11:19:41 +07:00
>
<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
debounce="300"
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"
options-cover
style="min-width: 150px"
class="gt-xs q-ml-sm"
/>
</q-toolbar>
<div class="col-12">
<d-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]"
@update:pagination="updatePagination"
>
<template v-slot:pagination="scope">
งหมด {{ total }} รายการ
<q-pagination
v-model="pagination.page"
active-color="primary"
color="dark"
:max="Number(totalList)"
size="sm"
boundary-links
direction-links
:max-pages="5"
@update:model-value="getListCommandDraf"
></q-pagination>
</template>
<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>
</d-table>
</div>
</div>
2024-09-24 11:19:41 +07:00
</q-card-section>
<q-separator />
<q-card-actions align="right">
<q-btn
label="บันทึก"
@click="onSubmit"
:disable="selected.length == 0 && commandType == ''"
2024-09-24 11:19:41 +07:00
color="public"
><q-tooltip>นท</q-tooltip></q-btn
>
</q-card-actions>
</q-card>
</q-dialog>
</template>
<style scoped></style>