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

import { useCounterMixin } from "@/stores/mixin";
/** import Type */
import type { QTableProps } from "quasar";
import type { FormDetail } from "@/modules/09_leave/interface/response/work";

export const useWorklistDataStore = defineStore("work", () => {
  /** รายการลงเวลาปฏิบัติงาน */
  const tabs = ref<string>("1");

  /** ข้อมูล Table */
  const mixin = useCounterMixin();
  const { date2Thai } = mixin;
  const columns = ref<QTableProps["columns"]>([]);
  const visibleColumns = ref<string[]>([]);

  /** ค้นหาวัน ข้อมูล Table  */
  const selectDate = ref<Date | null>(new Date());

  // convertSatatus
  /**
   * function แปลงค่าสถานะ
   * @param val ค่าสถานนะ
   */
  function convertSatatus(val: string) {
    switch (val) {
      case "NORMAL":
        return "ปกติ";
      case "LATE":
        return "สาย";
      case "ABSENT":
        return "ขาดราชการ";
      default:
        return "-";
    }
  }

  const formData = reactive<FormDetail>({
    checkInDate: null,
    checkInImg: "",
    checkInLat: null,
    checkInLon: null,
    checkInLocation: "",
    checkInTime: "",
    checkInStatus: "",
    checkInDescription: "",
    checkOutDate: null,
    checkOutImg: "",
    checkOutLat: null,
    checkOutLon: null,
    checkOutLocation: "",
    checkOutTime: "",
    checkOutStatus: "",
    fullName: "",
    checkOutDescription: "",
    isLocationCheckIn: false,
    checkInLocationName: "",
    isLocationCheckOut: false,
    checkOutLocationName: "",
  });

  function getData(data: any) {
    formData.checkInDate = data.checkInDate && date2Thai(data.checkInDate);
    formData.checkInImg = data.checkInImg;
    formData.checkInLat = data.checkInLat ? data.checkInLat : "";
    formData.checkInLon = data.checkInLon ? data.checkInLon : "";
    formData.checkInLocation = data.checkInPOI ? data.checkInPOI : "-";
    formData.checkInTime = data.checkInTime ? data.checkInTime : "-";
    formData.checkOutDate = data.checkOutDate && date2Thai(data.checkOutDate);
    formData.checkOutImg = data.checkOutImg;
    formData.checkOutLat = data.checkOutLat ? data.checkOutLat : "";
    formData.checkOutLon = data.checkOutLon ? data.checkOutLon : "";
    formData.checkOutLocation = data.checkOutLocation
      ? data.checkOutLocation
      : "-";
    formData.checkOutTime = data.checkOutTime ? data.checkOutTime : "-";
    formData.fullName = data.fullName;
    formData.checkOutLocation = data.checkOutPOI ? data.checkOutPOI : "-";
    formData.checkInDescription = data.checkInDescription
      ? data.checkInDescription
      : "-";
    formData.checkOutDescription = data.checkOutDescription
      ? data.checkOutDescription
      : "-";
    formData.checkInStatus = data.checkInStatus
      ? convertSatatus(data.checkInStatus)
      : "-";
    formData.checkOutStatus = data.checkOutStatus
      ? convertSatatus(data.checkOutStatus)
      : "-";

    formData.isLocationCheckIn = data.isLocationCheckIn;
    formData.checkInLocationName = data.checkInLocationName;
    formData.isLocationCheckOut = data.isLocationCheckOut;
    formData.checkOutLocationName = data.checkOutLocationName;
  }
  return {
    tabs,
    columns,
    visibleColumns,
    selectDate,
    convertSatatus,
    getData,
    formData,
  };
});
