<script setup lang="ts">
import { ref, onMounted, watch } from "vue";
import { VuePDF, usePDF } from "@tato30/vue-pdf";

const props = defineProps({
  pdfSrc: {
    type: String,
  },
  type: {
    type: String,
  },
});

const loadPDF = ref<boolean>(false);

/** function loadfile PDF*/
async function loadpdfData() {
  loadPDF.value = false;
  const pdfData = await usePDF(`${props.pdfSrc}`);
  setTimeout(() => {
    pdfSrc.value = pdfData.pdf.value;
    numOfPages.value = pdfData.pages.value;
  }, 1500);
  setTimeout(() => {
    loadPDF.value = true;
  }, 2000);
}

watch(
  () => props.pdfSrc,
  async () => {
    await loadpdfData();
  }
);

/** lifecycleHook*/
onMounted(async () => {
  await loadpdfData();
});

const splitterModel = ref(14);
const numOfPages = ref<number>(0);
const page = ref<number>(1);
const pdfSrc = ref<any>();

/** ไปหน้าต่อไปของรายงาน */
function nextPage() {
  if (page.value < numOfPages.value) {
    page.value++;
  }
}

/** กลับหน้าก่อนหน้าของรายงาน */
function backPage() {
  if (page.value !== 1) {
    page.value--;
  }
}
</script>

<template>
  <q-splitter
    v-model="splitterModel"
    horizontal
    class="bg-grey-1 q-px-sm"
    :style="
      props.type !== 'popup'
        ? 'height: 50vh; border-radius: 5px;'
        : 'border: 1px solid rgb(210, 210, 210);border-radius: 5px;'
    "
    before-class="overflow-hidden disable"
    separator-class="bg-white disabled"
  >
    <template v-slot:before>
      <div class="q-py-sm">
        <div class="row items-start items-center">
          <div class="col">
            <q-btn
              padding="xs"
              icon="mdi-chevron-left"
              color="grey-4"
              unelevated
              text-color="grey-8"
              size="md"
              class="my-auto"
              @click="backPage"
              :disable="page == 1"
            />
          </div>
          <div class="col-12 col-md-auto">
            <div class="q-pa-md flex">
              หน้าที่ {{ page }} จาก {{ numOfPages }}
            </div>
          </div>
          <div class="col text-right">
            <q-btn
              padding="xs"
              icon="mdi-chevron-right"
              color="grey-4"
              unelevated
              text-color="grey-8"
              size="md"
              @click="nextPage"
              :disable="page === numOfPages"
            />
          </div>
        </div>
      </div>
    </template>
    <template v-slot:after>
      <div v-if="loadPDF">
        <VuePDF ref="vuePDFRef" :pdf="pdfSrc" :page="page" fit-parent />
      </div>

      <div
        v-else
        class="text-center"
        style="
          display: flex;
          align-items: center;
          justify-content: center;
          height: 25vh;
        "
      >
        <q-spinner color="primary" size="3em" :thickness="2" />
      </div>
    </template>
    <template v-slot:default>
      <div class="q-py-sm">
        <div class="row items-start items-center">
          <div class="col">
            <q-btn
              padding="xs"
              icon="mdi-chevron-left"
              color="grey-4"
              unelevated
              text-color="grey-8"
              size="md"
              class="my-auto"
              @click="backPage"
              :disable="page == 1"
            />
          </div>

          <div class="col-12 col-md-auto">
            <div class="q-pa-md flex">
              หน้าที่ {{ page }} จาก {{ numOfPages }}
            </div>
          </div>
          <div class="col text-right">
            <q-btn
              padding="xs"
              icon="mdi-chevron-right"
              color="grey-4"
              unelevated
              text-color="grey-8"
              size="md"
              @click="nextPage"
              :disable="page === numOfPages"
            />
          </div>
        </div>
      </div>
    </template>
  </q-splitter>
</template>

<style scoped></style>
