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

/** importStores*/
import { useCounterMixin } from "@/stores/mixin";
import { useLeavelistDataStore } from "@/modules/09_leave/stores/LeaveStore";
const mixin = useCounterMixin();
const leaveStore = useLeavelistDataStore();

const router = useRouter();

/** เรียน funtion จาก stores*/
// const { date2Thai } = mixin;
// const { optionYear, searchDataFn, filterOption } = leaveStore;

/** ข้อมูลหัวตาราง รายการลา */
const columnsLeave = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "leaveTypeName",
    align: "left",
    label: "ประเภทการลา",
    sortable: true,
    field: "leaveTypeName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "citizenId",
    align: "left",
    label: "เลขประจำตัวประชาชน",
    field: "citizenId",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "fullName",
    align: "left",
    label: "ผู้ยื่นใบลา",
    sortable: true,
    field: "fullName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "agency",
    align: "left",
    label: "หน่วยงาน",
    sortable: true,
    field: "agency",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "org",
    align: "left",
    label: "ส่วนราชการ",
    sortable: true,
    field: "org",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "position",
    align: "left",
    label: "ตำแหน่ง",
    sortable: true,
    field: "position",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "level",
    align: "left",
    label: "ระดับ",
    sortable: true,
    field: "level",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "dateSendLeave",
    align: "left",
    label: "วันที่ยื่นใบลา",
    sortable: true,
    field: "dateSendLeave",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "dateLeave",
    align: "left",
    label: "วันที่ลา",
    sortable: true,
    field: "dateLeave",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "status",
    align: "left",
    label: "สถานะ",
    sortable: true,
    field: "status",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const visibleColumnsLeave = ref<string[]>([
  "no",
  "leaveTypeName",
  "citizenId",
  "fullName",
  "dateSendLeave",
  "dateLeave",
  "agency",
  "org",
  "position",
  "level",
  "status",
]);

const columnsReject = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "leaveTypeName",
    align: "left",
    label: "ประเภทการลา",
    sortable: true,
    field: "leaveTypeName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "fullName",
    align: "left",
    label: "ผู้ยื่นยกเลิกใบลา",
    sortable: true,
    field: "fullName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "dateSendLeave",
    align: "left",
    label: "วันที่ขอยกเลิก",
    sortable: true,
    field: "dateSendLeave",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "status",
    align: "left",
    label: "สถานะการยกเลิก",
    sortable: true,
    field: "status",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const visibleReject = ref<string[]>([
  "no",
  "leaveTypeName",
  "fullName",
  "dateSendLeave",
  "status",
]);

const props = defineProps({
  rows: {
    type: Object,
    require: true,
  },
  page: {
    type: Number,
    require: true,
  },
  rowsPerPage: {
    type: Number,
    require: true,
  },
  maxPage: {
    type: Number,
    require: true,
  },
});

const emit = defineEmits(["update:querySting"]);

function updateQuerySting(newPage: number, pageSize: number) {
  // ส่ง event ไปยัง parent component เพื่ออัพเดทค่า props
  emit("update:querySting", newPage, pageSize);
}

const currentPage = ref<number>(1);
const pagination = ref({
  descending: true,
  page: props.page,
  rowsPerPage: props.rowsPerPage,
});
function redirectToDetail(id: string) {
  const routePrefix = leaveStore.tabMenu === "1" ? "/leave" : "/leave-reject";
  router.push(`${routePrefix}/detail/${id}`);
}

function updatedPagination(newPageZize: any) {
  currentPage.value = 1;
  pagination.value.rowsPerPage = newPageZize.rowsPerPage;
}

watch([() => currentPage.value, () => pagination.value.rowsPerPage], () => {
  currentPage.value &&
    pagination.value.rowsPerPage &&
    updateQuerySting(currentPage.value, pagination.value.rowsPerPage);
});

/** Hook*/
onMounted(() => {
  if (leaveStore.tabMenu === "1") {
    leaveStore.visibleColumns = visibleColumnsLeave.value;
    leaveStore.columns = columnsLeave.value;
  } else if (leaveStore.tabMenu === "2") {
    leaveStore.visibleColumns = visibleReject.value;
    leaveStore.columns = columnsReject.value;
  }
});
</script>

<template>
  <d-table
    ref="table"
    :columns="leaveStore.columns"
    :rows="leaveStore.rows"
    row-key="id"
    flat
    bordered
    :paging="true"
    dense
    class="custom-header-table"
    :rows-per-page-options="[10, 25, 50, 100]"
    :visible-columns="leaveStore.visibleColumns"
    :pagination="pagination"
    @update:pagination="updatedPagination"
  >
    <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
          v-for="col in props.cols"
          :key="col.name"
          :props="props"
          @click.prevent="redirectToDetail(props.row.id)"
        >
          <div v-if="col.name == 'no'">
            {{
              (currentPage - 1) * Number(pagination.rowsPerPage) +
              props.rowIndex +
              1
            }}
          </div>

          <div v-else>
            {{ col.value }}
          </div>
        </q-td>
      </q-tr>
    </template>
    <template v-slot:pagination="scope">
      <q-pagination
        v-model="currentPage"
        active-color="primary"
        color="dark"
        :max="Number(props.maxPage)"
        size="sm"
        boundary-links
        direction-links
      ></q-pagination>
    </template>
  </d-table>
</template>

<style scoped></style>
