import { defineStore } from "pinia";
import { ref, } from "vue";
import { useCounterMixin } from "@/stores/mixin";

const mixin = useCounterMixin();
const {
  date2Thai,
} = mixin;

export const useAllocateDataStore = defineStore("insigniaallocate", () => {
  const insigniaName = ref<string>('')
  const insignia = ref<string>('')
  const insigniaOp = ref<any[]>([{ name: "ทั้งหมด", id: "", type: "" }])
  const insigniaType = ref<any>()
  const rows = ref<any>([])
  const listInsignia = ref<any>([])
  const mainTab = ref<string>("")

  const fetchDatainsignia = async (data: any) => {
    insignia.value = ''
    insigniaOp.value = [{ name: "ทั้งหมด", id: "", type: "" }]
    data.forEach((e: any) => {
      insigniaOp.value.push({ name: e.name, id: e.id, type: e.insigniaType.id })
    });
  }
  const fetchDatainsigniaType = async (data: any,) => {
    insigniaType.value = data.map((e: any) => ({ name: e.id, label: e.name }))
    // mainTab.value = insigniaType.value[0].name

  }
  const listinsignia = async (data: any) => {
    rows.value = [];
    let list = await data.map((e: any) => ({
      id: e.id,
      insignia: e.insignia,
      insigniaId: e.insigniaId,
      total: e.total,
      allocate: e.allocate,
      remain: e.remain,
      year: e.year,
    }))
    rows.value = list
    listInsignia.value = list
  }
  const selectInsignia = () => {
    console.log(insignia.value);
    if (insignia.value !== "") {
      rows.value = listInsignia.value.filter((e: any) => e.insigniaId === insignia.value)
    } else rows.value = listInsignia.value

  }
  const profileType = (val: string) => {
    switch (val) {
      case "officer":
        return "ข้าราชการ กทม.สามัญ";
      case "employee":
        return "ลูกจ้างประจำ";
    }
  }
  return {
    rows,
    insignia,
    insigniaOp,
    insigniaType,
    fetchDatainsignia,
    fetchDatainsigniaType,
    listinsignia,
    selectInsignia,
    profileType,
    insigniaName,
    mainTab
  };
});
