<template>
	<page-meta :page-font-size="fontValue+'px'" :root-font-size="fontValue+'px'"></page-meta>
  <view>
    <Navbar title="余额/充值" :showRightText="true" :isBack="false" :text="'充值记录'" @clickIcon="handleRightText" />
    <view class="content">
      <view class="top-cont">
        <view>总余额(元)</view>
        <view class="acc-bal">{{ (balanceInfo.accountAllBal/100).toFixed(2) }}</view>
        <view class="money">
          <view class="money-item">
            <view>个人钱包</view>
            <view class="item">
              <span style="font-size: 11px">￥</span>
              {{ (balanceInfo.walletBal/100).toFixed(2) }}
            </view>
          </view>
		  <view style="height: 40px;border: 1rpx solid #fff;"></view>
          <view class="money-item" @click="goSubPage">
            <view>补贴钱包</view>
            <view class="item">
              <span style="font-size: 11px">￥</span>
              {{ (balanceInfo.subsidyBal/100).toFixed(2) }}
            </view>
          </view>
          <!-- <view class="money-item">
            <view>红包钱包</view>
            <view class="item">
              <span style="font-size: 11px">￥</span>
              {{ (balanceInfo.redEnvelope/100).toFixed(2) }}
            </view>
          </view>
          <view class="money-item">
            <view>冻结钱包</view>
            <view class="item">
              <span style="font-size: 11px">￥</span>
              {{ (balanceInfo.walletFreezeBal/100).toFixed(2) }}
            </view>
          </view> -->
        </view>
      </view>

     <!-- <view class="up">快捷充值</view>
      <view class="box">
        <view class="box-item" :class="{ active: active == 1 }" @click="handleRecharge(1)">
          <span>￥</span>
          <span>50.00</span>
          <u-icon v-if="active == 1" class="icon" name="../../static/images/active.png" size="12" />
        </view>
        <view class="box-item" :class="{ active: active == 2 }" @click="handleRecharge(2)">
          <span>￥</span>
          <span>100.00</span>
          <u-icon v-if="active == 2" class="icon" name="../../static/images/active.png" size="12" />
        </view>
        <view class="box-item" :class="{ active: active == 3 }" @click="handleRecharge(3)">
          <span>￥</span>
          <span>200.00</span>
          <u-icon v-if="active == 3" class="icon" name="../../static/images/active.png" size="12" />
        </view>
        <view class="box-item" :class="{ active: active == 4 }" @click="handleRecharge(4)">
          <span>￥</span>
          <span>300.00</span>
          <u-icon v-if="active == 4" class="icon" name="../../static/images/active.png" size="12" />
        </view>
        <view class="box-item" :class="{ active: active == 5 }" @click="handleRecharge(5)">
          <span>￥</span>
          <span>500.00</span>
          <u-icon v-if="active == 5" class="icon" name="../../static/images/active.png" size="12" />
        </view>
        <view class="box-item" :class="{ active: active == 6 }" @click="handleRecharge(6)">
          <span v-if="amount === ''" style="font-size: 18px">自定义</span>
          <span v-else>
            ￥
            <span style="font-size: 18px">{{ amount }}</span>
          </span>
          <u-icon v-if="active == 6" class="icon" name="../../static/images/active.png" size="12" />
        </view>
      </view> -->

      <!-- 充值 -->
      <!-- <u-button shape="circle" color="#FF6816" @click="onRechargeNow" style="height: 48px; margin-top: 100px;font-size: 32rpx;">
        立即充值
      </u-button> -->

      <u-popup :show="localShow" @close="closePopup" mode="center" round="12" :closeOnClickOverlay="false">
        <view class="amount-popup">
          <view class="popup-title">自定义金额</view>

          <view class="input-box">
            <u--input
              v-model="amount"
              placeholder="输入金额"
              prefixIcon="¥"
			  @input="(v)=>(amount=v.replace(/[^\d.]/g,''))"
              :prefixIconStyle="{
                color: '#333',
                fontSize: '32rpx',
                fontWeight: 'bold'
              }"
              border="surround"
              clearable
            ></u--input>
          </view>

          <view class="button-group">
            <u-button
              text="取消"
              :customStyle="{
                width: '240rpx',
                height: '80rpx',
                border: 'none',
                background: '#f5f5f5',
                color: '#666666'
              }"
              @click="closePopup"
            ></u-button>
            <u-button
              type="warning"
              text="确认"
              :customStyle="{
                width: '240rpx',
                height: '80rpx',
                background: '#ff6633',
                border: 'none'
              }"
              @click="confirm"
            ></u-button>
          </view>
        </view>
      </u-popup>
	  
	  
	  <u-popup :show="chosenShow" @close="closeChosenShowPopup" mode="bottom" bgColor="transparent">
	    <view style="width: 100%;height: auto;background: transparent;">
			<view class="chosen-btn" @click="confirmPay">支付宝支付</view>
			<view class="chosen-btn" @click="closeChosenShowPopup">取消</view> 
	    </view>
	  </u-popup>
    </view>
  </view>
