import verification, {VerificationType} from '$utils/verification';

import popup from "$utils/tool/popup";

export default <LibMixins>{

    data(){
      return{
          moneyValue:'',
          disabled:false
      }
    },

    computed:{
        integral(){
            return (this.moneyValue || 0) * (this.$store.state.pay.payRatio || 0);
        }
    },

    methods:{

        allExChange(){

            if (this.disabled) return;

            let intMoney = parseInt(this.$store.getters.money);

            if (this.moneyValue !== intMoney) {
                this.moneyValue = intMoney;
            }
        },

        submitVerify(callback){

            if (!verification.trigger[VerificationType.empty](this.moneyValue)) {
                return popup.$toast('请输入兑换金额');
            } else if (this.moneyValue > this.$store.getters.money) {
                return popup.$toast('兑换金额不能大于余额');
            }

            return callback();

        },

        submit(obj){

            let value = this.moneyValue;
            let integral = this.integral;
            return this.$request({
                url:'user/exchange',
                data:{
                  money: value
                },
                next:({status})=>{
                    this.disabled = status;
                },
                token:true,
                message:true,
                failMessage:true
            }).then((data)=>{

                if (data.isSuccess) {

                    this.user = {
                        money: this.$store.getters.money - value,
                        integral: this.$store.getters.integral + integral
                    }

                    // 执行更新 补救措施
                    this.$store.dispatch('updateUserMoneyPromise');

                    return  this.close();
                }

                return obj.none();

            }).catch(obj.none);

        }

    }

}