import payData from '../data/pay';

import toast from '@/utils/tool/toast';

import price from '@/utils/tool/price';

export default {

    data(){
      return {
          configData:[],
          payData,
          type:'1',
          active:0
      }
    },

    methods:{

        triggerActive(index){
            if (this.active !== index) {
                this.active = index;
            }
        },

        fetch(obj){
            return this.$request({
                url:'user/getRechargeConfig',
                cache:true
            }).then((data)=>{
                this.configData = data.data.map((item)=>{

                    item.recharge = price.toIntOrFloat(item.recharge);
                    item.give = price.toIntOrFloat(item.give);

                    return item;
                });
               return obj.success();
            }).catch(obj.fail);
        },

        triggerRadio(item){
            if (item.type !== this.type) {
                this.type = item.type;
            }
        },

        triggerRecord(){
          return this.$router.navigateTo('recharge-record');
        },

        // 充值
        pay(){

            if (this.submitLoading) return null;

            if (!this.configData[this.active]) return toast.info('请选择充值金额');
            if (!this.type) return toast.info('请选择支付方式');

            return this.$request({
                url:'user/recharge',
                data:{
                    rechar_id: this.configData[this.active].id
                },
                token:true,
                next:({status})=> this.submitLoading = status,
                loading:'处理中',
                message:2,
                failMessage:true
            }).then((data)=>{
                if (data.isSuccess) {
                    this.wxPay(data.data,(status)=>{

                        if (status) {
                            let item =  this.configData[this.active];
                            let money = parseFloat(item.recharge) + parseFloat(item.give);
                            this.updateUserInfo({
                                money: ((parseFloat(this.user.money )|| 0) + money).toFixed(2)
                            });
                            this.$store.dispatch('updateUserInfoPromise',true);
                        }

                    },true);
                }
            });

        }

    }

}