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

import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";

/**
 * importType
 */
import type { QTableProps } from "quasar";
import type { HistoryPostType } from "@/modules/02_organization/interface/index/Main";

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

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

/**
 * props
 */
const modal = defineModel<boolean>("history", { required: true });
const type = defineModel<number>("type", { required: true });
const orgId = defineModel<string>("orgId", { required: true });

/**
 * ข้อมุล Table
 */
const rows = ref<HistoryPostType[]>([]); //ข้อมูลรายการประวัติหน่วยงาน
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: (row) => rows.value.indexOf(row) + 1,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "orgRevisionName",
    align: "left",
    label: "ชื่อโครงสร้าง",
    sortable: true,
    field: "orgRevisionName",
    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",
  },

  {
    name: "lastUpdatedAt",
    align: "left",
    label: "วันที่แก้ไข",
    sortable: true,
    field: (v) => date2Thai(v),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const visibleColumns = ref<string[]>([
  "no",
  "name",
  "orgRevisionName",
  "lastUpdatedAt",
]);

/**
 * function เรียกข้อมูลประวัติหน่วยงาน,ประวัติส่วนราชการ
 */
async function fetchHistory() {
  showLoader();
  await http
    .post(config.API.organizationHistoryPostNew, {
      id: orgId.value,
      type: type.value,
    })
    .then(async (res) => {
      const data = await res.data.result;
      rows.value = data;
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}

/**
 * ดูการเปลี่ยนแปลง  modal เมื่อเป็น true จะเรียกข้อมูลประวัติหน่วยงาน,ประวัติส่วนราชการ
 */
watch(
  () => modal.value,
  () => {
    if (modal.value == true) {
      fetchHistory();
    }
  }
);
</script>
<template>
  <template>
    <q-dialog v-model="modal" persistent>
      <q-card style="min-width: 70%">
        <DialogHeader
          :tittle="type == 0 ? `ประวัติหน่วยงาน` : `ประวัติส่วนราชการ`"
          :close="() => (modal = false)"
        />
        <q-separator />

        <q-card-section>
          <div class="row q-col-gutter-sm">
            <div class="col-12">
              <d-table
                ref="table"
                :columns="columns"
                :rows="rows"
                row-key="orgRevisionId"
                flat
                bordered
                :paging="true"
                dense
                class="custom-header-table"
                :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"
                      style="color: #000000; font-weight: 500"
                    >
                      <span
                        v-if="col.name === 'name'"
                        class="text-weight-medium"
                      >
                        {{ type === 0 ? "ชื่อหน่วยงาน" : "ส่วนราชการ" }}
                      </span>

                      <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.name"
                      :props="props"
                    >
                      <div class="table_ellipsis2" v-if="col.name == 'name'">
                        {{ col.value ?? "-" }}
                      </div>
                      <div v-else>
                        {{ col.value ?? "-" }}
                      </div>
                    </q-td>
                  </q-tr>
                </template>
              </d-table>
            </div>
          </div>
        </q-card-section>
      </q-card>
    </q-dialog>
  </template>
</template>
