<script setup lang="ts">
import { ref, computed, watch } from "vue";
import { useQuasar } from "quasar";
import http from "@/plugins/http";
import config from "@/app.config";

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

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

import { useCounterMixin } from "@/stores/mixin";
import { usePositionEmp } from "@/modules/16_positionEmployee/store/organizational";
const store = usePositionEmp();
const { showLoader, hideLoader, messageError, date2Thai } = useCounterMixin();
const $q = useQuasar();
const modal = defineModel<boolean>("modal", { required: true });

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: "lastUpdatedAt",
    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",
  },
]);
const rows = ref<any>([]);

const props = defineProps({
  rowId: {
    type: String,
  },
});

async function fetchHistoryPos(id: string) {
  showLoader();
  await http
    .get(config.API.orgPosHistory(id))
    .then((res) => {
      const data: HistoryPos[] = res.data.result;
      const list = data.map((e: HistoryPos) => ({
        ...e,
        lastUpdatedAt: e.lastUpdatedAt ? date2Thai(e.lastUpdatedAt) : "-",
        posMasterNoPrefix: e.posMasterNoPrefix ?? "-",
        posMasterNo: e.posMasterNo ?? "-",
        posMasterNoSuffix: e.posMasterNoSuffix ?? "-",
      }));
      rows.value = list;
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

watch(
  () => modal.value,
  () => {
    modal.value && props.rowId && fetchHistoryPos(props.rowId);
  }
);
</script>
<template>
  <q-dialog v-model="modal">
    <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>
