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

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

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 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 maxPage = defineModel<number>("maxPage", { required: true });

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

const pagination = ref({
  sortBy: "desc",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});

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

<template>
  <div class="col-12">
    <d-table
      ref="table"
      :columns="columns"
      :rows="rows"
      row-key="id"
      flat
      bordered
      :paging="true"
      dense
      class="custom-table2"
      :rows-per-page-options="[10, 25, 50, 100]"
      :visible-columns="visibleColumns"
      v-model:pagination="pagination"
      @update:pagination="props.updatePagination"
    >
      <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 === 'name'">
              {{
                `${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>
      <template v-slot:pagination="scope">
        ทั้งหมด {{ total }} รายการ
        <q-pagination
          v-model="formQuery.page"
          active-color="primary"
          color="dark"
          :max="Number(maxPage)"
          size="sm"
          boundary-links
          direction-links
          :max-pages="5"
          @update:model-value="props.fetchList?.()"
        ></q-pagination>
      </template>
    </d-table>
  </div>
</template>

<style scoped></style>
