useA
<script setup lang="ts">
import type { QTableProps } from "quasar";
import { ref, onMounted, reactive, watch } from "vue";
import { useQuasar } from "quasar";
import { useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";
import { useTransferDataStore } from "@/modules/02_transfer/store";
import { useAppealComplainStore } from "@/modules/07_appealComplain/store";

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

import type { FormType } from "@/modules/07_appealComplain/interface/response/mainType";
import type { DataOption } from "@/modules/07_appealComplain/interface/index/main";

const total = ref<number>(0);
const currentPage = ref<number>(1);
const maxPage = ref<number>(1);
const page = ref<number>(1);
const rowsPerPage = ref<number>(10);

const filterKeyword = ref<string>("");
const dataStore = useAppealComplainStore();
const transferData = useTransferDataStore();
const { statusText } = transferData;
const router = useRouter();
const $q = useQuasar();
const mixin = useCounterMixin();
const { date2Thai, messageError, showLoader, hideLoader } = mixin;
const type = ref<DataOption[]>([
  { id: "ALL", name: "ทั้งหมด" },
  ...dataStore.typeOptions,
]);
/**
 * ตั้งค่า pagination
 */
const pagination = ref({
  sortBy: "lastUpdatedAt",
  descending: true,
  page: page.value,
  rowsPerPage: rowsPerPage.value,
});

/**
 * เพิ่มหัวข้อตาราง
 */
const filter = ref<string>("");

const formData = reactive<FormType>({
  type: "ALL",
  status: "ALL",
  year: new Date().getFullYear(),
});

const visibleColumns = ref<string[]>([
  "no",
  "title",
  "type",
  "year",
  "caseType",
  "caseNumber",
  "fullname",
  "citizenId",
  "lastUpdatedAt",
  "status",
]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: true,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px; width:5px;",
  },
  {
    name: "title",
    align: "left",
    label: "เรื่องที่อุทธรณ์/ร้องทุกข์",
    sortable: true,
    field: "title",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px; width:15%;",
  },
  {
    name: "type",
    align: "left",
    label: "ประเภท",
    sortable: true,
    field: "type",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px; width:15%;",
  },
  {
    name: "year",
    align: "left",
    label: "ปีงบประมาณ",
    sortable: true,
    field: "year",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "caseType",
    align: "left",
    label: "ประเภทคดี",
    sortable: true,
    field: "caseType",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "caseNumber",
    align: "left",
    label: "คดีเลขที่",
    sortable: true,
    field: "caseNumber",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "fullname",
    align: "left",
    label: "ชื่อ-นามสกุลผู้อุทธรณ์/ร้องทุกข์",
    sortable: true,
    field: "fullname",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "citizenId",
    align: "left",
    label: "รหัสบัตรประชาชน",
    sortable: true,
    field: "citizenId",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px; width:10%;",
  },
  {
    name: "lastUpdatedAt",
    align: "left",
    label: "วันที่แก้ไข",
    sortable: true,
    field: "lastUpdatedAt",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "status",
    align: "left",
    label: "สถานะของคำร้อง",
    sortable: false,
    field: "status",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);

//นำข้อมูลมาแสดง
const getData = async () => {
  showLoader();
  await http
    .get(
      config.API.appealMainList(
        formData.status,
        formData.type,
        formData.year,
        currentPage.value,
        rowsPerPage.value,
        filterKeyword.value
      )
    )
    .then((res: any) => {
      total.value = res.data.result.total;
      maxPage.value = Math.ceil(res.data.result.total / rowsPerPage.value);
      let data = res.data.result.data;
      dataStore.fetchAppealComplain(data);
      console.log(data);
    })
    .catch((e: any) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
};

/**
 * กดเพื่อย้อนกลับ
 */
const clickBack = () => {
  router.push(`/`);
};

/** ดึงข้อมูลเมื่อมีการปรับฟิลเตอร์ */
function dataUpdate() {
  getData();
}

/** ดึงข้อมูลจาก keyword*/
function filterFn() {
  getData();
}

watch(
  () => currentPage.value,
  () => {
    rowsPerPage.value = pagination.value.rowsPerPage;
    getData();
  }
);

watch(
  () => pagination.value.rowsPerPage,
  () => {
    rowsPerPage.value = pagination.value.rowsPerPage;
    currentPage.value = 1;
    getData();
  }
);

/**
 * เปิดหน้าแก้ไข
 * @param id id ของ บุคคล
 */
function editPage(id: string) {
  router.push(`/appeal-complain/${id}`);
}

function redirectToPageadd() {
  // dataStore.rowsAdd = []
  router.push(`/appeal-complain/add`);
}

/**
 * เรียกฟังก์ชันทั้งหมดตอนเรียกใช้ไฟล์นี้
 */
onMounted(async () => {
  dataStore.visibleColumns = visibleColumns.value;
  dataStore.columns = columns.value;

  await getData();
});
</script>
<template>
  <div class="col-12 row justify-center">
    <div class="col-xs-12 col-sm-12 col-md-11">
      <div class="toptitle text-white col-12 row items-center">
        <q-btn
          icon="mdi-arrow-left"
          unelevated
          round
          dense
          flat
          color="primary"
          class="q-mr-sm"
          @click="clickBack"
        />
        อุทธรณ์ร้องทุกข์
      </div>
      <div class="col-12">
        <q-card bordered class="q-pa-md">
          <q-btn
            id="addComplaints"
            for="addComplaints"
            size="12px"
            flat
            round
            color="primary"
            icon="mdi-plus"
            @click="redirectToPageadd()"
            ><q-tooltip>เพิ่มการอุทธรณ์/ร้องทุกข์</q-tooltip></q-btn
          >

          <div class="row col-12 q-col-gutter-sm q-mb-sm">
            <div class="col-xs-12 col-md-2">
              <datepicker
                menu-class-name="modalfix"
                v-model="formData.year"
                class="col-2"
                :locale="'th'"
                autoApply
                year-picker
                :enableTimePicker="false"
                @update:model-value="dataUpdate"
              >
                <template #year="{ year }">{{ year + 543 }}</template>
                <template #year-overlay-value="{ value }">{{
                  parseInt(value + 543)
                }}</template>
                <template #trigger>
                  <q-input
                    dense
                    lazy-rules
                    outlined
                    :model-value="Number(formData.year) + 543"
                    :label="`${'ปีงบประมาณ'}`"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        style="color: var(--q-primary)"
                      >
                      </q-icon>
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div>

            <div class="col-xs-12 col-md-2">
              <q-select
                v-model="formData.type"
                label="ประเภท"
                dense
                outlined
                emit-value
                map-options
                option-label="name"
                option-value="id"
                :options="type"
                @update:model-value="dataUpdate"
              />
            </div>
            <div class="col-xs-12 col-md-2">
              <q-select
                v-model="formData.status"
                label="สถานะ"
                dense
                outlined
                emit-value
                map-options
                option-label="name"
                option-value="id"
                :options="dataStore.statusOptions"
                @update:model-value="dataUpdate"
              />
            </div>
            <q-space />

            <q-input
              class="col-xs-12 col-sm-3 col-md-2"
              id="filterTable"
              for="filterTable"
              dense
              outlined
              v-model="filterKeyword"
              label="ค้นหา"
              debounce="300"
              @keydown.enter.prevent="filterFn"
            >
              <template v-slot:append>
                <q-icon name="search" />
              </template>
            </q-input>

            <q-select
              id="visibleColumns"
              for="visibleColumns"
              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
              class="col-xs-12 col-sm-3 col-md-2"
            />

            <div class="col-12">
              <d-table
                flat
                bordered
                dense
                class="custom-table2"
                style="max-height: 80vh"
                :rows="dataStore.rows"
                :columns="dataStore.columns"
                :visible-columns="dataStore.visibleColumns"
                v-model:pagination="pagination"
                :rows-per-page-options="[10, 25, 50, 100]"
              >
                <template v-slot:pagination="scope">
                  ทั้งหมด {{ total }} รายการ
                  <q-pagination
                    v-model="currentPage"
                    active-color="primary"
                    color="dark"
                    :max="Number(maxPage)"
                    size="sm"
                    boundary-links
                    direction-links
                  ></q-pagination>
                </template>
                <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-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'">
                        {{
                          (currentPage - 1) * Number(pagination.rowsPerPage) +
                          props.rowIndex +
                          1
                        }}
                      </div>
                      <div>
                        {{ col.value }}
                      </div>
                    </q-td>
                  </q-tr>
                </template>

                <template #item="props">
                  <div class="q-pa-xs col-xs-12 col-sm-6 col-md-4 col-lg-3">
                    <q-card bordered flat>
                      <q-list @click="editPage(props.row.id)">
                        <q-item
                          v-for="col in props.cols.filter((col:any) => col.name !== 'desc')"
                          :key="col.name"
                        >
                          <q-item-section>
                            <q-item-label caption>{{ col.label }}</q-item-label>

                            <q-item-label v-if="col.name === 'no'">
                              {{ props.rowIndex + 1 }}
                            </q-item-label>

                            <q-item-label v-else>{{ col.value }}</q-item-label>
                          </q-item-section>
                        </q-item>
                      </q-list>
                    </q-card>
                  </div>
                </template>
              </d-table>
            </div>
          </div>
        </q-card>
      </div>
    </div>
  </div>
</template>
<style lang="scss">
.icon-color {
  color: #4154b3;
}

.custom-table2 {
  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;
  }

  .q-table td:nth-of-type(2) {
    z-index: 3 !important;
  }

  .q-table th:nth-of-type(2),
  .q-table td:nth-of-type(2) {
    position: sticky;
    left: 0;
    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>
