<template>
  <div class="patent-pdf">
    <div>
      <el-button-group class="float_left margin-bottom_10">
        <el-button size="small" :type="pdfType === 2 ? 'primary' : ''" @click="handleSelect(2)">授权文档</el-button>
        <el-button size="small" :type="pdfType === 1 ? 'primary' : ''" @click="handleSelect(1)">公开文档</el-button>
      </el-button-group>
      <el-upload
          class="float_right"
          action="#"
          :auto-upload="false"
          :show-file-list="false"
          :on-change="handleChange"
          :multiple="false"
      >
        <el-button type="success" size="small" :loading="btnLoading" :disabled="!($permission('/workspace/details/updataInstruction') && $r(projectId,[1,2]))">上传文档</el-button>
        <div slot="tip" class="el-upload__tip"></div>
      </el-upload>
    </div>
    <iframe v-if="show" :src="src" frameborder="0" :height="height" width="100%"></iframe>
    <div v-else class="no-pdf-file">
      <span>暂无文档</span>
    </div>
  </div>
</template>

<script>
import Config from '@/config'

export default {
  props: ['patent', 'projectId'],
  components: {},
  data() {
    return {
      src: '',
      fullHeight: document.documentElement.clientHeight,
      height: 0,
      show: false,
      btnLoading: false,
      pdfType: 2,
      pdf: null
    }
  },
  watch: {
    fullHeight() {
      this.refresh()
    },
    patent() {
      this.refresh()
    },
    patentId() {
      this.refresh()
    }
  },
  created(){
    window.addEventListener('resize', this.handleResize)
  },
  mounted() {
    this.refresh()
  },
  methods: {
    handleSelect(type) {
      this.pdfType = type
      this.refresh()
    },
    handleChange(file, fileList) {
      if (file.name.split('.')[1].toLowerCase() !== 'pdf') {
        this.$message.error('请选择PDF格式文件')
        return false
      }
      let formData = new FormData()
      formData.append('file', file.raw)
      formData.append('type', this.pdfType)
      formData.append('patentNo', this.patent.patentNo)
      if (this.pdf) {
        formData.append('url', this.pdf.url)
      }
      this.btnLoading = true
      this.$api.editPatentInstruction(formData).then(response => {
        this.$message.success('操作成功')
        this.btnLoading = false
        this.$emit('refresh')
        this.refresh()
      }).catch(error => {
        this.btnLoading = false
      })
    },
    refresh() {
      this.show = false
      this.height = (this.fullHeight - 230) + 'px'
      this.pdf = this.patent.pdf.find(item => item.type === this.pdfType)
      if (this.pdf) {
        this.$api.getPatentInstructionFile({ patentNo: this.patent.patentNo, type: this.pdfType }).then(response => {
          this.src = '/pdfjs/web/viewer.html?file=' + encodeURIComponent(window.URL.createObjectURL(response))
          this.$nextTick(() => {
            this.show = true
          })
        }).catch(error => {
          this.$message.error('加载失败')
        })
      }
    },
    handleResize(event) {
      this.fullHeight = document.documentElement.clientHeight
    },
  }
}
</script>

<style lang="scss">
.patent-pdf {
  height: 100%;
  .no-pdf-file {
    float: left;
    text-align: center;
    width: 100%;
    background: #e3e2e2;
    height: calc(100% - 120px);
    padding-top: 50px;
    span {
      font-size: 50px;
    }
  }
}
</style>