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

/** ImportStores*/
import { useRoundDataStore } from "@/modules/09_leave/stores/RoundStores";
import { useCounterMixin } from "@/stores/mixin";

/** ImportComponents*/
import DialogForm from "@/modules/09_leave/components/3_WorkTime/DialogForm.vue";

const mixin = useCounterMixin();
const dataStore = useRoundDataStore();
const { fetchData } = dataStore;
const { showLoader, hideLoader, messageError, dialogRemove, success } = mixin;
const $q = useQuasar(); //ใช้ noti quasar

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

const modal = ref<boolean>(false);
const detailData = ref<any>();
const editCheck = ref<string>("");

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

onMounted(() => {
  fetchListRoind();
});

/** Function get ข้อมูลรายการรอบการปฏิบัติงาน */
async function fetchListRoind() {
  showLoader();
  await http
    .get(config.API.roundDutytime())
    .then((res) => {
      const data = res.data.result;
      fetchData(data);
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

/**
 * Function delete รายการรอบการปฏิบัติงาน
 * @param id ค่า ID ของ rows
 */
function onClickDelete(id: string) {
  dialogRemove($q, async () => {
    await http
      .delete(config.API.roundDutytimeByid(id))
      .then(() => {
        success($q, "ลบข้อมูลสำเร็จ");
      })
      .catch((err) => {
        messageError($q, err);
      })
      .finally(async () => {
        await fetchListRoind();
      });
  });
}

/**
 *
 * @param data ข้อมูลรอบการปฏิบัติงาน
 * @param check action แก้ไข,เพิ่ม
 */
function openModal(data: any, check: string) {
  modal.value = true;
  editCheck.value = check;
  if (check === "edit") {
    detailData.value = data;
  }
}

/** Function ปิด popup รายละเอียดและการเพิ่ม*/
function closeDialog() {
  modal.value = false;
  editCheck.value = "add";
}

/**  pagination */
const pagination = ref({
  // sortBy: "desc",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});
</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="openModal(null, 'add')"
          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="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
        ref="table"
        :columns="dataStore.columns"
        :rows="dataStore.rows"
        :filter="filterKeyword"
        row-key="interrogated"
        flat
        bordered
        :paging="true"
        dense
        class="custom-header-table"
        v-bind="attrs"
        :visible-columns="dataStore.visibleColumns"
        v-model:pagination="pagination"
      >
        <template v-slot:header="props">
          <q-tr :props="props">
            <q-th auto-width></q-th>
            <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-th>
          </q-tr>
        </template>
        <template v-slot:body="props">
          <q-tr :props="props" class="cursor-pointer">
            <q-td auto-width>
              <q-icon
                v-if="props.row.isDefault === true"
                name="mdi-bookmark"
                size="xs"
                color="primary"
              >
                <q-tooltip>เวลา Default</q-tooltip>
              </q-icon>
            </q-td>
            <q-td
              v-for="col in props.cols"
              :key="col.name"
              :props="props"
              @click.prevent="openModal(props.row, 'edit')"
            >
              <div v-if="col.name === 'isActive'" class="text-center">
                <q-icon
                  v-if="props.row.isActive === true"
                  name="mdi-check"
                  size="sm"
                  color="positive"
                />
                <q-icon
                  v-if="props.row.isActive === false"
                  name="mdi-close"
                  size="sm"
                  color="grey"
                />
              </div>
              <div v-else>
                {{ col.value }}
              </div>
            </q-td>
            <q-td>
              <q-btn
                v-if="
                  props.row.isDefault === false && props.row.isActive === false
                "
                dense
                flat
                round
                color="red"
                icon="delete"
                @click="onClickDelete(props.row.id)"
              />
            </q-td>
          </q-tr>
        </template>
      </d-table>
    </div>
  </q-card>
  <DialogForm
    :modal="modal"
    :closeDialog="closeDialog"
    :editCheck="editCheck"
    :detailData="detailData"
    :fetchData="fetchListRoind"
  />
</template>
<style scoped lang="scss">
.q-table tbody td:before.no-background {
  background: none;
}
</style>
