<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>
      <div class="float_right" v-if="!outside">
        <el-button icon="el-icon-refresh" circle @click="exportData('5')"></el-button>
      </div>
    </div>
    <div style="height:calc(100% - 0px)" v-loading="loading">
      <myIframe v-if="show" :src="src" frameborder="0" :height="height" width="100%"></myIframe>
      <div v-else class="no-pdf-file">
        <span>暂无文档</span>
      </div>
    </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: true,
      btnLoading: false,
      pdfType: 1,
      pdf: null
    }
  },
  watch: {
    patentNo() {
      this.pdfType = 1
      // if (this.outside) {
      //   this.getData()
      // }else{
      //   this.refreshData()
      // }
      this.getData()
    }
  },
  created() {
    window.addEventListener('resize', this.handleResize)
  },
  mounted() {
    // outside为true是外部
    // if (this.outside) {//外部
    //   this.getData()
    // } else {
    //   this.refreshData()
    // }
    this.getData()
  },
  methods: {
    // 获取外部的pdf地址
    getData() {
      
      this.height = (this.fullHeight - 240) + 'px'
      if (this.patent.pdf && this.patent.pdf.length > 0) {
        this.getSrc()
        return false
      }
      var params = {
        appNo: this.patent.appNo,
        rowApplicationNo: this.patent.rowApplicationNo,
        publicNo:this.patent.publicNo,
        grantNo:this.patent.grantNo
      }
      this.loading = true
      this.$api.getCnPdf(params).then(response => {
        if (response.code == 200) {
          this.$set(this.patent, 'pdf', response.data)
          this.getSrc()
          this.loading = false
        }
      }).catch(error=>{
        this.loading = false
      })
    },
    // 内部专利根据guid获取路径
    getSrc() {
      if(this.patent.pdf.length==0){
        this.show = false
        return 
      }
      let obj = this.patent.pdf.find(item => {
        return item.type == this.pdfType
      })
      var guid = ''
      if (obj) {//内部是返回guid,外部返回绝对路径
        guid = obj.pdfGuid
      }else{
        this.pdfType = this.patent.pdf[0].type
        guid = this.patent.pdf[0].pdfGuid
      }
      // if (this.outside) {
      //   this.src = guid
      // }else{
      //   this.src = this.$commonJS.checkGuid(guid)
      // }
      this.src = guid
      this.$nextTick(() => {
        this.show = true
      })
    },
    // 点击切换按钮（授权、公开）
    handleSelect(type) {
      this.pdfType = type
      this.show = false
      this.getSrc()
    },
    
    // 请求内部pdf的guid
    refreshData() {
      this.show = false
      this.height = (this.fullHeight - 240) + 'px'
      if(this.patent.pdf && this.patent.pdf.length>0){
        this.getSrc()
        return
      }
      let params = {
        appNo: this.patent.patentNo,
        // type: this.pdfType,//0公开1授权
      }
      this.loading = true
      this.$api.getTextPdf(params).then(response => {
        if (response.code == 200) {
          this.$set(this.patent, 'pdf', response.data)
          this.getSrc()
          this.loading  =false
        }
      }).catch(error=>{
        this.loading = false
      })
    },
    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% - 50px);
    padding-top: 50px;

    span {
      font-size: 50px;
    }
  }
}
</style>