<template>
  <v-header scroll title="提现" backgroundColor="#fff">
    <view class="withdrawal-container flex">
      <view class="withdrawal-header jCenter">
        <view class="flex-all-1"></view>
        <view class="withdrawal-header-info row aCenter">
          <view class="flex-all-1">提现金额</view>
          <view @click="triggerRecord" class="withdrawal-record">提现记录</view>
        </view>
        <view class="withdrawal-input-container row aCenter">
          <view class="withdrawal-price-em">¥</view>
          <view class="flex-all-1">
            <input v-model="price" @blur="blurFormatPrice" type="digit" class="withdrawal-input" />
          </view>
        </view>
        <view class="row withdrawal-footer aCenter">
          <view>可提现余额￥{{user.profit || '0.00'}}</view>
          <view class="withdrawal-cash" @click="triggerToAll">全部提现</view>
        </view>

      </view>
      <view class="center">
        <view @click="triggerSubmit" class="withdrawal-button center">确定提现</view>
      </view>
    </view>
  </v-header>
</template>

<script>
import vHeader from '@/components/v-header/main.vue';
import user from '@/mixins/user';
import toast from '@/utils/tool/toast';
import globalMixins from '@/mixins/global';
import modal from '@/utils/tool/modal';
export default {
  name: "withdrawal",
  mixins:[globalMixins,user],
  data(){
    return {
      price:''
    }
  },

  methods:{
    blurFormatPrice(){

      let price = this.price.toString();

      if (/\d+.{0,1}\d*/.test(price)) {

        let usePrice = price.split('.') || [];

        let lastLength = usePrice[1] && usePrice[1].length || 0;
        lastLength = lastLength >= 2? 2:0;

        if (lastLength) {
          usePrice[1] = usePrice[1].substr(0,lastLength);
          price = usePrice.join('.');
        }
        // 不能小于0
        price = price <0?0:price;

        let maxPrice = parseFloat(this.user.profit) || 0;

        price = price >= maxPrice ? this.user.profit : price;

        price = parseFloat(price).toString();

        this.price = price;
        this.beforePrice = price;

      } else {
        this.price = this.beforePrice;
      }


    },
    triggerRecord(){
      return this.$router.navigateTo('withdrawal-record');
    },
    triggerToAll(){
      if (this.price !== this.user.profit) {
        this.price = this.user.profit;
      }
    },
    triggerSubmit(){

      if (this.cashStatus) return null;
      let price = parseFloat(this.price);
      if (!price) return toast.info('请输入提现金额');
      else if (price <= 0) return  toast.info('金额必须大于零');
      else if (this.user.withdrawLimit && price < parseFloat(this.user.withdrawLimit)) return  toast.info('最少提现'+this.user.withdrawLimit+'元');
      else if (price > parseFloat(this.user.profit)) return toast.info('超出提现金额');

      modal.confirm({
        title:'提现确认',
        content:'是否执行提现操作',
        confirm:()=>{
          return  this.$request({
            url:'user/profitWithdraw',
            token:true,
            data:{
              money: price
            },
            loading:'处理中',
            message:1,
            failMessage:true,
            next:({status})=> this.cashStatus = status
          }).then((data)=>{
            if (data.isSuccess) {
              this.updateUserInfo({
                profit: (parseFloat(this.user.profit) - price).toFixed(2)
              });
              this.beforePrice = '';
              this.$store.dispatch('updateUserInfoPromise',true);
              this.price = '';
            }
          });
        }
      });

    }
  },

  components:{
    vHeader
  },

  created(){
    this.beforePrice = this.price;
  }
}
</script>

<style scoped lang="scss" src="./style.scss"></style>