import { defineStore } from "pinia";
import { ref } from "vue";
import type { FormOrderPlacementMainData } from "@/modules/10_order/interface/request/Main";

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,
  };
});