hrms-admin/src/modules/01_metadata/views/05_insignia.vue
2024-09-09 13:18:50 +07:00

350 lines
10 KiB
Vue

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useQuasar } from "quasar";
import { useRouter } from "vue-router";
import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import { useInsigniaDataStore } from "@/modules/01_metadata/stores/InsigniaStore";
import type { QTableProps } from "quasar";
import dialogHeader from "@/components/DialogHeader.vue";
const $q = useQuasar();
const router = useRouter();
const store = useInsigniaDataStore();
const { dialogConfirm, success, messageError, showLoader, hideLoader } =
useCounterMixin();
// Table
const filterKeyword = ref<string>("");
const columns = ref<QTableProps["columns"]>([
{
name: "name",
align: "left",
label: "ลำดับชั้นเครื่องราชฯ",
sortable: true,
field: "name",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
},
{
name: "createdAt",
align: "center",
label: "วันที่สร้าง",
sortable: true,
field: "createdAt",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
},
{
name: "lastUpdatedAt",
align: "center",
label: "วันที่แก้ไข",
sortable: true,
field: "lastUpdatedAt",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
},
{
name: "lastUpdateFullName",
align: "left",
label: "ผู้ดำเนินการ",
sortable: true,
field: "lastUpdateFullName",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
},
{
name: "isActive",
align: "left",
label: "สถานะ",
sortable: true,
field: "isActive",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
},
]);
const visibleColumns = ref<string[]>([
"name",
"createdAt",
"lastUpdatedAt",
"lastUpdateFullName",
"isActive",
]);
const dialog = ref<boolean>(false); // popup เพิ่ม แก้ไข
const dialogStatus = ref<string>(""); // สถานะการแก้ไข
const name = ref<string>(""); // ลำดับชั้นเครื่องราชฯ
const isActive = ref<boolean>(false); // สถานะการใช้งาน
const editId = ref<string>(""); // id รายการที่จะแก้ไข
/**
* ดึงข้อมูลเครื่องราชอิสริยาภรณ์
*
* และบันทึกใน store.fetchData
*/
async function fetchData() {
showLoader();
await http
.get(config.API.insigniaTypeOrg)
.then(async (res) => {
await store.fetchData(res.data.result);
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* ยืนยันการบันทึกข้อมูลเครื่องราชอิสริยาภรณ์
*
* dialogStatus.value เป็น "create" จะเพิ่มข้อมูล ถ้าไม่จะเป็นการแก้ไขข้อมูล
*/
function onSubmit() {
dialogConfirm($q, async () => {
showLoader();
// กำหนด Path API
const path =
dialogStatus.value === "create"
? config.API.insigniaTypeOrg
: config.API.insigniaTypeNewIdOrg(editId.value);
// กำหนด method
const method = dialogStatus.value === "create" ? "post" : "put";
// call API
await http[method](path, { name: name.value, isActive: isActive.value })
.then(async () => {
await fetchData();
success($q, "บันทึกข้อมูลสำเร็จ");
closeDialog();
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
});
}
/**
* ปืด popup เพิ่ม,แก้ไข ข้อมูล
*
* และ กำหนดลำดับชั้นเครื่องราช เป็นค่าว่าง สถานะการใช้งาน เป็น false
*/
function closeDialog() {
dialog.value = false;
name.value = "";
isActive.value = false;
}
/**
* ไปยังหน้ารายการข้อมูลเครื่องราชอิสริยาภรณ์
* @param id ลำดับชั้นเครื่องราชอิสริยาภรณ์
*/
function onclickDetail(id: string) {
router.push(`/master-data/insignia/detail/${id}`);
}
/**
* hook จำทำเมื่อมีการเรียกใช้ components
*
* ทำการดึงข้อมูลเครื่องราชอิสริยาภรณ์
*/
onMounted(async () => {
await fetchData();
});
</script>
<template>
<div class="toptitle text-dark col-12 row items-center">
อมลเครองราชอสรยาภรณ
</div>
<q-card flat bordered>
<q-card-section>
<q-toolbar style="padding: 0">
<q-btn
flat
round
color="primary"
icon="add"
@click.stop="
() => {
dialogStatus = 'create';
dialog = true;
}
"
>
<q-tooltip> เพิ่มข้อมูล </q-tooltip>
</q-btn>
<q-space />
<div class="row q-gutter-sm">
<q-input
outlined
dense
v-model="filterKeyword"
label="นหา"
></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"
/>
</div>
</q-toolbar>
<div class="col-12">
<d-table
ref="table"
:columns="columns"
:rows="store.row"
:filter="filterKeyword"
row-key="name"
flat
bordered
:paging="true"
dense
class="custom-header-table"
:visible-columns="visibleColumns"
>
<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"
class="cursor-pointer"
@click.stop="onclickDetail(props.row.id)"
>
<q-td auto-width>
<q-btn
color="edit"
flat
dense
round
class="q-mr-xs"
icon="edit"
clickable
@click.stop="
() => {
dialogStatus = 'edit';
dialog = true;
editId = props.row.id;
name = props.row.name;
isActive = props.row.isActive;
}
"
>
<q-tooltip>แก้ไขข้อมูล</q-tooltip>
</q-btn>
</q-td>
<q-td v-for="col in props.cols" :key="col.id">
<div v-if="col.name == 'isActive'">
<q-icon
v-if="col.value == false"
name="mdi-close"
color="red"
class="text-h5"
/>
<q-icon
v-else
name="mdi-check"
color="positive"
class="text-h5"
/>
</div>
<div v-else>
{{ col.value }}
</div>
</q-td>
</q-tr>
</template>
</d-table>
</div>
</q-card-section>
</q-card>
<q-dialog v-model="dialog" class="dialog" persistent>
<q-card style="max-width: 350px">
<q-form greedy @submit.prevent @validation-success="onSubmit">
<dialog-header
:tittle="dialogStatus === 'edit' ? 'แก้ไขข้อมูล' : 'เพิ่มข้อมูล'"
:close="closeDialog"
/>
<q-separator />
<q-card-section>
<div class="row q-col-gutter-sm">
<div class="col-12">
<q-input
ref="nameRef"
outlined
v-model="name"
label="ลำดบชนเครองราชฯ"
dense
lazy-rules
borderless
:rules="[
(val:string) => val.length > 0 || 'กรุณากรอกลำดับชั้นเครื่องราชฯ',
]"
hide-bottom-space
class="inputgreen"
/>
</div>
<div class="col-12">
<div class="row items-center justify-between">
<p class="q-ma-none">สถานะการใช้งาน</p>
<label>
<q-checkbox v-model="isActive" color="primary" />
<span class="control"></span>
</label>
</div>
</div>
</div>
</q-card-section>
<q-separator />
<q-card-actions align="right">
<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 scoped></style>