<script setup lang="ts">
import { ref, watch, computed } from "vue";
import { useRoute } from "vue-router";
import { useQuasar } from "quasar";
import type { QTableProps } from "quasar";

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

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

const $q = useQuasar();
const route = useRoute();
const mixins = useCounterMixin();
const { date2Thai, messageError } = mixins;

const isLoading = ref<boolean>(false);

const evaluateId = computed(() => {
  const id = route.params.id ? route.params.id.toString() : "";
  return id;
});
/** รับ props Tab 1  */
const props = defineProps({
  id: {
    type: String,
    require: true,
  },
  modal: {
    type: Boolean,
    require: true,
  },
  close: {
    type: Function,
    default: () => "",
  },
});

const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "step",
    align: "left",
    label: "การแก้ไข",
    sortable: true,
    field: "step",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "lastUpdateFullName",
    align: "left",
    label: "ผู้ดำเนินการ",
    sortable: true,
    field: "lastUpdateFullName",
    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",
  },
]);
const row = ref<any[]>([]);

/**
 * function เรียกข้อมูลประวัติการประเมิน
 * @param id การประเมิน
 */
async function fetchListHistory(id: string) {
  const thaiOptions: Intl.DateTimeFormatOptions = {
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit",
  };
  isLoading.value = true;
  await http
    .get(config.API.evaluationHistory(id))
    .then((res) => {
      const data = res.data.result;
      const list = data.map((e: any) => ({
        step: e.step,
        lastUpdateFullName: e.lastUpdateFullName,
        lastUpdatedAt: `${date2Thai(e.lastUpdatedAt)} เวลา  ${new Date(
          e.lastUpdatedAt
        ).toLocaleTimeString("th-TH", thaiOptions)} น.`,
      }));

      row.value = list;
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      isLoading.value = false;
    });
}

/** callbaclFunction เรียกข้อมูลประวัติการประเมิน ทำงานเมื่อ props.modal === true*/
watch(
  () => props.modal,
  () => {
    props.modal && fetchListHistory(evaluateId.value);
  }
);
</script>
<template>
  <q-dialog v-model="props.modal">
    <q-card style="width: 700px; max-width: 80vw">
      <HeaderDialog :tittle="'ประวัติการประเมิน'" :close="props.close" />
      <q-separator />
      <q-card-section>
        <div class="col-xs-12 col-sm-12 col-md-12 row q-col-gutter-md">
          <div class="col-12">
            <d-table
              ref="table"
              flat
              bordered
              class="custom-header-table"
              :columns="columns"
              :rows="row"
              dense
              :rows-per-page-options="[10, 25, 50, 100]"
              :pagination="{ page: 1, rowsPerPage: 10 }"
              :loading="isLoading"
            >
              <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" v-html="col.label" />
                  </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>
                      {{ col.value }}
                    </div>
                  </q-td>
                </q-tr>
              </template>
            </d-table>
          </div>
        </div>
      </q-card-section>
    </q-card>
  </q-dialog>
</template>

<style lang="scss" scoped></style>
