<template>
  <div class="home_wrap">
    <div class="pdf_down">
      <div class="pdf_set_left" @click="scaleD">➕</div>
      <div class="pdf_set_middle" @click="scaleX">➖</div>
      <!-- <div class="pdf-pre" @click="prePage">上一页</div> -->
      <!-- <div class="pdf-next" @click="nextPage">下一页</div> -->
    </div>
    <div :style="{ width: pdf_div_width, margin: '0 auto' }">
      <!-- <canvas id="the_canvas"></canvas> -->
      <canvas
        v-for="page in pdf_pages"
        :id="'the_canvas' + page"
        :key="page"
      ></canvas>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue"
import * as PDF from "pdfjs-dist"

PDF.GlobalWorkerOptions.workerSrc = new URL(
  'pdfjs-dist/build/pdf.worker.mjs',
  import.meta.url
).toString()

const props = defineProps({
  data: Array,
})

const pdf_scale = ref(1.0)
const pdf_pages = ref([])
const pdf_div_width = ref("")
const currentPage = ref(1)
let pdfDoc = null

const scaleD = () => {
  let max = 0
  if (window.screen.width > 1440) {
    max = 1.4
  } else {
    max = 1.2
  }
  if (pdf_scale.value >= max) {
    return
  }
  pdf_scale.value = pdf_scale.value + 0.1
  loadFile()
}

const scaleX = () => {
  let min = 1.0
  if (pdf_scale.value <= min) {
    return
  }
  pdf_scale.value = pdf_scale.value - 0.1
  loadFile()
}

const loadFile = async () => {
  pdfDoc = await PDF.getDocument(props.data).promise
  pdf_pages.value = pdfDoc.numPages
  renderPage()
}

const renderPage = async (num = 1) => {
  currentPage.value = num
  const page = await pdfDoc.getPage(num)
  const canvas = document.getElementById("the_canvas" + num)
  const ctx = canvas.getContext("2d")
  const dpr = window.devicePixelRatio || 1
  const bsr =
    ctx.webkitBackingStorePixelRatio ||
    ctx.mozBackingStorePixelRatio ||
    ctx.msBackingStorePixelRatio ||
    ctx.oBackingStorePixelRatio ||
    ctx.backingStorePixelRatio ||
    1
  const ratio = dpr / bsr
  const viewport = page.getViewport({ scale: pdf_scale.value })
  canvas.width = viewport.width * ratio
  canvas.height = viewport.height * ratio
  canvas.style.width = viewport.width + "px"
  pdf_div_width.value = viewport.width + "px"
  canvas.style.height = viewport.height + "px"
  ctx.setTransform(ratio, 0, 0, ratio, 0, 0)
  const renderContext = {
    canvasContext: ctx,
    viewport: viewport,
  }
  page.render(renderContext)
  if (pdf_pages.value > num) {
    setTimeout(() => {
      return renderPage(num + 1)
    })
  }
}

const nextPage = () => {
  if (pdf_pages.value > currentPage.value) {
    renderPage(currentPage.value + 1)
  }
}

const prePage = () => {
  if (currentPage.value > 1) {
    renderPage(currentPage.value - 1)
  }
}


onMounted(() => {
  loadFile()
})
</script>

<style scoped>
.home_wrap {
  width: 100%;
  height: 100%;
}
.home_wrap .pdf_down {
  position: fixed;
  display: flex;
  z-index: 20;
  right: 26px;
  bottom: 7%;
  cursor: pointer;
}
.home_wrap .pdf-pre {
  position: fixed;
  display: flex;
  z-index: 20;
  right: 160px;
  bottom: 9%;
  cursor: pointer;
}
.home_wrap .pdf-next {
  position: fixed;
  display: flex;
  z-index: 20;
  right: 100px;
  bottom: 9%;
}
.home_wrap .pdf_down .pdf_set_left {
  width: 30px;
  height: 40px;
  color: #408fff;
  font-size: 15px;
  padding-top: 25px;
  text-align: center;
  margin-right: 5px;
  cursor: pointer;
}
.home_wrap .pdf_down .pdf_set_middle {
  width: 30px;
  height: 40px;
  color: #408fff;
  font-size: 15px;
  padding-top: 25px;
  text-align: center;
  margin-right: 5px;
  cursor: pointer;
}
</style>
