import toast from '@/utils/tool/toast';
import notice from "@/utils/notice/index";
import car from '@/utils/controls/car';
export default {

    methods:{

        /*
        *   2 为加入购物车
        *   1 为立即购买
        * */
        triggerBuy(type=1){



            if(type === 1) {
                return this.routerBuyOrder();
            } else {
                this.cancelRuleModal();
                return this.addCarNumber();
            }

        },

        routerBuyOrder(){
            let rules = {};
            if (this.item.is_datebuy) {
                rules = this.rules;

                for (let i=0,count=this.rulesOption.length;i<count;i++) {
                    let item = this.rulesOption[i];
                    if (rules[item.key] === undefined) {
                        return toast.info('请选择规格');
                    }
                }
            }

            this.cancelRuleModal();
            return this.$router.navigateTo({
               name:'confirm-order',
               params:{
                   ...rules,
                   id:this.$params.id,
                   is_cart:0,
                   number: this.buyNumber
               }
            });

        },

        addCarNumber(){

            if (this.addCarStatus) return ;

            return this.$request({
                url:'product/addCart',
                data:{
                    product_id: this.$params.id,
                    number: this.buyNumber || 1
                },
                next:({status})=> this.addCarStatus = status,
                token:true,
                message:true,
                failMessage:true
            }).then((data)=>{
                if (data.isSuccess) {
                    notice.trigger('car',{
                        update:true
                    });

                    car.setCartNumber(data.data);

                    this.buyNumber = 1;
                }
            });
        }
    }



}