<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useQuasar } from 'quasar'
import { useRouter } from 'vue-router'
import Table from '@/components/TableHistory.vue'
import ToolBar from '@/components/ToolBar.vue'
import http from '@/plugins/http'
import config from '@/app.config'
import { useCounterMixin } from '@/stores/mixin'
// importStores
import { useChekIn } from '@/stores/chekin'

const mixin = useCounterMixin()
const router = useRouter()
const stores = useChekIn()
const { showLoader, hideLoader, messageError } = mixin
const { fetchHistoryList } = stores
const $q = useQuasar() //ใช้ noti quasar

// paging
const page = ref<number>(1)
const year = ref<number>(new Date().getFullYear())
const pageSize = ref<number>(10)
const total = ref<number>(0)
const maxPage = ref<number>(1)
const filter = ref<string>('') //search data table

/**
 * ฟังก์ชั่น api เปลี่ยนหน้า
 * @param pageVal page
 * @param pageSizeVal pagesize
 * @param loading loading
 */
async function changePage(
  pageVal: number,
  pageSizeVal: number,
  loading: false,
  key: string
) {
  page.value = await pageVal
  pageSize.value = await pageSizeVal
  filter.value = await key
  fetchlistHistory(loading)
}

/**
 * ฟังก์ชั่นดึงข้อมูลรายการประวัติการลงเวลาจาก api มาแสดง
 * @param loading แสดง loading ไหม true คือแสดง, false คือไม่แสดง
 */
async function fetchlistHistory(loading = true) {
  loading === true && showLoader()
  await http
    .get(
      config.API.history() +
        `?year=${year.value}&page=${page.value}&pageSize=${pageSize.value}&keyword=${filter.value}`
    )
    .then(async (res) => {
      const data = res.data.result.data
      total.value = res.data.result.total
      maxPage.value = await Math.ceil(total.value / pageSize.value)

      fetchHistoryList(data) // ปิดกรณีมี total
    })
    .catch((err) => {
      messageError($q, err)
    })
    .finally(() => {
      hideLoader()
    })
}

/**
 * function updateYear
 * @param y ปีที่อัปเดท
 */
async function updateYear(y: number) {
  year.value = y
  y && fetchlistHistory(true)
}

/** Hook*/
onMounted(() => {
  fetchlistHistory(true)
})
</script>
<template>
  <div class="col-12 row justify-center">
    <div class="col-xs-12 col-sm-12 col-md-12">
      <q-card flat class="row col-12 cardNone">
        <div
          class="bg-secondary text-white col-12 row items-center q-px-md q-py-sm"
        >
          <div class="col-2">
            <q-btn
              icon="arrow_back"
              round
              dense
              flat
              color="white"
              @click="router.go(-1)"
            />
          </div>
          <q-space />
          <span class="text-body1 text-weight-bold col-8 text-center"
            >ประวัติการลงเวลา</span
          >
          <div class="col-2"></div>
        </div>
        <div class="col-12 q-pa-md text-grey-9">
          <ToolBar :fetchData="fetchlistHistory" @update:year="updateYear" />
          <Table
            :fetchData="fetchlistHistory"
            :page-size="pageSize"
            :total="total"
            :page="page"
            :paging="true"
            @update:change-page="changePage"
            :max-page="maxPage"
          />
        </div>
      </q-card>
    </div>
  </div>
</template>
