hrms-mgt/src/modules/09_leave/stores/LeaveStore.ts
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 906b948ad8 ปรับแก้ ui form การลา
2023-11-10 09:50:14 +07:00

274 lines
9 KiB
TypeScript

import { defineStore } from "pinia";
import { ref, onMounted } from "vue";
import { useCounterMixin } from "@/stores/mixin";
/** importType*/
import type { DataOption } from "@/modules/09_leave/interface/index/Main";
import type { QTableProps } from "quasar";
import type { DataRows } from "@/modules/09_leave/interface/response/leave";
import type { ListsData } from "@/modules/09_leave/interface/request/leave";
const mixin = useCounterMixin();
const { date2Thai, showLoader, hideLoader } = mixin;
export const useLeavelistDataStore = defineStore("leave", () => {
/** TABMENU*/
const amounttab1 = ref<number>(0);
const amounttab2 = ref<number>(0);
/**ข้อมูลใน Table*/
const mainData = ref<any>([]);
const rows = ref<DataRows[]>([]);
const selectStatus = ref<string>("PENDING");
const columns = ref<QTableProps["columns"]>([]);
const visibleColumns = ref<string[]>([]);
const loadTable = ref<boolean>(false);
const leaveOp = [
{ id: "all", name: "ทั้งหมด" },
{ id: "leave1", name: "ลากิจส่วนตัว" },
{ id: "leave2", name: "ลาป่วย" },
];
const statusOp = [
{ id: "all", name: "ทั้งหมด" },
{ id: "NEW", name: "ใหม่" },
{ id: "PENDING", name: "อยู่ระหว่างดำเนินการ" },
{ id: "APPROVE", name: "อนุมัติ" },
{ id: "REJECT", name: "ไม่อนุมัติ" },
];
const leaveOps = ref<any>(leaveOp);
const statusOps = ref<any>(statusOp);
/**
* ฟังก์ชั่น fetchList
* @param data รับข้อมูลจาก Page
*/
async function fetchList(data: ListsData[]) {
let datalist = data.map((e: ListsData) => ({
id: e.id,
leaveType: e.leaveType,
name: e.name,
Date: date2Thai(e.Date),
status: e.status,
}));
console.log(datalist);
mainData.value = datalist;
const filteramounttab1 = datalist.filter((e) => e.status === "PENDING");
amounttab1.value = filteramounttab1.length;
amounttab2.value = datalist.length;
await searchDataFn(selectType.value, selectStatus.value);
}
/**ref ของการค้นหาข้อมูล */
const selectYear = ref<string>("all");
const selectType = ref<string>("all");
const optionYear = ref<DataOption[]>([{ id: "all", name: "ทั้งหมด" }]);
const optionType = ref<DataOption[]>([]);
const optionStatus = ref<DataOption[]>([]);
const optionTypeMain = ref<DataOption[]>([]);
const optionStatusMain = ref<DataOption[]>([]);
const filterTable = ref<string>("");
/**
* ฟังก์ชั่นค้นหาข้อมูลใน Table
* @param type รับข้อมูลประเภท
* @param status รับค่าสถานะ
*/
function searchDataFn(type: string, status: string) {
type = type || "all";
status = status || "all";
// showLoader()
loadTable.value = true;
if (selectYear.value == "all" && type == "all" && status == "all") {
console.log(1);
rows.value = mainData.value.map((e: any) => ({
id: e.id,
leaveType: convertLeave(e.leaveType),
name: e.name,
Date: date2Thai(e.Date),
status: convertSatatus(e.status),
}));
} else if (selectYear.value !== "all" && type == "all" && status == "all") {
console.log(2);
} else if (selectYear.value == "all" && type !== "all" && status == "all") {
console.log(3);
rows.value = mainData.value
.filter((e: any) => e.leaveType === type)
.map((e: any) => ({
id: e.id,
leaveType: convertLeave(e.leaveType),
name: e.name,
Date: date2Thai(e.Date),
status: convertSatatus(e.status), // แปลงค่า status เมื่อเป็น "PENDING"
}));
} else if (selectYear.value == "all" && type == "all" && status !== "all") {
console.log(4);
console.log(status);
rows.value = mainData.value
.filter((e: any) => e.status === status)
.map((e: any) => ({
id: e.id,
leaveType: convertLeave(e.leaveType),
name: e.name,
Date: date2Thai(e.Date),
status: convertSatatus(e.status), // แปลงค่า status เมื่อเป็น "PENDING"
}));
} else if (
selectYear.value !== "all" &&
type !== "all" &&
status == "all"
) {
console.log(5);
} else if (
selectYear.value !== "all" &&
type == "all" &&
status !== "all"
) {
console.log(6);
} else if (
selectYear.value == "all" &&
type !== "all" &&
status !== "all"
) {
console.log(7);
console.log(type);
rows.value = mainData.value
.filter((e: any) => e.leaveType === type && e.status === status)
.map((e: any) => ({
id: e.id,
leaveType: convertLeave(e.leaveType),
name: e.name,
Date: date2Thai(e.Date),
status: convertSatatus(e.status), // แปลงค่า status เมื่อเป็น "PENDING"
}));
} else console.log("ค้นหาจากทั้งหมด");
setTimeout(function () {
loadTable.value = false;
}, 500);
}
/**
* ฟังก์ชั่นเคลียร์ข้อมูลค้นหา
*/
function clearFilter() {
selectYear.value = "all";
selectType.value = "all";
selectStatus.value = "all";
filterTable.value = "";
}
/**
* ฟังก์ชั่นค้นหาข้อมูลของ Option Filter
* @param val คำที่ค้นหา
* @param update Function
* @param name ประเภทของ Select
*/
function filterOption(val: string, update: any, name: string) {
update(() => {
const needle = val.toLowerCase();
if (name === "type") {
leaveOps.value = leaveOp.filter(
(v: any) => v.name.toLowerCase().indexOf(needle) > -1
);
} else if (name === "status") {
statusOps.value = statusOp.filter(
(v: any) => v.name.toLowerCase().indexOf(needle) > -1
);
}
});
}
/**
* ฟังก์ชั่นแปลงสถานะ
* @param val ค่าสถานะ
*/
function convertSatatus(val: string) {
switch (val) {
case "NEW":
return "ใหม่";
case "PENDING":
return "อยู่ระหว่างดำเนินการ";
case "APPROVE":
return "อนุมัติ";
case "REJECT":
return "ไม่อนุมัติ";
}
}
/**
* ฟังก์ชั่นแปลงประเภทการลา
* @param val ค่าประเภทของกาลา
*/
function convertLeave(val: string) {
switch (val) {
case "leave1":
return "ลากิจส่วนตัว";
case "leave2":
return "ลาป่วย";
case "leave3":
return "ลาคลอดบุตร";
case "leave4":
return "ลาไปช่วยเหลือภริยาที่คลอดบุตร";
case "leave5":
return "ลาพักผ่อน";
case "leave6":
return "ลาอุปสมบท";
case "leave7":
return "ลาประกอบพิธีฮัจย์";
case "leave8":
return "ลาเข้ารับการตรวจเลือกหรือเข้ารับการเตรียมพล";
case "leave9":
return "ลาไปศึกษา";
case "leave10":
return "ลาไปฝึกอบรม ปฏิบัติการวิจัย หรือดูงาน";
case "leave11":
return "ลาไปปฏิบัติงานในองค์การระหว่างประเทศ";
case "leave12":
return "ลาติดตามคู่สมรส";
case "leave13":
return "ลาไปฟื้นฟูสมรรถภาพด้านอาชีพ";
}
}
/**
* ฟังชั่นแปลงค่าการลา
* @param val ค่าของการลา
*/
function convertLeaveDaytype(val: string) {
switch (val) {
case "allday":
return "ลาทั้งวัน";
case "halfmorning":
return "ลาครึ่งวันเช้า";
case "halfafternoon":
return "ลาครึ่งวันบ่าย";
}
}
return {
amounttab1,
amounttab2,
/**ข้อมูลใน Table */
rows,
fetchList,
loadTable,
columns,
visibleColumns,
/**ค้นหาข้อมูล */
filterTable,
selectYear,
selectType,
selectStatus,
optionYear,
optionType,
optionStatus,
clearFilter,
searchDataFn,
filterOption,
leaveOp,
statusOp,
leaveOps,
statusOps,
/** Function แปลงค่า */
convertLeave,
convertLeaveDaytype,
};
});