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

import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";

/** importType*/
import type { QTableProps } from "quasar";
import type { DataDiscipline } from "@/modules/13_salary/interface/response/Main";

/** importComponents*/
import SkeletonTable from "@/components/SkeletonTable.vue";

const $q = useQuasar();
const { date2Thai, messageError, onSearchDataTable } = useCounterMixin();

/** props*/
const profileId = defineModel<string>("profileId", { required: true });
const employeeClass = defineModel<string>("employeeClass", { required: true });

const isLoading = ref<boolean>(true);

/** Table*/
const keyword = ref<string>("");
const rows = ref<DataDiscipline[]>([]);
const rowsData = ref<DataDiscipline[]>([]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "year",
    align: "left",
    label: "ปีที่ยื่นขอ",
    sortable: true,
    field: "year",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => (v ? v + 543 : "-"),
  },
  {
    name: "receiveDate",
    align: "left",
    label: "วันที่ได้รับ",
    sortable: true,
    field: "receiveDate",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => date2Thai(v),
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "insignia",
    align: "left",
    label: "ชื่อเครื่องราชฯ",
    sortable: true,
    field: "insigniaId",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => insigniaData.value.find((r) => r.id === v)?.name,
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "insigniaType",
    align: "left",
    label: "ลำดับชั้น",
    sortable: true,
    field: "insigniaType",
    format(val, row) {
      return row.insignia.insigniaType.name;
    },
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "no",
    align: "left",
    label: "ลำดับที่",
    sortable: true,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "issue",
    align: "left",
    label: "ราชกิจจาฯ ฉบับที่",
    sortable: true,
    field: "issue",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "volumeNo",
    align: "left",
    label: "เล่มที่",
    sortable: true,
    field: "volumeNo",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "volume",
    align: "left",
    label: "เล่ม",
    sortable: true,
    field: "volume",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "section",
    align: "left",
    label: "ตอน",
    sortable: true,
    field: "section",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "page",
    align: "left",
    label: "หน้า",
    sortable: true,
    field: "page",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "dateAnnounce",
    align: "left",
    label: "วันที่ประกาศในราชกิจจาฯ",
    sortable: true,
    field: "dateAnnounce",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => date2Thai(v),
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "refCommandNo",
    align: "left",
    label: "เลขที่คำสั่ง",
    sortable: true,
    field: "refCommandNo",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "refCommandDate",
    align: "left",
    label: "เอกสารอ้างอิง (ลงวันที่)",
    sortable: true,
    field: "refCommandDate",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => date2Thai(v),
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "note",
    align: "left",
    label: "หมายเหตุ",
    sortable: true,
    field: "note",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);
const visibleColumns = ref<String[]>([
  "insignia",
  "insigniaType",
  "year",
  "no",
  "issue",
  "volumeNo",
  "volume",
  "section",
  "page",
  "receiveDate",
  "dateAnnounce",
  "refCommandNo",
  "refCommandDate",
  "note",
]);

const insigniaData = ref<any[]>([]);

/** fetch ข้อมูลรายการเครื่องราชอิสริยาภรณ์*/
async function fetchListInsignia() {
  http
    .get(
      config.API.profileNewInsignByProfileId(
        profileId.value,
        employeeClass.value
      )
    )
    .then((res) => {
      rowsData.value = res.data.result;
      onSearch();
    })
    .catch((err) => {
      messageError($q, err);
    });
}

/** fetch ข้อมูลเครื่องราชอิสริยาภรณ์*/
async function fetchInsignia() {
  isLoading.value = true;
  await http
    .get(config.API.insigniaOrg)
    .then(async (res) => {
      insigniaData.value = res.data.result;
      await fetchListInsignia();
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      isLoading.value = false;
    });
}

function onSearch() {
  rows.value = onSearchDataTable(
    keyword.value,
    rowsData.value,
    columns.value ? columns.value : []
  );
}

onMounted(async () => {
  await fetchInsignia();
});
</script>

<template>
  <q-card-section class="q-pt-none">
    <div class="row items-center q-gutter-x-sm q-pb-sm">
      <q-space />
      <q-input
        dense
        outlined
        v-model="keyword"
        label="ค้นหา"
        class="q-mr-sm"
        @keydown.enter.prevent="onSearch"
      >
        <template v-slot:append>
          <q-icon name="search" />
        </template>
      </q-input>

      <q-select
        v-model="visibleColumns"
        multiple
        outlined
        dense
        options-dense
        :display-value="$q.lang.table.columns"
        emit-value
        map-options
        :options="columns"
        option-value="name"
        style="min-width: 140px"
      />
    </div>

    <div class="col-12">
      <SkeletonTable v-if="isLoading" :columns="columns" />

      <d-table
        v-else
        ref="table"
        row-key="id"
        flat
        dense
        bordered
        :paging="true"
        :rows-per-page-options="[20, 50, 100]"
        :visible-columns="visibleColumns"
        :rows="rows"
        :columns="columns"
      >
        <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.id">
              <div class="table_ellipsis">
                {{ col.value ? col.value : "-" }}
              </div>
            </q-td>
          </q-tr>
        </template>
      </d-table>
    </div>
  </q-card-section>
</template>

<style lang="sass" scoped></style>
