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

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

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

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

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

//หัวตาราง
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "name",
    align: "left",
    label: "ชื่อ-นามสกุล",
    sortable: true,
    field: "name",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format(val, row) {
      return `${row.prefix}${row.firstName} ${row.lastName}`;
    },
  },
  {
    name: "posMasterNoOld",
    align: "left",
    label: "ตำแหน่งเลขที่",
    sortable: true,
    field: "posMasterNoOld",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format(val, row) {
      return findPosMasterNoOld(row);
    },
  },
  {
    name: "positionOld",
    align: "left",
    label: "ตำแหน่งในสายงาน",
    sortable: true,
    field: "positionOld",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "positionLevel",
    align: "left",
    label: "ตำแหน่งประเภท",
    sortable: true,
    field: "positionLevel",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format(val, row) {
      let name = "";
      if (row.posTypeNameOld && row.posLevelNameOld) {
        name = `${row.posTypeNameOld} (${row.posLevelNameOld})`;
      } else if (row.posTypeNameOld) {
        name = `${row.posTypeNameOld}`;
      } else if (row.posLevelNameOld) {
        name = `(${row.posLevelNameOld})`;
      } else name = "-";
      return name;
    },
  },
  {
    name: "organizationPositionOld",
    align: "left",
    label: "สังกัด",
    sortable: true,
    field: "organization",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (val, row) => findOrgNameOld(row),
  },
  {
    name: "organization",
    align: "left",
    label: "หน่วยงานที่ให้ช่วยราชการ",
    sortable: true,
    field: "organization",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "dateStart",
    align: "left",
    label: "วันเริ่มช่วยราชการ",
    sortable: true,
    field: "dateStart",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (val) => date2Thai(val),
  },
  {
    name: "dateEnd",
    align: "left",
    label: "วันสิ้นสุดการช่วยราชการ",
    sortable: true,
    field: "dateEnd",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (val) => date2Thai(val),
  },
  {
    name: "createdAt",
    align: "left",
    label: "วันที่ดำเนินการ",
    sortable: true,
    field: "createdAt",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (val) => date2Thai(val),
  },
  {
    name: "status",
    align: "left",
    label: "สถานะ",
    sortable: true,
    field: "status",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (val) => statusText(val),
  },
  {
    name: "commandNo",
    align: "left",
    label: "เลขที่คำสั่ง",
    sortable: true,
    field: "commandNo",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (val) => statusText(val),
  },
]);

const visibleColumns = ref<string[]>([
  "no",
  "name",
  "posMasterNoOld",
  "positionOld",
  "positionLevel",
  "organizationPositionOld",
  "organization",
  "dateStart",
  "dateEnd",
  "createdAt",
  "status",
  "commandNo",
]);

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

    default:
      return "-";
  }
}

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

/** ดึงข้อมูล */
function getData() {
  showLoader();
  http
    .get(config.API.placementKeycloak + `/${store.dataProfile.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();
    } else rows.value = [];
  }
);
</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" class="cursor-pointer">
                  <q-td v-for="col in props.cols" :key="col.id">
                    <div v-if="col.name === 'no'">
                      {{ props.rowIndex + 1 }}
                    </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>
