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

import { useCounterMixin } from "@/stores/mixin";
import { useDisciplineMainStore } from "@/modules/11_discipline/store/Main";

import type { DataOption } from "@/modules/11_discipline/interface/index/Main";
import type {
  DataList,
  DataListRow,
  ocListType,
} from "@/modules/11_discipline/interface/response/Complaint";

import type { QTableProps } from "quasar";

const mainStore = useDisciplineMainStore();

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

export const useComplainstDataStore = defineStore(
  "DisciplineComplainst",
  () => {
    const rows = ref<DataListRow[]>([]);
    const visibleColumns = ref<string[]>([]);
    const columns = ref<QTableProps["columns"]>([]);

    function fetchComplainst(data: DataList[]) {
      let dataList: DataListRow[] = data.map((e: DataList) => ({
        id: e.id,
        personId: e.personId,
        title: e.title ? e.title : "-",
        dateReceived: e.dateReceived ? date2Thai(e.dateReceived) : "-",
        respondentType: e.respondentType
          ? mainStore.convertComplaintType(e.respondentType)
          : "-",
        offenseDetails: e.offenseDetails
          ? mainStore.convertOffenseDetailst(e.offenseDetails)
          : "-",
        createdAt: e.createdAt ? date2Thai(e.createdAt)! : "-",
        levelConsideration: e.levelConsideration
          ? levelConsiderationTran(e.levelConsideration)
          : "-",
        dateConsideration: e.dateConsideration
          ? date2Thai(e.dateConsideration)
          : "-",
        status: e.status ? statusTothai(e.status) : "-",
      }));
      rows.value = dataList;
    }

    const complainantoptions = ref<DataOption[]>(
      mainStore.complainantoptionsMain
    );
    const consideredAgencytoptions = ref<DataOption[]>([]);
    const organizationIdOp = ref<DataOption[]>([]);
    const levelConsiderationtOptions = ref<DataOption[]>([
      { id: "NORMAL", name: "ปกติ" },
      { id: "URGENT", name: "ด่วน" },
      { id: "VERY_URGENT", name: "ด่วนมาก" },
    ]);

    const agencytoptions = ref<DataOption[]>(consideredAgencytoptions.value);
    const optionListNameMain = ref<DataOption[]>([]);
    const optionListName = ref<DataOption[]>([]);

    const statusOptions = ref<DataOption[]>([
      { id: "ALL", name: "ทั้งหมด" },
      { id: "NEW", name: "ใหม่" },
      { id: "STOP", name: "ยุติเรื่อง" },
      { id: "SEND_INVESTIGATE", name: "มีมูลส่งไปสืบสวนแล้ว" },
    ]);

    /**
     * แปลง status เป็น Text
     * @param val status
     * @returns text
     */
    function statusTothai(val: string) {
      switch (val) {
        case "NEW":
          return "ใหม่";
        case "STOP":
          return "ยุติเรื่อง";
        case "SEND_INVESTIGATE":
          return "มีมูลส่งไปสืบสวนแล้ว";
        default:
          return "-";
      }
    }

    /**
     * แปลง option ที่รับมาเป็น ไทย
     * @param val ค่าที่ได้จาก API
     * @returns ส่ง name ที่ id ตรงกันออกไป
     */
    function levelConsiderationTran(val: string) {
      return (
        levelConsiderationtOptions.value.find((v: any) => v.id === val)?.name ??
        "-"
      );
    }

    /**
     * ฟังชั่น เก็บค่า options โดยแบ่งเก็บไว้ ตัวหลัก กับตัวรอง
     * @param list ชุดข้อมูล options
     */
    function selectComplainantType(list: any) {
      optionListNameMain.value = list;
      optionListName.value = list;
    }

    /**
     * ฟิลเตอร์ตาม text ใน input
     * @param val text
     * @param update ฟังชั่น qursar
     * @param type ค่า type ที่กำหนดไว้เอาไว้ แยกประเภท
     */
    function filterSelector(val: string, update: Function, type: string) {
      update(() => {
        const needle = val.toLowerCase();
        if (type === "filterrespondentType") {
          complainantoptions.value = mainStore.complainantoptionsMain.filter(
            (v: any) => v.name.toLowerCase().indexOf(needle) > -1
          );
        } else if (type === "filteragencytoptions") {
          agencytoptions.value = consideredAgencytoptions.value.filter(
            (v: any) => v.name.toLowerCase().indexOf(needle) > -1
          );
        } else if (type === "filtercomplainantOP") {
          optionListName.value = optionListNameMain.value.filter(
            (v: any) => v.name.toLowerCase().indexOf(needle) > -1
          );
        }
      });
    }

    /**
     * ดึงข้อมูล เฉพาะ สังกัด โดย เก็บ id/name
     * @param data ชุดข้อมูลรวม
     */
    function ocListFn(data: ocListType[]) {
      let dataList: DataOption[] = data.map((item: ocListType) => ({
        id: item.organizationId,
        name: item.organizationName,
      }));
      consideredAgencytoptions.value = dataList;
      organizationIdOp.value = dataList;
    }
    return {
      rows,
      visibleColumns,
      columns,
      fetchComplainst,
      selectComplainantType,
      filterSelector,
      complainantoptions,
      consideredAgencytoptions,
      optionListName,
      organizationIdOp,
      ocListFn,
      levelConsiderationtOptions,
      statusOptions,
    };
  }
);
