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

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

import type { Persons } from "@/modules/11_discipline/interface/request/Disciplinary";
import type {
  investigateDisDataRowType,
  DataOption,
  directorType,
  responseType,
  DataOptioGroup,
  DataNumberOption,
} from "@/modules/11_discipline/interface/index/Main";

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

export const useInvestigateDisStore = defineStore(
  "DisciplineInvestigateDis",
  () => {
    const tabMenu = ref<string>("disciplinary");
    const rows = ref<investigateDisDataRowType[]>([]);
    const rowSent = ref<Persons[]>([]);
    const rows2 = ref<directorType[]>([]);
    const selected = ref<directorType[]>([]);

    const daysExtendOps = ref<DataNumberOption[]>([
      { id: 15, name: "15 วัน" },
      { id: 30, name: "30 วัน" },
      { id: 45, name: "45 วัน" },
      { id: 60, name: "60 วัน" },
    ]);

    const offenseDetailsOps = ref<DataOption[]>([
      { id: "NOT_SPECIFIED", name: "ยังไม่ระบุ" },
      { id: "HAVE_CAUSE", name: "มีมูล" },
      { id: "NO_CAUSE", name: "ไม่มีมูล" },
    ]);

    const optionsfaultLevel = ref<DataOptioGroup[]>([
      { id: "0", name: "ไม่ร้ายแรง", disable: true },
      { id: "ภาคทัณฑ์", name: "ภาคทัณฑ์", disable: false },
      {
        id: "ตัดเงินเดือน",
        name: "ตัดเงินเดือน",
        disable: false,
      },
      {
        id: "ลดขั้นเงินเดือน",
        name: "ลดขั้นเงินเดือน",
        disable: false,
      },
      { id: "4", name: "ร้ายแรง", disable: true },
      { id: "ปลดออก", name: "ปลดออก", disable: false },
      { id: "ไล่ออก", name: "ไล่ออก", disable: false },
      { id: "อื่นๆ", name: "อื่นๆ", disable: false },
    ]);

    const statusOps = ref<DataOption[]>([
      { id: "NEW", name: "กำลังสอบสวน" },
      { id: "STOP", name: "ยุติเรื่อง" },
      { id: "DONE", name: "ส่งไปสรุปผลการพิจารณาแล้ว" },
    ]);

    const causeTextOptions = ref<DataOption[]>([
      { id: "ร้ายแรง", name: "ร้ายแรง" },
      { id: "ไม่ร้ายแรง", name: "ไม่ร้ายแรง" },
    ]);
    const statusOptions = ref<DataOption[]>([
      { id: "ALL", name: "ทั้งหมด" },
      { id: "NEW", name: "กำลังสอบสวน" },
      { id: "STOP", name: "ยุติเรื่อง" },
      { id: "DONE", name: "ส่งไปสรุปผลการพิจารณาแล้ว" },
    ]);
    const visibleColumns = ref<String[]>([
      "no",
      "title",
      "respondentType",
      "offenseDetails",
      "disciplinaryFaultLevel",
      "disciplinaryCaseFault",
      "disciplinaryDate",
      "createdAt",
      "status",
    ]);

    const columns = ref<QTableProps["columns"]>([
      {
        name: "no",
        align: "center",
        label: "ลำดับ",
        sortable: false,
        field: "no",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "title",
        align: "left",
        label: "เรื่องร้องเรียน",
        sortable: true,
        field: "title",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "respondentType",
        align: "left",
        label: "ผู้ถูกสอบสวน",
        sortable: true,
        field: "respondentType",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
        sort: (a: string, b: string) =>
          a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
      },
      {
        name: "offenseDetails",
        align: "left",
        label: "ลักษณะความผิด",
        sortable: true,
        field: "offenseDetails",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
        sort: (a: string, b: string) =>
          a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
      },
      {
        name: "disciplinaryFaultLevel",
        align: "left",
        label: "ระดับโทษความผิด",
        sortable: true,
        field: "disciplinaryFaultLevel",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
        sort: (a: string, b: string) =>
          a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
      },
      {
        name: "disciplinaryCaseFault",
        align: "left",
        label: "กรณีความผิด",
        sortable: true,
        field: "disciplinaryCaseFault",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
        sort: (a: string, b: string) =>
          a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
      },
      {
        name: "disciplinaryDate",
        align: "left",
        label: "วันที่สอบสวน",
        sortable: true,
        field: "disciplinaryDate",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "createdAt",
        align: "left",
        label: "วันที่รับเรื่อง",
        sortable: true,
        field: "createdAt",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "status",
        align: "left",
        label: "สถานะ",
        sortable: true,
        field: "status",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
    ]);

    const visibleColumnsDirector = ref<String[]>([
      "no",
      "name",
      "position",
      "duty",
      "commandNo",
      "email",
      "phone",
      "total",
    ]);
    const columnsDirector = ref<QTableProps["columns"]>([
      {
        name: "no",
        align: "center",
        label: "ลำดับ",
        sortable: false,
        field: "no",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "name",
        align: "left",
        label: "ชื่อ-นามสกุล",
        sortable: true,
        field: "name",
        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",
      },
      {
        name: "duty",
        align: "left",
        label: "หน้าที่",
        sortable: true,
        field: "duty",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "commandNo",
        align: "left",
        label: "มติ/คำสั่ง",
        sortable: true,
        field: "commandNo",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "email",
        align: "left",
        label: "อีเมล",
        sortable: true,
        field: "email",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "phone",
        align: "left",
        label: "เบอร์โทรศัพท์",
        sortable: true,
        field: "phone",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
    ]);

    /**
     * แปลง status จาก id
     * @param val id ที่รับมาจาก API เเล้วมาทำการ find จาก id
     * @returns name
     */
    function convertStatus(val: string) {
      const result = statusOps.value.find((x: any) => x.id == val)?.name;
      return result ? result : "-";
    }

    /**
     * จัดเรียงข้อมูลจาก API
     * @param data ข้อมูลจาก API
     */
    async function fetchList(data: investigateDisDataRowType[]) {
      let datalist: any[] = data.map((e: any) => ({
        id: e.id,
        title: e.title ? e.title : "-",
        respondentType: e.respondentType
          ? convertRespondentType(e.respondentType)
          : "-",
        offenseDetails: e.offenseDetails ? convertFault(e.offenseDetails) : "-",
        disciplinaryFaultLevel: e.disciplinaryFaultLevel ?? "-",
        disciplinaryCaseFault: e.disciplinaryCaseFault ?? "-",
        disciplinaryDate:
          e.disciplinaryDateStart && e.disciplinaryDateEnd
            ? `${date2Thai(e.disciplinaryDateStart)} - ${date2Thai(
                e.disciplinaryDateEnd
              )}`
            : "-",
        status: convertStatus(e.status) ?? "-",
        createdAt: e.createdAt ? date2Thai(e.createdAt) : "-",
      }));

      rows.value = datalist;
    }

    /**
     * จัดเรียงข้อมูล กรรมการ จาก API
     * @param data ข้อมูลจาก API
     */
    async function fecthDirector(data: directorType[]) {
      let datalistDirector: responseType[] = data.map((e: directorType) => ({
        id: e.id,
        name: `${e.prefix}${e.firstName} ${e.lastName}`,
        prefix: e.prefix,
        firstName: e.firstName,
        lastName: e.lastName,
        position: e.position,
        email: e.email == "" ? "-" : e.email,
        phone: e.phone == "" ? "-" : e.phone,
      }));
      rows2.value = datalistDirector;
      selected.value = rows2.value;
    }

    /**
     * แปลง status เป็น text
     * @param val status
     * @returns text
     */
    function convertRespondentType(val: string) {
      switch (val) {
        case "PERSON":
          return "บุคคล";
        case "ORGANIZATION":
          return "หน่วยงาน";
        case "BANGKOK":
          return "กรุงเทพมหานคร";
      }
    }

    /**
     * แปลง status เป็น text
     * @param val status
     * @returns text
     */
    function convertFault(val: string) {
      switch (val) {
        case "NOT_SPECIFIED":
          return "ความผิดวินัยยังไม่ระบุ";
        case "NOT_DEADLY":
          return "ความผิดวินัยไม่ร้ายแรง";
        case "DEADLY":
          return "ความผิดวินัยร้ายแรง";
        default:
          return "-";
      }
    }

    /**
     * ฟิลเตอร์ข้อมูลจาก input
     * @param val ค่าที่ป้อนให้ input
     * @param update function จาก quasar
     * @param type type ที่กำหนด ของ input นั้นๆ
     */
    function filterFnOptionsType(
      val: string | number,
      update: any,
      type: string
    ) {
      update(() => {
        if (type === "offenseDetailsOps") {
          offenseDetailsOps.value = offenseDetailsOps.value.filter(
            (v: any) => v.name.toLowerCase().indexOf(val) > -1
          );
        }
      });
    }

    return {
      tabMenu,
      fetchList,
      rows,
      rows2,
      rowSent,
      offenseDetailsOps,
      optionsfaultLevel,
      fecthDirector,

      visibleColumns,
      columns,
      visibleColumnsDirector,
      columnsDirector,
      daysExtendOps,

      filterFnOptionsType,
      causeTextOptions,

      convertRespondentType,
      convertFault,
      statusOptions,
    };
  }
);
