import { defineStore } from "pinia";
import { ref } from "vue";
import type { QTableProps } from "quasar";

import type { EvaluateList } from "@/modules/12_evaluatePersonal/interface/index/evalute";
import type { EvaluateRes } from "@/modules/12_evaluatePersonal/interface/response/evalute";

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

const mixin = useCounterMixin();
const { date2Thai } = mixin;
export const useEvalutuonStore = defineStore("EvalutuonStore", () => {
  /**
   * function Convert สถานะ
   * @param val สถานะ
   */
  function convertStatus(val: string) {
    switch (val) {
      case "CHECK_SPEC":
        return "ตรวจสอบคุณสมบัติด้วยตนเอง";
      case "PREPARE_DOC_V1":
        return "จัดเตรียมเอกสารเล่ม 1";
      case "CHECK_DOC_V1":
        return "ตรวจสอบเอกสารเล่ม 1";
      case "WAIT_CHECK_DOC_V1":
        return "รอตรวจสอบคุณสมบัติ";
      case "ANNOUNCE_WEB":
        return "ประกาศบนเว็บไซต์";
      case "PREPARE_DOC_V2":
        return "จัดเตรียมเอกสารเล่ม 2";
      case "WAIT_CHECK_DOC_V2":
        return "ตรวจสอบเอกสารเล่ม 2";
      case "CHECK_DOC_V2":
        return "รอพิจารณาผลการประเมิน";
      case "DONE":
        return "เสร็จสิ้น";
    }
  }

  /**
   * function Convert ระดับที่ยื่นขอ
   * @param val ระดับที่ยื่นขอ
   */
  function convertType(val: string) {
    if (val === "EXPERT") {
      return "ชำนาญการ";
    } else {
      return "ชำนาญการพิเศษ";
    }
  }

  const rows = ref<EvaluateList[]>([]);
  /**
   * function เรียกข้่อมูลรายการคำขอประเมิน
   * @param data รายการคำขอประเมิน
   */
  function fetchData(data: EvaluateRes[]) {
    const dataList: EvaluateList[] = data.map(
      (data: EvaluateRes) =>
        ({
          id: data.id,
          citizanId: data.citizenId,
          fullName: data.fullName,
          position: data.position,
          level: convertType(data.type),
          positionNumber: data.posNo,
          agency: data.oc,
          status: convertStatus(data.step),
        } as EvaluateList)
    );
    rows.value = dataList;
  }

  const visibleColumns = ref<String[]>([
    "no",
    "citizanId",
    "fullName",
    "position",
    "level",
    "positionNumber",
    "agency",
    "status",
  ]);
  const columns = ref<QTableProps["columns"]>([
    {
      name: "no",
      align: "center",
      label: "ลำดับ",
      sortable: false,
      field: "no",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "level",
      align: "left",
      label: "ระดับที่ยื่นขอ",
      sortable: true,
      field: "level",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
      sort: (a: string, b: string) =>
        a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
    },
    {
      name: "citizanId",
      align: "center",
      label: "เลขประจำตัวประชาชน",
      sortable: false,
      field: "citizanId",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "fullName",
      align: "left",
      label: "ชื่อ-นามสกุล",
      sortable: true,
      field: "fullName",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "position",
      align: "left",
      label: "ตำแหน่ง",
      sortable: true,
      field: "position",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
      sort: (a: string, b: string) =>
        a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
    },
    {
      name: "positionNumber",
      align: "left",
      label: "ตำแหน่งเลขที่",
      sortable: true,
      field: "positionNumber",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
      sort: (a: string, b: string) =>
        a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
    },
    {
      name: "agency",
      align: "left",
      label: "สังกัด",
      sortable: true,
      field: "agency",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
      sort: (a: string, b: string) =>
        a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
    },
    {
      name: "status",
      align: "left",
      label: "สถานะ",
      sortable: true,
      field: "status",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
  ]);

  return {
    visibleColumns,
    columns,
    rows,
    convertStatus,
    fetchData,
  };
});
