(function ($) {
    var car_cookie = "car-cookie-name1606281";
    var car_sumbit_cookie = "car_sumbit_cookie-car-cookie-name1606281";
    var expires = 1;
    jQuery.cart = {
        //不存在时添加,存在时会更新(quantity=0时删除)
        add: function (productguid, productname, price, marketprice, quantity, pic, inventory, groupguid) {
            var len = arguments.length;
            switch (len) {
                case 7:
                    var car = $.cookie(car_cookie);
                    if (car != undefined && car != null) {
                        var oldproduct = JSON.parse(car);
                        var isexit = false;
                        var i = 0;
                        $.each(oldproduct.product, function () {
                            if (this.productguid == productguid) {
                                isexit = true
                                this.productname = productname;//修改
                                this.price = price;//修改
                                this.marketprice = marketprice;//修改
                                this.quantity = parseFloat(this.quantity) + parseFloat(quantity);//修改
                                this.pic = pic;//修改
                                this.inventory = inventory;
                                if (quantity == 0) {//删除
                                    oldproduct.product.splice(i, 1);
                                }
                                return;
                            }
                            i++;
                        });
                        //不存在时添加
                        if (isexit == false) {
                            var product = { "productguid": productguid, "productname": productname, "price": price, "marketprice": marketprice, "quantity": quantity, "price": price, "pic": pic, "inventory": inventory };
                            oldproduct.product.push(product);
                        }
                        $.cookie(car_cookie, JSON.stringify(oldproduct), { expires: expires, path: '/' });
                    }
                    else {
                        var product = { "product": [{ "productguid": productguid, "productname": productname, "price": price, "marketprice": marketprice, "quantity": quantity, "price": price, "pic": pic, "inventory": inventory }] };
                        $.cookie(car_cookie, JSON.stringify(product), { expires: expires, path: '/' });
                    }
                    break;
                case 8:
                    var car = $.cookie(car_cookie);
                    if (car != undefined && car != null) {
                        var oldproduct = JSON.parse(car);
                        var isexit = false;
                        var i = 0;
                        $.each(oldproduct.product, function () {
                            if (this.productguid == productguid && this.groupguid == groupguid) {
                                isexit = true
                                this.productname = productname;//修改
                                this.price = price;//修改
                                this.marketprice = marketprice;//修改
                                this.quantity = parseFloat(this.quantity) + parseFloat(quantity);//修改
                                this.pic = pic;//修改
                                this.inventory = inventory;
                                if (quantity == 0) {//删除
                                    oldproduct.product.splice(i, 1);
                                }
                                return;
                            }
                            i++;
                        });
                        //不存在时添加
                        if (isexit == false) {
                            var product = { "productguid": productguid, "productname": productname, "price": price, "marketprice": marketprice, "quantity": quantity, "price": price, "pic": pic, "inventory": inventory, "groupguid": groupguid };
                            oldproduct.product.push(product);
                        }
                        $.cookie(car_cookie, JSON.stringify(oldproduct), { expires: expires, path: '/' });
                    }
                    else {
                        var product = { "product": [{ "productguid": productguid, "productname": productname, "price": price, "marketprice": marketprice, "quantity": quantity, "price": price, "pic": pic, "inventory": inventory, "groupguid": groupguid }] };
                        $.cookie(car_cookie, JSON.stringify(product), { expires: expires, path: '/' });
                    }
                    break;
            }

        },
        //删除
        del: function (productguid, groupguid) {
            var len = arguments.length;
            switch (len) {
                case 1:
                    var car = $.cookie(car_cookie);groupguid
                    if (car != undefined && car != null) {
                        var oldproduct = JSON.parse(car);
                        var i = 0;
                        $.each(oldproduct.product, function () {
                            if (this.productguid == productguid) {
                                oldproduct.product.splice(i, 1);
                                return;
                            }
                            i++;
                        });
                        $.cookie(car_cookie, JSON.stringify(oldproduct), { expires: expires, path: '/' });
                    }
                    break;
                case 2:
                    var car = $.cookie(car_cookie);
                    if (car != undefined && car != null) {
                        var oldproduct = JSON.parse(car);
                        var i = 0;
                        $.each(oldproduct.product, function () {
                            if (this.productguid == productguid && this.groupguid == groupguid) {
                                oldproduct.product.splice(i, 1);
                                return;
                            }
                            i++;
                        });
                        $.cookie(car_cookie, JSON.stringify(oldproduct), { expires: expires, path: '/' });
                    }
                    break;
            }
        },
        //删除
        delall: function () {
            var car = $.cookie(car_cookie);
            if (car != undefined && car != null) {
                var oldproduct = JSON.parse(car);
                var i = 0;
                $.each(oldproduct.product, function () {
                    oldproduct.product.splice(i, 1);
                    i++;
                });
                $.cookie(car_cookie, JSON.stringify(oldproduct), { expires: expires, path: '/' });
            }
        },
        //存在时会更新(quantity=0时删除)
        updatequantity: function (productguid, quantity, groupguid) {
            var len = arguments.length;
            switch (len) {
                case 2:
                    var car = $.cookie(car_cookie);
                    if (car != undefined && car != null) {
                        var oldproduct = JSON.parse(car);
                        var isexit = false;
                        var i = 0;
                        $.each(oldproduct.product, function () {
                            if (this.productguid == productguid) {
                                isexit = true
                                this.quantity = quantity;//修改
                                if (quantity == 0) {//删除
                                    oldproduct.product.splice(i, 1);
                                }
                                return;
                            }
                            i++;
                        });
                        if (isexit == true) {
                            $.cookie(car_cookie, JSON.stringify(oldproduct), { expires: expires, path: '/' });
                        }
                    }
                    break;
                case 3:
                    var car = $.cookie(car_cookie);
                    if (car != undefined && car != null) {
                        var oldproduct = JSON.parse(car);
                        var isexit = false;
                        var i = 0;
                        $.each(oldproduct.product, function () {
                            if (this.productguid == productguid && this.groupguid == groupguid) {
                                isexit = true
                                this.quantity = quantity;//修改
                                if (quantity == 0) {//删除
                                    oldproduct.product.splice(i, 1);
                                }
                                return;
                            }
                            i++;
                        });
                        if (isexit == true) {
                            $.cookie(car_cookie, JSON.stringify(oldproduct), { expires: expires, path: '/' });
                        }
                    }
                    break;
            }

        },
        //获取所有商品
        getproductlist: function () {
            var car = $.cookie(car_cookie);
            if (car != undefined && car != null) {
                var oldproduct = JSON.parse(car);
                return oldproduct.product;
            }
        },
        //获取总金额
        gettotalprice: function () {
            var car = $.cookie(car_cookie);
            var value = 0;
            if (car != undefined && car != null) {
                var oldproduct = JSON.parse(car);
                for (var i = 0; i < oldproduct.product.length; i++) {
                    var quantity = parseFloat(oldproduct.product[i].quantity);
                    var price = parseFloat(oldproduct.product[i].price);
                    value += quantity * price;
                }
            }
            else {
                value = 0;
            }
            return value;
        },
        //获取商品总数量
        gettotalquantity: function () {
            var car = $.cookie(car_cookie);
            var value = 0;
            if (car != undefined && car != null) {
                var oldproduct = JSON.parse(car);
                for (var i = 0; i < oldproduct.product.length; i++) {
                    value += parseFloat(oldproduct.product[i].quantity);
                }
            }
            else {
                value = 0;
            }
            return value;
        },
        submitproduct: function (productguid, groupguid) {
            var len = arguments.length;
            switch (len) {
                case 1:
                    var car = $.cookie(car_sumbit_cookie);
                    if (car != undefined && car != null) {
                        var oldproduct = JSON.parse(car);
                        var isexit = false;
                        var i = 0;
                        $.each(oldproduct.product, function () {
                            if (this.productguid == productguid) {
                                isexit = true
                                return;
                            }
                            i++;
                        });
                        //不存在时添加
                        if (isexit == false) {
                            var product = { "productguid": productguid };
                            oldproduct.product.push(product);
                        }
                        $.cookie(car_sumbit_cookie, JSON.stringify(oldproduct), { expires: expires, path: '/' });
                    }
                    else {
                        var product = { "product": [{ "productguid": productguid }] };
                        $.cookie(car_sumbit_cookie, JSON.stringify(product), { expires: expires, path: '/' });
                    }
                    break;
                case 2:
                    var car = $.cookie(car_sumbit_cookie);
                    if (car != undefined && car != null) {
                        var oldproduct = JSON.parse(car);
                        var isexit = false;
                        var i = 0;
                        $.each(oldproduct.product, function () {
                            if (this.productguid == productguid && this.groupguid == groupguid) {
                                isexit = true
                                return;
                            }
                            i++;
                        });
                        //不存在时添加
                        if (isexit == false) {
                            var product = { "productguid": productguid, "groupguid": groupguid };
                            oldproduct.product.push(product);
                        }
                        $.cookie(car_sumbit_cookie, JSON.stringify(oldproduct), { expires: expires, path: '/' });
                    }
                    else {
                        var product = { "product": [{ "productguid": productguid, "groupguid": groupguid }] };
                        $.cookie(car_sumbit_cookie, JSON.stringify(product), { expires: expires, path: '/' });
                    }
                    break;
            }

        },
        deletesubmitproduct: function () {
            var car = $.cookie(car_sumbit_cookie);
            if (car != undefined && car != null) {
                var oldproduct = JSON.parse(car);
                for (var i = 0; i < oldproduct.product.length; i++) {
                    if (oldproduct.product[i].groupguid) {
                        $.cart.del(oldproduct.product[i].productguid, oldproduct.product[i].groupguid);
                    } else {
                        $.cart.del(oldproduct.product[i].productguid);
                    }

                }
            }
        },
    };
})(jQuery);
//$.cart.add();