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

/** importStore*/
import { useEvaluateDetailStore } from "@/modules/12_evaluatePersonal/store/EvaluateDetail";

/** use*/
const store = useEvaluateDetailStore();

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

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--;
  }
}

watch(
  () => store.log,
  () => {
    if (store.log === 1) {
      numOfPages.value = store.numOfPagesStore;
      pdfSrc.value = store.pdfSrcStore;
    }
  }
);
onMounted(() => {
  setTimeout(() => {
    numOfPages.value = store.numOfPagesStore;
    pdfSrc.value = store.pdfSrcStore;
  }, 100);
});
</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-px-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 class="q-pa-md">
        <VuePDF ref="vuePDFRef" :pdf="pdfSrc" :page="page" fit-parent />
      </div>
    </template>
    <template v-slot:default>
      <div class="q-pa-md">
        <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>
