hrms-admin/src/modules/02_users/components/RoleOrganization/DialogAddPerson.vue

332 lines
9.9 KiB
Vue

<script setup lang="ts">
import { reactive, ref, watch, onMounted } from "vue";
import { useQuasar } from "quasar";
import { storeToRefs } from "pinia";
import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import { useDataStoreUser } from "@/modules/02_users/stores/main";
import { usekeycloakPosition } from "@/stores/keycloakPosition";
import { tokenParsed } from "@/plugins/auth";
import type { QTableProps } from "quasar";
import type {
DataProfile,
Pagination,
} from "@/modules/02_users/interface/index/Main";
import DialogHeader from "@/components/DialogHeader.vue";
const $q = useQuasar();
const store = useDataStoreUser();
const { dataPosition } = storeToRefs(usekeycloakPosition());
const { showLoader, hideLoader, messageError, success, dialogConfirm } =
useCounterMixin();
const modal = defineModel<boolean>("modal", { required: true });
const orgId = defineModel<string>("orgId", { required: true });
const props = defineProps({
fetchData: {
type: Function,
required: true,
},
});
const qurey = reactive({
searchKeyword: "", // คำค้นหา
searchField: "fullName", // field ที่ต้องการค้นหา
page: 1, // หน้า
pageSize: 10, // จำนวนที่ต้องการ
});
const selected = ref<DataProfile[]>([]); //รายชื่อที่เลือก
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: "posNo",
align: "left",
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",
},
]);
/**
* ฟังก์ชันดึงข้อมูลรายชื่อ
* @param newPage โหลดหน้าแรก ถ้าเป็บ true โหลดหน้าแรก false ให้โหลดหน้าปัจจุบัน
*
* เก็บมูลรายชื่อไว่ใน rows.value
*/
async function onSearchListPerson(newPage: boolean = false) {
qurey.page = newPage ? 1 : qurey.page;
selected.value = [];
showLoader();
await http
.get(config.API.permissionOrgProfile, {
params: qurey,
})
.then((res) => {
const data = res.data.result;
maxPage.value = Math.ceil(data.total / qurey.pageSize);
total.value = data.total;
rows.value = data.data;
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* ฟังก์ชันยืนยันการเพิ่มราชื่อ
*
* เมื่อเพิ่มเสร็จจะดึงข้อมูลรายชื่อคนที่มีสิทธิ์จัดการโครงสร้างตามหน่วยงาน
*/
function onSubmitPerson() {
const arrayId = selected.value.map((e: DataProfile) => e.id);
dialogConfirm(
$q,
async () => {
showLoader();
const person = selected.value[0];
const body = {
nodeId: orgId.value,
personId: arrayId,
};
await http
.post(config.API.permissionOrg, body)
.then(async () => {
await props.fetchData?.(false);
success($q, "เพิ่มรายชื่อสำเร็จ");
onClose();
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
},
"ยืนยันการเพิ่มรายชื่อ",
"ต้องการยืนยันการเพิ่มรายชื่อนี้หรือไม่ ?"
);
}
/**
* function updatePagination
* @param newPagination ข้อมูล Pagination ใหม่
*/
function updatePagination(newPagination: Pagination) {
qurey.pageSize = newPagination.rowsPerPage;
}
/**
* ฟังก์ชันปิด popup ราชชื่อ
*
* และกำหนดค่าของ qurey ไปเป็นค่า defult rows.value และ selected.value ไปเป็นค่าว่าง
*/
function onClose() {
modal.value = false;
qurey.page = 1;
qurey.pageSize = 10;
qurey.searchField = "fullName";
qurey.searchKeyword = "";
rows.value = [];
selected.value = [];
}
const tokenParsedData = ref<string[]>([]);
/**
* ซ่อน checkbox ของผู้ใช้งานที่กำลังล็อกอินอยู่
* @param id รหัสผู้ใช้งาน
*/
function hideCheckbox(id: string) {
if (
dataPosition.value?.profileId === id &&
tokenParsedData.value.includes("ADMIN") &&
!tokenParsedData.value.includes("SUPER_ADMIN")
) {
return false;
} else {
return true;
}
}
/**
* ดูการเปลี่ยนแปลงของ pageSize ใน queryBody
*
* เมื่อ pageSize มีการเปลี่ยนแปลงให้โหลดข้อมูลหน้าแรก
*/
watch(
() => qurey.pageSize,
() => {
onSearchListPerson(true);
}
);
onMounted(async () => {
const token = await tokenParsed();
tokenParsedData.value = token?.role || [];
});
</script>
<template>
<q-dialog v-model="modal" persistent>
<q-card style="min-width: 60%">
<DialogHeader :tittle="'รายชื่อ'" :close="onClose" />
<q-separator />
<q-card-section style="max-height: 50vh" class="scroll">
<div class="col-12">
<q-toolbar style="padding: 0">
<q-select
outlined
dense
v-model="qurey.searchField"
:options="store.searchFieldOption"
label="ค้นหาจาก"
emit-value
map-options
option-label="name"
option-value="id"
style="width: 250px"
/>
<q-toolbar-title>
<q-input
outlined
dense
v-model="qurey.searchKeyword"
label="คำค้น"
@keydown.enter.prevent="onSearchListPerson(true)"
>
<!-- <template v-slot:append>
<q-icon name="search" />
</template> -->
</q-input>
</q-toolbar-title>
<q-btn
outline
icon="search"
class="full-height"
label="ค้นหา"
color="primary"
@click="onSearchListPerson(true)"
/>
</q-toolbar>
</div>
<div class="col-12">
<d-table
:columns="columns"
:rows="rows"
:paging="true"
row-key="id"
flat
bordered
dense
:rows-per-page-options="[10, 25, 50, 100]"
selection="multiple"
v-model:selected="selected"
@update:pagination="updatePagination"
>
<template v-slot:header="props">
<q-tr :props="props">
<q-th auto-width>
<q-checkbox
keep-color
color="primary"
dense
v-model="props.selected"
/>
</q-th>
<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">
<td auto-width>
<q-checkbox
v-if="hideCheckbox(props.row.id)"
keep-color
color="primary"
dense
v-model="props.selected"
/>
</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="qurey.page"
active-color="primary"
color="dark"
:max="maxPage"
:max-pages="5"
size="sm"
boundary-links
direction-links
@update:model-value="onSearchListPerson(false)"
></q-pagination>
</template>
</d-table>
</div>
</q-card-section>
<q-separator />
<q-card-actions align="right">
<q-btn
label="เพิ่ม"
color="public"
:disable="selected.length === 0"
@click="onSubmitPerson"
/>
</q-card-actions>
</q-card>
</q-dialog>
</template>
<style scoped></style>