import { defineStore } from "pinia";
import { ref } from "vue";
import type { QTableProps } from "quasar";
import type {
  DataOption,
  directorType,
} from "@/modules/11_discipline/interface/index/Main";
import type {
  DataResult,
  DataResultList,
} from "@/modules/11_discipline/interface/response/result";
import { useCounterMixin } from "@/stores/mixin";

import { useDisciplineMainStore } from "@/modules/11_discipline/store/main";
const mixin = useCounterMixin();
const storeMain = useDisciplineMainStore();
const { date2Thai } = mixin;

export const useDisciplineResultStore = defineStore(
  "disciplineResultStore",
  () => {
    const tabMenu = ref<string>("result");
    const rows = ref<DataResultList[]>([]);
    const rows2 = ref<directorType[]>([]);
    const selected = ref<directorType[]>([]);

    const complainantoptions = ref<DataOption[]>(
      storeMain.complainantoptionsMain
    );

    const statusOptions = ref<DataOption[]>([
      { id: "ALL", name: "ทั้งหมด" },
      { id: "DONE", name: "กำลังสรุปผลการพิจารณา" },
      { id: "REPORT", name: "ส่งไปออกคำสั่ง" },

    ]);

    const visibleColumns = ref<String[]>([
      "no",
      "title",
      "respondentType",
      "offenseDetails",
      "disciplinaryFaultLevel",
      "disciplinaryCaseFault",
      "createdAt",
      "resultDisciplineType",
      "resultTitleType",
      "resultOc",
      "resultYear",
      "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: "createdAt",
        align: "left",
        label: "วันที่สอบสวน",
        sortable: true,
        field: "createdAt",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "resultDisciplineType",
        align: "left",
        label: "ประเภทวินัย",
        sortable: true,
        field: "resultDisciplineType",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "resultTitleType",
        align: "left",
        label: "ประเภทของเรื่อง",
        sortable: true,
        field: "resultTitleType",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "resultOc",
        align: "left",
        label: "หน่วยงาน/ส่วนราชการ",
        sortable: true,
        field: "resultOc",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "resultYear",
        align: "left",
        label: "ปีงบประมาณ",
        sortable: true,
        field: "resultYear",
        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",
      "email",
      "telephone",
      "role",
    ]);

    const columnsDirector = ref<QTableProps["columns"]>([
      {
        name: "no",
        align: "left",
        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: "email",
        align: "left",
        label: "อีเมล",
        sortable: true,
        field: "email",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "telephone",
        align: "left",
        label: "เบอร์โทรศัพท์",
        sortable: true,
        field: "telephone",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "role",
        align: "left",
        label: "จำนวนเรื่องสืบสวน",
        sortable: true,
        field: "role",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
    ]);

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

    /**
     * จัดเรียงข้อมูลจาก API
     * @param data ข้อมูลจาก API
     */
    async function fetchList(data: DataResult[]) {
      const datalist: DataResultList[] = data.map((e: DataResult) => ({
        id: e.id,
        title: e.title,
        respondentType: e.respondentType
          ? storeMain.convertComplaintType(e.respondentType)
          : "-",
        offenseDetails: e.offenseDetails
          ? storeMain.convertFault(e.offenseDetails)
          : "-",
        disciplinaryFaultLevel: e.disciplinaryFaultLevel
          ? e.disciplinaryFaultLevel
          : "-",
        disciplinaryCaseFault: e.disciplinaryCaseFault
          ? e.disciplinaryCaseFault
          : "-",
        status: e.status && convertStatus(e.status),
        createdAt: e.createdAt && date2Thai(e.createdAt),
        resultDisciplineType: e.resultDisciplineType ? e.resultDisciplineType : '-',
        resultTitleType: e.resultTitleType ? e.resultTitleType : '-',
        resultOc: e.resultOc ? e.resultOc : '-',
        resultYear: e.resultYear ? e.resultYear + 543 : '-',
      }));
      rows.value = datalist;
    }

    /**
     * แปลง status เป็น text
     * @param val status
     * @returns text
     */
    function convertStatus(val: string) {
      switch (val) {
        case "DONE":
          return "กำลังสรุปผลการพิจารณา";
        case "REPORT":
          return "ส่งไปออกคำสั่ง";
      }
    }

    return {
      tabMenu,
      fetchList,
      rows,
      rows2,
      visibleColumns,
      columns,
      visibleColumnsDirector,
      columnsDirector,
      complainantoptions,
      filterSelector,
      statusOptions
    };
  }
);
