<template>
  <div class="cmain">
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="微信充值" name="微信充值"></el-tab-pane>
      <el-tab-pane label="转账充值" name="转账充值"></el-tab-pane>
    </el-tabs>
    <div class="bos" v-if="activeName == '微信充值'">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="充值金额" prop="money">
          <el-input type="number" v-model="form.money" placeholder="请输入充值金额" clearable>
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="bos" v-if="activeName == '转账充值'">
      <el-form ref="platform" disabled :model="platform" label-width="120px">
        <el-steps :active="1" align-center class="cg">
          <el-step title="充值信息"></el-step>
          <el-step title="平台确认"></el-step>
          <el-step title="余额到账"></el-step>
        </el-steps>
        <el-form-item label="服务公司名称">
          <el-input v-model="platform.serviceCompany" />
        </el-form-item>
        <el-form-item label="开户行">
          <el-input v-model="platform.bankName" />
        </el-form-item>
        <el-form-item label="充值到账">
          <el-input v-model="platform.bankAccount" />
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="充值金额" prop="money">
          <el-input type="number" v-model="form.money" placeholder="请输入充值金额" clearable>
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="充值凭证" prop="certificate">
          <ImageUpload v-model="form.certificate" :isShowTip="false" :limit="1"></ImageUpload>
          <span class="tips">例如：付款或者转账截图</span>
        </el-form-item>
      </el-form>
    </div>
    <div class="mfooter">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="$layer.close(layerid)">取 消</el-button>
    </div>
    <!--扫码支付-->
    <el-dialog title="扫码支付" :visible.sync="open" :width="'500px'" @close="close()" :close-on-click-modal="false" append-to-body>
      <div class="pcontents">
        <div id="qrcode"></div>
        <div class="sm">请用微信扫码，完成支付</div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
let timer = null;
import QRCode from 'qrcodejs2';
export default {
  data() {
    return {
      activeName: '微信充值',
      open: false,
      platform: {},
      form: {},
      rules: {
        money: [{ required: true, message: '充值金额不能为空', trigger: 'blur' }],
        certificate: [{ required: true, message: '充值凭证不能为空', trigger: 'blur' }]
      }
    };
  },
  props: {
    param: {
      type: Object,
      default: () => {
        return {};
      }
    },
    layerid: {
      type: String
    }
  },
  mounted() {
    this.ajax({ url: '/work/platform/detail' }).then((response) => {
      this.platform = response.data;
    });
    if (this.param.id) {
      this.ajax({ url: '/work/recharge/detail/' + this.param.id }).then((response) => {
        this.form = response.data;
      });
    }
  },
  methods: {
    //取消支付
    close() {
      this.open = false;
      clearInterval(timer);
      this.$modal.msgError('取消支付');
    },
    //每3秒获取一次支付结果
    refresh() {
      timer = setInterval(() => {
        this.ajax({ url: '/work/wxPay/payState', data: { ident: this.form.ident }, loading: 'false' }).then((response) => {
          if (response.data) {
            clearInterval(timer);
            this.$modal.msgSuccess('充值成功');
            this.$layer.close(this.layerid);
          }
        });
      }, 3000);
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.activeName == '微信充值') {
            this.form.ident = Date.now().toString() + Math.floor(Math.random() * 1000).toString();
            this.ajax({ method: 'post', url: '/work/wxPay/pay', data: this.form }).then((response) => {
              this.open = true;
              this.refresh();
              this.$nextTick(() => {
                //生成二维码
                document.getElementById('qrcode').innerHTML = '';
                new QRCode('qrcode', {
                  width: 250,
                  height: 250,
                  text: JSON.parse(response.data).code_url
                });
              });
            });
          } else {
            this.ajax({ method: 'post', url: '/work/recharge/add', data: this.form }).then((response) => {
              this.$modal.msgSuccess('提交成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          }
        }
      });
    }
  },
  destroyed() {
    clearInterval(timer);
  }
};
</script>
