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

import { useKpiDataStore } from "@/modules/08_KPI/store";

import type { PropsTable } from "@/interface/PropsTable";
import type { ResEvaluatorAssessor } from "@/modules/08_KPI/interface/response/index";
import type { FormQuery } from "@/modules/08_KPI/interface/request/index";

const store = useKpiDataStore();
const router = useRouter();
const isLoad = defineModel<boolean>("isLoad", { required: true });
const visibleColumns = defineModel<string[]>("visibleColumns", {
  required: true,
});
const columns = defineModel<QTableProps["columns"]>("columns", {
  required: true,
});
const rows = defineModel<ResEvaluatorAssessor[]>("rows", { required: true });
const formQuery = defineModel<FormQuery>("formQuery", { required: true });
const total = defineModel<number>("total", { required: true });

const props = defineProps({
  fetchList: { type: Function },
});

const pagination = ref<PropsTable.Pagination>({
  sortBy: formQuery.value.sortBy,
  descending: formQuery.value.descending,
  page: formQuery.value.page,
  rowsPerPage: formQuery.value.pageSize,
  rowsNumber: total.value,
});

async function redirectViewDetail(id: string) {
  store.tabMain = "";
  router.push(`/KPI-evaluator/${id}`);
}

/**
 * ฟังก์ชันรับ request จากตาราง เมื่อมีการเปลี่ยน pagination
 * @param requestProps ข้อมูลการร้องขอจากตาราง
 */
function onTableRequest(requestProps: PropsTable.RequestProps) {
  const newPagination = requestProps?.pagination || requestProps;
  if (!newPagination?.page || !newPagination?.rowsPerPage) return;
  formQuery.value.page = newPagination.page;
  formQuery.value.pageSize = newPagination.rowsPerPage;
  formQuery.value.sortBy = newPagination.sortBy;
  formQuery.value.descending = newPagination.descending;
  props?.fetchList?.();
}

watch(
  [
    () => formQuery.value.page,
    () => formQuery.value.pageSize,
    () => total.value,
  ],
  ([page, pageSize, total]) => {
    pagination.value.page = page;
    pagination.value.rowsPerPage = pageSize;
    pagination.value.rowsNumber = total;
  }
);
</script>

<template>
  <div class="col-12">
    <d-table
      ref="table"
      :columns="columns"
      :rows="rows"
      row-key="id"
      flat
      bordered
      :paging="true"
      dense
      :rows-per-page-options="[1, 10, 25, 50, 100]"
      :visible-columns="visibleColumns"
      v-model:pagination="pagination"
      @request="onTableRequest"
      :loading="isLoad"
    >
      <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"
            @click="redirectViewDetail(props.row.id)"
          >
            <div v-if="col.name === 'firstName'">
              {{
                `${props.row.prefix}${props.row.firstname}  ${props.row.lastname}`
              }}
            </div>
            <div v-else>
              {{ col.value ? col.value : "-" }}
            </div>
          </q-td>
        </q-tr>
      </template>
      <template #item="props">
        <div class="q-pa-xs col-xs-12 col-sm-6 col-md-4 col-lg-3">
          <q-card bordered flat @click="redirectViewDetail(props.row.id)">
            <q-list>
              <q-item v-for="col in props.cols" :key="col.name">
                <q-item-section>
                  <q-item-label caption>{{ col.label }}</q-item-label>

                  <q-item-label>
                    <div v-if="col.name === 'name'">
                      {{
                        `${props.row.prefix}${props.row.firstname}  ${props.row.lastname}`
                      }}
                    </div>
                    <div v-else>
                      {{ col.value ? col.value : "-" }}
                    </div></q-item-label
                  >
                </q-item-section>
              </q-item>
            </q-list>
          </q-card>
        </div>
      </template>
    </d-table>
  </div>
</template>

<style scoped></style>
