<template>
  <div>
    <iframe :src="src + pdfUrl" style="width: 100vw; height: 100vh"></iframe>
  </div>
</template>

<script>
import { getShareKey, saveShareRecord } from '@/api/ebiz/manpower/manpower'

export default {
  data() {
    return {
      // src: 'http://47.96.143.111/pdfjs/web/viewer.html?file=http://47.96.143.111:8000/app/images/jsSDK.pdf'
      pdfUrl: '',
      src: location.origin + '/pdfjs/web/viewer.html?file=',
      shareContent: '',
      redisKey: ''
    }
  },
  created() {
    setTimeout(() => {
      // 右上角的显示
      this.$jump({
        flag: 'webview_right_button',
        extra: {
          btns: [
            {
              img: this.$assetsUrl + 'images/share@3x.png'
            }
          ]
        }
      })
    }, 1000)
    window['appCallBack'] = this.appCallBack
  },
  mounted() {
    this.init()
    this.redisKey = this.$route.query.redisKey
    if (this.redisKey) {
      this.saveShareRecord('1')
      if (this.$route.query.token) {
        localStorage.setItem('token', this.$route.query.token)
      }
    } else {
      this.getShareKey()
    }
  },
  methods: {
    init() {
      let num = this.$route.query.pdf
      switch (num) {
        case '0':
          this.pdfUrl = this.$assetsUrl + 'personnel/recruitment.pdf'
          this.shareContent = '个险渠道2020年聘才方案'
          document.title = '个险渠道2020年聘才方案'
          break
        case '1':
          this.pdfUrl = this.$assetsUrl + 'personnel/allowance.pdf'
          this.shareContent = '个险渠道筹备津贴方案'
          document.title = '个险渠道筹备津贴方案'
          break
      }
    },
    // 原生点击右上角按钮事件
    appCallBack(data) {
      if (data.trigger == 'right_button_click') {
        this.saveShareRecord('0')
        let pdf = this.$route.query.pdf
        let token = window.localStorage.getItem('token')
        // eslint-disable-next-line no-undef
        EWebBridge.webCallAppInJs('bridge', {
          flag: 'share',
          extra: {
            title: '国富人寿欢迎您！',
            content: this.shareContent,
            url: `${location.origin}/#/manpower/increaseStaffTools/PdfShare?pdf=${pdf}&redisKey=${this.redisKey}&token=${token}`,
            img: this.$assetsUrl + 'images/logo.png'
          }
        })
      }
    },
    getShareKey() {
      let data = {
        linkType: '0002',
        url: `${location.origin}/pdfjs/web/viewer.html?file=${this.pdfUrl}`
      }
      getShareKey(data)
        .then((res) => {
          console.log(res)
          if (res.result == 0) {
            this.redisKey = res.content
          } else {
            this.$toast(res.resultMessage)
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    saveShareRecord(flag) {
      let data = {
        linkType: '0002',
        flag: flag,
        redisKey: this.redisKey
      }
      saveShareRecord(data)
    }
  }
}
</script>
