hrms-mgt/src/modules/09_leave/stores/LeaveStore.ts

226 lines
7.1 KiB
TypeScript
Raw Normal View History

2023-10-06 13:32:54 +07:00
import { defineStore } from "pinia";
2023-11-08 18:02:52 +07:00
import { ref, onMounted } from "vue";
2023-10-06 13:32:54 +07:00
import { useCounterMixin } from "@/stores/mixin";
import type { DataOption } from "@/modules/09_leave/interface/index/Main";
2023-10-20 16:57:32 +07:00
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";
2023-10-06 13:32:54 +07:00
const mixin = useCounterMixin();
const { date2Thai, showLoader, hideLoader } = mixin;
export const useLeavelistDataStore = defineStore("leave", () => {
//TABMENU
const amounttab1 = ref<number>(0);
const amounttab2 = ref<number>(0);
//ข้อมูลในตาราง
const mainData = ref<any>([]);
const rows = ref<DataRows[]>([]);
2023-11-06 12:12:54 +07:00
const selectStatus = ref<string>("PENDING");
const columns = ref<QTableProps["columns"]>([]);
const visibleColumns = ref<string[]>([]);
const loadTable = ref<boolean>(false);
2023-11-06 12:12:54 +07:00
const leaveOp = [
2023-11-08 18:02:52 +07:00
{ id: "all", name: "ทั้งหมด" },
{ id: "leave1", name: "ลากิจส่วนตัว" },
{ id: "leave2", name: "ลาป่วย" },
];
2023-11-06 12:12:54 +07:00
const statusOp = [
2023-11-08 18:02:52 +07:00
{ 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);
async function fetchList(data: ListsData[]) {
let datalist = data.map((e: ListsData) => ({
id: e.id,
leaveType: e.leaveType,
name: e.name,
2023-11-03 15:08:53 +07:00
Date: date2Thai(e.Date),
2023-11-06 12:12:54 +07:00
status: e.status,
}));
2023-11-08 18:02:52 +07:00
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);
}
//filter table
const selectYear = ref<string>("all");
const selectType = ref<string>("all");
2023-11-06 12:12:54 +07:00
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>("");
function searchDataFn(type: string, status: string) {
// selectYear.value = selectYear.value || "all"
type = type || "all";
status = status || "all";
// showLoader()
loadTable.value = true;
if (selectYear.value == "all" && type == "all" && status == "all") {
2023-11-08 18:02:52 +07:00
console.log(1);
2023-11-06 12:12:54 +07:00
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") {
2023-11-06 12:12:54 +07:00
console.log(2);
} else if (selectYear.value == "all" && type !== "all" && status == "all") {
2023-11-06 12:12:54 +07:00
console.log(3);
2023-11-08 18:02:52 +07:00
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") {
2023-11-06 12:12:54 +07:00
console.log(4);
console.log(status);
2023-11-08 18:02:52 +07:00
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"
) {
2023-11-06 12:12:54 +07:00
console.log(5);
} else if (
selectYear.value !== "all" &&
type == "all" &&
status !== "all"
) {
2023-11-06 12:12:54 +07:00
console.log(6);
} else if (
selectYear.value == "all" &&
type !== "all" &&
status !== "all"
) {
2023-11-06 12:12:54 +07:00
console.log(7);
console.log(type);
2023-11-08 18:02:52 +07:00
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 = "";
}
2023-10-06 13:32:54 +07:00
// filter option
2023-11-06 12:12:54 +07:00
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
);
}
2023-11-06 12:12:54 +07:00
});
}
2023-10-06 13:32:54 +07:00
// convertSatatus
function convertSatatus(val: string) {
switch (val) {
2023-11-03 15:08:53 +07:00
case "NEW":
return "ใหม่";
2023-11-03 15:08:53 +07:00
case "PENDING":
return "อยู่ระหว่างดำเนินการ";
case "APPROVE":
return "อนุมัติ";
2023-11-03 15:08:53 +07:00
case "REJECT":
return "ไม่อนุมัติ";
2023-10-06 13:32:54 +07:00
}
}
2023-11-06 12:12:54 +07:00
function convertLeave(val: string) {
switch (val) {
case "leave1":
return "ลากิจส่วนตัว";
case "leave2":
return "ลาป่วย";
2023-11-08 18:02:52 +07:00
case "leave3":
return "ลาคลอดบุตร";
case "leave4":
return "ลาไปช่วยเหลือภริยาที่คลอดบุตร";
case "leave5":
return "ลาพักผ่อน";
}
}
function convertLeaveDaytype(val: string) {
switch (val) {
case "allday":
return "ลาทั้งวัน";
case "halfmorning":
return "ลาครึ่งวันเช้า";
case "halfafternoon":
return "ลาครึ่งวันบ่าย";
2023-11-06 12:12:54 +07:00
}
}
return {
amounttab1,
amounttab2,
//ข้อมูลในตาราง
rows,
fetchList,
loadTable,
columns,
visibleColumns,
2023-10-06 13:32:54 +07:00
//filter table
filterTable,
selectYear,
selectType,
selectStatus,
optionYear,
optionType,
optionStatus,
clearFilter,
searchDataFn,
filterOption,
2023-11-06 12:12:54 +07:00
leaveOp,
statusOp,
leaveOps,
2023-11-08 18:02:52 +07:00
statusOps,
//
convertLeave,
convertLeaveDaytype,
};
});