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

import { useCounterMixin } from "@/stores/mixin";
import { usePositionEmp } from "@/modules/16_positionEmployee/store/organizational";
import http from "@/plugins/http";
import config from "@/app.config";

/**
 * importType
 */
import type { QTableProps } from "quasar";
import type { HistoryPos } from "@/modules/16_positionEmployee/interface/response/organizational";

/**
 * importCompoonents
 */
import Header from "@/components/DialogHeader.vue";

/**
 * use
 */
const store = usePositionEmp();
const { showLoader, hideLoader, messageError, date2Thai } = useCounterMixin();
const $q = useQuasar();

/**
 * props
 */
const modal = defineModel<boolean>("modal", { required: true });
const props = defineProps({
  rowId: {
    type: String,
  },
});

const rows = ref<HistoryPos[]>([]); //รายการแก้ไขประวัติ
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "orgShortName",
    align: "left",
    label: "อักษรย่อ",
    sortable: true,
    field: "orgShortName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "lastUpdatedAt",
    align: "left",
    label: "วันที่แก้ไข",
    field: (v) => date2Thai(v),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "posMasterNoPrefix",
    align: "left",
    label: " Prefix ตำแหน่งเลขที่",
    sortable: true,
    field: "posMasterNoPrefix",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "posMasterNo",
    align: "left",
    label: "ตำแหน่งเลขที่",
    sortable: true,
    field: "posMasterNo",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "posMasterNoSuffix",
    align: "left",
    label: "Suffix ตำแหน่งเลขที่",
    sortable: true,
    field: "posMasterNoSuffix",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);

/**
 *  function เรียกข้อมูลประวัติตำแหน่ง
 * @param id
 */
function fetchHistoryPos(id: string) {
  showLoader();
  http
    .get(config.API.orgPosHistory(id))
    .then((res) => {
      const data: HistoryPos[] = res.data.result;
      rows.value = data;
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

/**
 * callback function ทำงานเมื่อ   modal === true
 */
watch(
  () => modal.value,
  () => {
    modal.value && props.rowId && fetchHistoryPos(props.rowId);
  }
);
</script>
<template>
  <q-dialog v-model="modal" persistent>
    <q-card style="width: 700px; max-width: 80vw">
      <Header
        :tittle="'ประวัติตำแหน่ง'"
        :close="
          () => {
            modal = false;
          }
        "
      />
      <q-separator />

      <q-card-section class="q-pt-none q-pa-sm">
        <d-table
          flat
          bordered
          :rows="rows"
          :columns="columns"
          row-key="id"
          no-data-label="ไม่มีข้อมูล"
        >
          <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.name" :props="props">
                <div v-if="col.name == 'no'">
                  {{
                    store.typeOrganizational === "current"
                      ? props.rowIndex + 1
                      : props.rowIndex + 1 == 1
                      ? "1 (แบบร่าง)"
                      : props.rowIndex + 1 == 2
                      ? "2 (ปัจจุบัน)"
                      : props.rowIndex + 1
                  }}
                </div>

                <div v-else>
                  {{ col.value }}
                </div>
              </q-td>
            </q-tr>
          </template>
        </d-table>
      </q-card-section>
    </q-card>
  </q-dialog>
</template>

<style scoped></style>
