<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useQuasar } from "quasar";
import { useRouter } from "vue-router";
import http from "@/plugins/http";
import config from "@/app.config";

import Table from "@/modules/11_discipline/components/4_Result/Table.vue";
import { useCounterMixin } from "@/stores/mixin";
import { useDisciplineResultStore } from "@/modules/11_discipline/store/ResultStore";
import type { Pagination } from "@/modules/03_recruiting/interface/index/Main";

const $q = useQuasar(); // show dialog
const router = useRouter();

const mixin = useCounterMixin();
const store = useDisciplineResultStore();
const { showLoader, hideLoader, messageError } = mixin;
const { fetchList } = store;

const initialPagination = ref<Pagination>({
  rowsPerPage: 0,
});

const page = ref<number>(1);
const pageSize = ref<number>(10);
const maxPage = ref<number>(1);
const filter = ref<string>("");

/** function เรียกรายการสรุปผลการพิจารณาทางวินัย*/
async function fetchListResult() {
  showLoader();
  await http
    .get(
      config.API.listResult() +
        `?page=${page.value}&pageSize=${pageSize.value}&keyword=${filter.value}`
    )
    .then(async (res) => {
      const data = res.data.result.data;
      maxPage.value = Math.ceil(res.data.result.total / pageSize.value);
      await fetchList(data);
      console.log(res);
    })
    .catch((err) => {
      console.log(err);
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

async function updateQueryString(p: number, pS: number, key: string) {
  page.value = pS === pageSize.value ? p : 1;
  pageSize.value = pS;
  filter.value = key;
  await fetchListResult();
}

/**
 * ไปหน้าแก้ไข
 * @param id ไอดีเฉพาะ รายบุคคล
 */
function openEdit(id: string) {
  console.log(id);
  router.push(`/discipline-result/${id}`);
}

/**เมื่อเริ่มโหลดหน้า
 * ส่งข้อมูลจำลองไปยัง store
 */
onMounted(async () => {
  await fetchListResult();
});
</script>

<template>
  <div class="toptitle text-dark col-12 row items-center">
    รายการสรุปผลการพิจารณาความผิดทางวินัย
  </div>
  <q-card flat bordered class="col-12 q-mt-sm q-pt-sm q-pa-md">
    <div>
      <Table
        style="max-height: 80vh"
        :rows="store.rows"
        :columns="store.columns"
        :visible-columns="store.visibleColumns"
        v-model:inputfilter="filter"
        v-model:inputvisible="store.visibleColumns"
        :pagination="initialPagination"
        :nornmalData="true"
        :paging="true"
        :titleText="''"
        :page="page"
        :fetchListResult="fetchListResult"
        :pageSize="pageSize"
        :maxPage="maxPage"
        @update:queryString="updateQueryString"
        v-model:open-edit="openEdit"
      >
      </Table>
    </div>
  </q-card>
</template>

<style></style>
