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

/**
 * importComponents
 */
import DialogStatus from "@/modules/04_registryNew/components/requestEdit/DialogStatus.vue";

/**
 * importType
 */
import type { QTableProps } from "quasar";
import type { DataOption } from "@/modules/04_registryNew/interface/index/Main";

/**
 * importStore
 */
import { useRequestEditStore } from "@/modules/04_registryNew/stores/RequestEdit";

/**
 * use
 */
const router = useRouter();
const store = useRequestEditStore();

/**
 * Table
 */
const rows = ref<any[]>([]);
const dataList = 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: "fullname",
    align: "left",
    label: "ชื่อ-นามสกุล",
    sortable: false,
    field: "fullname",
    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",
  "fullname",
  "topic",
  "detail",
  "document",
  "status",
  "remark",
]);

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

function fetchListRequest() {
  const data = [
    {
      id: "1",
      fullname: "นายเกียรติศักดิ์ บัณฑิต",
      topic: "ขอแก้ไขคำนำหน้านาม ชื่อ นามสกุล",
      detail: "ขอแก้ไขคำนำหน้านาม",
      document: null,
      status: "PENDING",
      remark: "",
    },
    {
      id: "2",
      fullname: "นายนิมมาน วงศ์วโรทัย",
      topic: "ขอแก้ไขข้อมูลประวัติการศึกษา",
      detail: "ขอแก้ไขคำนำหน้านาม",
      document: null,
      status: "COMPLETE",
      remark: "",
    },
    {
      id: "3",
      fullname: "นายพัฒนกิจ จันทร์สว่างวงศ์",
      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
    );
  });
}

function onclickEdit(data: any) {
  modalStatus.value = true;
  dataRequest.value = data;
}

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

<template>
  <div class="row items-center">
    <div class="toptitle text-dark row items-center q-py-xs">
      <q-btn
        icon="mdi-arrow-left"
        unelevated
        round
        dense
        flat
        color="primary"
        class="q-mr-sm"
        @click="router.go(-1)"
      />
      รายการคำร้องขอแก้ไขทะเบียนประวัติ
    </div>
  </div>
  <q-card flat bordered class="q-pa-md">
    <div class="row q-mb-sm q-col-gutter-sm">
      <div class="col-xs-10 col-md-3">
        <q-select
          style="max-width: 250px"
          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>
      <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-th auto-width />
          </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-td>
              <q-btn
                icon="edit"
                round
                dense
                flat
                color="edit"
                size="12px"
                @click.pervent="onclickEdit(props.row)"
              >
                <q-tooltip>แก้ไขสถานะคำร้อง</q-tooltip>
              </q-btn>
            </q-td>
          </q-tr>
        </template>
      </d-table>
    </div>
  </q-card>

  <DialogStatus
    v-model:modal="modalStatus"
    :fetchData="fetchListRequest"
    :data="dataRequest"
  />
</template>

<style scoped></style>
