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

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

/** importStore*/
import { useCounterMixin } from "@/stores/mixin";

/**use*/
const mixins = useCounterMixin();
const $q = useQuasar();

const { showLoader, hideLoader, date2Thai, messageError } = mixins;

/** รับ 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>();

/**
 * functio เรียกข่้อมูลประวัติการประเมิน
 * @param id  ประเมิน
 */
async function fetchListHistory(id: string) {
  showLoader();
  const thaiOptions: Intl.DateTimeFormatOptions = {
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit",
  };
  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(() => {
      hideLoader();
    });
}

watch(
  () => props.modal,
  () => {
    props.modal && props.id && fetchListHistory(props.id);
  }
);
</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 class="q-pt-none">
        <div class="col-xs-12 col-sm-12 col-md-12 row q-col-gutter-md">
          <div class="col-12 q-mt-sm">
            <q-table
              ref="table"
              flat
              bordered
              class="custom-header-table"
              :columns="columns"
              :rows="row"
              dense
              :rows-per-page-options="[10, 25, 50, 100]"
            >
              <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>
            </q-table>
          </div>
        </div>
      </q-card-section>
    </q-card>
  </q-dialog>
</template>

<style lang="scss" scoped>
.custom-header-table {
  height: auto;
  .q-table tr:nth-child(odd) td {
    background: white;
  }
  .q-table tr:nth-child(even) td {
    background: #f8f8f8;
  }

  .q-table thead tr {
    background: #ecebeb;
  }

  .q-table thead tr th {
    position: sticky;
    z-index: 1;
  }
  /* this will be the loading indicator */
  .q-table thead tr:last-child th {
    /* height of all previous header rows */
    top: 48px;
  }
  .q-table thead tr:first-child th {
    top: 0;
  }
}
</style>
