<script setup lang="ts">
import { ref, watch } from "vue";
import type { QTableProps } from "quasar";
import { useQuasar } from "quasar";

import http from "@/plugins/http";
import config from "@/app.config";
import { useKpiDataStore } from "@/modules/08_KPI/store";
import { useCounterMixin } from "@/stores/mixin";

import DialogHeader from "@/components/DialogHeader.vue";

const $q = useQuasar();
const store = useKpiDataStore();
const mixin = useCounterMixin();
const {
  showLoader,
  hideLoader,
  messageError,
  findPosMasterNoOld,
  findOrgNameOld,
  findOrgNameOldHtml,
  date2Thai,
} = mixin;

const modal = defineModel<boolean>("modal", { required: true });
const filterKeyword = ref<string>("");
const rows = ref<any[]>([]);

const visibleColumns = ref<string[]>([
  "commandName",
  "agency",
  "dateStart",
  "dateEnd",
  "commandNo",
  "document",
  "lastUpdateFullName",
  "lastUpdatedAt",
]);

//หัวตาราง
const columns = ref<QTableProps["columns"]>([
  {
    name: "commandName",
    align: "left",
    label: "ประเภทคำสั่ง",
    sortable: true,
    field: "commandName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },

  {
    name: "agency",
    align: "left",
    label: "หน่วยงานที่ให้ช่วยราชการ/ส่งตัวกลับ",
    sortable: true,
    field: "agency",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "dateStart",
    align: "left",
    label: "วันเริ่มช่วยราชการ",
    sortable: true,
    field: "dateStart",
    format: (v) => date2Thai(v),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "dateEnd",
    align: "left",
    label: "วันสิ้นสุดการช่วยราชการ",
    sortable: true,
    field: "dateEnd",
    format: (v) => date2Thai(v),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "commandNo",
    align: "left",
    label: "เลขที่คำสั่ง",
    sortable: true,
    field: "commandNo",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "document",
    align: "left",
    label: "เอกสารอ้างอิง",
    sortable: true,
    field: "document",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "lastUpdateFullName",
    align: "left",
    label: "ผู้ดำเนินการ",
    sortable: true,
    field: "lastUpdateFullName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "lastUpdatedAt",
    align: "left",
    label: "วันที่แก้ไข",
    sortable: true,
    field: "lastUpdatedAt",
    format: (v) => date2Thai(v, false, true),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);

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

    default:
      return "-";
  }
}

/** ปิด dialog */
function close() {
  modal.value = false;
}

/** ดึงข้อมูล */
function getData() {
  showLoader();
  http
    .get(config.API.orgAssistance(store.dataEvaluation.profileId))
    .then((res) => {
      const data = res.data.result;
      rows.value = data;
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}

watch(
  () => modal.value,
  (n) => {
    if (n == true) {
      getData();
    }
  }
);
</script>
<template>
  <q-dialog persistent v-model="modal">
    <q-card bordered style="min-width: 80vw">
      <DialogHeader tittle="ข้อมูลการช่วยราชการ" :close="close" />
      <q-separator />
      <q-card-section>
        <div class="row">
          <div class="col-12">
            <d-table
              ref="table"
              :columns="columns"
              :rows="rows"
              :filter="filterKeyword"
              row-key="id"
              flat
              bordered
              :paging="true"
              dense
              :rows-per-page-options="[10, 25, 50, 100]"
              :visible-columns="visibleColumns"
            >
              <template v-slot:header="props">
                <q-tr :props="props">
                  <q-th
                    v-for="col in props.cols"
                    :key="col.name"
                    :props="props"
                  >
                    <span class="text-weight-medium">{{ col.label }}</span>
                  </q-th>
                </q-tr>
              </template>
              <template v-slot:body="props">
                <q-tr :props="props">
                  <q-td v-for="col in props.cols" :key="col.id">
                    <div v-if="col.name === 'no'">
                      {{ props.rowIndex + 1 }}
                    </div>
                    <div
                      v-else-if="col.name == 'organizationPositionOld'"
                      class="text-html"
                    >
                      {{ props.row ? findOrgNameOldHtml(props.row) : "-" }}
                    </div>
                    <div v-else>
                      {{ col.value ? col.value : "-" }}
                    </div>
                  </q-td>
                </q-tr>
              </template>
            </d-table>
          </div>
        </div>
      </q-card-section>
    </q-card>
  </q-dialog>
</template>
