<script setup lang="ts">
import { ref, useAttrs, onMounted } from "vue";
import router from "@/router";
import { useCounterMixin } from "@/stores/mixin";
import { useQuasar } from "quasar";
import { useDisciplineChannelDataStore } from "@/modules/11_discipline/store/ChannelStore";
import config from "@/app.config";
import http from "@/plugins/http";
import type { typeItem } from "@/modules/11_discipline/interface/response/channel";

const dataStore = useDisciplineChannelDataStore();
const mixin = useCounterMixin();
const { dialogRemove, showLoader, messageError, hideLoader, success } = 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();
  }
};

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

/**
 * clickไปหน้าเพิ่มchanel
 */
function clickAdd(){
  dataStore.getType('')
  router.push(`/discipline/channel/add`);
};

/** get data */
async function getComplaintChanal() {
  showLoader();
  await http
    .get(config.API.complaintChannel())
    .then((res) => {
      const data = res.data.result.data;
      dataStore.fetchData(data);
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
}

/**
 * ลบข้อมูล
 * @param id ไอดีของข้อมูล
 */
function clickDelete(id: string) {
  dialogRemove($q, async () => deleteData(id), `ลบข้อมูล`);
}

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

/**
 * ไปหน้า แก้ไข
 * @param id type
 */
function editPage(data:typeItem){
  dataStore.getType(data.name)
  router.push(`/discipline/channel/${data.id}`)
}
/**เมื่อเริ่มโหลดหน้า เรียกใช้ฟังชั่น*/
onMounted(() => {
  getComplaintChanal();
});
</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
          @click="clickAdd()"
          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"
        for="filterKeyword"
        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
        v-model="dataStore.visibleColumns"
        multiple
        for="visibleColumns"
        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"
        row-key="id"
        :filter="filterKeyword"
        :visible-columns="dataStore.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">
              <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)"
            >
              <div v-if="col.name === 'no'">
                {{ props.rowIndex + 1 }}
              </div>
              <div v-else>
                {{ col.value}}
              </div>
            </q-td>
            <q-td>
              <q-btn
                dense
                size="12px"
                flat
                round
                color="red"
                @click="clickDelete(props.row.id)"
                icon="mdi-delete"
              >
                <q-tooltip>ลบข้อมูล</q-tooltip>
              </q-btn>
            </q-td>
          </q-tr>
        </template>
      </d-table>
    </div>
  </q-card>
</template>

<style lang="scss" scope>
.filter-card {
  background-color: #f1f1f1b0;
}

.toggle-expired-account {
  font-size: 12px;
  font-weight: 400;
  font-size: 15px;
  line-height: 150%;
  color: #35373c;
}

.icon-color {
  color: #4154b3;
}

.custom-header-table {
  max-height: 64vh;

  .q-table tr:nth-child(odd) td {
    background: white;
  }

  .q-table tr:nth-child(even) td {
    background: #f8f8f8;
  }

  .q-table thead tr {
    background: #ecebeb;
  }

  .q-table thead tr th {
    position: sticky;
    z-index: 1;
  }

  /* this will be the loading indicator */
  .q-table thead tr:last-child th {
    /* height of all previous header rows */
    top: 48px;
  }

  .q-table thead tr:first-child th {
    top: 0;
  }
}
</style>