</template>

<script> 
import uIcon from '../../uni_modules/uview-ui/components/u-icon/u-icon.vue'
import UUpload from '../../uni_modules/uview-ui/components/u-upload/u-upload.vue'
import { getWalletBalanceAPI,personalRechargeForAppApi,listAvailPayTypeApi} from '../../api/index/index'

export default {
  components: { UUpload },
  data() {
    return {
		fontValue:uni.getStorageSync('fontSize') || 8,
		timer:null,
      // 余额
      balance: {
        accBal: '1.23', // 账户总余额
        walletBal: '1.23', // 个人钱包
        subsidyBal: '1.23' // 补贴钱包
      },
      userId: uni.getStorageSync('userId'), // 用户id
      custThirdId: '', // 用户第三方id
      active: 1,
      accTradeType: 8, // 交易类型 8微信扫码充值 9支付宝扫码充值 10现金充值
      accType: 1, // 钱包类型 1个人钱包 2补贴钱包
      rechargeAmount: 5000, // 充值金额
      payChannel: '', // 支付渠道
      payType: '', // 支付方式
      thirdRechargeNum: '', // 第三方充值流水号

      localShow: false, // 自定义充值弹窗
      amount: '',
	  
	  chosenShow: false,
		
      // 钱包信息
      balanceInfo: {}
    }
  },
  onShow() {
    this.getWalletBalanceData() 
  },
  onLoad() {
	  this.timer = setInterval(() => {
	     this.getWalletBalanceData() 
	  }, 10000)
  }, 
  onUnload() {
	clearInterval(this.timer)
	this.timer = null
  },
  methods: {
	// async getPayTypeList(){
	// 	let param = {
	// 	}
	// 	const res = await listAvailPayTypeApi(param)
	// 	console.log('?? ~ getBalance ~ res', res)
	// },
	//跳转历史记录
    handleRightText() {
      console.log('?? ~ handleRightText ~ ')
      uni.navigateTo({
        url: '/pages/remainingSum/rechargeRecord'
      })
    },
	// 获取钱包余额
	async getWalletBalanceData() {
		const { data: res } = await getWalletBalanceAPI({ "userId":uni.getStorageSync('userId') })
		this.balanceInfo = res
	  console.log(res, '余额信息--')
	},
    // 充值
    handleRecharge(index) {
      this.active = index
      if (index == 1) {
        this.rechargeAmount = 5000
      } else if (index == 2) {
        this.rechargeAmount = 10000
      } else if (index == 3) {
        this.rechargeAmount = 20000
      } else if (index == 4) {
        this.rechargeAmount = 30000
      } else if (index == 5) {
        this.rechargeAmount = 50000
      } else if (index == 6) {
        this.rechargeAmount = 0
        this.openPopup()
      }
    },
    openPopup() {
      this.localShow = true
    },
    closePopup() {
      this.localShow = false
    },
    confirm() {
      if (!this.amount) {
        uni.$u.toast('请输入金额')
        return
      } else if (/[^\d.]/g.test(this.amount)) {
        this.amount = ''
        this.rechargeAmount = 0
        uni.$u.toast('金额必须为数字')
        return
      }
	  this.rechargeAmount = Number(this.amount)*100
      // this.$emit('confirm', this.rechargeAmount)
      this.closePopup()
    },
	//支付宝支付
	closeChosenShowPopup() {
	  this.chosenShow = false
	},
	//确认支付宝支付
	async confirmPay(){
		let param = {
			"userId": uni.getStorageSync('userId'),
			"payType":"18",
			"rechargeAmount":this.rechargeAmount,
			"sourceType":"7",
			"payPassword":"",
		}
		const res = await personalRechargeForAppApi(param)
		console.log(res)
		if(res.code==200){
			uni.setStorageSync('Form', res.data.payParamStr) //form表单
			uni.navigateTo({
			  url: `/pages/remainingSum/payLoding?accTradeId=${res.data.accTradeId}`
			})
		}
	},
    // 立即充值按钮
	onRechargeNow() {
		console.log(this.rechargeAmount)
		this.chosenShow = true
    },
    goSubPage(){
		uni.navigateTo({
		  url: `/pages/remainingSum/subsidyList?subsidyBal=${this.balanceInfo.subsidyBal}`
		})
		
	}
  }
}
</script>

