import { defineStore } from "pinia";
import { ref } from "vue";

import { useCounterMixin } from "@/stores/mixin";

import type { QTableProps } from "quasar";
import type {
  DataList,
  DataListRes,
} from "@/modules/11_discipline/interface/response/order";

const mixin = useCounterMixin();
const { date2Thai } = mixin;
export const useOrderStore = defineStore("DisciplineOrder", () => {
  const columns = ref<QTableProps["columns"]>([]);
  const visibleColumns = ref<string[]>([]);

  const mainData = ref<DataList[]>([]);
  const rows = ref<DataList[]>([]);

  async function fetchOrder(data: DataListRes[]) {
    let datalist: DataList[] = data.map((e: DataListRes) => ({
      fiscalYear: e.fiscalYear,
      orderBy: e.orderBy,
      orderById: e.orderById,
      orderByOrganization: e.orderByOrganization,
      orderDate: e.orderDate ? date2Thai(e.orderDate) : "",
      orderId: e.orderId,
      orderName: e.orderName,
      orderNo: e.orderNo,
      orderStatusName: e.orderStatusName,
      orderStatusValue: e.orderStatusValue,
      orderTypeName: e.orderTypeName,
      orderTypeValue: e.orderTypeValue,
      signatoryBy: e.signatoryBy,
      signatoryPosition: e.signatoryPosition,
      faultLevel: e.faultLevel ?? "-",
      caseFault: e.caseFault ?? "-",
      fullName: e.fullName ?? "-",
      result: e.result ?? "-",
      refRaw: e.refRaw ?? "-",
    }));
    mainData.value = datalist;
    filterListOrder("", "ทั้งหมด", 0);
  }

  function filterListOrder(
    filter_1: string,
    filter_2: string,
    filterYear: number | null
  ) {
    rows.value = [];
    if (filter_1 === "" && filter_2 === "ทั้งหมด" && filterYear === 0) {
      rows.value = mainData.value;
    } else if (filter_1 !== "" && filter_2 === "ทั้งหมด" && filterYear === 0) {
      rows.value = mainData.value.filter(
        (item) => item.orderTypeValue === filter_1
      );
    } else if (filter_1 === "" && filter_2 !== "ทั้งหมด" && filterYear === 0) {
      rows.value = mainData.value.filter(
        (item) => item.orderStatusName === filter_2
      );
    } else if (filter_1 === "" && filter_2 === "ทั้งหมด" && filterYear !== 0) {
      rows.value = mainData.value.filter(
        (item) => Number(item.fiscalYear) === filterYear
      );
    } else if (filter_1 !== "" && filter_2 !== "ทั้งหมด" && filterYear === 0) {
      rows.value = mainData.value.filter(
        (item) =>
          item.orderTypeValue === filter_1 && item.orderStatusName === filter_2
      );
    } else if (filter_1 !== "" && filter_2 === "ทั้งหมด" && filterYear !== 0) {
      rows.value = mainData.value.filter(
        (item) =>
          item.orderTypeValue === filter_1 &&
          Number(item.fiscalYear) === filterYear
      );
    } else if (filter_1 === "" && filter_2 !== "ทั้งหมด" && filterYear !== 0) {
      rows.value = mainData.value.filter(
        (item) =>
          item.orderStatusName === filter_2 &&
          Number(item.fiscalYear) === filterYear
      );
    } else if (filter_1 !== "" && filter_2 !== "ทั้งหมด" && filterYear !== 0) {
      rows.value = mainData.value.filter(
        (item) =>
          item.orderTypeValue === filter_1 &&
          item.orderStatusName === filter_2 &&
          Number(item.fiscalYear) === filterYear
      );
    }
  }

  return {
    rows,
    mainData,
    columns,
    visibleColumns,
    fetchOrder,
    filterListOrder,
  };
});
