<template>
  <div class="pay-code">
    <div class="paymount">扫码付款：<span class="exp">￥</span><span class="price">{{ data.payAmount | formatPrice }}</span></div>
    <el-image v-loading="isLoading" :src="payUrl" class="qrcode" />
    <div class="pay-tip">
      <i class="icon-alipay" />
      <span>使用支付宝扫码</span>
    </div>
  </div>
</template>

<script>
import qrcode from 'qrcode'
import { payVipWithAlipay } from '@/api/pay'
export default {
  props: {
    data: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      payData: {},
      isLoading: false,
      payUrl: '',
      timer: null
    }
  },
  created() {
    this.init()
  },
  beforeDestroy() {
    this.stopPayCheck()
  },
  methods: {
    init() {
      const params = {
        payType: 'ZFBEWM',
        bankCode: 'ALIPAY',
        // returnUrl: 'https://www.caimei365.com/',
        returnUrl: process.env.VUE_APP_BASE_SERVER,
        vipRecordId: this.data.vipRecordId
      }
      this.isLoading = true
      payVipWithAlipay(params).then(res => {
        const result = JSON.parse(res.data)
        console.log(result)
        if (result.rt2_retCode === '0000') {
          this.payData = {
            mbOrderId: result.rt5_orderId,
            qrcodeUrl: result.rt8_qrcode
          }
          this.drawQrcode()
        } else {
          this.$message.error('支付系统遇到点小问题，请稍后重试')
          this.isLoading = false
        }
      })
    },
    // 绘制支付二维码
    drawQrcode() {
      qrcode
        .toDataURL(this.payData.qrcodeUrl, { width: 180, margin: 0, errorCorrectionLevel: 'H', type: 'image/jpeg', scale: 4 })
        .then(url => {
          this.payUrl = url
          this.checkedPayConfirm()
        })
        .catch(err => {
          console.error(err)
        }).finally(() => {
          this.isLoading = false
        })
    },
    stopPayCheck() {
      clearInterval(this.timer)
    },
    // 验证确认支付
    checkedPayConfirm() {
      this.timer = setInterval(() => {
        this.$emit('pay-confirm', {
          type: 'ALIPAY',
          data: {
            ...this.payData,
            ...this.data
          }
        })
      }, 3000)
    }
  }
}
</script>

<style scoped lang="scss">
.pay-code {
  .paymount {
    margin-bottom: 16px;
    font-size: 16px;
    color: #101010;

    .exp,
    .price {
      color: #ff6d6d;
    }

    .exp {
      font-size: 14px;
    }

    .price {
      font-size: 20px;
      font-weight: bold;
    }
  }

  .qrcode {
    width: 180px;
    height: 180px;
    border: 1px solid #1890ff;
    box-sizing: border-box;
    padding: 8px;
  }

  .pay-tip {
    width: 180px;
    margin-top: 12px;
    font-size: 14px;
    color: #101010;
    line-height: 25px;
    text-align: center;
  }

  .icon-alipay,
  .icon-wechat {
    display: inline-block;
    width: 25px;
    height: 25px;
    vertical-align: middle;
    margin-right: 10px;
  }

  .icon-wechat {
    background: url(~@/assets/pay/icon-wechat.png) no-repeat center;
    background-size: 25px;
  }

  .icon-alipay {
    background: url(~@/assets/pay/icon-alipay.png) no-repeat center;
    background-size: 25px;
  }
}
</style>
