<script setup lang="ts">
import { ref, watch, reactive } from "vue";
import DialogHeader from "@/components/DialogHeader.vue";
import { useCounterMixin } from "@/stores/mixin";
import { useQuasar, type QTableProps } from "quasar";
import type { RowList } from "@/modules/04_registryNew/interface/index/other";

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

import type {
  RequestItemsObject,
  FormFilter,
} from "@/modules/04_registryNew/interface/index/discipline";

const modal = defineModel<boolean>("modal", { required: true });
const id = defineModel<string>("id", { required: true });

const $q = useQuasar();
const mixin = useCounterMixin();
const { showLoader, hideLoader, messageError, date2Thai } = mixin;
const historyPagination = ref({
  page: 1,
  rowsPerPage: 10,
});
const currentPage = ref<number>(1);
const maxPage = ref<number>(1);
const filterKeyword = ref<string>("");
const rows = ref<RowList[]>([]); //select data history
const formFilter = reactive<FormFilter>({
  page: 1,
  pageSize: 12,
  keyword: "",
  type: "",
  posType: "",
  posLevel: "",
  retireYear: "",
  rangeYear: { min: 0, max: 60 },
  isShowRetire: false,
  isProbation: false,
});

const visibleColumns = ref<String[]>([
  "date",
  "detail",
  "lastUpdateFullName",
  "lastUpdatedAt",
]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "date",
    align: "left",
    label: "วันที่",
    sortable: true,
    field: "date",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px; width: 50px;",
    format: (v) => date2Thai(v),
  },
  {
    name: "detail",
    align: "left",
    label: "รายละเอียด",
    sortable: true,
    field: "detail",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "lastUpdateFullName",
    align: "left",
    label: "ผู้ดำเนินการ",
    sortable: true,
    field: "lastUpdateFullName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "lastUpdatedAt",
    align: "left",
    label: "วันที่แก้ไข",
    sortable: true,
    field: "lastUpdatedAt",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => date2Thai(v),
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);
function getHistory() {
  showLoader();
  http
    .get(config.API.profileNewOtherHisById(id.value))
    .then((res) => {
      let data = res.data.result;
      rows.value = [];
      data.map((e: RowList) => {
        rows.value.push({
          id: e.id,
          date: e.date,
          detail: e.detail,
          lastUpdateFullName: e.lastUpdateFullName,
          lastUpdatedAt: new Date(e.lastUpdatedAt),
        });
      });
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}

watch(modal, (status) => {
  if (status == true) {
    getHistory();
    filterKeyword.value = "";
  } else {
    filterKeyword.value = "";
  }
});
</script>

<template>
  <q-dialog v-model="modal" persistent>
    <q-layout
      view="lHh lpr lFf"
      container
      style="height: 500px; min-width: 80%"
      class="bg-white"
    >
      <q-header>
        <q-toolbar>
          <DialogHeader
            tittle="ประวัติแก้ไขอื่นๆ"
            :close="() => (modal = false)"
          />
        </q-toolbar>
        <q-separator color="grey-4" />
      </q-header>

      <q-page-container>
        <q-page class="q-pa-md">
          <div class="row q-gutter-sm q-mb-sm">
            <q-space />
            <q-input
              standout
              dense
              v-model="filterKeyword"
              ref="filterRef"
              outlined
              placeholder="ค้นหา"
              debounce="300"
            >
              <template v-slot:append>
                <q-icon
                  v-if="filterKeyword == ''"
                  name="search"
                  @click.stop.prevent="filterKeyword = ''"
                  class="cursor-pointer"
                />
                <q-icon
                  v-if="filterKeyword"
                  name="cancel"
                  @click.stop.prevent="filterKeyword = ''"
                  class="cursor-pointer"
                />
              </template>
            </q-input>
            <q-select
              v-model="visibleColumns"
              multiple
              outlined
              dense
              options-dense
              :display-value="$q.lang.table.columns"
              emit-value
              map-options
              :options="columns"
              option-value="name"
              options-cover
              style="min-width: 150px"
            />
          </div>
          <d-table
            ref="table"
            flat
            bordered
            dense
            :columns="columns"
            :rows="rows"
            :paging="true"
            :rows-per-page-options="[10, 25, 50, 100]"
            :visible-columns="visibleColumns"
            :filter="filterKeyword"
          >
            >
            <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>
                    {{ col.value ? col.value : "-" }}
                  </div>
                </q-td>
              </q-tr>
            </template>
          </d-table>
        </q-page>
      </q-page-container>
    </q-layout>
  </q-dialog>
</template>
