<template>
  <div>
    <div class="edit-container">
      <div class="edit-top">
        <div class="left-top">
          <img src="@/assets/images/back.png" @click="cancel()" />
          <div class="left-title"></div>
        </div>
        <div class="center-top">
        </div>
        <div class="right-top">
          <el-button @click="cancel()">{{ $t('form.cancel') }}</el-button>
          <el-button type="primary" @click="yuedu" v-if="form.zt == 0">{{
            $t('page.business.study.studyMethod.yuedu') }}</el-button>
        </div>
      </div>
      <div class="edit-content ">
        <div class="pdf-layout">
          <div class="pdf-content">
            <pdf id="pdfBox" :page="pageNum" :src="pdfSrc" @progress="loadedRatio = $event"
              @num-pages="totalPages = $event"></pdf>
          </div>
          <div class="btn-layout" v-if="totalPages">
            <div class="pageNum">{{ pageNum }} / {{ totalPages }}</div>
            <el-button-group>
              <el-button round plain type="primary" icon="el-icon-arrow-left" size="mini"
                @click="prePage">上一页</el-button>
              <el-button round plain type="primary" size="mini" @click="nextPage">下一页<i
                  class="el-icon-arrow-right el-icon--right"></i></el-button>
            </el-button-group>
          </div>
        </div>
      </div>
    </div>
    <Yd ref="Yd" @callback="closeYd"/>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import pdf from 'vue-pdf'
import Yd from './Yd.vue'
export default {
  name: "Xq",
  components: { pdf, Yd },
  data() {
    return {
      pdfSrc: '',
      open: false,
      pageNum: 1,
      loadedRatio: 0, // 当前页面的加载进度，范围是0-1 ，等于1的时候代表当前页已经完全加载完成了
      totalPages: 0, //pdf总页数
      form: {},
    }
  },
  computed: {
    ...mapGetters([
      'nickName'
    ]),
  },
  created() {

  },
  methods: {
    yuedu() {
      this.$refs.Yd.show(this.form)
    },

    cancel() {
      this.$emit('close')
    },

    show(row) {
      this.open = true
      this.form = row
      this.pdfSrc = process.env.VUE_APP_FILE_DOMAIN + row.fileUrl
      this.getPageNum()
    },

    // 获取PDF总页数
    getPageNum() {
      let loadingTask = pdf.createLoadingTask(this.fileUrl);
      loadingTask.promise
        .then((pdf) => {
          this.totalPages = pdf.numPages;
          this.$nextTick(() => {
            this.setWatermarkContent();
          });
        })
        .catch((err) => {
          this.$message.warning("pdf加载失败");
        });
    },
    // 上一页
    prePage() {
      let page = this.pageNum;
      page = page > 1 ? page - 1 : this.totalPages;
      this.pageNum = page;
      window.scrollTo(0, 0);
    },

    // 下一页
    nextPage() {
      let page = this.pageNum;
      page = page < this.totalPages ? page + 1 : 1;
      this.pageNum = page;
      window.scrollTo(0, 0);
    },

    closeYd(val) {
      this.form.zt = val
    }
  }
}
</script>
<style lang="scss">
.pdf-layout {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.pdf-content {
  min-width: 1000px;
  min-height: 550px;
  position: relative;
  margin: 0 auto
}

.btn-layout {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.content-edit {
  background: #f5f5f5;
  padding: 0;

  .edit-top {
    background: #fff;
    padding: 10px 20px;
    margin-bottom: 10px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;

    .left-top {
      flex-shrink: 0;
      display: flex;
      flex-direction: row;
      align-items: center;

      img {
        height: 16px;
        margin-right: 20px;
        cursor: pointer;
      }

      .right-top {}

    }
  }

  .edit-content {
    background: #fff;
    padding: 20px;
    min-height: 500px;

    .edit-form {
      width: 800px;
      margin: 0 auto;
    }
  }
}
</style>