<template>
  <div class="container">
    <pdf v-for="(item, index) in pdfNumPages" :key="index" :src="pdfUrl" :page="index + 1" />
  </div>
</template>

<script>
import pdf from "vue-pdf";
import CMapReaderFactory from "vue-pdf/src/CMapReaderFactory.js";
import { mapActions } from "vuex";
export default {
  name: "PolicyVirtualDetail",
  components: {
    pdf
  },
  data() {
    return {
      url: "",
      pdfUrl: "",
      pdfNumPages: null
    };
  },
  mounted() {
    this.setDetail();
  },
  methods: {
    ...mapActions(["setIsLoading"]),
    setDetail() {
      const { url } = this.$route.query;
      this.setIsLoading(true);
      if (!url) this.$router.back();
      this.url = url;
      this.pdfTask();
    },
    pdfTask() {
      this.pdfUrl = pdf.createLoadingTask({ url: this.url, CMapReaderFactory });
      this.pdfUrl.promise
        .then(pdf => {
          this.pdfNumPages = pdf.numPages;
          this.setIsLoading(false);
        })
        .catch(err => {
          console.error(err);
        });
    }
  }
};
</script>
<style lang="less" scoped></style>
