<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="handleExport" v-if="showExportBtn">{{
            $t('page.business.study.studyMethod.daochu') }}</el-button>
          <el-button type="primary" @click="yuedu" v-if="form.zt == 0 && !showExportBtn">{{
            $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 ref="contentToPdf">
            <div class="content-title">
              <div class="line"></div>
              <div class="subtitle"> {{ $t('page.business.study.studyMethod.qmhz') }}</div>
            </div>
            <el-table v-loading="loading" :data="list">
              <el-table-column :label="$t('page.business.study.studyMethod.qmr')" prop="qmrMc" />
              <el-table-column :label="$t('page.business.study.studyMethod.qmsj')" prop="createTime" />
              <el-table-column :label="$t('page.business.study.studyMethod.qmyy')" prop="qmyy" />
              <el-table-column :label="$t('page.business.study.studyMethod.remark')" prop="remark" />
            </el-table>
          </div>

        </div>

      </div>
    </div>
    <Yd ref="Yd" @callback="closeYd" />
  </div>
</template>

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

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

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

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

      studyMethod_readList({ studyMethodId: row.id }).then(response => {
        this.list = response.data
        this.loading = false
        this.getPageNum()
      })
    },

    // 获取PDF总页数
    getPageNum() {
      let loadingTask = pdf.createLoadingTask(this.pdfSrc);
      loadingTask.promise
        .then((pdf) => {
          this.totalPages = pdf.numPages;
          this.loadPdfFromUrl()
          // this.$nextTick(() => {
          //   this.setWatermarkContent();
          // });

        })
        .catch((err) => {
          this.$message.msgError("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
    },

    handleExport() {
      studyMethod_export({studyMethodId: this.form.id}).then(response => {
        let fileUrl = response.data.fileUrl
        this.$download.saveAs(process.env.VUE_APP_FILE_DOMAIN + fileUrl, this.form.ffmc + ".pdf");
      })
    }

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