import { defineStore } from "pinia";
import { ref } from "vue";
import type {
  DirectorRows,
  DirectorRowsResponse,
} from "@/modules/11_discipline/interface/response/director";
import type { QTableProps } from "quasar";

// store ระบบวินัย >> ข้อมูลพื้นฐาน >> กรรมการ
export const useDisciplineDirectorDataStore = defineStore(
  "disciplineDirector",
  () => {
    //ค้นหา คอลัมน์ คอลัมน์ที่แสดง
    const rows = ref<DirectorRowsResponse[]>([]);
    const visibleColumns = ref<string[]>([
      "no",
      "firstName",
      "position",
      "email",
      "phone",
      "totalInvestigate",
      "totalDisciplinary",
    ]);

    // หัวตาราง
    const columns = ref<QTableProps["columns"]>([
      {
        name: "no",
        align: "center",
        label: "ลำดับ",
        sortable: false,
        field: "no",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "firstName",
        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",
      },
      {
        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",
      },
      {
        name: "totalInvestigate",
        align: "left",
        label: "จำนวนการสืบสวน",
        sortable: true,
        field: "totalInvestigate",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
      {
        name: "totalDisciplinary",
        align: "left",
        label: "จำนวนการสอบสวน",
        sortable: true,
        field: "totalDisciplinary",
        headerStyle: "font-size: 14px",
        style: "font-size: 14px",
      },
    ]);

    /**
     * เก็บข้อมูลที่ได้จาก API เเล้วจัดเรียง
     * @param data ข้อมูลจาก API
     */
    function fetchData(data: DirectorRows[]) {
      const dataList: DirectorRowsResponse[] = data.map(
        (item: DirectorRows) => ({
          id: item.id,
          fullName: `${item.prefix}${item.firstName} ${item.lastName}`,
          position: item.position,
          email: item.email ? item.email : "-",
          phone: item.phone ? item.phone : "-",
          totalInvestigate: item.totalInvestigate,
          totalDisciplinary: item.totalDisciplinary,
        })
      );
      rows.value = dataList;
    }

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