import { LevelUp, GetNeedFunding } from "../../api/pay"
import { GetUserAccount } from "../../api/help"
export default {
  data() {
    return {

      upgradeList: {
        amount: "",
        network: "TRX(Tron TRC20)",
        address: "copy 地址",
        id: "",
        label: ""

      },
      AccountUpgradeLoading: false,
      accounts: [{//L3 月度付费用户
        id: "advance",
        label: "login.monthlyPlay",
        charges: "10(USDT、USDC、BUSD)",
        amount: "10",
        value: 0,
        disabled: false,
      },
      {//L3 年度付费用户
        id: "advance",
        label: "login.yearsPlay",
        charges: "100(USDT、USDC、BUSD)",
        amount: "100",
        value: 1,
        disabled: false,
      },
      {//L4 vip年度付费用户
        id: "vip",
        label: "login.vipYearsPlay",
        charges: "500(USDT、USDC、BUSD)",
        amount: "500",
        value: 2,
        disabled: false,
      },],
      options: [{
        value: 'USDT',
        label: 'USDT'
      }, {
        value: 'USDC',
        label: 'USDC'
      }, {
        value: 'BUSD',
        label: 'BUSD'
      }],
      value: "",
      dialogVisible: false,
      account: "",
      params: {
        type: "",
        amount: "0"
      },
      ConfirmationPaymentLoading: false,
      balances: {
        total: "",
        usdc: "",
        usdt: "",
        userId: "",
        userName: ""

      },
      balanceDialog: false,
      memberBalances: [{

      },],//等级对应金额
      formula: "",
      bthDisabled:""

    }
  },
  mounted() {

    this.fetchUserAccount()//请求当前余额 
    this.fetchNeedFunding()//获取对应等级所需金额
    if (this.$route.params.amount) {
      // this.params.type = this.$route.params.amount.value
      this.params.type = this.accounts.find(item => item.amount == this.$route.params.amount.amount).value 
     
    }
    this.level = JSON.parse(localStorage.getItem("identity")).level
    //判断等级 禁用相应按钮
    if (this.level == "L3") {
      this.bthDisabled = 1
    } else if (this.level == "L4") {
      this.bthDisabled = 2
    } else if (this.level == "L2" || this.level == "L1") {
      this.bthDisabled = -1
    } else if(!this.level) {
      this.bthDisabled = -2
    }else{
      this.bthDisabled = 10
    }




  },
  methods: {

    //获取对应等级所需金额
    async fetchNeedFunding(params) {
      const { data } = await GetNeedFunding(params)
      this.memberBalances = data.data
      this.params.amount = this.memberBalances.find(item => item.type == this.params.type).value
      this.formula = this.memberBalances.find(item => item.type == this.params.type).formula


    },

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

    },
    //请求升级账户
    async fetchLevelUp(params) {

      this.ConfirmationPaymentLoading = true
      const { data } = await LevelUp(params)

   
      if (data.code == 200) {//升级成功提示 更新token 并跳转至个人中心
        this.$message({
          type: 'success',
          message: this.$t(`pay.upgradeSuccessful`)
        });
        this.dialogVisible = false
        this.$addStorageEvent(1, "token", data.data.access_token);
        this.$addStorageEvent(1, "identity",JSON.stringify(data.data.role));
        this.$addStorageEvent(1, "loginTime",data.data.expires_in);
        
        setTimeout(() => {
          this.$router.push(`/personal`)
        }, 10)


      }

      this.ConfirmationPaymentLoading = false
    },
    //提交支付
    handelPay() {
      // if (!this.params.type || !this.params.amount) {//判空
      //   this.$message({
      //     message: this.$t(`help.resubmit`),
      //     type: "error",
      //   });
      //   return
      // }
      console.log( this.params,"就覅哦附加费");
      for (let key in this.params) {
        if (this.params.hasOwnProperty(key)) {
          if (this.params[key] === null || this.params[key] === undefined || this.params[key] === '') {
            this.$message({
              message: this.$t(`pay.grade`),
              type: "error",
            });

            return // 属性值为空
          }
        }
      }



      //判断总金额是否足够
      if (this.balances.total - this.params.amount <= 0) {
        this.balanceDialog = true
        return
      } else {
        this.dialogVisible = true
      }



    },
    //选择账户升级
    handelAccounts(a) {

      // this.upgradeList.amount = this.account

      // this.upgradeList.label = this.accounts.find(item=>{ return item.amount == this.account }).label //确认支付弹窗显示账户等级
      if (this.memberBalances[0]) {
        this.params.amount = this.memberBalances.find(item => item.type == a).value
        this.formula = this.memberBalances.find(item => item.type == a).formula
      }
    


    },
    //处理对应等级label显示
    handelLevel(value) {
      if (value) {
        return this.$t(this.accounts.find(item => item.value == value).label)
      }
      
     

    },
    //点击弹窗的确认支付
    handelConfirmPayment() {

      this.fetchLevelUp(this.params)


    },
    //点击去充值
    handelRecharge() {
      this.$router.push(`/Recharge`)
    }
  }
}