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

import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import { useInvestigateDisStore } from "@/modules/11_discipline/store/InvestigateDisStore";

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

const total = ref<number>(0);
const totalList = ref<number>(1);
const pagination = ref({
  sortBy: "createdAt",
  descending: true,
  page: 1,
  rowsPerPage: 10,
});

const router = useRouter();
const mixin = useCounterMixin();
const dataInvestigateDis = useInvestigateDisStore();
const { showLoader, hideLoader } = mixin;
const { fetchList } = dataInvestigateDis;

const filter = ref<string>(""); //search data table

const status = ref<string>("NEW");
async function fetchListDisciplinary() {
  showLoader();
  await http
    .get(
      config.API.disciplineDisciplinary() +
        `?page=${pagination.value.page}&pageSize=${pagination.value.rowsPerPage}&keyword=${filter.value.trim()}&status=${status.value}`
    )
    .then((res) => {
      const data = res.data.result.data;
      totalList.value = Math.ceil(
        res.data.result.total / pagination.value.rowsPerPage
      );
      total.value = res.data.result.total;
      fetchList(data);
    })
    .catch((err) => {})
    .finally(() => {
      hideLoader();
    });
}

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

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

function getSearch() {
  pagination.value.page = 1;
  fetchListDisciplinary();
}

watch(
  () => pagination.value.rowsPerPage,
  async () => {
    getSearch();
  }
);

/**เมื่อเริ่มโหลดหน้า
 * ส่งข้อมูลจำลองไปยัง 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"
        :nornmalData="true"
        :paging="true"
        :titleText="''"
        v-model:pagination="pagination"
        v-model:total="total"
        v-model:total-list="totalList"
        :fetchListDisciplinary="fetchListDisciplinary"
        v-model:open-edit="openEdit"
        :open-detail="openDetail"
        :filterStatus="filterStatus"
        :get-search="getSearch"
      >
      </Table>
    </div>
  </q-card>
</template>

<style></style>
