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

/** import Type*/
import type { QTableProps } from "quasar";

/** import Stores */
import { useInsigniaDataStore } from "@/modules/07_insignia/store";
import { useCounterMixin } from "@/stores/mixin";
/** useStore*/
const mixin = useCounterMixin();
const { showLoader, hideLoader, messageError } = mixin;
const DataStore = useInsigniaDataStore();

const $q = useQuasar();

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

/** ข้อมูลตาราง*/
const rows = ref<any[]>([]);
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: any) => ({
        orgId: e.orgId,
        orgName: e.orgName,
      }));
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

/** Hook*/
onMounted(async () => {
  await fecthOrg();
  DataStore.mainTab = props.tab;
});
</script>
<template>
  <div class="col-12 row q-pa-md">
    <div class="row col-12">
      <div class="col-12">
        <d-table
          ref="table"
          :columns="columns"
          :rows="rows"
          row-key="name"
          dense
          flat
          bordered
          :paging="true"
          class="custom-header-table"
          :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" class="cursor-pointer">
              <q-td key="no" style="width: 50px" :props="props">
                {{ props.rowIndex + 1 }}
              </q-td>
              <q-td key="orgName" style="width: 300px" :props="props">
                {{ props.row.orgName }}
              </q-td>
            </q-tr>
          </template>
        </d-table>
      </div>
    </div>
  </div>
</template>
