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

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

import type {
  listData,
  dataType,
  DataOption,
} from "@/modules/11_discipline/interface/response/Suspend";
import type { QTableProps } from "quasar";

export const useDisciplineSuspendStore = defineStore(
  "disciplineSuspendStore",
  () => {
    const rows = ref<dataType[]>([]);
    const columns = ref<QTableProps["columns"]>([]);
    const visibleColumns = ref<string[]>([]);

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

    const offenseDetailsOps = ref<DataOption[]>([
      { id: "NOT_SPECIFIED", name: "ยังไม่ระบุ" },
      { id: "NOT_DEADLY", name: "ไม่ร้ายแรง" },
      { id: "DEADLY", name: "ร้ายแรง" },
    ]);

    /**
     * จัดเรียงข้อมูลจาก API
     * @param data ข้อมูลจาก API
     */
    async function getData(data: listData[]) {
      const dataList: dataType[] = data.map((item: listData) => ({
        id: item.id,
        profileId: item.profileId,
        citizenId: item.citizenId,
        name: `${item.prefix}${item.firstName} ${item.lastName}`,
        prefix: item.prefix,
        firstName: item.firstName,
        lastName: item.lastName,
        organization: item.organization,
        position: item.position,
        posNo: item.posNo,
        positionType: item.positionType,
        positionLevel: item.positionLevel,
        salary: item.salary,
        status: statusTothai(item.status),
        statusEn: item.status,
        descriptionSuspend: item.descriptionSuspend,
        dateTotal:
          item.startDateSuspend && item.endDateSuspend
            ? `${date2Thai(item.startDateSuspend)} - ${date2Thai(
                item.endDateSuspend
              )}`
            : "-",
        startDateSuspend: item.startDateSuspend,
        endDateSuspend: item.endDateSuspend,
        title: item.title,
        offenseDetails: item.offenseDetails,
        disciplinaryFaultLevel: item.disciplinaryFaultLevel,
        disciplinaryCaseFault: item.disciplinaryCaseFault,
      }));

      rows.value = dataList;
    }

    /**
     * แปลง status เป็น text
     * @param val status
     * @returns text
     */
    const statusTothai = (val: string) => {
      switch (val) {
        case "WAITTING":
          return "รอดำเนินการ";
        case "PENDING":
          return "รอออกคำสั่ง";
        case "APPROVE":
          return "อนุมัติ";
        case "REJECT":
          return "ไม่อนุมัติ";
        case "REPORT":
          return "ส่งรายชื่อไปออกคำสั่ง";
        case "DONE":
          return "ออกคำสั่งเสร็จแล้ว";

        default:
          return "-";
      }
    };

    return {
      rows,
      columns,
      visibleColumns,
      getData,
      offenseDetailsOps,
    };
  }
);
