hrms-admin/src/modules/02_users/views/04_roleOrganization.vue

455 lines
16 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { ref, onMounted, reactive, watch } from "vue";
import { useQuasar } from "quasar";
import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import { useDataStoreUser } from "@/modules/02_users/stores/main";
import type { QTableProps } from "quasar";
import type {
DataTree,
DataProfile,
Pagination,
} from "@/modules/02_users/interface/index/Main";
import type { QueryProfile } from "@/modules/02_users/interface/request/Main";
import DialogAddPerson from "@/modules/02_users/components/RoleOrganization/DialogAddPerson.vue";
const $q = useQuasar();
const store = useDataStoreUser();
const { showLoader, hideLoader, messageError, success, dialogRemove } =
useCounterMixin();
/******* โครงสร้าง *******/
const filter = ref<string>(""); // ค้นหาข้อมูลรายการโครงสร้าง
const nodeTree = ref<DataTree[]>([]); // ข้อมูลรายการโครงสร้าง
const expanded = ref<Array<string>>([]); // เปิดรายการโครงสร้าง
const orgId = ref<string>(""); // id หน่วยงานที่เลือก
/**
* งกนดงขอมลโครงสราง
* เกบขอมลโครงสรางไวใน nodeTree
*/
async function fatchOrg() {
showLoader();
await http
.get(config.API.permissionOrg)
.then(async (res) => {
const data = await res.data.result;
nodeTree.value = data;
if (data.length === 1) {
selectedOrg(data[0].id);
} else {
await fetchListPerson(); // ดึงข้อมูลรายชื่อคนที่มีสิทธิ์จัดการโครงสร้าง
}
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* งกนเลอกหนวยงาน
* @param id หนวยงานทเลอก
* กำหนดคาของ qureyBody ใหเปนค defult และกำหนดคาของ qureyBody.id เปนหนวยงานทเลอก
* และดงขอมลรายชอคนททธดการโครงสรางในหนวยงานทเลอก
*/
function selectedOrg(id: string) {
orgId.value = id;
qureyBody.id = id;
qureyBody.searchKeyword = "";
qureyBody.searchField = "fullName";
qureyBody.page = 1;
fetchListPerson(true);
}
/******* รายชื่อคนที่มีสิทธิ์จัดการโครงสร้าง *******/
const qureyBody = reactive<QueryProfile>({
searchKeyword: "", // คำค้นหา
searchField: "fullName", // field ที่ต้องการค้นหา
page: 1, // หน้า
pageSize: 10, // จำนวนที่ต้องการ
id: null, // หน่วยงานที่เลือก
});
const rows = ref<DataProfile[]>([]); // ข้อมูลรายชื่อคนที่มีสิทธิ์จัดการโครงสร้าง
const total = ref<number>(0); // จำนวนข้อมูลทั้งหมด
const maxPage = ref<number>(0); // จำนวนหน้า
const columns = ref<QTableProps["columns"]>([
{
name: "fullName",
align: "left",
label: "ชื่อ-นามสกุล",
sortable: true,
field: (row) => `${row.prefix}${row.firstName} ${row.lastName}`,
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "org",
align: "left",
label: "หน่วยงาน",
sortable: true,
field: "org",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "posNo",
align: "left",
2024-09-13 15:22:31 +07:00
label: "เลขที่ตำแหน่ง",
sortable: true,
field: "posNo",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "position",
align: "left",
label: "ตำแหน่งในสายงาน",
sortable: true,
field: "position",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "posType",
align: "left",
label: "ประเภทตำแหน่ง",
sortable: true,
field: (row) =>
`${row.posType ? row.posType : "-"} ${
row.posLevel ? `(${row.posLevel})` : ``
} `,
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
]);
const modalAdd = ref<boolean>(false); // modal เพิ่มข้อมูลรายชื่อ
/**
* งกนดงขอมลรายชอคนททธดการโครงสราง
* @param newPage โหลดหนาแรก าเป true โหลดหนาแรก false ใหโหลดหนาปจจ
*
* เกบมลรายชอคนททธดการโครงสรางไวใน rows.value
*/
async function fetchListPerson(newPage: boolean = false) {
qureyBody.page = newPage ? 1 : qureyBody.page;
showLoader();
await http
.post(config.API.permissionOrgProfile, qureyBody)
.then(async (res) => {
const data = await res.data.result;
maxPage.value = Math.ceil(data.total / qureyBody.pageSize); // คำนวนหาจำนวนหน้า
total.value = data.total; // จำนวนรายชื่อคนที่มีสิทธิ์จัดการโครงสร้าง
rows.value = data.data;
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* งกนยนยนการลบรายชอคนททธดการโครงสราง
* @param id รายชอคนททธดการโครงสราง
* ลบเสรจจะโหลดขอมลรายชอคนททธดการโครงสราง
*/
function onDeletePerson(id: string) {
dialogRemove($q, async () => {
showLoader();
await http
.delete(config.API.permissionOrg + `/${id}`)
.then(async () => {
// ถ้า maxPage.value ไม่เท่ากับ 1 ให้ โหลดข้อมูลรายชื่อคนที่มีสิทธิ์จัดการโครงสร้างหน้าปัจจุบัน
if (maxPage.value !== 1) {
// ถ้ามีแค่หนึ่งแถวในหน้าปัจจุบัน ให้ลด page ลง 1
if (rows.value.length === 1) {
qureyBody.page = qureyBody.page - 1;
}
await fetchListPerson(false); // โหลดข้อมูลจากหน้าปัจจุบันโดยไม่รีเซ็ต
} else {
await fetchListPerson(true); // โหลดข้อมูลหน้าแรก
}
success($q, "ลบข้อมูสำเร็จ");
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
});
}
/**
* function updatePagination
* @param newPagination อม Pagination ใหม
*/
function updatePagination(newPagination: Pagination) {
qureyBody.pageSize = newPagination.rowsPerPage;
}
/**
* การเปลยนแปลงของ pageSize ใน queryBody
* เม pageSize การเปลยนแปลงใหโหลดขอมลหนาแรก
*/
watch(
() => qureyBody.pageSize,
() => {
fetchListPerson(true);
}
);
/**
* hook ทำงานเม Components กเรยกใชงาน
*/
onMounted(async () => {
await fatchOrg(); // ดึงข้อมูลโครงสร้าง
});
</script>
<template>
<div class="toptitle text-dark col-12 row items-center">
กำหนดสทธดการโครงสราง
</div>
<q-card style="height: 100%">
2024-09-11 09:57:39 +07:00
<q-card-section :horizontal="$q.screen.gt.sm">
<q-card-section class="col-lg-3 col-md-4 col-xs-12 q-gutter-sm">
<div>
<q-input dense outlined v-model="filter" label="ค้นหา">
<template v-slot:append>
<q-icon
v-if="filter !== ''"
name="clear"
class="cursor-pointer"
@click="filter = ''"
/>
2024-09-11 09:57:39 +07:00
<q-icon v-else name="search" color="grey-5" />
</template>
</q-input>
</div>
<div class="bg-white tree-container q-pa-xs">
<q-tree
class="q-pa-sm q-gutter-sm"
dense
:nodes="nodeTree"
2024-09-11 09:57:39 +07:00
node-key="orgRootName"
label-key="orgRootName"
:filter="filter"
no-results-label="ไม่พบข้อมูลที่ค้นหา"
no-nodes-label="ไม่มีข้อมูล"
v-model:expanded="expanded"
>
<template v-slot:default-header="prop">
<q-item
clickable
class="row col-12 items-center text-dark q-py-xs q-pl-sm rounded-borders my-list"
:active="orgId == prop.node.id"
active-class="my-list-link text-primary text-weight-medium"
@click.stop="selectedOrg(prop.node.id)"
>
<div>
2024-10-28 17:04:55 +07:00
<div
v-if="prop.node.isDeputy == true"
class="text-info text-weight-medium"
>
{{ prop.node.orgRootName }}
</div>
<div v-else class="text-weight-medium">
{{ prop.node.orgRootName }}
</div>
<div class="text-weight-light text-grey-8">
{{
prop.node.orgRootCode == null
? null
: prop.node.orgRootCode
}}
{{
prop.node.orgRootShortName == null
? null
: prop.node.orgRootShortName
}}
</div>
</div>
</q-item>
</template>
</q-tree>
</div>
</q-card-section>
2024-09-11 09:57:39 +07:00
<q-separator :vertical="$q.screen.gt.sm" />
<q-card-section
class="col-lg-9 col-md-8 col-xs-12 scroll"
style="height: 83vh"
>
<q-card bordered style="height: 100%; border: 1px solid #d6dee1">
<div class="col-12 text-weight-medium bg-grey-1 q-py-sm q-px-md">
รายชอคนททธดการโครงสราง
</div>
<div class="col-12"><q-separator /></div>
<q-card-section>
<div class="row col-12 q-col-md">
<div class="col-2">
<q-btn
v-if="orgId"
flat
dense
color="primary"
icon="add"
round
@click="modalAdd = true"
>
<q-tooltip>เพมขอม</q-tooltip>
</q-btn>
</div>
<div class="col-md-10">
<div class="row col-12 q-col-gutter-sm">
<div class="col-md-4">
<q-select
outlined
dense
v-model="qureyBody.searchField"
:options="store.searchFieldOption"
label="เลือกที่ต้องการค้นหา"
emit-value
map-options
option-label="name"
option-value="id"
/>
</div>
<div class="col-md-6">
<q-input
v-model="qureyBody.searchKeyword"
outlined
dense
lazy-rules
label="คำค้น"
hide-bottom-space
>
<template v-slot:append>
<q-icon
v-if="qureyBody.searchKeyword"
name="cancel"
@click="qureyBody.searchKeyword = ''"
class="cursor-pointer"
></q-icon>
</template>
</q-input>
</div>
<div class="row col-md-2">
<q-btn
color="primary"
icon="search"
label="ค้นหา"
class="full-width"
@click="fetchListPerson(true)"
/>
</div>
</div>
</div>
</div>
<div class="col-12 q-mt-sm">
<d-table
:columns="columns"
:rows="rows"
:paging="true"
row-key="id"
flat
bordered
dense
2024-09-06 16:32:19 +07:00
:rows-per-page-options="[10, 25, 50, 100]"
@update:pagination="updatePagination"
>
<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">
<q-td auto-width>
<q-btn
flat
dense
color="red"
icon="delete"
round
@click.pervent="onDeletePerson(props.row.id)"
>
<q-tooltip>ลบขอม</q-tooltip>
</q-btn>
</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>
<template v-slot:pagination="scope">
งหมด {{ total }} รายการ
<q-pagination
v-model="qureyBody.page"
active-color="primary"
color="dark"
:max="maxPage"
:max-pages="5"
size="sm"
boundary-links
direction-links
@update:model-value="fetchListPerson(false)"
>
</q-pagination>
</template>
</d-table>
</div>
</q-card-section>
</q-card>
</q-card-section>
</q-card-section>
</q-card>
<DialogAddPerson
v-model:modal="modalAdd"
v-model:org-id="orgId"
:fetch-data="fetchListPerson"
/>
</template>
<style scoped>
.tree-container {
overflow: auto;
2024-10-28 17:04:55 +07:00
height: 70vh;
border: 1px solid #e6e6e7;
border-radius: 10px;
}
.my-list-link {
color: rgb(118, 168, 222);
border-radius: 5px;
background: #a3d3fb48 !important;
font-weight: 600;
border: 1px solid rgba(175, 185, 196, 0.217);
}
</style>