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

import { checkPermission } from "@/utils/permissions";
import router from "@/router";
import http from "@/plugins/http";
import config from "@/app.config";

/**
 * importStore
 */
import { useCounterMixin } from "@/stores/mixin";
import { useEvaluateMeetingDataStore } from "@/modules/12_evaluatePersonal/store/MeetingStore";

const $q = useQuasar();
const dataStore = useEvaluateMeetingDataStore();
const mixin = useCounterMixin();
const { messageError, showLoader, hideLoader, dialogRemove, success,onSearchDataTable } = mixin;

/**
 * function fetch ข้อมูลรายการการประชุม
 */
function getList() {
  showLoader();
  http
    .get(config.API.meeting())
    .then((res) => {
      const data = res.data.result;
      dataStore.fetchData(data);
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}

/**
 * ลบรายการการประชุม
 * @param id รายการการประชุม
 */
function clickDelete(id: string) {
  dialogRemove($q, async () => {
    showLoader();
    http
      .delete(config.API.meetingById(id))
      .then(async () => {
        await getList();
        await success($q, "ลบข้อมูลสำเร็จ");
      })
      .catch((e) => {
        messageError($q, e);
        showLoader();
      });
  });
}

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

/**
 *pagination ของตาราง
 */
const pagination = ref({
  descending: true,
  sortBy:'date',
  page: 1,
  rowsPerPage: 10,
});

function onSearch() {
  dataStore.rows = onSearchDataTable(
    filterKeyword.value,
    dataStore.rowsData,
    dataStore.columns ? dataStore.columns : []
  );
}

/**เมื่อเริ่มโหลดหน้า
 * ส่งข้อมูลจำลองไปยัง store
 */
onMounted(() => {
  getList();
});
</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 v-if="checkPermission($route)?.attrIsCreate">
        <q-btn
          @click="router.push(`/evaluate/meeting/add`)"
          flat
          round
          color="primary"
          icon="mdi-plus"
        >
          <q-tooltip>เพิ่มการประชุม</q-tooltip>
        </q-btn>
      </div>
      <q-space />

      <q-input
        class="col-xs-12 col-sm-3 col-md-2"
        standout
        dense
        v-model="filterKeyword"
        ref="filterRef"
        outlined
        placeholder="ค้นหา"
        @keydown.enter.prevent="onSearch"
      >
        <template v-slot:append>
          <q-icon name="search" />
        </template>
      </q-input>

      <q-select
        v-model="dataStore.visibleColumns"
        multiple
        outlined
        dense
        options-dense
        :display-value="$q.lang.table.columns"
        emit-value
        map-options
        :options="dataStore.columns"
        option-value="name"
        style="min-width: 140px"
        class="col-xs-12 col-sm-3 col-md-2"
      />
    </div>

    <div class="col-12">
      <d-table
        :columns="dataStore.columns"
        :rows="dataStore.rows"
        row-key="tb-list"
        flat
        bordered
        :paging="true"
        dense
        v-model:pagination="pagination"
        :visible-columns="dataStore.visibleColumns"
      >
        <template v-slot:header="props">
          <q-tr :props="props">
            <q-th auto-width />
            <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.prevent="
                  router.push(`/evaluate/meeting-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.prevent="
                  router.push(`/evaluate/meeting/${props.row.id}`)
                "
              >
                <q-tooltip>แก้ไขข้อมูล</q-tooltip>
              </q-btn>
              <q-btn
                v-if="checkPermission($route)?.attrIsDelete"
                dense
                flat
                round
                color="red"
                @click.prevent="clickDelete(props.row.id)"
                icon="mdi-delete"
              >
                <q-tooltip>ลบข้อมูล</q-tooltip>
              </q-btn>
            </q-td>
            <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>
                {{ col.value }}
              </div>
            </q-td>
          </q-tr>
        </template>
      </d-table>
    </div>
  </q-card>
</template>
