<template>
  <div class="pdf">
    <iframe style="width:100vw;height:100vh" :src="pdfUrl"></iframe>
  </div>
</template>
<script>
import { makePdf, share } from '@/api/ebiz/proposal/proposal.js'
import { Toast } from 'vant'
import config from '@/config'
import dataDictionary from '@/assets/js/utils/data-dictionary' //使用数据字典中的险种类型
import { weixinShare } from '@/assets/js/utils/wxShare.js'
import { queryPersonal } from '@/api/ebiz/laurelClub/laurelClub'
export default {
  data() {
    let isWeixin = this.$utils.device().isWeixin //判断环境
    return {
      pdfUrl: '',
      isWeixin,
      title: ''
    }
  },
  components: {
    [Toast.name]: Toast
  },
  created() {
    if (this.$route.query.checkRule == '1') {
      document.title = '体检规则'
      console.log(this.$assetsUrl + `images/underWritingRule.pdf`)
      this.pdfUrl = location.origin + '/pdfjs/web/viewer.html?file=' + this.$assetsUrl + `images/underWritingRule.pdf`
    } else {
      this.init()
    }
  },
  mounted() {
    let riskCode = localStorage.pdfShareCode
    dataDictionary.riskType.some(item => {
      if (item.code == riskCode) {
        this.title = item.shortName + '计划书pdf'
        return true
      }
    })
    weixinShare({
      title: this.title,
      imgUrl: this.$assetsUrl + 'images/logo.png',
      desc: '国富为您量身定制的保险产品，请查收'
    })
    //重置左上角按钮，变成返回
    window.EWebBridge.webCallAppInJs("webview_left_button",{
      reset:"1" //1：重制 其他值不变
    })
    window.EWebBridge.webCallAppInJs("navigation",{ 
      title:"PDF预览"
    })
  },
  methods: {
    async init() {
      if (this.$route.query.url) {
        document.title = this.$route.query.name
        let pdfUrl = this.$route.query.url
        this.pdfUrl = location.origin + '/pdfjs/web/viewer.html?file=' + pdfUrl
      } else {
        this.$route.query.proposalNo && (localStorage.proposalNo = this.$route.query.proposalNo)
        this.$route.query.token && (localStorage.token = this.$route.query.token)
        let params = {
          proposalInfoDTO: {
            // proposalNo: localStorage.proposalNo
            proposalNo: localStorage.proposalNo
          }
        }
        let pdfUrl = ''
        let make = await makePdf(params)
        if (make.result == '0') {
          pdfUrl = encodeURIComponent(config.imgDomain + '/returnDirectStream?imgPath=' + make.content)
          this.pdfUrl = location.origin + '/pdfjs/web/viewer.html?file=' + pdfUrl
          if (!this.isWeixin) {
            setTimeout(() => {
              this.filterBtn() // 初始化分享按钮
              window.appCallBack = this.appCallBack //app回调
            }, 1000)
          }
        } else {
          Toast.fail(make.resultMessage)
        }
      }
    },
    // 分享按钮
    filterBtn() {
      window.EWebBridge.webCallAppInJs('webview_right_button', {
        btns: [
          {
            img: this.$assetsUrl + 'images/share@3x.png'
          }
        ]
      })
    },
    async appCallBack(data) {
      let date = new Date()
      let month = date.getMonth() + 1
      if (month <= 9) {
        month = '0' + month
      }
      let reqData = {
        mdType: 'm',
        monthStr:date.getFullYear() + month
      }
      let shareContent;
      let resData = await queryPersonal(reqData)
      if (resData.content.length&&resData.content[0].glevel>0&&resData.content[0].ggrade) {
        if(resData.content[0].glevel<3){
          resData.content[0].ggrade='00'
        }
        let ggrade =this.memberConversion(resData.content[0].ggrade);
        shareContent='国富桂冠人力'+resData.content[0].glevel+'级'+ggrade+resData.content[0].name+'为您量身定制的保险产品，请查收';
      } else {
        shareContent='国富为您量身定制的保险产品，请查收';
      }
      if (data.trigger == 'right_button_click') {
        // eslint-disable-next-line no-undef
        EWebBridge.webCallAppInJs('bridge', {
          flag: 'share',
          extra: {
            title: this.title,
            content: shareContent,
            url: location.origin + '/#/proposal/pdf?proposalNo=' + localStorage.proposalNo + '&token=' + localStorage.token,
            img: this.$assetsUrl + 'images/logo.png'
          }
        })
        share()
      }
    },
    memberConversion(status) {
      let text=''
      switch (status) {
        case '00':
          text = '准会员'
          break
        case '01':
          text = '正式会员'
          break
        case '02':
          text = '铜牌会员'
          break
        case '03':
          text = '银牌会员'
          break
        case '04':
          text = '金牌会员'
          break
        case '05':
          text = '白金会员'
          break
        case '06':
          text = '终身会员'
          break
      }
      return text
    }
  }
}
</script>
