hrms-admin/src/modules/02_users/views/01_user.vue
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 15d05597b4 fix select
2024-11-29 10:20:25 +07:00

512 lines
15 KiB
Vue

<script setup lang="ts">
import { ref, onMounted, watch } 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";
/** importType*/
import type { QTableProps } from "quasar";
import type {
DataOption,
ItemsMenu,
Pagination,
} from "@/modules/02_users/interface/index/Main";
import type { Users, Roles } from "@/modules/02_users/interface/response/Main";
/** importComponents*/
import DialogAddUser from "@/modules/02_users/components/Users/DialogFormUser.vue";
/** use*/
const $q = useQuasar();
const router = useRouter();
const {
dialogRemove,
messageError,
showLoader,
hideLoader,
success,
dialogConfirm,
} = useCounterMixin();
/** Table*/
const keyword = ref<string>(""); // คำค้นหา
const rows = ref<Users[]>([]); // ข้อมูลรายาการผู้ใช้งาน
const currentPage = ref<number>(1); // หน้า
const total = ref<number>(0); // จำนวนรายการ
const maxPage = ref<number>(0); // จำนวนหน้า
const pageSize = ref<number>(10); // จำนวนข้อมูลต่อหน้า
const employeeClass = ref<string>("officer");
const columns = ref<QTableProps["columns"]>([
{
name: "no",
align: "left",
label: "ลำดับ",
sortable: true,
field: (row) =>
(currentPage.value - 1) * pageSize.value + rows.value.indexOf(row) + 1,
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "username",
align: "left",
label: "ชื่อผู้ใช้งาน",
sortable: true,
field: "username",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "email",
align: "left",
label: "อีเมล",
sortable: true,
field: "email",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "firstname",
align: "left",
label: "ชื่อ",
sortable: true,
field: "firstname",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "lastname",
align: "left",
label: "นามสกุล",
sortable: true,
field: "lastname",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "role",
align: "left",
label: "สิทธิ์",
sortable: true,
headerStyle: "font-size: 14px",
style: "font-size: 14px",
field: (row) => {
const names = row.roles.map((role: Roles) => role.name);
return names.join(", ");
},
},
{
name: "enabled",
align: "left",
label: "สถานะการใช้งาน",
sortable: true,
field: "enabled",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
]);
const visibleColumns = ref<string[]>([
"no",
"username",
"email",
"firstname",
"lastname",
"role",
"enabled",
]);
/** เพิ่มข้อมูลผู้ใช้งาน*/
const modalDialogAdd = ref<boolean>(false); // เปิด ปิด popup
const isStatusEdit = ref<boolean>(false); // สถานะการแก้ไข
const userId = ref<string>(""); // id ผู้ใช้งานที่ต้องการแก้ไข
// รายการตัวเลือก
const itemMenu = ref<ItemsMenu[]>([
{
label: "ระงับการใช้งาน",
icon: "mdi-lock-outline",
color: "orange-10",
type: "close",
},
{
label: "เปิดใช้งาน",
icon: "mdi-lock-open-outline",
color: "primary",
type: "open",
},
{
label: "จัดการสิทธิ์",
icon: "mdi-account-group",
color: "blue-9",
type: "managementRole",
},
{
label: "ลบ",
icon: "delete",
color: "red",
type: "delete",
},
]);
const employeeClassMain = ref<DataOption[]>([
{ id: "officer", name: "ข้าราชการ กทม.สามัญ" },
{ id: "employee", name: "ลูกจ้างประจำ" },
]);
const employeeClassOption = ref<DataOption[]>(employeeClassMain.value);
/**
* ฟังก์ชันดึงข้อมูลรายชื่อผู้ใช้งาน
*/
async function fetchListUsers() {
showLoader();
await http
.get(
config.API.managementUser +
`?pageSize=${pageSize.value}&page=${currentPage.value}&keyword=${keyword.value}&type=${employeeClass.value}`
)
.then(async (res) => {
const data = await res.data.result;
total.value = data.total;
maxPage.value = Math.ceil(total.value / pageSize.value);
rows.value = data.data.map((e: Users) => ({
...e,
roles: Array.isArray(e.roles)
? e.roles.filter(
(e: Roles) =>
e.name === "STAFF" ||
e.name === "SUPER_ADMIN" ||
e.name === "ADMIN" ||
e.name === "USER"
)
: [],
}));
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* ฟังก์ชันจัดการการกระทำต่าง ๆ กับข้อมูลผู้ใช้งาน
* @param type ประเภทตัวเลือก
* @param data ข้อมูลรายการผู้ใช้งาน
*/
function onClickAction(type: string, data: Users) {
// ตรวจสอบประเภทของการกระทำ
if (type === "edit") {
// แก้ไข
modalDialogAdd.value = true;
isStatusEdit.value = true;
userId.value = data.id;
} else if (type === "delete") {
// เรียกใช้ฟังก์ชันสำหรับลบข้อมูลผู้ใช้งาน
onDeleteUser(data.id);
} else if (type === "managementRole") {
// ไปยังหน้าจัดการสิทธิ์ของผู้ใช้งาน
data && router.push(`/users/roles/${data.id}`);
} else if (type === "open" || type === "close") {
// เปลี่ยนสถานะเปิด/ปิดของผู้ใช้งาน
const status = type === "open" ? true : false;
onLockUser(data.id, status);
}
}
/**
* ฟังก์ชันเปิด popup สำหรับเพิ่มข้อมูลผู้ใช้งาน
*
* กำหนดสถานะการแก้ไขเป็น false และ id ผู้ใช้งานที่ต้องการแก้ไขเป็นค่าว่าง
*/
function openDialog() {
modalDialogAdd.value = true;
isStatusEdit.value = false;
userId.value = "";
}
/**
* function ยืนยันการลบข้อมูลรายการผู้ใช้งาน
* @param id รายการผู้ใช้งาน
*
* ลบข้อมูลรายชื่อเสร็จแล้วทำการดึงข้อมูลรายชื่อผู้ใช้งานใหม่
*/
function onDeleteUser(id: string) {
dialogRemove($q, () => {
showLoader();
http
.delete(config.API.managementUser + `/${id}`)
.then(async () => {
await fetchListUsers();
success($q, "ลบข้อมูลสำเร็จ");
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
});
}
/**
* ฟังก์ชันระงับการใช้งานผู้ใช้งาน
* @param id ผู้ใช้งาน
* @param type เปิดใช้งาน,ระงับการใช้งาน
*
* เสร็จแล้วทำการดึงข้อมูลรายชื่อผู้ใช้งานใหม่
*/
function onLockUser(id: string, type: boolean) {
dialogConfirm(
$q,
() => {
showLoader();
http
.put(config.API.managementUser + `/${id}/enableStatus/${type}`)
.then(async () => {
await fetchListUsers();
success($q, type ? "เปิดใช้งานสำเร็จ" : "ระงับการใช้งานสำเร็จ");
})
.catch((err) => {
messageError($q, err);
hideLoader();
});
},
type ? "ยืนยันการเปิดใช้" : "ยืนยันการระงับใช้งาน",
type
? "ต้องการยืนยันการเปิดใช้นี้หรือไม่ ?"
: "ต้องการยืนยันระงับการใช้งานนี้หรือไม่ ?"
);
}
/**
* function อัปเดท paging
* @param initialPagination ข้อมูล pagination
*
* กำหนดหน้าไปยังหน้าแรก
*/
function updatePagination(initialPagination: Pagination) {
currentPage.value = 1;
pageSize.value = initialPagination.rowsPerPage;
}
/**
* function ค้นหาข้อมูล Option
* @param val คำค้นหา
* @param update function
* @param type ประเภท option
*/
function filterFnOptions(val: string, update: Function, type: string) {
switch (type) {
case "employeeClass":
update(() => {
employeeClassOption.value = employeeClassMain.value.filter(
(v: DataOption) => v.name.indexOf(val) > -1
);
});
break;
}
}
/**
* ดูการเปลี่ยนแปลงของจำนวนข้อมูลต่อหน้า
*
* ดึงข้อมูลรายชื่อผู้ใช้งานตามจำนวนข้อมูลต่อหน้า
*/
watch(
() => pageSize.value,
() => {
fetchListUsers();
}
);
/**
* hook ทำงานเมื่อมีการเรียกใช้งาน Components
*
* ดึงข้อมูลรายชื่อผู้ใช้งาน
*/
onMounted(async () => {
await fetchListUsers();
});
</script>
<template>
<div class="row items-center">
<div class="toptitle text-dark row items-center q-py-xs">
ดการผใชงาน
</div>
</div>
<q-card flast bordered class="q-pa-md">
<div class="items-center col-12 row q-col-gutter-sm">
<q-select
outlined
dense
v-model="employeeClass"
:options="employeeClassOption"
label="สถานภาพ"
emit-value
map-options
option-label="name"
option-value="id"
use-input
hide-selected
fill-input
style="width: 300px"
@update:model-value="(currentPage = 1), fetchListUsers()"
@filter="(inputValue: string,
doneFn: Function) => filterFnOptions(inputValue, doneFn,'employeeClass')"
><template v-slot:no-option>
<q-item>
<q-item-section class="text-grey"> ไม่มีข้อมูล </q-item-section>
</q-item>
</template>
</q-select>
<q-btn
class="q-ml-sm"
flat
round
dense
color="primary"
icon="add"
@click="openDialog()"
>
<q-tooltip>เพิ่มผู้ใช้งาน </q-tooltip>
</q-btn>
<q-space />
<q-input
borderless
dense
outlined
v-model="keyword"
clearable
@clear="keyword = ''"
placeholder="ค้นหา"
@keydown.enter.prevent="(currentPage = 1), fetchListUsers()"
>
<template v-slot:append v-if="keyword === ''">
<q-icon name="search" />
</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"
/>
</div>
<div class="col-12 q-pt-sm">
<d-table
ref="table"
:columns="columns"
:rows="rows"
row-key="id"
flat
bordered
:paging="true"
dense
:rows-per-page-options="[10, 25, 50, 100]"
:visible-columns="visibleColumns"
@update:pagination="updatePagination"
>
<template v-slot:header="props">
<q-tr :props="props">
<q-th v-for="col in props.cols" :key="col.name" :props="props">
<span class="text-weight-medium">{{ col.label }}</span>
</q-th>
<q-th auto-width></q-th>
</q-tr>
</template>
<template v-slot:body="props">
<q-tr :props="props" class="cursor-pointer">
<q-td v-for="col in props.cols" :key="col.name" :props="props">
<div v-if="col.name === 'enabled'">
<q-icon
:name="col.value ? 'check' : 'close'"
:color="col.value ? 'primary' : 'red'"
size="24px"
/>
</div>
<div v-else>
{{ col.value ? col.value : "-" }}
</div>
</q-td>
<q-td>
<q-btn
flat
dense
icon="mdi-dots-vertical"
class="q-pa-none q-ml-xs"
color="grey-13"
size="12px"
>
<q-menu>
<q-list dense style="min-width: 200px">
<q-item
v-for="(item, index) in props.row.enabled
? itemMenu.filter((e) => e.type !== 'open')
: itemMenu.filter((e) => e.type !== 'close')"
:key="index"
clickable
v-close-popup
@click.stop="onClickAction(item.type, props.row)"
>
<q-item-section>
<div class="row items-center">
<q-icon
:color="item.color"
size="17px"
:name="item.icon"
/>
<div class="q-pl-md">{{ item.label }}</div>
</div>
</q-item-section>
</q-item>
</q-list>
</q-menu>
</q-btn>
</q-td>
</q-tr>
</template>
<template v-slot:pagination="scope">
ทั้งหมด {{ total }} รายการ
<q-pagination
v-model="currentPage"
active-color="primary"
color="dark"
:max="Number(maxPage)"
size="sm"
boundary-links
direction-links
:max-pages="5"
@update:model-value="fetchListUsers"
></q-pagination>
</template>
</d-table>
</div>
</q-card>
<DialogAddUser
:fetchlist="fetchListUsers"
v-model:modal="modalDialogAdd"
v-model:is-status-edit="isStatusEdit"
:user-id="userId"
:employee-class="employeeClass"
/>
</template>
<style scoped></style>