<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 type { Pagination } from "@/modules/03_recruiting/interface/index/Main";

import Table from "@/modules/11_discipline/components/3_InvestigateDisciplinary/Table.vue";

import { useCounterMixin } from "@/stores/mixin";
import { useInvestigateDisStore } from "@/modules/11_discipline/store/InvestigateDisStore";

const mixin = useCounterMixin();
const dataInvestigateDis = useInvestigateDisStore();
const { showLoader, hideLoader } = mixin;
const { fetchList } = dataInvestigateDis;
const rowsPerPage = ref<number>(10);
const $q = useQuasar(); // show dialog
const router = useRouter();
const filter = ref<string>(""); //search data table
const page = ref<number>(1);
const maxPage = ref<number>(1);
const status = ref<string>("NEW");
async function fetchListDisciplinary() {
  showLoader();
  await http
    .get(
      config.API.disciplineDisciplinary() +
        `?page=${page.value}&pageSize=${rowsPerPage.value}&keyword=${filter.value}&status=${status.value}`
    )
    .then((res) => {
      const data = res.data.result.data;
      maxPage.value = Math.ceil(res.data.result.total / rowsPerPage.value);
      fetchList(data);
    })
    .catch((err) => {})
    .finally(() => {
      hideLoader();
    });
}

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

async function updatePagingProp(rowPerpage: number, pageCurrent: number) {
  rowsPerPage.value = rowPerpage;
  page.value = pageCurrent;
  await fetchListDisciplinary();
}

function filterStatus(statusReturn: string) {
  status.value = statusReturn;
  fetchListDisciplinary()
}

/**เมื่อเริ่มโหลดหน้า
 * ส่งข้อมูลจำลองไปยัง store
 */
onMounted(async () => {
  await fetchListDisciplinary();
});
</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="dataInvestigateDis.rows"
        :columns="dataInvestigateDis.columns"
        :visible-columns="dataInvestigateDis.visibleColumns"
        v-model:inputfilter="filter"
        v-model:inputvisible="dataInvestigateDis.visibleColumns"
        :pagination="rowsPerPage"
        :nornmalData="true"
        :paging="true"
        :titleText="''"
        :rowsPerPage="rowsPerPage"
        :page="page"
        :maxPage="maxPage"
        :fetchListDisciplinary="fetchListDisciplinary"
        @update:pagination="updatePagingProp"
        v-model:open-edit="openEdit"
        :filterStatus="filterStatus"
      >
      </Table>
    </div>
  </q-card>
</template>

<style></style>
