<template>
  <view class="page">
    <view class="card">

      <view class="input">
        <view class="prefix">￥</view>
        <view class="inner">
          <u-input border="none" v-model="price" :fontSize="30" type="digit" placeholder="请输入提现金额"></u-input>
        </view>
        <view class="text" @click="getAll">
          全部提现
        </view>
      </view>
      <view class="amount">可提现金额：{{ currency(amount, {
        symbol: '￥',
        fromCents: true
      }).format() }}</view>

      <view class="title">
        提现至微信
      </view>

      <view class="info">
        <text>
          {{ info.isTransfer ? '可以提现,当前您可以进行提现的金额:' + info.total +'元' : '不可提现,' + info.remark }}
          提现规则:
          {{ getRole() }}，您可进行上月返利额提现操作余额提现至微信钱包，提现限额单笔最低{{ info.minTotal / 100}}元/笔限额{{ info.maxTotal / 100 }}元/日，{{ getRole(true) }}恢复提现额度
        </text>
      </view>

    </view>
    <view class="btn">
      <u-button color="#1F1F1F" :loading="loading" @click="submit" size="large" shape="circle">确定</u-button>
    </view>
  </view>
</template>

<script>
import { transfer, getWallet, transferRulerInfo } from '@/api/wallet'
import currency from "@/utils/currency"
export default {
  data() {
    return {
      currency,
      amount: 0,
      walletId: '',
      price: null,
      openid: '',
      loading: false,
      info: {
        commission: 1,
        createTime: 0,
        dayMaxTotal: 0,
        endDay: null,
        endHour: 0,
        endMinute: 0,
        id: "",
        isTransfer: false,
        maxTotal: 0,
        minTotal: 0,
        remark: "可提现余额不足",
        size: 0,
        startDay: null,
        startHour: 0,
        startMinute: 0,
        timeUnit: "DAY",
        total: 0,
        updateTime: 0,
      }
    };
  },
  methods: {
    getRole(isEnd) {
      if (this.info.timeUnit == 'DAY') {
        let text = ''
        if (isEnd) {
          text = `次日${this.info.startHour.toString().padStart(2, 0)}:${this.info.startMinute.toString().padStart(2, 0)}`
        } else {
          text = `每日${this.info.startHour.toString().padStart(2, 0)}:${this.info.startMinute.toString().padStart(2, 0)}-${this.info.endHour.toString().padStart(2, 0)}:${this.info.endMinute.toString().padStart(2, 0)}`
        }
        return text
      } else if (this.info.timeUnit == 'MONTH') {
        let text = ''
        if (isEnd) {
          return `次月${this.info.startDay}日`
        } else {
          return `每月${this.info.startDay}日-${this.info.endDay}日`
        }
        return text

      } else {
        return '每日'
      }
    },
    submit() {
      uni.showLoading({
        title: '加载中'
      });
      this.loading = true
      if (!isNaN(this.price) && this.price > 0) {
        console.log(this.walletId)
        transfer({
          appid: 'wx3be1d6d84d46cdf7',
          openid: this.openid,
          total: this.price * 100,
          walletId: this.walletId
        }).then(
          res => {
            this.loading = false
            if (res.state == 'Success') {
              uni.showToast({
                title: '提现成功',
                icon: 'success',
                duration: 5000
              });
              this.getWallet()
              this.price = null
            }
            uni.hideLoading({ noConflict: true });
          }
        )
      } else {
        uni.showToast({
          title: '请输入正确的金额',
          icon: 'none',
          duration: 2000
        });
        this.price = null
        this.loading = false
        uni.hideLoading({ noConflict: true });
      }
    },
    getAll() {
      if (this.amount > 0) {
        this.price = this.amount / 100;
      }
    },
    getWallet() {
      getWallet().then(({ content }) => {
        if (content) {
          this.amount = content.amount
        }
      })
    }
  },
  onShow() {
    const userInfo = JSON.parse(uni.getStorageSync('userInfo'));
    this.openid = userInfo.openId;
    transferRulerInfo().then(res => {
      if (res.success) {
        this.info = res.content
      }
    })
  },
  onLoad({ amount }) {
    const walletId = uni.getStorageSync('walletId');
    this.walletId = walletId;
    this.amount = amount;
  },
};
</script>

<style lang="scss" scoped>
.page {
  min-height: 100vh;
  background-color: #F9F9F9;
  padding: 20rpx;

  .card {
    border-radius: 16rpx;
    background-color: #ffffff;
    padding: 40rpx;

    .input {
      display: flex;
      align-items: center;
      padding-bottom: 20rpx;

      .prefix {
        font-size: 80rpx;
        font-weight: bold;
        color: #333333;
      }

      .inner {
        flex: 1;
        padding: 0 20rpx;
      }

      .text {
        color: #333333;
        font-weight: bold;
        font-size: 28rpx;
      }
    }

    .amount {
      border-bottom: 1px solid #f2f2f2;
      padding-bottom: 30rpx;
      color: #AAAAAA;
    }

    .title {
      font-size: 32rpx;
      color: #333333;
      margin: 30rpx 0;
      font-weight: bold;
    }

    .info {
      font-size: 28rpx;
      color: #666666;
      line-height: 40rpx;
      margin-top: 20rpx 0 40rpx 0;
    }




  }

  .btn {
    margin: 60rpx 0;
  }


}

/deep/.u-input__content__field-wrapper__field {
  height: 80rpx !important;
}
</style>
