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

import { useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";
import { useDataStore } from "@/modules/06_retirement/store/resignMain";
import { useRetirementDataStore } from "@/modules/06_retirement/store/Main";
import { checkPermission } from "@/utils/permissions";
import http from "@/plugins/http";
import config from "@/app.config";

import type { QTableProps } from "quasar";
import type { DataStatus } from "@/modules/06_retirement/interface/index/Main";
import type { ResponseItems } from "@/modules/06_retirement/interface/response/Main";

import DialogSendToCommand from "@/modules/06_retirement/components/02_resign/DialogSendToCommand.vue";

/** use */
const $q = useQuasar(); //ใช้ noti quasar
const stroe = useRetirementDataStore();
const stroeResign = useDataStore();
const { statusText } = stroe;
const router = useRouter();
const mixin = useCounterMixin();
const { messageError, date2Thai, showLoader, hideLoader } = mixin;

/** Table */
const rows = ref<ResponseItems[]>([]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: false,
    field: (row) => rows.value.indexOf(row) + 1,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "fullname",
    align: "left",
    label: "ชื่อ-นามสกุล",
    sortable: true,
    field: (row) => `${row.prefix}${row.firstName} ${row.lastName}`,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "location",
    align: "left",
    label: "สถานที่ยื่นขอลาออกจากราชการ",
    sortable: true,
    field: "location",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "positionLevel",
    align: "left",
    label: "ประเภทตำแหน่ง",
    sortable: true,
    field: "positionLevel",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format(val, row) {
      let name = "";
      if (row.positionTypeOld && row.positionLevelOld) {
        name = `${row.positionTypeOld} (${row.positionLevelOld})`;
      } else if (row.positionTypeOld) {
        name = `${row.positionTypeOld}`;
      } else if (row.positionLevelOld) {
        name = `(${row.positionLevelOld})`;
      } else name = "-";
      return name;
    },
  },
  {
    name: "positionNumberOld",
    align: "left",
    label: "เลขที่",
    sortable: true,
    field: "positionNumberOld",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "organizationPositionOld",
    align: "left",
    label: "ตำแหน่ง/สังกัดเดิม",
    sortable: true,
    field: "organizationPositionOld",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "datetext",
    align: "left",
    label: "วันที่ยื่น",
    sortable: true,
    // field: (row) => date2Thai(new Date(row.createdAt)),
    field: "createdAt",
    format(val, row) {
      return date2Thai(new Date(val));
    },
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sortOrder: "da",
  },
  {
    name: "status",
    align: "left",
    label: "สถานะ",
    sortable: true,
    field: (row) => statusText(row.status),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const visibleColumns = ref<string[]>([
  "no",
  "prefix",
  "fullname",
  "location",
  "positionLevel",
  "positionNumberOld",
  "organizationPositionOld",
  "datetext",
  "status",
]);
const rowsSendToCommand = ref<ResponseItems[]>([]);
const modal = ref<boolean>(false);
const filterKeyword2 = ref<string>("");
const filterKeyword = ref<string>("");

const status = ref<string>("");
const optionStatus = ref<DataStatus[]>([]);

/**Setting pagination */
const pagination = ref({
  sortBy: "datetext",
  descending: true,
  page: 1,
  rowsPerPage: 10,
});

/** ฟังก์ชันปิด Popup*/
function closeModal() {
  modal.value = false;
  filterKeyword2.value = "";
}

/** */
async function openModalOrder() {
  const pathAPI =
    stroeResign.mainTabs === "1"
      ? `${config.API.listResign()}?type=APPROVE`
      : `${config.API.listResign()}/cancel?type=APPROVE`;
  showLoader();
  await http
    .get(pathAPI)
    .then(async (res) => {
      const data = await res.data.result;
      const row = await data.filter(
        (r: ResponseItems) =>
          (r.status == "REJECT" || r.status == "APPROVE") &&
          r.organizationPositionOld &&
          r.positionTypeOld &&
          r.positionLevelOld &&
          r.positionNumberOld &&
          r.location &&
          r.sendDate
      );
      rowsSendToCommand.value = row;
      modal.value = true;
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}

/** รายการลาออก*/
async function fecthlist() {
  const pathAPI =
    stroeResign.mainTabs === "1"
      ? `${config.API.listResign()}?type=${status.value}`
      : `${config.API.listResign()}/cancel?type=${status.value}`;
  showLoader();
  await http
    .get(pathAPI)
    .then(async (res) => {
      const data = await res.data.result;
      rows.value = data;
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}

/**
 * ฟังก์ชันต้นหาข้อมูลของ Option ขอสถานะ
 * @param val ค่าที่ต้องการฟิลเตอร์
 * @param update  อัพเดทค่า
 */
function filterOption(val: string, update: Function) {
  update(() => {
    optionStatus.value = stroeResign.optionStatus.filter(
      (v: DataStatus) => v.name.indexOf(val) > -1
    );
  });
}

function onRedirectToDetail(type: string, id: string) {
  const test = stroeResign.mainTabs === "1" ? "" : "-reject";
  router.push(`/retirement/${type}${test}/${id}`);
}

/**Hook */
onMounted(async () => {
  status.value = stroeResign.formQurey.status;
  optionStatus.value = stroeResign.optionStatus;
  await fecthlist();
});
</script>

<template>
  <q-card>
    <div class="row col-12 q-col-gutter-sm">
      <div class="row col-12">
        <div class="row q-gutter-sm">
          <q-select
            v-model="status"
            :label="`${'สถานะ'}`"
            option-label="name"
            :options="optionStatus"
            option-value="value"
            dense
            emit-value
            map-options
            lazy-rules
            hide-bottom-space
            outlined
            use-input
            hide-selected
            fill-input
            @update:model-value="
              (stroeResign.formQurey.status = status), fecthlist()
            "
            @filter="(inputValue:string,doneFn:Function) => filterOption(inputValue, doneFn) "
          >
            <template v-slot:no-option>
              <q-item>
                <q-item-section class="text-grey"> ไม่มีข้อมูล </q-item-section>
              </q-item>
            </template>
          </q-select>
          <q-btn
            v-if="checkPermission($route)?.attrIsUpdate"
            @click="openModalOrder"
            flat
            round
            color="add"
            icon="mdi-account-arrow-right"
          >
            <q-tooltip>ส่งไปออกคำสั่งลาออก</q-tooltip>
          </q-btn>
        </div>
        <q-space />
        <div class="row q-gutter-sm">
          <q-input
            standout
            dense
            v-model="filterKeyword"
            ref="filterRef"
            outlined
            debounce="300"
            placeholder="ค้นหา"
          >
            <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
          :columns="columns"
          :rows="rows"
          :filter="filterKeyword.trim()"
          row-key="id"
          :visible-columns="visibleColumns"
          v-model:pagination="pagination"
        >
          <template v-slot:header="props">
            <q-tr :props="props">
              <q-th auto-width></q-th>
              <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 auto-width>
                <q-btn
                  v-if="checkPermission($route)?.attrIsGet"
                  flat
                  dense
                  round
                  color="info"
                  icon="mdi-eye"
                  @click.pervent="
                    onRedirectToDetail('resign-detail', props.row.id)
                  "
                >
                  <q-tooltip>รายละเอียด</q-tooltip>
                </q-btn>
                <q-btn
                  v-if="
                    checkPermission($route)?.attrIsGet &&
                    checkPermission($route)?.attrIsUpdate
                  "
                  flat
                  dense
                  round
                  color="edit"
                  icon="edit"
                  @click.pervent="onRedirectToDetail('resign', props.row.id)"
                >
                  <q-tooltip>แก้ไขข้อมูล</q-tooltip>
                </q-btn>
              </q-td>
              <q-td v-for="col in props.cols" :key="col.id">
                <div
                  :class="
                    col.name === 'organizationPositionOld' ||
                    col.name === 'organization'
                      ? 'table_ellipsis2'
                      : ''
                  "
                >
                  {{ col.value ? col.value : "" }}
                </div>
              </q-td>
            </q-tr>
          </template>
        </d-table>
      </div>
    </div>
  </q-card>

  <DialogSendToCommand
    v-model:modal="modal"
    v-model:filter-keyword2="filterKeyword2"
    :close-modal="closeModal"
    :rows="rowsSendToCommand"
    :fecth-list="fecthlist"
    :main-tabs="stroeResign.mainTabs"
  />
</template>
<style scoped lang="scss"></style>
