<script setup lang="ts">
import { ref, onMounted } from "vue";
import { checkPermission } from "@/utils/permissions";
import { useQuasar } from "quasar";
import router from "@/router";
import http from "@/plugins/http";
import config from "@/app.config";

/**
 * importComponents
 */
import DialogDetail from "@/modules/12_evaluatePersonal/components/Director/DialogDetail.vue";

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

/**
 * use
 */
const $q = useQuasar();
const dataStore = useEvaluateDirectorDataStore();
const mixin = useCounterMixin();
const { messageError, showLoader, hideLoader, dialogRemove, success } = mixin;

const modalDetail = ref<boolean>(false);
const directorId = ref<string>("");
const actionType = ref<string>("");

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

function getList() {
  showLoader();
  http
    .get(config.API.evaluateDirectorMain())
    .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 () => deleteData(id), `ลบข้อมูล`);
}

/**
 * ลบข้อมูล
 * @param id type
 */
function deleteData(id: string) {
  showLoader();
  http
    .delete(config.API.evaluateDirectorById(id))
    .then(async () => {
      getList();
      success($q, "ลบข้อมูลสำเร็จ");
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}

function onOpenDetail(id: string, type: string) {
  directorId.value = id;
  actionType.value = type;
  modalDetail.value = true;
}

/**
 * ค้นหาในตาราง
 */
const filterKeyword = ref<string>("");
const filterRef = ref<HTMLInputElement | null>(null);

function resetFilter() {
  filterKeyword.value = "";
  if (filterRef.value) {
    filterRef.value.focus();
  }
}

function filterFn() {
  getList();
}

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/director/add`)"
          size="12px"
          flat
          round
          color="add"
          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
        debounce="300"
        placeholder="ค้นหา"
        @keydown.enter.prevent="filterFn"
      >
        <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
        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"
        options-cover
        style="min-width: 150px"
        class="col-xs-12 col-sm-3 col-md-2"
      />
    </div>

    <div class="col-12">
      <d-table
        :columns="dataStore.columns"
        :rows="dataStore.rows"
        :filter="filterKeyword"
        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.pervent="onOpenDetail(props.row.id, 'VIEW')"
              >
                <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="onOpenDetail(props.row.id, 'EDIT')"
              >
                <q-tooltip>แก้ไขข้อมูล</q-tooltip>
              </q-btn>
              <q-btn
                v-if="checkPermission($route)?.attrIsDelete"
                dense
                size="12px"
                flat
                round
                color="red"
                @click.pervent="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>

  <!-- รายละเอียด -->
  <DialogDetail
    v-model:modal="modalDetail"
    :directorId="directorId"
    :actionType="actionType"
    :fetchDataList="getList"
  />
</template>
