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

import router from "@/router";
import { useQuasar } from "quasar";
// import { useCounterMixin } from "@/stores/mixin";

// import config from "@/app.config";
// import http from "@/plugins/http";

import { useInvestigateFactStore } from "@/modules/11_discipline/store/InvestigateFactStore";
const dataInvestigate = useInvestigateFactStore();
const { fecthList } = dataInvestigate;
// const mixin = useCounterMixin();
// const {
//   date2Thai,
//   success,
//   messageError,
//   showLoader,
//   hideLoader,
//   dialogConfirm,
//   dialogRemove,
// } = mixin;
const $q = useQuasar(); //ใช้ noti quasar

/** ค้นหาข้อมูลในตาราง */
const filterKeyword = ref<string>("");
const filterRef = ref<HTMLInputElement | null>(null);
const resetFilter = () => {
  filterKeyword.value = "";
  if (filterRef.value) {
    filterRef.value.focus();
  }
};

const attrs = ref<any>(useAttrs());

/** แสดงจำนวนในตาราง */
const pagination = ref({
  // sortBy: "desc",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});

/**ไปยังหน้าเพิ่มข้อมูล */
const clickAdd = () => {
  router.push(`/discipline/investigatefacts/add`);
};

/**
 * ไปหน้าแก้ไข
 * @param id ไอดีเฉพาะ รายบุคคล
 */
function editPage(id: string) {
  router.push(`/discipline/investigatefacts/${id}`);
}

/**เมื่อเริ่มโหลดหน้า 
 * ส่งข้อมูลจำลองไปยัง store
 */
onMounted(async () => {
  await fecthList([
    {
      id: "001",
      subject: "ทุจริตในหน้าที่",
      interrogated: "บุคคล",
      complaintOffenseDetails: "NOT_SPECIFIED",
      investigationDetail: "appoint_directors",
      startDate: new Date ("2023-11-20"),
      endDate: new Date ("2023-11-25"),
      statusResult: "not_specified",
      status: "new"
    },
  ]);
});
</script>
<template>
  <div class="toptitle text-dark col-12 row items-center">
    รายการสืบสวนข้อเท็จจริง
  </div>
  <q-card flat bordered class="col-12 q-mt-sm q-pa-md">
    <div class="row col-12 q-col-gutter-sm q-mb-sm">
      <div>
        <!-- <q-btn
          for="#addInvestigatefacts"
          @click="clickAdd()"
          size="12px"
          flat
          round
          color="add"
          icon="mdi-plus"
        >
          <q-tooltip>เพิ่มรายการสืบสวนข้อเท็จจริง</q-tooltip>
        </q-btn> -->
      </div>
      <q-space />

      <q-input
        for="#search"
        class="col-xs-12 col-sm-3 col-md-2"
        standout
        dense
        v-model="filterKeyword"
        ref="filterRef"
        outlined
        debounce="300"
        placeholder="ค้นหา"
      >
        <template v-slot:append>
          <q-icon v-if="filterKeyword == ''" name="search" />
          <q-icon
            v-if="filterKeyword !== ''"
            name="clear"
            class="cursor-pointer"
            @click="resetFilter"
          />
        </template>
      </q-input>

      <q-select
        for="#select"
        v-model="dataInvestigate.visibleColumns"
        multiple
        outlined
        dense
        options-dense
        :display-value="$q.lang.table.columns"
        emit-value
        map-options
        :options="dataInvestigate.columns"
        option-value="name"
        options-cover
        style="min-width: 150px"
        class="col-xs-12 col-sm-3 col-md-2"
      />
    </div>
    
    <div class="col-12">
      <d-table
        ref="table"
        :columns="dataInvestigate.columns"
        :rows="dataInvestigate.rows"
        :filter="filterKeyword"
        row-key="interrogated"
        flat
        bordered
        :paging="true"
        dense
        class="custom-header-table"
        v-bind="attrs"
        :visible-columns="dataInvestigate.visibleColumns"
        v-model:pagination="pagination"
      >
        <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-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"
              @click="editPage(props.row.id)"
            >
              <div v-if="col.name == 'no'">
                {{ props.rowIndex + 1 }}
              </div>
              <div>
                {{ col.value }}
              </div>
            </q-td>
            <q-td
              auto-width
              style="font-size: 14px; width: 10%"
              @click="editPage(props.row.id)"
            >
              {{ props.row.active }}
            </q-td>
          </q-tr>
        </template>
      </d-table>
    </div>
  </q-card>
</template>
