<script setup lang="ts">
import type { QTableProps } from "quasar";
import { ref, onMounted } from "vue";
import { useQuasar } from "quasar";
import { useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";
import http from "@/plugins/http";
import config from "@/app.config";
import { useRestDataStore } from "@/modules/03_retire/store";
import Table from "@/components/Table.vue";

const RestData = useRestDataStore();
const { statusText } = RestData;
const router = useRouter();
const $q = useQuasar();
const mixin = useCounterMixin();
const { date2Thai, messageError, showLoader, hideLoader } = mixin;

/**
 * เพิ่มหัวข้อตาราง
 */
const rows = ref<any>([]);
const filter = ref<string>("");
const visibleColumns = ref<String[]>([
  "no",
  "placeLeave",
  "dateStartLeave",
  "dateLeave",
  "statustext",
]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: true,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px; width:5%;",
  },
  {
    name: "placeLeave",
    align: "left",
    label: "สถานที่ยื่นขอลาออกจากราชการ",
    sortable: true,
    field: "placeLeave",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px; width:15%;",
  },
  {
    name: "dateStartLeave",
    align: "left",
    label: "วันที่ยื่นขอลาออกจากราชการ",
    sortable: true,
    field: "dateStartLeave",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px; width:15%;",
  },
  {
    name: "dateLeave",
    align: "left",
    label: "วันที่ขอลาออกจากราชการ",
    sortable: true,
    field: "dateLeave",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "statustext",
    align: "left",
    label: "สถานะ",
    sortable: true,
    field: "statustext",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px; width:10%;",
  },
]);

/**
 * ตั้งค่า pagination
 */
const initialPagination = ref({
  rowsPerPage: 0,
});

/**
 * เรียกฟังก์ชันทั้งหมดตอนเรียกใช้ไฟล์นี้
 */
onMounted(() => {
  fectListleave();
});

//นำข้อมูลมาแสดง
const fectListleave = async () => {
  showLoader();
  await http
    .get(config.API.listUser())
    .then((res: any) => {
      let data = res.data.result;
      rows.value = data.map((e: any) => ({
        id: e.id,
        placeLeave: e.location,
        dateStartLeave: date2Thai(e.sendDate),
        dateLeave: date2Thai(e.activeDate),
        status: e.status,
        statustext: statusText(e.status),
      }));
    })
    .catch((e: any) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
};

/**
 * ฟังก์ชั่นกดเพิ่มไปหน้าเพิ่มลาออก
 */
const clickAdd = async () => {
  router.push(`/retire/add`);
};

/**
 * กดเพื่อย้อนกลับ
 */
const clickBack = () => {
  router.push(`/`);
};
</script>
<template>
  <div class="col-12 row justify-center">
    <div class="col-xs-12 col-sm-12 col-md-11">
      <div class="toptitle text-white col-12 row items-center">
        <q-btn
          icon="mdi-arrow-left"
          unelevated
          round
          dense
          flat
          color="primary"
          class="q-mr-sm"
          @click="clickBack"
        />
        ลาออก
      </div>
      <div class="col-12">
        <q-card bordered class="q-pa-md">
          <div class="q-pb-sm row">
            <div>
              <q-btn
                size="14px"
                flat
                dense
                color="blue"
                @click="clickAdd"
                icon="mdi-plus"
              >
                <q-tooltip>เพิ่มข้อมูล</q-tooltip>
              </q-btn>
            </div>
            <q-space />
            <div class="items-center q-gutter-sm" style="display: flex">
              <!-- ค้นหาข้อความใน table -->
              <q-input
                standout
                dense
                v-model="filter"
                ref="filterRef"
                outlined
                debounce="300"
                placeholder="ค้นหา"
                style="max-width: 200px"
              >
                <template v-slot:append>
                  <q-icon v-if="filter == ''" name="search" />
                  <q-icon
                    v-if="filter !== ''"
                    name="clear"
                    class="cursor-pointer"
                    @click="filter = ''"
                  />
                </template>
              </q-input>
              <!-- แสดงคอลัมน์ใน table -->
              <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"
                options-cover
                style="min-width: 150px"
              />
            </div>
          </div>
          <d-table
            ref="table"
            :columns="columns"
            :rows="rows"
            :filter="filter"
            row-key="name"
            flat
            bordered
            :paging="true"
            dense
            class="custom-header-table"
            :visible-columns="visibleColumns"
            :rows-per-page-options="[10, 25, 50, 100]"
          >
            <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.id"
                  @click="router.push(`/retire/` + props.row.id)"
                >
                  <div v-if="col.name == 'no'">
                    {{ props.rowIndex + 1 }}
                  </div>
                  <div v-else>
                    {{ col.value }}
                  </div>
                </q-td>
              </q-tr>
            </template>
          </d-table>
        </q-card>
      </div>
    </div>
  </div>
</template>
<style></style>
