<template>
  <view>
    <Navbar title="余额/充值" :showRightText="true" :isBack="false" :text="'充值记录'" @clickIcon="handleRightText" />
    <div class="content">
      <div class="top-cont">
        <div>总余额(元)</div>
        <div class="acc-bal">{{ balanceInfo.walletBal }}</div>
        <div class="money">
          <div class="money-item">
            <div>个人钱包</div>
            <div class="item">
              <span style="font-size: 11px">￥</span>
              {{ balanceInfo.walletBal }}
            </div>
          </div>
          <div class="money-item">
            <div>补贴钱包</div>
            <div class="item">
              <span style="font-size: 11px">￥</span>
              {{ balanceInfo.subsidyBal }}
            </div>
          </div>
          <div class="money-item">
            <div>红包钱包</div>
            <div class="item">
              <span style="font-size: 11px">￥</span>
              0.00
            </div>
          </div>
          <div class="money-item">
            <div>冻结钱包</div>
            <div class="item">
              <span style="font-size: 11px">￥</span>
              0.00
            </div>
          </div>
        </div>
      </div>

      <div class="up">快捷充值</div>
      <div class="box">
        <div 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" />
        </div>
        <div 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" />
        </div>
        <div 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" />
        </div>
        <div 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" />
        </div>
        <div 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" />
        </div>
        <div 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 }}.00</span>
          </span>
          <u-icon v-if="active == 6" class="icon" name="../../static/images/active.png" size="12" />
        </div>
      </div>

      <!-- 充值 -->
      <u-button shape="circle" color="#FF6816" @click="onRechargeNow" style="height: 32px; margin-top: 100px">
        立即充值
      </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="¥"
              :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>
    </div>
  </view>
</template>

<script>
import { getAccountBalance } from '@/api/dining-hall'
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, accountChargeAPI } from '../../api/index/index'

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

      localShow: false, // 自定义充值弹窗
      amount: '',

      // 钱包信息
      balanceInfo: {}
    }
  },
  onLoad() {
    this.getWalletBalanceData(uni.getStorageSync('accountId') || '')
  },
  methods: {
    handleRightText() {
      console.log('🚀 ~ handleRightText ~ ')
      uni.navigateTo({
        url: '/pages/remainingSum/rechargeRecord'
      })
    },
    // 获取余额
    async getBalance() {
      const params = {
        custId: this.custId,
        custThirdId: this.custThirdId
      }
      const res = await getAccountBalance(params)
      console.log('🚀 ~ getBalance ~ res', res)
    },
    // 充值
    handleRecharge(index) {
      this.active = index
      if (index == 1) {
        this.rechargeAmount = 50
      } else if (index == 2) {
        this.rechargeAmount = 100
      } else if (index == 3) {
        this.rechargeAmount = 200
      } else if (index == 4) {
        this.rechargeAmount = 300
      } else if (index == 5) {
        this.rechargeAmount = 500
      } 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+$/.test(this.amount)) {
        this.amount = ''
        this.rechargeAmount = 0
        uni.$u.toast('金额必须为整数')
        return
      }
      this.$emit('confirm', this.rechargeAmount)
      this.closePopup()
    },

    // 立即充值
    async onRechargeNow() {
      const params = {
        accTradeType: 0,
        accType: 0,
        custId: 0,
        custThirdId: '',
        payChannel: 0,
        payType: 0,
        rechargeAmount: 0,
        remark: '',
        thirdRechargeNum: ''
      }

      const res = await accountChargeAPI(params)
      console.log(res, '充值结果')
    },
    // 获取钱包余额
    async getWalletBalanceData(custId) {
      const { data: res } = await getWalletBalanceAPI({ custId })
      this.balanceInfo = res
      console.log(res, '余额信息--')
    }
  }
}
</script>

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

.content {
  padding: 16px;

  .top-cont {
    padding: 16px;
    height: 190px;
    color: #fff;
    font-size: 14px;
    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: 14px;
        }
      }
    }
  }

  .up {
    margin: 20px 0 10px;
    font-weight: 500;
    font-size: 14px;
    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: 18px;
      color: #0f274b;
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;

      :first-child {
        font-size: 12px;
      }

      .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;
  }
}

::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>
