<script setup lang="ts">
import { ref, reactive, watch } from "vue";
import { useQuasar } from "quasar";
import { useCounterMixin } from "@/stores/mixin";
import type { QTableProps } from "quasar";
import type { HistoryType } from "@/modules/02_organizationalNew/interface/index/Main";

import DialogHeader from "@/components/DialogHeader.vue";
import Modal from "@/modules/05_placement/components/AppointEmployee/Modal.vue";
import { getDateMeta } from "@fullcalendar/core/internal";

import http from "@/plugins/http";
import config from "@/app.config";

const props = defineProps({
  modal: Boolean,
  close: Function,
});

const $q = useQuasar();
const mixin = useCounterMixin();
const { showLoader, hideLoader, messageError, date2Thai } = mixin;
const rows = ref<HistoryType[]>([]);

const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    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: "lastUpdatedAt",
    align: "left",
    label: "วันที่แก้ไข",
    sortable: true,
    field: "lastUpdatedAt",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "orgRevisionIsCurrent",
    align: "center",
    label: "โครงสร้างที่ใช้อยู่",
    sortable: false,
    field: "orgRevisionIsCurrent",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "orgRevisionIsDraft",
    align: "center",
    label: "โครงสร้างแบบร่าง",
    sortable: false,
    field: "orgRevisionIsDraft",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "orgRevisionCreatedAt",
    align: "left",
    label: "วันเริ่มใช้โครงสร้าง",
    sortable: true,
    field: "orgRevisionCreatedAt",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const visibleColumns = ref<string[]>([
  "no",
  "orgRevisionName",
  "orgRevisionIsCurrent",
  "orgRevisionIsDraft",
  "orgRevisionCreatedAt",
]);

function getDate() {
  showLoader()
  http
  .get(config.API.organizationHistoryNew)
  .then((res)=>{
      const dataList = res.data.result
      dataList.map((item:HistoryType)=>(
        {
          orgRevisionId:item.orgRevisionId ? item.orgRevisionId:'-',
          orgRevisionName:item.orgRevisionName ? item.orgRevisionName:'-',
          orgRevisionIsCurrent:item.orgRevisionIsCurrent ? item.orgRevisionIsCurrent:'-',
          orgRevisionIsDraft:item.orgRevisionIsDraft ? item.orgRevisionIsDraft:'-',
          orgRevisionCreatedAt:item.orgRevisionCreatedAt ? date2Thai(item.orgRevisionCreatedAt as Date):'-',
        }
      ))
      rows.value = dataList
  }).catch((e)=>{
    messageError($q,e)
  })
  .finally(()=>{
    hideLoader()
  })
}
watch(
  () => props.modal,
  () => {
    if (props.modal == true) {
      getDate();
    }
  }
);
</script>
<template>
  <template>
    <q-dialog v-model="props.modal" persistent>
      <q-card style="min-width: 40vw">
        <DialogHeader :tittle="`ประวัติโครงสร้าง`" :close="props.close" />
        <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 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'">
                        {{ props.rowIndex + 1 }}
                      </div>

                      <div v-else-if="col.name == 'orgRevisionIsCurrent'">
                        <q-icon
                          :name="props.row.orgRevisionIsCurrent == true ? 'mdi-check':''"
                          :color="props.row.orgRevisionIsCurrent == true ? 'positive':''"
                          class="text-h5"
                        />
                      </div>
                      <div v-else-if="col.name == 'orgRevisionIsDraft'">
                        <q-icon
                          :name="props.row.orgRevisionIsDraft == true ? 'mdi-check':''"
                          :color="props.row.orgRevisionIsDraft == true ? 'positive':''"
                          class="text-h5"
                        />
                      </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>
