<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 { mapActions } from "vuex";
let CMAP_URL = "https://unpkg.com/pdfjs-dist@2.0.943/cmaps/";

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, cMapUrl: CMAP_URL, cMapPacked: true });
      this.pdfUrl.promise
        .then(pdf => {
          this.pdfNumPages = pdf.numPages;
          this.setIsLoading(false);
        })
        .catch(err => {
          console.error(err);
        });
    }
  }
};
</script>
<style lang="less" scoped></style>
