<template>
  <div class="qrpay">
    <div class="qrpay-hd">
      <div class="pic">
        <img src="https://webapp-pub.ezijing.com/project/application/images/my_pay_01.png" />
      </div>
      <h1>欢迎您</h1>
      <p class="t1">
        申请万达酒店及度假村中/高端酒店总经理培养项目，本项目的注册费，申请费共计<span>600</span>元，请扫描二维码缴费以完成报名。
      </p>
    </div>
    <div class="qrcode-error" v-if="qrcodeError">
      生成二维码失败请刷新<i class="el-icon-refresh-left" @click="getOrder" title="刷新"></i>
    </div>
    <div class="qrcode" v-else>
      <qrcode-vue :value="qrcodeValue" size="100"></qrcode-vue>
      <span @click="getOrder">刷新</span>
    </div>
  </div>
</template>

<script>
import QrcodeVue from 'qrcode.vue'
import * as api from '@/api/my'

export default {
  components: { QrcodeVue },
  data() {
    return {
      order: { id: '', payment_url: '' },
      qrcodeError: false,
    }
  },
  computed: {
    // 二维码地址
    qrcodeValue() {
      return this.order.payment_url
    },
  },
  methods: {
    // 获取订单
    getOrder() {
      api.getOrder({ payment_method: 'WX_PAY_QR' }).then((response) => {
        const { data, error, message } = response
        this.qrcodeError = error.toString() === '1'
        if (error.toString() === '0') {
          this.order = data
          this.$emit('update', this.order)
        } else {
          this.$message.error(message)
        }
      })
    },
  },
  beforeMount() {
    this.getOrder()
  },
}
</script>

<style lang="scss" scoped>
.qrpay-hd {
  .pic {
    text-align: center;
  }
  h1 {
    padding: 10px 0;
    font-size: 24px;
    font-weight: 500;
    color: #222;
    line-height: 1;
    text-align: center;
  }
}
.qrcode {
  padding: 20px 0;
  text-align: center;
  span {
    margin-top: 10px;
    font-size: 12px;
    color: #999;
    cursor: pointer;
  }
}
.qrcode-error {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 100px;
  height: 100px;
  padding: 10px;
  margin: 20px auto;
  color: var(--main-color);
  border-style: dashed;
  border-width: 1px;
  border-color: var(--main-color);
  box-sizing: border-box;
  text-align: center;
  i {
    margin-top: 10px;
    cursor: pointer;
  }
}
.t1 {
  font-size: 14px;
  color: #303030;
  line-height: 20px;
  text-align: center;
  span {
    color: #ffa448;
  }
}
</style>
