<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import config from "@/app.config";
import http from "@/plugins/http";

import type { QTableProps } from "quasar";
import type { DataOption } from "@/modules/10_registry/interface/index/Main";

import DialogAddPetiton from "@/modules/10_registry/components/DialogAddPetiton.vue";

import { useRequestEditStore } from "@/modules/10_registry/store/RequestEdit";
import { useCounterMixin } from "@/stores/mixin";

const store = useRequestEditStore();
const router = useRouter();
const { showLoader, hideLoader, messageError } = useCounterMixin();

/**
 * ตัวแปร
 */
const modalPetiton = ref<boolean>(false);
const status = ref<string>("");
const keyword = ref<string>("");
const statusOption = ref<DataOption[]>(store.optionStatus);
const dataList = ref<any[]>([]);

/**
 * Table
 */
const rows = ref<any[]>([]);
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",
  },
  {
    name: "topic",
    align: "left",
    label: "ชื่อเรื่อง",
    sortable: true,
    field: "topic",
    format: (v) => (v ? v : "-"),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "detail",
    align: "left",
    label: "รายละเอียด",
    sortable: true,
    field: "detail",
    format: (v) => (v ? v : "-"),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "document",
    align: "left",
    label: "หลักฐานอ้างอิง",
    sortable: true,
    field: "document",
    format: (v) => (v ? v : "-"),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "status",
    align: "left",
    label: "สถานะคำร้อง",
    sortable: true,
    field: "status",
    format: (v) => store.convertStatus(v),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "remark",
    align: "left",
    label: "หมายเหตุ ",
    sortable: true,
    field: "remark",
    format: (v) => (v ? v : "-"),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const visibleColumns = ref<string[]>([
  "no",
  "topic",
  "detail",
  "document",
  "status",
  "remark",
]);

function onclickBackPage() {
  router.push(`/registry`);
}

function onClickAdd() {
  modalPetiton.value = true;
}

function fetchListRequset() {
  const data = [
    {
      topic: "ขอแก้ไขคำนำหน้านาม ชื่อ นามสกุล",
      detail: "ขอแก้ไขคำนำหน้านาม",
      document: null,
      status: "PENDING",
      remark: "",
    },
    {
      topic: "ขอแก้ไขข้อมูลประวัติการศึกษา",
      detail: "ขอแก้ไขคำนำหน้านาม",
      document: null,
      status: "COMPLETE",
      remark: "",
    },
    {
      topic: "ขอแก้ไขชื่อ - นามสกุล คู่สมรส",
      detail: "ขอแก้ไขคำนำหน้านาม",
      document: null,
      status: "REJECT",
      remark: "",
    },
  ];
  dataList.value = data;
  rows.value = dataList.value;
}

function updateStatusValue(val: string) {
  if (val) {
    rows.value = dataList.value.filter((e) => e.status === val);
  } else {
    rows.value = dataList.value;
  }
}

function clearStatus() {
  status.value = "";
  statusOption.value = store.optionStatus;
}

function filterOption(val: string, update: Function) {
  update(() => {
    status.value = val ? "" : status.value;
    statusOption.value = store.optionStatus.filter(
      (v: any) => v.name.indexOf(val) > -1
    );
  });
}

onMounted(() => {
  fetchListRequset();
});
</script>

<template>
  <div class="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="onclickBackPage"
        />
        รายการยื่นคำร้องขอแก้ไขข้อมูล
      </div>
      <q-card bordered class="q-pa-md">
        <div class="row q-mb-sm q-col-gutter-sm">
          <div class="col-xs-10 col-md-2">
            <q-select
              v-model="status"
              label="สถานะคำร้อง"
              dense
              outlined
              emit-value
              map-options
              option-label="name"
              option-value="id"
              :options="statusOption"
              @update:model-value="updateStatusValue"
              :clearable="status !== ''"
              @clear="clearStatus"
              use-input
              @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>
          </div>
          <div>
            <q-btn
              flat
              round
              color="primary"
              icon="mdi-plus"
              @click="onClickAdd()"
            >
              <q-tooltip>ยื่นคำร้องขอแก้ไขข้อมูล</q-tooltip>
            </q-btn>
          </div>
          <q-space />
          <q-input
            v-model="keyword"
            outlined
            clearable
            dense
            label="ค้นหา"
            style="width: 250px"
          >
          </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"
            options-cover
            class="col-xs-12 col-sm-3 col-md-2"
          />
        </div>
        <div class="col-12">
          <d-table
            :columns="columns"
            :rows="rows"
            row-key="id"
            :rows-per-page-options="[10, 25, 50, 100]"
            :paging="true"
            :visible-columns="visibleColumns"
            :filter="keyword"
          >
            <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">
                  <div v-if="col.name == 'no'">
                    {{ props.rowIndex + 1 }}
                  </div>
                  <div v-else class="table_ellipsis2">
                    {{ col.value ? col.value : "-" }}
                  </div>
                </q-td>
              </q-tr>
            </template>
          </d-table>
        </div>
      </q-card>
    </div>
  </div>

  <DialogAddPetiton
    v-model:modal="modalPetiton"
    :fetchData="fetchListRequset"
  />
</template>

<style scoped></style>
