import { defineStore } from "pinia";
import { ref } from "vue";
import type { FormPlacementMainData } from "@/modules/05_placement/interface/request/Main";
import type { FormOrderPlacementMainData } from "@/modules/05_placement/interface/request/Main";

export const useProfileDataStore = defineStore("profile", () => {
  interface profile {
    main: { columns: String[] };
    education: { columns: String[] };
    certicate: { columns: String[] };
    train: { columns: String[] };
    insignia: { columns: String[] };
    coined: { columns: String[] };
    assessment: { columns: String[] };
    salary: { columns: String[] };
    discipline: { columns: String[] };
    leave: { columns: String[] };
    talent: { columns: String[] };
    work: { columns: String[] };
    record: { columns: String[] };
    other: { columns: String[] };
    document: { columns: String[] };
  }

  const birthDate = ref<Date>(new Date());
  const retireText = ref<string | null>(null);
  const changeRetireText = (val: string | null) => {
    retireText.value = val;
  };
  const changeBirth = (val: Date) => {
    birthDate.value = val;
  };
  const profileData = ref<profile>({
    main: { columns: [] },
    education: { columns: [] },
    certicate: { columns: [] },
    train: { columns: [] },
    insignia: { columns: [] },
    coined: { columns: [] },
    assessment: { columns: [] },
    salary: { columns: [] },
    discipline: { columns: [] },
    leave: { columns: [] },
    talent: { columns: [] },
    work: { columns: [] },
    record: { columns: [] },
    other: { columns: [] },
    document: { columns: [] },
  });

  const changeProfileColumns = (system: String, val: String[]) => {
    if (system == "main") profileData.value.main.columns = val;
    if (system == "education") profileData.value.education.columns = val;
    if (system == "certicate") profileData.value.certicate.columns = val;
    if (system == "train") profileData.value.train.columns = val;
    if (system == "insignia") profileData.value.insignia.columns = val;
    if (system == "coined") profileData.value.coined.columns = val;
    if (system == "assessment") profileData.value.assessment.columns = val;
    if (system == "salary") profileData.value.salary.columns = val;
    if (system == "discipline") profileData.value.discipline.columns = val;
    if (system == "leave") profileData.value.leave.columns = val;
    if (system == "talent") profileData.value.talent.columns = val;
    if (system == "work") profileData.value.work.columns = val;
    if (system == "record") profileData.value.record.columns = val;
    if (system == "other") profileData.value.other.columns = val;
    if (system == "document") profileData.value.document.columns = val;
    localStorage.setItem("profile", JSON.stringify(profileData.value));
  };

  if (localStorage.getItem("profile") !== null) {
    profileData.value = JSON.parse(localStorage.getItem("profile") || "{}");
  }

  return {
    profileData,
    changeProfileColumns,
    birthDate,
    changeBirth,
    retireText,
    changeRetireText,
  };
});
export const usePlacementDataStore = defineStore("placement", () => {
  interface placement {
    mappingPosition: { columns: String[] };
  }
  const placementData = ref<placement>({
    mappingPosition: { columns: [] },
  });
  const changePlacementColumns = (system: String, val: String[]) => {
    if (system == "mappingPosition")
      placementData.value.mappingPosition.columns = val;
    localStorage.setItem("placement", JSON.stringify(placementData.value));
  };

  if (localStorage.getItem("placement") !== null) {
    placementData.value = JSON.parse(localStorage.getItem("placement") || "{}");
  }

  let DataMainYear = ref<object[]>([]); // ข้อมูลจำนวนปี
  let DataMainOrig = ref<FormPlacementMainData[]>([]); // ข้อมูลหลักดั้งเดิม
  let DataMainUpdate = ref<FormPlacementMainData[]>([]); // ข้อมูลเปลี่ยนแปลง
  const DataMainYearSet = (val: FormPlacementMainData[]) =>
    (DataMainYear.value = val);
  const DataMain = (val: FormPlacementMainData[]) => (DataMainOrig.value = val);
  const DataUpdateMain = (
    filter_1: number | string,
    filter_2: string | null,
    filter_3: boolean
  ) => {
    DataMainUpdate.value = [];
    if (filter_1 === "all" && filter_2 === "all" && filter_3 === false) {
      // console.log('case 1');
      // return DataMainUpdate.value = DataMainOrig.value; อันเก่า
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) => item.isExpired === filter_3
      ));
    } else if (filter_1 === "all" && filter_2 === "all" && filter_3 === false) {
      // console.log('case 1.1');
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) => item.isExpired === filter_3
      ));
    } else if (filter_1 !== "all" && filter_2 === "all" && filter_3 === false) {
      // console.log('case 2');
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) => item.examOrder === filter_1 && item.isExpired === filter_3
      ));
    } else if (filter_1 !== "all" && filter_2 === "all" && filter_3 === false) {
      // console.log('case 2.1');
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) => item.examOrder === filter_1 && item.isExpired === filter_3
      ));
    } else if (filter_1 !== "all" && filter_2 !== "all" && filter_3 === false) {
      // console.log('case 2.2');
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) =>
          item.examOrder === filter_1 &&
          item.examTypeValue === filter_2 &&
          item.isExpired === filter_3
      ));
    } else if (filter_1 === "all" && filter_2 !== "all" && filter_3 === false) {
      // console.log('case 3');
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) => item.examTypeValue === filter_2 && item.isExpired === filter_3
      ));
    } else if (filter_1 === "all" && filter_2 === "all" && filter_3 === true) {
      // console.log('case 3.1');
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) => item.isExpired === filter_3
      ));
    } else if (filter_1 === "all" && filter_2 === "all" && filter_3 === true) {
      // console.log('case 3.2');
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) => item.isExpired === filter_3
      ));
    } else if (filter_1 !== "all" && filter_2 === "all" && filter_3 === true) {
      // console.log('case 3.3');
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) => item.examOrder == filter_1 && item.isExpired == filter_3
      ));
    } else if (filter_1 !== "all" && filter_2 === "all" && filter_3 === true) {
      // console.log('case 3.4');
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) => item.examOrder == filter_1 && item.isExpired == filter_3
      ));
    } else if (filter_1 !== "all" && filter_2 !== "all" && filter_3 === true) {
      // console.log('case 3.5');
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) =>
          item.examOrder == filter_1 &&
          item.examTypeValue == filter_2 &&
          item.isExpired == filter_3
      ));
    } else if (filter_1 === "all" && filter_2 !== "all" && filter_3 === true) {
      // console.log('case 3.6');
      return (DataMainUpdate.value = DataMainOrig.value.filter(
        (item) => item.examTypeValue == filter_2 && item.isExpired == filter_3
      ));
    }
  };

  return {
    placementData,
    changePlacementColumns,
    DataMainYear,
    DataMainOrig,
    DataMainUpdate,
    DataMainYearSet,
    DataMain,
    DataUpdateMain,
  };
});
export const useOrderPlacementDataStore = defineStore("placementOrder", () => {
  interface placementOrder {
    mappingPosition: { columns: String[] };
  }
  const placementOrderData = ref<placementOrder>({
    mappingPosition: { columns: [] },
  });
  const changePlacementColumns = (system: String, val: String[]) => {
    if (system == "mappingPosition")
      placementOrderData.value.mappingPosition.columns = val;
    localStorage.setItem(
      "placementOrder",
      JSON.stringify(placementOrderData.value)
    );
  };

  if (localStorage.getItem("placementOrder") !== null) {
    placementOrderData.value = JSON.parse(
      localStorage.getItem("placementOrder") || "{}"
    );
  }

  const DataMainYearOrder = ref<object[]>([]); // ข้อมูลจำนวนปี
  const DataMainOrigOrder = ref<FormOrderPlacementMainData[]>([]); // ข้อมูลหลักดั้งเดิม
  const DataMainUpdateOrder = ref<FormOrderPlacementMainData[]>([]); // ข้อมูลเปลี่ยนแปลง
  const DataMainYearSetOrder = (val: FormOrderPlacementMainData[]) =>
    (DataMainYearOrder.value = val);

  const DataMainOrder = (val: FormOrderPlacementMainData[]) =>
    (DataMainOrigOrder.value = val);

  const DataUpdateOrder = (
    filter_1: string,
    filter_2: string,
    filterYear: number | null
  ) => {
    DataMainUpdateOrder.value = [];

    if (filter_1 === "" && filter_2 === "" && filterYear === 0) {
      DataMainUpdateOrder.value = DataMainOrigOrder.value;
    } else if (filter_1 !== "" && filter_2 === "" && filterYear === 0) {
      DataMainUpdateOrder.value = DataMainOrigOrder.value.filter(
        (item) => item.OrderType === filter_1
      );
    } else if (filter_1 !== "" && filter_2 !== "" && filterYear === null) {
      DataMainUpdateOrder.value = DataMainOrigOrder.value.filter(
        (item) => item.OrderType === filter_1 && item.OrderStatus === filter_2
      );
    } else if (filter_1 !== "" && filter_2 === "" && filterYear !== 0) {
      DataMainUpdateOrder.value = DataMainOrigOrder.value.filter(
        (item) => item.OrderType === filter_1 && item.fiscalYear === filterYear
      );
    } else if (filter_1 === "" && filter_2 !== "" && filterYear === 0) {
      DataMainUpdateOrder.value = DataMainOrigOrder.value.filter(
        (item) => item.OrderStatus === filter_2
      );
    } else if (filter_1 === "" && filter_2 === "" && filterYear !== 0) {
      DataMainUpdateOrder.value = DataMainOrigOrder.value.filter(
        (item) => item.fiscalYear === filterYear
      );
    } else if (filter_1 === "" && filter_2 !== "" && filterYear !== null) {
      DataMainUpdateOrder.value = DataMainOrigOrder.value.filter(
        (item) =>
          item.OrderStatus === filter_2 && item.fiscalYear === filterYear
      );
    } else if (filter_1 !== "" && filter_2 !== "" && filterYear === 0) {
      DataMainUpdateOrder.value = DataMainOrigOrder.value.filter(
        (item) => item.OrderStatus === filter_2 && item.OrderType === filter_1
      );
    } else if (filter_1 !== "" && filter_2 !== "" && filterYear !== null) {
      DataMainUpdateOrder.value = DataMainOrigOrder.value.filter(
        (item) =>
          item.OrderType === filter_1 &&
          item.OrderStatus === filter_2 &&
          item.fiscalYear === filterYear
      );
    } else if (
      filter_1 == "ทั้งหมด" &&
      filter_2 == "ทั้งหมด" &&
      filterYear !== null
    ) {
      DataMainUpdateOrder.value = DataMainOrigOrder.value;
    }
  };

  return {
    placementOrderData,
    changePlacementColumns,
    DataMainYearOrder,
    DataMainOrigOrder,
    DataMainUpdateOrder,
    DataUpdateOrder,
    DataMainYearSetOrder,
    DataMainOrder,
  };
});
export const useProbationDataStore = defineStore("probationDataStore", () => {
  const ratingColors = ref<string[]>([
    "light-blue-3",
    "light-blue-6",
    "blue",
    "blue-9",
    "blue-10",
  ]);
  const orderId = ref<string>("");

  const stepOrder = ref<number>(2);

  const setOrderId = (val: string) => (orderId.value = val);
  const setStepOrder = (val: number) => (stepOrder.value = val);

  return {
    ratingColors,
    orderId,
    stepOrder,
    setOrderId,
    setStepOrder,
  };
});
export const useTransferDataStore = defineStore("transferDataStore", () => {
  const statusText = (val: string) => {
    switch (val) {
      case "WAITTING":
        return "รอดำเนินการ";
      case "PENDING":
        return "เลือกตำแหน่งแล้ว";
      case "APPROVE":
        return "อนุมัติ";
      case "REJECT":
        return "ไม่อนุมัติ";
      case "REPORT":
        return "ส่งรายชื่อไปออกคำสั่ง";
      case "DONE":
        return "ออกคำสั่งเสร็จแล้ว";

      default:
        return "-";
    }
  };

  return {
    statusText,
  };
});
