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

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

import type { QTableProps } from "quasar";

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

export const useEvaluateDetailStore = defineStore("evaluateDetailStore", () => {
  const tabMenu = ref<string>("1");
  const step = ref<number>(1);
  const currentStep = ref<number>(1);
  const titel = ref<string[]>([
    "ตรวจสอบคุณสมบัติ",
    "จัดเตรียมเอกสารเล่ม 1",
    "ตรวจสอบเอกสารเล่ม 1",
    "รอตรวจสอบคุณสมบัติ",
    "ประกาศบนเว็บไซต์",
    "จัดเตรียมเอกสารเล่ม 2",
    "ตรวจสอบเอกสารเล่ม 2",
    "รอพิจารณาผลการประเมิน",
    "เสร็จสิ้น",
  ]);

  const tabPanels = ref<string>("1");

  const log = ref<any>(0);
  const urlDownloadFile = ref<string>("");
  const pdfSrcStore = ref<any>();
  const numOfPagesStore = ref<any>();
  const currentObjectUrl = ref<string | null>(null);

  /** คอลัมน์ ใบอนุญาตประกอบวิชาชีพ*/
  const columnsCertificates = ref<QTableProps["columns"]>([
    {
      name: "certificateType",
      align: "left",
      label: "ชื่อใบอนุณาต",
      sortable: false,
      field: "certificateType",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "issuer",
      align: "left",
      label: "หน่วยงานผู้ออกใบอนุญาต",
      sortable: true,
      field: "issuer",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "certificateNo",
      align: "left",
      label: "เลขที่ใบอนุญาต",
      sortable: true,
      field: "certificateNo",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "issueDate",
      align: "left",
      label: "วันที่ออกใบอนุญาต",
      sortable: true,
      field: "issueDate",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "expireDate",
      align: "left",
      label: "วันที่หมดอายุ",
      sortable: true,
      field: "expireDate",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
  ]);

  /** คอลัมน์ ประวัติการรับราชการ*/
  const columnSalaries = ref<QTableProps["columns"]>([
    {
      name: "commandDateAffect",
      align: "left",
      label: "วันที่คำสั่งมีผล",
      sortable: true,
      field: "commandDateAffect",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
      format: (v) => date2Thai(v),
    },
    {
      name: "commandDateSign",
      align: "left",
      label: "วันที่ลงนาม",
      sortable: true,
      field: "commandDateSign",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
      format: (v) => date2Thai(v),
    },
    {
      name: "posNumCodeSit",
      align: "left",
      label: "หน่วยงานที่ออกคำสั่ง",
      sortable: false,
      field: "posNumCodeSit",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
      format(val, row) {
        return row.posNumCodeSitAbb && row.posNumCodeSit
          ? `${row.posNumCodeSit} (${row.posNumCodeSitAbb})`
          : row.posNumCodeSit
          ? row.posNumCodeSit
          : "-";
      },
    },
    {
      name: "posNo",
      align: "left",
      label: "เลขที่ตำแหน่ง",
      sortable: true,
      field: "posNo",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
      format(val, row) {
        return row.posNoAbb && row.posNo
          ? `${row.posNoAbb} ${row.posNo}`
          : row.posNo
          ? row.posNo
          : "-";
      },
      sort: (a: string, b: string) =>
        a
          .toString()
          .localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
    },
    {
      name: "positionName",
      align: "left",
      label: "ตำแหน่งในสายงาน",
      sortable: true,
      field: "positionName",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
      sort: (a: string, b: string) =>
        a
          .toString()
          .localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
    },
    {
      name: "positionType",
      align: "left",
      label: "ตำแหน่งประเภท",
      sortable: true,
      field: "positionType",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "positionLevel",
      align: "left",
      label: "ระดับ",
      sortable: true,
      field: "positionLevel",
      format(val, row) {
        return `${
          row.positionLevel
            ? row.positionLevel
            : row.positionCee
            ? row.positionCee
            : "-"
        }`;
      },
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "positionExecutive",
      align: "left",
      label: "ตำแหน่งทางการบริหาร",
      sortable: true,
      field: "positionExecutive",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "commandNo",
      align: "left",
      label: "เลขที่คำสั่ง",
      sortable: true,
      field: "commandNo",
      format(val, row) {
        return row.commandNo && row.commandYear
          ? `${row.commandNo}/${Number(row.commandYear) + 543}`
          : "";
      },
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "commandCode",
      align: "left",
      label: "ประเภทคำสั่ง",
      sortable: true,
      field: "commandCode",
      format(val, row) {
        return row.commandName;
      },
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "organization",
      align: "left",
      label: "สังกัด",
      sortable: true,
      field: "organization",
      headerStyle: "font-size: 14px;min-width:280px",
      style: "font-size: 14px",

      format(val, row) {
        return findOrgName({
          root: row.orgRoot,
          child1: row.orgChild1,
          child2: row.orgChild2,
          child3: row.orgChild3,
          child4: row.orgChild4,
        });
      },
    },
    {
      name: "amount",
      align: "left",
      label: "เงินเดือน",
      sortable: true,
      field: "amount",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
      format(v, row) {
        return row.amount
          ? `${row.amount.toLocaleString()}${
              row.amountSpecial !== 0 && row.amountSpecial
                ? ` (${row.amountSpecial.toLocaleString()})`
                : ""
            }`
          : "-";
      },
    },
    {
      name: "remark",
      align: "left",
      label: "หมายเหตุ",
      sortable: true,
      field: "remark",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
  ]);

  /** คอลัมน์ ประวัติการฝึกอบรมดูงาน*/
  const columnTraining = ref<QTableProps["columns"]>([
    {
      name: "name",
      align: "left",
      label: "ชื่อโครงการ/หลักสูตรการฝึกอบรม",
      sortable: false,
      field: "name",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "topic",
      align: "left",
      label: "หัวข้อการฝึกอบรม/ดูงาน",
      sortable: true,
      field: "topic",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "startDate",
      align: "left",
      label: "วันเริ่มต้นการฝึกอบรม/ดูงาน",
      sortable: true,
      field: "startDate",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "endDate",
      align: "left",
      label: "วันสิ้นสุดการฝึกอบรม/ดูงาน",
      sortable: true,
      field: "endDate",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "yearly",
      align: "left",
      label: "ปีที่อบรม/ดูงาน",
      sortable: true,
      field: "yearly",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "duration",
      align: "left",
      label: "รวมระยะเวลาในการฝึกอบรม/ดูงาน",
      sortable: true,
      field: "duration",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "department",
      align: "left",
      label: "หน่วยงานที่รับผิดชอบจัดการฝึกอบรม/ดูงาน",
      sortable: true,
      field: "department",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
  ]);

  //ผลงานที่เคยเสนอขอประเมิน (ถ้ามี)
  const columnAssessments = ref<QTableProps["columns"]>([
    {
      name: "year",
      align: "left",
      label: "ปี พ.ศ.",
      sortable: true,
      field: "year",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "type",
      align: "left",
      label: "ระดับตอนที่ยื่นขอ",
      sortable: true,
      field: "type",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "subject",
      align: "left",
      label: "เรื่อง/ชื่อเรื่อง",
      sortable: true,
      field: "subject",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "evaluationResult",
      align: "left",
      label: "ผลการประเมิน (ผ่าน/ไม่ผ่าน)",
      sortable: true,
      field: "evaluationResult",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
  ]);

  //ประสบการณ์ในการปฏิบัติงาน
  const columnExperience = ref<QTableProps["columns"]>([
    {
      name: "name",
      align: "left",
      label: "ชื่อผลงาน",
      sortable: true,
      field: "name",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
    {
      name: "detail",
      align: "left",
      label: "รายละเอียดผลงาน",
      sortable: true,
      field: "detail",
      headerStyle: "font-size: 14px",
      style: "font-size: 14px",
    },
  ]);

  function textRangePoint(val: number | undefined) {
    if (val == undefined) val = -1;
    if (val < 60.0) return "(คะแนนต่ำกว่าร้อยละ 60.00)";
    if (val >= 60.0 && val <= 69.99) return "(คะแนนร้อยละ 60.00 - 69.99)";
    if (val >= 70.0 && val <= 79.99) return "(คะแนนร้อยละ 70.00 - 79.99)";
    if (val >= 80.0 && val <= 89.99) return " (คะแนนร้อยละ 80.00 - 89.99)";
    if (val >= 90.0) return " (คะแนนร้อยละ 90.00 ขึ้นไป)";
    else return "";
  }

  function textPoint(val: number | undefined) {
    if (val == undefined) val = -1;
    if (val < 60.0) return "ต้องปรับปรุง";
    if (val >= 60.0 && val <= 69.99) return "พอใช้";
    if (val >= 70.0 && val <= 79.99) return "ดี";
    if (val >= 80.0 && val <= 89.99) return "ดีมาก";
    if (val >= 90.0) return "ดีเด่น";
    else return "-";
  }

  return {
    tabMenu,
    step,
    titel,
    tabPanels,
    /** step1*/
    columnsCertificates,
    columnSalaries,
    columnTraining,
    columnAssessments,
    columnExperience,

    pdfSrcStore,
    currentObjectUrl,
    urlDownloadFile,
    numOfPagesStore,
    log,
    currentStep,
  };
});
