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([]); let typeOc = ref("") const agency = ref('') let rows = ref([]) const listinsignia = ref([]) const typeinsignia = ref("all"); let typeinsigniaOptions = ref([{ id: "all", name: "ทั้งหมด" }]); const fetchData = async (data: any) => { if (data !== null) { rows.value = await data.map((e: any) => ({ id: e.id, citizenId: e.citizenId, 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), requestNote: e.requestNote })) listinsignia.value = await rows.value filtertypeInsignia() } else rows.value = [] } const fetchOption = (op: any) => { if (agency.value !== null) { typeOc.value = agency.value optionsTypeOc.value = op.filter((e: any) => e.id == agency.value ) } else { optionsTypeOc.value = op, typeOc.value = op[2].id } } const filtertypeInsignia = async () => { typeinsignia.value = "all" if (listinsignia.value.length !== 0) { const double_name = [ ...new Set(listinsignia.value.map((item: any) => item.insigniaSend)), ]; typeinsigniaOptions.value = [{ id: "all", name: "ทั้งหมด" }] for (let i = 1; i <= double_name.length; i++) { const type = double_name[i - 1]; const listtype = { id: type, name: type, }; // typeinsigniaOptions.value = [{ id: "all", name: "ทั้งหมด" }] 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.insigniaSend === typeinsignia.value ); } } }; return { optionsTypeOc, typeOc, rows, typeinsigniaOptions, typeinsignia, agency, fetchData, fetchOption, searchFilterTable, }; });