<style lang="scss" scoped>
page {
  background: #fff;
}

.content {
  padding: 16px;

  .top-cont {
    padding: 16px;
    height: 190px;
    color: #fff;
    font-size: 28rpx;
    background: linear-gradient(134deg, #ff6816 0%, rgba(255, 104, 22, 0.2) 99%), rgba(255, 104, 22, 0.6);
    border-radius: 12px 12px 12px 12px;
    position: relative;
    box-sizing: border-box;

    .acc-bal {
      margin-top: 15px;
      font-weight: 500;
      font-size: 48px;
    }

    .money {
      margin-top: 16px;
      width: 100%;
      display: flex;
      // position: absolute;
      justify-content: space-around;
      // bottom: 16px;

      .money-item {
        // margin-right: 60px;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;

        .item {
          margin-top: 10px;
          font-size: 28rpx;
        }
      }
    }
  }

  .up {
    margin: 20px 0 10px;
    font-weight: 500;
    font-size: 28rpx;
    color: #0f274b;
    line-height: 16px;
  }

  .box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    .box-item {
      margin-bottom: 12px;
      width: 30%;
      height: 66px;
      border-radius: 4px 4px 4px 4px;
      border: 1px solid rgba(15, 39, 75, 0.4);
      font-weight: 500;
      font-size: 36rpx;
      color: #0f274b;
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;

      :first-child {
        font-size: 24rpx;
      }

      .icon {
        position: absolute;
        right: 0;
        bottom: 0;
      }
    }

    .active {
      background: rgba(255, 104, 22, 0.1);
      border: 1px solid #ff6816;
    }
  }
}

.amount-popup {
  width: 600rpx;
  background-color: #ffffff;
  border-radius: 24rpx;
  padding: 40rpx 30rpx;

  .popup-title {
    text-align: center;
    font-size: 32rpx;
    color: #333333;
    font-weight: 500;
    margin-bottom: 30rpx;
  }

  .input-box {
    margin-bottom: 40rpx;
  }

  .button-group {
    display: flex;
    justify-content: space-between;
  }
}
	.chosen-btn{
		width: 94%;height: 40px;line-height: 40px;margin: 10rpx auto;border-radius: 8px;background-color: #FFF;text-align: center;font-size: 36rpx;
	}
::v-deep .u-input {
  &__content {
    height: 80rpx;
    background-color: #ffffff;

    &__prefix-icon {
      margin-right: 10rpx;
      padding-left: 10rpx;
    }

    &__field-wrapper {
      &__field {
        font-size: 28rpx;
      }
    }
  }
}
</style>
