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

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

import type { QTableColumn } from "quasar";

import DialogHeader from "@/components/DialogHeader.vue";

const $q = useQuasar();
const route = useRoute();
const { showLoader, hideLoader, messageError, date2Thai, onSearchDataTable } =
  useCounterMixin();

const profileId = ref<string>(route.params.id.toString());

const modal = defineModel<boolean>("modal", { required: true });
const empType = defineModel<string>("empType", { required: true });

const keyword = ref<string>("");
const rows = ref<any[]>([]);
const rowsMain = ref<any[]>([]);
const columns = ref<QTableColumn[]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },

  {
    name: "detailForEdit",
    align: "left",
    label: "รายละเอียดการแก้ไข",
    sortable: true,
    field: "detailForEdit",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",

    sort: (a: string, b: string) =>
      a
        .toString()
        .localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "lastUpdateFullName",
    align: "left",
    label: "ผู้ดำเนินการ",
    sortable: true,
    field: "lastUpdateFullName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a
        .toString()
        .localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "returnedDate",
    align: "left",
    label: "วันที่ตีกลับ",
    sortable: true,
    field: "returnedDate",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => date2Thai(v, false, true),
    sort: (a: string, b: string) =>
      a
        .toString()
        .localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);
const visibleColumns = ref<string[]>([
  "no",
  "returnedDate",
  "detailForEdit",
  "lastUpdateFullName",
]);

/** function ปิด popup ประวัติการดำเนินการ*/
function onClickCloseDialog() {
  modal.value = false;
}

/** function fetch รายการข้อมูลประวัติการดำเนินการ*/
function fetchDataHistory() {
  showLoader();
  http
    .get(config.API.salaryTemp + `/return-edit/history/${profileId.value}`)
    .then((res) => {
      const data = res.data.result;
      rows.value = data;
      rowsMain.value = data;
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

/** function ค้นหาข้อมูลรายการในตาราง*/
function serchDataTable() {
  rows.value = onSearchDataTable(
    keyword.value,
    rowsMain.value,
    columns.value ? columns.value : []
  );
}

/** ตรวจสอบค่าของตัวแปร modal */
watch(modal, async (val) => {
  // เช็คว่าถ้า modal เป็น true
  if (val) {
    //เรียกฟังก์ชันดึงข้อมูลประวัติการดำเนินการ
    fetchDataHistory();
  } else {
    keyword.value = "";
  }
});
</script>

<template>
  <q-dialog v-model="modal" persistent>
    <q-card style="min-width: 80%">
      <DialogHeader
        :tittle="'ประวัติการดำเนินการ'"
        :close="onClickCloseDialog"
      />
      <q-separator />

      <q-card-section>
        <div class="row q-col-gutter-sm">
          <div class="col-12">
            <div class="row q-col-gutter-sm">
              <q-space />
              <q-input
                dense
                outlined
                v-model="keyword"
                label="ค้นหา"
                @keydown.enter.prevent="serchDataTable"
              >
                <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>

          <div class="col-12">
            <d-table
              ref="table"
              row-key="id"
              flat
              bordered
              dense
              :columns="columns"
              :rows="rows"
              :paging="true"
              :rows-per-page-options="[20, 50, 100]"
              :visible-columns="visibleColumns"
            >
              >
              <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 v-if="col.name === 'no'">
                      {{ props.rowIndex + 1 }}
                    </div>

                    <div v-else>
                      {{ col.value ? col.value : "-" }}
                    </div>
                  </q-td>
                </q-tr>
              </template>
            </d-table>
          </div>
        </div>
      </q-card-section>

      <q-separator />
    </q-card>
  </q-dialog>
</template>

<style scoped></style>
