import { Apply ,getEmailCode} from "../../api/pay"
import { GetUserAccount } from "../../api/help"
export default {
  data() {
    return {
      params: {
        coin: 'USDT',
        chain: '',
        amount: '',
        address: "",  
        emailCode: "",
      },
      value: "",
      options: [{ 
        value: '10',
        label: '10'
      }, {
        value: '20',
        label: '20'
      }, {
        value: '50',
        label: '50'
      }, {
        value: '100',
        label: '100'
      }, {
        value: '200',
        label: '200'
      }, {
        value: '500',
        label: '500'
      }, {
        value: '1000',
        label: '1000'
      }],
      coinOptions: [{
        value: 'USDT',
        label: 'USDT'
      }, {
        value: 'USDC',
        label: 'USDC'
      }, {
        value: 'BUSD',
        label: 'BUSD'
      }],
      chainOptions: [{
        value: 'TRX',
        label: 'TRX-TRC20',
        disabled: false
      }, {
        value: 'ETH',
        label: 'ETH-ERC20',
        disabled: false
      }, {
        value: 'BSC',
        label: 'BSC-BEP20',
        disabled: true
      }],
      outerVisible: false,
      innerVisible: false,
      transactionData: {
        orderId: "",
        address: ""
      },
      WithdrawalLoading: false,
      ConfirmationLoading: false,
      CloseTransferLoading: false,
      ProblemLoading: false,
      dialogVisibleSuccess: false,
      codeBtn: {
        btnStatus: false,
        btnText: "login.getCode",
        btnType: "primary",
      },
      balances: {
        total: "",
        usdc: "",
        usdt: "",
        userId: "",
        userName: ""

      },
      outTimer: "",
      codeTimer: null,

    }
  },
  mounted() {
    this.fetchUserAccount()//请求当前余额

  },
  methods: {
     //请求邮箱验证码
     async fetchCode(params) {
  
      const { data } = await getEmailCode(params)
      
      if (data.code === 200) {//提现成功
        this.$message({
          type: 'success',
          message:this.$t(`pay.mailbox`),
          duration: 3000,
          showClose: true,
        });

       
      } else {
        //获取验证码失败清除按钮倒计时
        clearTimeout(this.outTimer);
        clearInterval(this.codeTimer);
        //取消按钮禁用
        this.codeBtn.btnStatus = false;
        //按钮文字改成获取验证码
        this.codeBtn.btnText = "login.getCode";
      }

    },
    //请求申请提现
    async fetchWithdrawal(params) {
      this.WithdrawalLoading = true
      const { data } = await Apply(params)

      if (data.code === 200) {//提现成功
        this.$message({
          type: 'success',
          message: data.msg,
          duration: 3000,
          showClose: true,
        });

        this.$router.push(`/personal`)
      }

      this.WithdrawalLoading = false
    },
    //请求当前余额
    async fetchUserAccount(params) {
      const { data } = await GetUserAccount(params)
      this.balances = data.data
      this.params.amount = this.balances.usdt

    },



    //点击确认提交提现
    handelWithdrawal() {

      let pattern = /^\d+(\.\d+)?$/; //金额只能是数字正则表达
      if (!pattern.test(this.params.amount)) {
        this.$message({
          message: this.$t(`pay.rechargeAmount`),
          type: "error",
        });

        return
      }
      if (this.params.amount <= 0) {
        this.$message({
          message: this.$t(`pay.paymentAmount`),
          type: "error",
        });
        return
      }


      for (const key in this.params) { //判空


        if (this.params[key] == "") {
          switch (key) {
            case "coin":
                this.$message({
                message: this.$t(`pay.paymentCurrency`),
                type: "error",
              });
              break;
            case "chain":
              this.$message({
                message: this.$t(`pay.paymentNetwork`),
                type: "error",
              });
              break;
            case "address":
              this.$message({
                message: this.$t(`pay.paymentAddress`),
                type: "error",
              });
              break;
            case "emailCode":
              this.$message({
                message: this.$t(`pay.paymentCode`),
                type: "error",
              });
              break;
            case "amount":
              this.$message({
                message: this.$t(`pay.paymentAmount`),
                type: "error",
              });
              break;


            default:
              break;
          }

        
          return
        }
      }




      // 判断总金额是否足够
      switch (this.params.coin) {
        case "USDT":


          if (this.balances.usdt - this.params.amount < 0) {
            this.$message({
              type: 'error',
              message:this.$t(`pay.Insufficient`)
            });
            return
          }
          break;
        case "USDC":

          if (this.balances.usdc - this.params.amount < 0) {
            this.$message({
              type: 'error',
              message:this.$t(`pay.Insufficient`)
            });
            return
          }

          break;
        case "BUSD":

          if (this.balances.busd - this.params.amount < 0) {
            this.$message({
              type: 'error',
              message:this.$t(`pay.Insufficient`)
            });
            return
          }

          break;

        default:
          break;
      }
      this.fetchWithdrawal(this.params)
    },
    //选择币种  金额随之变化
    handelCoin(value) {
      // 币种变化 金额随之变化
      // switch (this.params.coin) {
      //   case "USDT":
      //     this.params.amount = this.balances.usdt
      //     break;
      //   case "USDC":
      //     this.params.amount = this.balances.usdc


      //     break;
      //   case "BUSD":

      //     this.params.amount = this.balances.busd

      //     break;

      //   default:
      //     break;
      // }
    },
    //获取邮箱验证
    handelCodeBtn(){  

      this.fetchCode()
       // 获取验证码后禁用按钮
       this.codeBtn.btnStatus = true;
       // 按钮倒计时
       this.outTimer = setTimeout(() => {
         this.countDown(60);
       }, 5);
   
    },
      //验证码倒计时
      countDown(val) {
        //判断定时器是否存在
        if (this.codeTimer) {
          clearInterval(this.codeTimer);
        }
        //按钮文字改成发送中
        this.codeBtn.btnText = "login.sending";
        this.codeBtn.btnStatus = false;
        //setTiemOut 执行一次
        // setInterval 不断执行，需要条件才会停止
        let tim = val;
        this.codeTimer = setInterval(() => {
          tim--;
          if (tim === 0) {
            clearInterval(this.codeTimer);
            this.codeBtn.btnStatus = false;
            //按钮文字改成获取验证码
            this.codeBtn.btnText = "login.getCode";
          } else {
            this.codeBtn.btnStatus = true;
            this.codeBtn.btnText = tim;
          }
        }, 1000);
      },




  }
}