hrms-mgt/src/modules/07_insignia/store.ts

79 lines
2.2 KiB
TypeScript

import { defineStore } from "pinia";
import { ref, } from "vue";
import { useCounterMixin } from "@/stores/mixin";
const mixin = useCounterMixin();
const {
date2Thai
} = mixin;
export const useInsigniaDataStore = defineStore("insignia", () => {
let optionsTypeOc = ref<any>([]);
let typeOc = ref<string>("")
let rows = ref<any>([])
const listinsignia = ref<any>([])
const typeinsigniaValues: Set<number> = new Set();
const typeinsignia = ref<number | string>("all");
let typeinsigniaOptions = ref<any>([{ id: "all", name: "ทั้งหมด" }]);
const fetchData = async (data: any) => {
if (data !== null) {
rows.value = await data.map((e: any) =>
({
id: e.id,
profileId: e.profileId,
name: e.fullName,
position: e.position,
level: e.rank,
salary: e.salary,
salary2: Number(e.salary).toLocaleString(),
insigniaType: e.lastInsignia,
insigniaSend: e.requestInsignia,
insigniaLevel: e.level,
dateSend: date2Thai(e.requestDate),
}))
listinsignia.value = await rows.value
filtertypeInsignia()
} else rows.value = []
}
const filtertypeInsignia = async () => {
typeinsignia.value = "all"
if (listinsignia.value.length !== 0) {
const double_name = [
...new Set(listinsignia.value.map((item: any) => item.insigniaType)),
];
for (let i = 1; i <= double_name.length; i++) {
const type = double_name[i - 1];
const listtype = {
id: type,
name: type,
};
typeinsigniaOptions.value.push(listtype)
}
} else typeinsigniaOptions.value = [{ id: "all", name: "ทั้งหมด" }]
};
const searchFilterTable = async () => {
if (typeinsignia.value !== undefined && typeinsignia.value !== null) {
if (typeinsignia.value === "all") {
rows.value = listinsignia.value;
} else {
rows.value = listinsignia.value.filter(
(e: any) => e.insigniaType === typeinsignia.value
);
}
}
};
return {
optionsTypeOc,
typeOc,
rows,
typeinsigniaOptions,
typeinsignia,
fetchData,
searchFilterTable,
};
});