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

import { useInsigniaDataStore } from "@/modules/07_insignia/store";
import { useCounterMixin } from "@/stores/mixin";
import http from "@/plugins/http";
import config from "@/app.config";

/** import Type*/
import type { QTableProps } from "quasar";
import type { ResponseNoSend } from "@/modules/07_insignia/interface/response/Main";

/** use*/
const $q = useQuasar();
const mixin = useCounterMixin();
const { showLoader, hideLoader, messageError } = mixin;
const DataStore = useInsigniaDataStore();

/**
 * props
 */
const props = defineProps({
  roundId: {
    type: String,
  },
  tab: {
    type: String,
  },
});

/** ข้อมูลตาราง*/
const rows = ref<ResponseNoSend[]>([]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "orgName",
    align: "left",
    label: "หน่วยงาน",
    sortable: true,
    field: "orgName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const visibleColumns = ref<string[]>(["no", "orgName"]);
const pagination = ref({
  sortBy: "name",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});

/** function เรียกหน่วยงานที่ยังไม่ได้เสนอชื่อ*/
async function fecthOrg() {
  showLoader();
  await http
    .get(config.API.insigniaNosend(props.roundId))
    .then((res) => {
      let data = res.data.result;
      rows.value = data.map((e: ResponseNoSend) => ({
        orgId: e.orgId,
        orgName: e.orgName,
      }));
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

/** Hook*/
onMounted(async () => {
  await fecthOrg();
  DataStore.mainTab = props.tab;
});
</script>

<template>
  <q-card>
    <q-card-section>
      <d-table
        :columns="columns"
        :rows="rows"
        row-key="name"
        dense
        flat
        bordered
        :paging="true"
        :visible-columns="visibleColumns"
        v-model:pagination="pagination"
      >
        <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">
            <q-td v-for="col in props.cols" :key="col.name" :props="props">{{
              col.name == "no" ? props.rowIndex + 1 : col.value
            }}</q-td>
          </q-tr>
        </template>
      </d-table>
    </q-card-section>
  </q-card>
</template>
