<template>
  <div class="patent-pdf height_100">
    <div>
      <el-button-group class="float_left margin-bottom_10" v-if="patent.pdf && patent.pdf.length > 1">
        <el-button size="small" :type="pdfType === 1 ? 'primary' : ''" @click="handleSelect(1)">授权文档</el-button>
        <el-button size="small" :type="pdfType === 0 ? 'primary' : ''" @click="handleSelect(0)">公开文档</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" v-if="projectId"
          :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 { patentDetails } from './mixins';
export default {
  mixins: [patentDetails],
  props: ['patent', 'projectId', 'patentNo'],
  components: {},
  data() {
    return {
      src: '',
      fullHeight: document.documentElement.clientHeight,
      height: 0,
      show: false,
      btnLoading: false,
      pdfType: 1,
      pdf: null
    }
  },
  watch: {
    patent() {
      if (!this.outside) {
        this.refresh()
      }
    },
    patentNo() {
      if (this.outside) {
        this.getData()
      }
    }
  },
  created() {
    window.addEventListener('resize', this.handleResize)
  },
  mounted() {
    // outside为true是外部
    if (this.outside) {//外部
      this.getData()
    } else {
      this.refresh()
    }
  },
  methods: {
    // 获取外部的pdf地址
    getData() {
      if (this.patent.pdf && this.patent.pdf.length > 0) {
        return false
      }
      this.show = false
      this.height = (this.fullHeight - 230) + 'px'
      var params = {
        appNo: this.patent.appNo,
        rowApplicationNo: this.patent.rowApplicationNo,
      }
      this.$api.getCnPdf(params).then(response => {
        if (response.code == 200) {
          this.$set(this.patent, 'pdf', response.data)
          this.getSrc()
        }
      })
    },
    // 内部专利根据guid获取路径
    getSrc() {
      let obj = this.patent.pdf.find(item => {
        return item.type == this.pdfType
      })
      // if (this.outside) {
      //   var src = obj.pdfGuid
      //   this.src = `http://${this.$c.staticURL}:8012/onlinePreview?url=` + btoa(encodeURIComponent(src + '&fullfilename=test.pdf'))
      // } else {
      if (obj) {//外部内部都是返回guid
        var src = this.$commonJS.checkViewer(obj.pdfGuid)
        this.src = `http://${this.$c.staticURL}:8012/onlinePreview?url=` + btoa(encodeURIComponent(src + '&fullfilename=test.pdf'))
      }
      // }
      this.$nextTick(() => {
        this.show = true
      })
    },
    // 点击切换按钮（授权、公开）
    handleSelect(type) {
      this.pdfType = type
      this.show = false
      if (this.outside) {//外部
        this.getSrc()
      } else {//内部
        this.getSrc()
      }
    },
    // 上传文档
    // 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
    //   })
    // },
    // 请求内部pdf的guid
    refresh() {
      this.show = false
      this.height = (this.fullHeight - 230) + 'px'
      let params = {
        appNo: this.patent.appNo,
        // type: this.pdfType,//0公开1授权
      }
      this.$api.getTextPdf(params).then(response => {
        if (response.code == 200) {
          this.$set(this.patent, 'pdf', response.data)
          this.getSrc()
        }
      })
    },
    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>