import { defineStore } from "pinia";
import { ref } from "vue";
import type {
  dataRowRound,
  roundShow,
} from "@/modules/09_leave/interface/response/round.ts";
import type { QTableProps } from "quasar";

// store ลา >> รอบการปฏิบัติงาน
export const useRoundDataStore = defineStore("roundWorkStore", () => {
  /** ค้นหา คอลัมน์ คอลัมน์ที่แสดง */
  const visibleColumns = ref<string[]>([
    "round",
    "morningRound",
    "afternoonRound",
    "isActive",
  ]);

  /** หัวตาราง */
  const columns = ref<QTableProps["columns"]>([
    {
      name: "round",
      align: "left",
      label: "รอบ",
      sortable: true,
      field: "round",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "morningRound",
      align: "left",
      label: "ช่วงเช้า",
      sortable: true,
      field: "morningRound",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "afternoonRound",
      align: "left",
      label: "ช่วงบ่าย",
      sortable: true,
      field: "afternoonRound",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "isActive",
      align: "center",
      label: "สถานะการใช้งาน",
      sortable: true,
      field: "isActive",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
  ]);

  /** ข้อมูลในตาราง */
  const rows = ref<dataRowRound[]>([]);
  /**
   * Function รับข้อมูลจาก API map
   * @param data รับข้อมูลจาก API
   */
  async function fetchData(data: dataRowRound[]) {
    let datalist: roundShow[] = data.map((e: dataRowRound) => {
      return {
        id: e.id,
        round: `${e.startTimeMorning}-${e.endTimeAfternoon}`,
        morningRound: `${e.startTimeMorning}-${e.endTimeMorning}`,
        afternoonRound: `${e.startTimeAfternoon}-${e.endTimeAfternoon}`,
        createdAt: e.createdAt,
        createdFullName: e.createdFullName,
        createdUserId: e.createdUserId,
        description: e.description,
        endTimeAfternoon: e.endTimeAfternoon,
        endTimeMorning: e.endTimeMorning,
        startTimeMorning: e.startTimeMorning,
        startTimeAfternoon: e.startTimeAfternoon,
        lastUpdateFullName: e.lastUpdateFullName,
        lastUpdateUserId: e.lastUpdateUserId,
        lastUpdatedAt: e.lastUpdatedAt,
        isActive: e.isActive,
        isDefault: e.isDefault,
      };
    });
    rows.value = datalist;
  }

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