<script setup lang="ts">
import { onMounted, ref, watch } from "vue";
import { useCounterMixin } from "@/stores/mixin";
import { useQuasar } from "quasar";
import type { QTableProps } from "quasar";
import http from "@/plugins/http";
import config from "@/app.config";
import { useInsigniaDataStore } from "@/modules/07_insignia/store";

const $q = useQuasar();
const mixin = useCounterMixin();
const { showLoader, hideLoader, messageError } = mixin;
const DataStore = useInsigniaDataStore();
const props = defineProps({
  roundId: {
    type: String,
  },
  tab: {
    type: String,
  },
});

const visibleColumns = ref<string[]>(["no", "orgName"]);
// หัวตาราง
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: true,
    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 rows = ref<any[]>([]);

onMounted(async () => {
  await fecthOrg();
  DataStore.mainTab = props.tab;
});
watch(props, async () => {
  if (props.tab === "organization") {
    await fecthOrg();
  }
});

const fecthOrg = async () => {
  showLoader();
  await http
    .get(config.API.insigniaNosend(props.roundId))
    .then((res) => {
      console.log(res);
      let data = res.data.result;
      rows.value = data.map((e: any) => ({
        orgId: e.orgId,
        orgName: e.orgName,
      }));
      console.log(rows.value);
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
};

const pagination = ref({
  sortBy: "name",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});
const paging = ref<boolean>(true);
const paginationLabel = (start: number, end: number, total: number) => {
  if (paging.value == true) return " " + start + "-" + end + " ใน " + total;
  else return start + "-" + end + " ใน " + total;
};
</script>
<template>
  <div class="col-12 row q-pa-md">
    <div class="row col-12">
      <div class="col-12">
        <q-table
          ref="table"
          :columns="columns"
          :rows="rows"
          row-key="name"
          dense
          flat
          bordered
          :paging="true"
          class="custom-header-table"
          :visible-columns="visibleColumns"
          :pagination-label="paginationLabel"
          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>
          <template v-slot:pagination="scope">
            <q-pagination
              v-model="pagination.page"
              active-color="primary"
              color="primary"
              :max="scope.pagesNumber"
              :max-pages="5"
              size="sm"
              boundary-links
              direction-links
            ></q-pagination>
          </template>
        </q-table>
      </div>
    </div>
  </div>
</template>
