<template>
  <div id='PaymentCode'>
    <modal :show.sync="paymentCodeShow" backdrop="false">
      <header slot="modal-header" class="modal-header">
        <h4 class="modal-title">请扫描用户付款码</h4>
      </header>
      <article slot="modal-body" class="modal-body">
        <div class="form-horizontal">
          <div class="form-group">
            <input type="text" class="form-control" @keyup.enter="ConfirmCode" @blur="paycodefocus"
                   v-model="paymentCode" placeholder="请扫描用户付款码" v-el:paycode>
          </div>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button type="button" class="btn btn-default" @click='closeCode'>取消</button>
      </footer>
    </modal>
  </div>
</template>

<script>
  export default {
    name: 'PaymentCode',
    props: {
      // 付款方式
      payment: {},
      // 用户信息
      row: {},
      // 付款金额
      money: {}
    },
    data () {
      return {
        // 付款码弹框显示
        paymentCodeShow: false,
        // 付款码值
        paymentCode: null,
        // 付款码流程控制
        paycoderesolve: null,
        // 付款码返回数据
        paymentCodeReturnData: {},
        // 定时器
        timer: null,
        // 收费成功id
        sellinggasId: null
      }
    },
    created () {
    },
    ready () {
    },
    methods: {
      // 返回结果为true意味着可以通行
      flowPath () {
        return new Promise((resolve) => {
          this.paycoderesolve = resolve
          // 如果是付款码支付则显示界面扫用户付款码收钱
          if (this.payment.toString().indexOf('&') !== -1) {
            this.paymentCodeShow = true
            this.paycodefocus()
          } else {
            this.paycoderesolve({msg: '不是付款码支付', result: true})
          }
        })
      },
      // 付款码框获得焦点
      paycodefocus () {
        this.$nextTick(() => {
          this.$els.paycode.focus()
        })
      },
      // 付款码支付流程
      async ConfirmCode () {
        let data = {
          f_userinfo_id: this.row.f_userinfo_id,
          f_user_id: this.row.f_user_id,
          f_userfiles_id: this.row.f_userfiles_id,
          money: this.money,
          f_payment: this.payment,
          auth_code: this.paymentCode,
          filiale: this.$login.f.orgid,
          body: this.$login.f.orgs
        }
        let res = await this.$resetpost('rs/logic/WeiXinPay', data, {resolveMsg: null, rejectMsg: '服务器内部出错,'})
        this.paymentCodeReturnData = res = res.data
        if (res.result_msg == '支付确认成功') {
          this.paycoderesolve({msg: '支付完成', result: true})
          this.$closeAlert()
        } else if (res.result_msg == '支付结果未知') {
          // 支付结果未知, 根据商户订单号继续查询
          this.$showAlert(`正在查询用户付款状态,请勿进行任何操作,耐心等待,如长时间未响应可手动取消`, 'warning')
          let self = this
          // 启动定时器之前先关闭定时器,防止之前的定时器未关闭
          if (this.timer) {
            this.closeTimer()
          }
          let times = 1
          this.timer = setInterval(async () => {
            times++
            let res1 = await self.$resetpost('rs/logic/WeiXinPayStatus', res, {resolveMsg: null, rejectMsg: '付款码支付查询出错,'})
            res1 = res1.data
            // 支付成功关闭定时器
            if (res1.result_msg == '支付确认成功') {
              self.paycoderesolve({msg: '支付完成', result: true})
              self.closeTimer()
              return
            }
            if (times > 12) {
              self.$showAlert(`多次查询未成功，请重新扫描用户付款码`, 'danger')
              self.closeTimer()
            }
          }, 5000)
        } else {
          this.$showAlert(`${res.result_msg}, 请重新扫描用户付款码`, 'danger')
        }
        // 清空付款码
        this.paymentCode = ''
      },
      // 付款成功更新收费记录订单号
      async updateSellinggas () {
        if (this.payment.toString().indexOf('&') !== -1) {
          let sqldata = `update t_sellinggas set f_serial_id = '${this.paymentCodeReturnData.f_out_trade_no}' where id = '${this.sellinggasId}'`
          await this.$resetpost('rs/logic/runSQL', {data: {sql: sqldata}}, {resolveMsg: null, rejectMsg: '更新收费记录订单号失败'})
        }
      },
      // 关闭定时器
      closeTimer () {
        console.log('关闭定时器')
        clearInterval(this.timer)
        this.timer = null
      },
      closeCode () {
        this.$showMessage('取消后无法继续收款,确定取消吗?', ['confirm', 'cancel']).then((res) => {
          if (res == 'confirm') {
            this.paymentCodeShow = false
            this.$closeAlert()
            this.paycoderesolve({msg: '支付取消', result: false})
            this.closeTimer()
          }
        })
      }
    }
  }
</script>

<style lang="less">

</style>
