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

import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import { useRestDataStore } from "@/modules/03_retire/store";

import type {
  MainList,
  MainListResponse,
} from "@/modules/03_retire/interface/Main";

const $q = useQuasar();
const router = useRouter();
const mixin = useCounterMixin();
const { date2Thai, messageError, showLoader, hideLoader } = mixin;
const RestData = useRestDataStore();
const { statusText } = RestData; //Func แปลง status to text

const rows = ref<MainList[]>([]);
const filter = ref<string>("");
const pagination = ref({
  sortBy: "desc",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});

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%;",
  },
]);

/** ดึงข้อมูล */
async function fectListleave() {
  await http
    .get(config.API.listUser())
    .then(async (res) => {
      let data = await res.data.result;
      rows.value = data.map((e: MainListResponse) => ({
        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(() => {});
}

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

/** ทำก่อน mounted */
onBeforeMount(() => {
  showLoader();
});

/**
 * เรียกฟังก์ชันทั้งหมดตอนเรียกใช้ไฟล์นี้
 */
onMounted(async () => {
  await fectListleave();
  hideLoader();
});
</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">ขอลาออก</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"
                :display-value="$q.lang.table.columns"
                multiple
                outlined
                dense
                :options="columns"
                options-dense
                option-value="name"
                map-options
                emit-value
                style="min-width: 150px"
                class="gt-xs"
              >
                <template> </template>
              </q-select>
            </div>
          </div>
          <div>
            <d-table
              flat
              bordered
              dense
              row-key="id"
              :rows="rows"
              :columns="columns"
              :visible-columns="visibleColumns"
              :rows-per-page-options="[10, 25, 50, 100]"
              v-model:pagination="pagination"
            >
              <template v-slot:pagination="scope">
                ทั้งหมด {{ rows.length }} รายการ
                <q-pagination
                  v-model="pagination.page"
                  active-color="primary"
                  color="dark"
                  :max="scope.pagesNumber"
                  :max-pages="5"
                  size="sm"
                  boundary-links
                  direction-links
                ></q-pagination>
              </template>
              <template v-slot:header="props">
                <q-tr :props="props">
                  <q-th
                    v-for="col in props.cols"
                    :key="col.name"
                    :props="props"
                    style="color: #000000; font-weight: 500"
                  >
                    <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="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>
              <template #item="props">
                <div class="q-pa-xs col-xs-12 col-sm-6 col-md-4 col-lg-3">
                  <q-card
                    bordered
                    flat
                    @click="router.push(`/retire/` + props.row.id)"
                  >
                    <q-list>
                      <q-item
                        v-for="col in props.cols.filter((col:any) => col.name !== 'desc')"
                        :key="col.name"
                      >
                        <q-item-section>
                          <q-item-label caption>{{ col.label }}</q-item-label>

                          <q-item-label v-if="col.name === 'no'">
                            {{ props.rowIndex + 1 }}
                          </q-item-label>

                          <q-item-label v-else>{{ col.value }}</q-item-label>
                        </q-item-section>
                      </q-item>
                    </q-list>
                  </q-card>
                </div>
              </template>
            </d-table>
          </div>
        </q-card>
      </div>
    </div>
  </div>
</template>
<style scoped lang="scss"></style>
