<template>
  <div class="app-container">
     <el-tabs v-model="activeName"  >
      <el-tab-pane label="在线充值" name="first">
           <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="当前余额"  >
             <span style="font-size:20px;color:red;">{{money}}</span>元
          </el-form-item>
          <el-form-item label="充值金额" prop="money">
            <el-input v-model="form.money" style="width:200px" placeholder="请输入充值金额" />
          </el-form-item>
          <el-form-item label="支付方式" prop="payType">
               <div class="radio">
                 <div class="radio-item">
                     <el-radio v-model="form.payType" :label=1><img class="radio-img" src="../../../assets/image/WeChat_icon30.png">微信</el-radio>
                 </div>
                 <div class="radio-item">
                     <el-radio v-model="form.payType" :label=2><img class="radio-img" src="../../../assets/image/alipay_icon30.png">支付宝</el-radio>
                 </div>
               </div>
            </el-form-item>
            <div class="footer-btn">
              <el-button type="primary" @click="submitForm">充值</el-button>
            </div>
            
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <el-dialog :title="payTitle" :visible.sync="payVisible" top="10vh" width="550px" class="my-dialog">
      <div class="pay-dialog-cont">
        <div class="price">
          <span class="label">应付金额</span>
          <span class="unit">¥</span>
          <span class="num">{{form.money}}</span>
        </div>
        <div class="order-sn">订单编号：{{orderNo}}</div>
        <div class="code-box">
          <div class="qrcode" ref="qrCodeUrl"></div>
          <!-- <img src="../../assets/image/qr.png" alt=""> -->
        </div>
        <div class="tip-box">
          <span>请使用<span class="orange">微信</span>扫一扫</span>
          <span>二维码完成支付</span>
        </div>
      </div>
    </el-dialog>
     
  </div>
</template>

<script>
import { listCompanyRecharge, getCompanyRecharge, delCompanyRecharge, addCompanyRecharge, updateCompanyRecharge, exportCompanyRecharge } from "@/api/company/companyRecharge";
import { getCompanyInfo } from "@/api/company/company";
import { weixinPay } from "@/api/company/pay";
import QRCode from 'qrcodejs2'

export default {
  name: "CompanyRecharge",
  data() {
    return {
      orderNo:"",
      payVisible: false, // 支付弹窗
      payTitle: '微信支付',
      toPayFlag:true,
      activeName:"first",
      money:0,
      // 表单参数
      form: {
        payType:1,
      },
      // 表单校验
      rules: {
        money: [
          { required: true, message: "金额不能为空", trigger: "blur" },
        ],
      }
    };
  },
  created() {
     
    this.getDicts("company_pay_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("company_pay_type").then((response) => {
      this.payTypeOptions = response.data;
    });
    getCompanyInfo().then(response => {
      this.money = response.data.money;
    });
  },
  methods: {
    creatQrCode(url) {
      var qrcode = new QRCode(this.$refs.qrCodeUrl, {
          text: url, // 需要转换为二维码的内容
          width: 200,
          height: 200,
          colorDark: '#000000',
          colorLight: '#ffffff',
          correctLevel: QRCode.CorrectLevel.H
      })
    },
    toPay(orderNo) {
      if(this.form.payType == 1) {
        this.payTitle = '微信支付'
        var param={orderNo:orderNo,orderType:1};
        weixinPay(param).then(response => {
          this.creatQrCode(response.qr)
        });
        this.payVisible = true
        
      } else {
        this.payTitle = '支付宝支付'
      }
      
    },
    /** 提交按钮 */
    submitForm() {
      this.msgError("未开放此功能");
      return;
      var that=this;
       if(this.orderNo!=""){
        this.payVisible = true
        return;
      }
      if(!this.toPayFlag){
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          that.toPayFlag=false;
            addCompanyRecharge(this.form).then(response => {
              if (response.code === 200) {
                
                that.orderNo=response.rechargeNo
                that.toPay(response.rechargeNo);
              }
            });
        }
      });
    },
  }
};
</script>
<style  lang="scss" scoped>
.el-radio{
  margin-right: 0px;
}
.radio{
  display: flex;
  align-items: center;
   

}
.radio-item{
  padding-right:15px;
  .radio-img{
    margin-right: 10px;
  }
  /deep/.el-radio{
    display: flex;
    align-items: center;
    .el-radio__label{
      display: flex;
      align-items: center;
    }
  }
}
.footer-btn{
  padding-top: 30px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
}
.pay-dialog-cont{
    padding: 5px 0 50px;
    display: flex;
    flex-direction: column;
    align-items: center;
    .price{
      display: flex;
      align-items: flex-end;
      .label{
        font-size: 14px;
        color: #666666;
        margin-right: 10px;
        line-height: 20px;
      }
      .unit{
        font-size: 16px;
        font-weight: bold;
        color: #FF6633;
        margin-right: 5px;
        line-height: 20px;
      }
      .num{
        font-size: 24px;
        font-weight: bold;
        color: #FF6633;
      }
    }
    .order-sn{
      font-size: 14px;
      color: #666666;
      line-height: 1;
      margin-top: 20px;
    }
    .code-box{
      width: 200px;
      height: 200px;
      margin-top: 40px;
      img{
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
    .tip-box{
      margin-top: 30px;
      display: flex;
      align-items: center;
      flex-direction: column;
      font-size: 14px;
      color: #333333;
      line-height: 22px;
      .orange{
        color: #FF6633;
      }
    }
  }
</style>
