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

/** importType*/
import type { DateFilter } from "@/modules/09_leave/interface/request/leave";
import type { QTableProps } from "quasar";
import type {
  DataRows,
  ListLeave,
} from "@/modules/09_leave/interface/response/leave";
import type { FremData } from "@/modules/09_leave/interface/request/leave";

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

export const useLeavelistDataStore = defineStore("leave", () => {
  /** ข้อมูล Main*/
  const tabMenu = ref<string>("1");

  /** ข้อมูล Tab 1*/
  const tabView = ref<string>("list");
  /** filter*/
  const filter = reactive<DateFilter>({
    year: new Date().getFullYear(), //*ปีในการยื่นขอใบลา(ใช้เป็น คศ.)
    type: "00000000-0000-0000-0000-000000000000", //*Id ประเภทการลา
    status: "NEW", //*สถานะการของลา
    keyword: "", //keyword ค้นหา
  });

  /**ข้อมูลใน Table*/
  const mainData = ref<any>([]);
  const rows = ref<DataRows[]>([]);
  const columns = ref<QTableProps["columns"]>([]);
  const visibleColumns = ref<string[]>([]);

  const leaveType = ref<any>([]);

  /**
   * ฟังก์ชั่น fetchListLeave
   * @param data รับข้อมูลจาก Page
   */
  async function fetchListLeave(data: ListLeave[]) {
    let datalist = data.map((e: ListLeave) => ({
      id: e.id,
      leaveTypeName: e.leaveTypeName ?? "-",
      leaveTypeId: e.leaveTypeId ?? "-",
      citizenId: e.citizenId ?? "-",
      fullName:
        e.fullName !== undefined && e.fullName !== null && e.fullName !== ""
          ? e.fullName
          : "-",
      agency:
        e.agency !== undefined && e.agency !== null && e.agency !== ""
          ? e.agency
          : "-",
      org: e.org !== undefined && e.org !== null && e.org !== "" ? e.org : "-",
      position: e.position ?? "-",
      level: e.level ?? "-",
      hajjDayStatus: e.hajjDayStatus,
      dateSendLeave: e.dateSendLeave && date2Thai(e.dateSendLeave),
      dateLeave:
        e.leaveStartDate && e.leaveEndDate
          ? e.leaveStartDate === e.leaveEndDate
            ? date2Thai(e.leaveEndDate)
            : date2Thai(e.leaveStartDate) + " - " + date2Thai(e.leaveEndDate)
          : "-",
      status: e.status && convertSatatus(e.status),
    }));
    mainData.value = datalist;
    rows.value = mainData.value;
  }

  /**
   * ฟังก์ชั่น fetchListLeave
   * @param data รับข้อมูลจาก Page
   */
  async function fetchListLeaveReject(data: any[]) {
    let datalist = data.map((e: FremData) => ({
      id: e.id,
      leaveTypeName: e.leaveTypeName,
      leaveTypeId: e.leaveTypeId,
      fullName: e.fullName,
      dateSendLeave: e.dateSendLeave && date2Thai(e.dateSendLeave),
      status: convertSatatus2(e.status),
    }));

    mainData.value = datalist;
    rows.value = mainData.value;
  }

  /**
   * ฟังก์ชั่นแปลงสถานะ tab1
   * @param val ค่าสถานะ
   */
  function convertSatatus(val: string) {
    switch (val) {
      case "NEW":
        return "ใหม่";
      case "PENDING":
        return "อยู่ระหว่างดำเนินการ";
      case "APPROVE":
        return "อนุมัติ";
      case "REJECT":
        return "ไม่อนุมัติ";
      case "DELETE":
        return "ยกเลิก";
    }
  }

  /**
   * ฟังก์ชั่นแปลงสถานะ tab2
   * @param val ค่าสถานะ
   */
  function convertSatatus2(val: string) {
    switch (val) {
      case "NEW":
        return "ใหม่";
      case "APPROVE":
        return "อนุมัติ";
      case "REJECT":
        return "ไม่อนุมัติ";
    }
  }

  /**
   * ฟังชั่นแปลงค่าการลา
   * @param val ค่าของการลา
   */
  function convertLeaveDaytype(val: string) {
    switch (val) {
      case "ALL_DAY":
        return "ลาทั้งวัน";
      case "HAFT_MORNING":
        return "ลาครึ่งวันเช้า";
      case "HAFT_AFTERNOON":
        return "ลาครึ่งวันบ่าย";
    }
  }

  function colorType(val: string) {
    const dataType = leaveType.value.find((item: any) => item.id === val);
    switch (dataType.code) {
      case "LV-001":
        return "#FFD1D1"; // Light Red
      case "LV-002":
        return "#C8E6C9"; // Light Green
      case "LV-003":
        return "#BBDEFB"; // Light Blue
      case "LV-004":
        return "#E1BEE7"; // Light Purple
      case "LV-005":
        return "#DCEDC8"; // Light Lime
      case "LV-006":
        return "#FFE0B2"; // Light Orange
      case "LV-007":
        return "#FFECB3"; // Light Amber
      case "LV-008":
        return "#CFD8DC"; // Light Blue Grey
      case "LV-009":
        return "#FFCCBC"; // Light Deep Orange
      case "LV-010":
        return "#FFF9C4"; // Light Amber Lighten
      case "LV-011":
        return "#B2DFDB"; // Light Teal
    }
  }

  return {
    tabMenu,
    filter,
    tabView,
    rows,
    fetchListLeave,
    columns,
    visibleColumns,
    fetchListLeaveReject,
    convertSatatus,
    convertLeaveDaytype,
    leaveType,
    colorType,
  };
});
