<template>
  <div id='contractSigning'>
    <modal :show.sync="show" backdrop="false">
      <header slot="modal-header" class="modal-header">
        <h3 style="color:black" class="modal-title">合同</h3>
      </header>
      <article slot="modal-body" class="modal-body">
        <iframe :src="url" v-if="url" width="100%" height="500px">
        </iframe>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button  type="button" class="btn btn-default" @click="handleReset">签名</button>
        <button  type="button" class="btn btn-default" @click="handleGenerate">保存</button>
        <button  type="button" class="btn btn-default" @click="clasehand">关闭</button>
      </footer>
    </modal>
    <work-busy :is-busy="showbuy"></work-busy>
  </div>
</template>
<script>
  import * as jsign from '../../../plugins/jsign'
  export default {
    title: '合同签名页面',
    data () {
      return {
        url: '',
        imgstr: '',
        imgurl: '',
        scheduleData: '',
        showclase: false,
        showbuy: false
      }
    },
    props: ['show'],
    ready () {
    },
    compiled () {
    },
    methods: {
      clasehand () {
        this.url = ''
        this.imgstr = ''
        this.scheduleData = ''
        this.show = false
      },
      getUserInfo () {
        jsign.jsign_set_string('singurl', encodeURI(this.imgurl))
      },
      getContractData (userinfoid) {
        console.log(userinfoid)
        this.showbuy = true
        this.f_userinfo_id = userinfoid
        console.log("uuuurl", window.location.href)
        this.$resetpost('wx/rs/logic/wordToosRept', {f_userinfo_id: this.f_userinfo_id}, {resolveMsg: null, rejectMsg: '获取合同失败！！'}).then(item => {
          if (item.data.length > 0) {
            this.scheduleData = item.data[0]
            this.url = item.data[0].f_visitpc_url
            // 1. 替换反斜杠为斜杠
            // 2. 对 URL 进行编码
            let fullUrl = encodeURI(this.url.replace(/\\/g, '/'))
            this.$resetpost(`rs/convert/convertImg`, {url: fullUrl}, {resolveMsg: null, rejectMsg: null}).then((ret) => {
              let href = window.location.href.split('#')[0]
              let img = href + 'files' + ret.data
              this.imgurl = encodeURI(img.replace(/\\/g, '/'))
              console.log(this.imgurl)
              this.showbuy = false
            })
          }
        }).catch(err => {
          console.log(err)
          this.showbuy = false
        })
      },
      handleReset () {
        console.log('进入sign()')
        console.log('jsign.dictGetArray', jsign.dictGetArray)
        jsign.jsign_open(this)
        setTimeout(() => {
          jsign.jsign_get_device_status()
        }, 1500)
        setTimeout(() => {
          if (jsign.dictGetArray['devicestatus'] === 'fail') {
            console.log('没签字屏')
            this.$showAlert('没有可用签字屏', 'danger', 0)
            this.busy = 1
            return
          }
          // jsign.jsign_set_string('name', 'jsigner')
          // jsign.jsign_set_string('businessname', 'payment')
          this.getUserInfo()
          jsign.jsign_set_string('picture', '')

          jsign.jsign_run_sign(jsign.htmlPath + `sign.html`)
        }, 3000)
      },
      handleGenerate () {
        // this.$dispatch('toggle')
        if (this.imgstr === '') {
          this.$showAlert('请先签字！', 'danger', 2000)
          return
        }
        var data = {
          content: this.imgstr,
          f_userinfo_id: this.scheduleData.f_userinfo_id,
          f_contract_signature_id: this.scheduleData.f_contract_signature_id
        }
        this.$resetpost('wx/rs/logic/pdfToosSignature', data).then((res) => {
          if (res.data) {
            this.showclase = true
            this.url = res.data.f_visitpc_url
          } else {
            this.$vux.alert.show({title: '操作提示', content: '提交失败请稍后重试！'})
          }
        })
      }
    },
    computed: {
    }
  }
</script>
