jQuery(function($){
    $('.hc_num_box a').click(function(){
        click_change_goodsnum($(this));
    });
    $('.hc_num_box input').blur(function(){
        click_change_goodsnum($(this).siblings('a'));
    });
    $('#miniCartPaybtn').click(function(){//结算按钮
        sub_cart();
    });
    //$('div.tabCon_3 dd a').click(function(){
    //    window.location.reload();
    //});
    compute_price();
});
//计算总价
function compute_price(){
    var all_goods_price = totalnum = 0;
    $("dl.shop_tiem").each(function(){
        var shop_all_weight = shop_goods_num = 0;
        $(this).find('dd.hc_cart_cur').each(function () {
            if ($(this).find('.hc_select_box').hasClass('hc_selected')) {
                $(this).attr('checked',true);
                //商品单价
                var goods_unit_price = parseFloat($(this).find('input.hc_minicart_num').attr('uni'));
                //商品数量
                var goods_num = parseInt($(this).find('input.hc_minicart_num').val());
                shop_goods_num+=parseInt(goods_num);
                totalnum+=parseInt(goods_num);
                //单重
                var goods_weight = parseFloat($(this).find('input.hc_minicart_num').attr('uwe'));
                //总重
                shop_all_weight += parseFloat(goods_num * goods_weight);
                //商品总价
                all_goods_price += parseFloat(goods_unit_price * goods_num);
            }else{
                $(this).attr('checked',false);
            }
        });
        $(this).find('.shop_goods_num').html(parseInt(shop_goods_num));
        $(this).find('.hc_min_sum em').html(foramtmoney(shop_all_weight)+'KG');
    });
    $('#totalPrice').html('¥'+foramtmoney(all_goods_price));
    $('#miniCartPaybtn').html('立即结算('+parseInt(totalnum)+')');
}
//购物数量加减
function click_change_goodsnum(obj){
    var goodsnum = parseInt($(obj).siblings('input').val());
    var goodsstock = parseInt($(obj).siblings('input').attr('sto'));
    var goodsinu = parseInt($(obj).siblings('input').attr('inu'));
    var goodsanu = parseInt($(obj).siblings('input').attr('anu'));
    if(goodsnum>0){
        var class_name=$(obj).attr('class');
        if(class_name=='hc_plus'){
            if(((goodsanu>0) && (goodsnum>=goodsanu)) || (goodsnum>=goodsstock)){
                $(obj).attr('class','hc_plus_disable');
            } else{
                goodsnum = goodsnum+1;
                if($(obj).parent().find('.hc_minus_disable'))$(obj).parent().find('.hc_minus_disable').attr('class','hc_minus');
            }
        }else if(class_name=='hc_minus'){
            if(((goodsinu>0) && (goodsnum<=goodsinu)) || (goodsnum<=1)){
                $(obj).attr('class','hc_minus_disable');
            }else{
                goodsnum = goodsnum-1;
                if($(obj).parent().find('.hc_plus_disable'))$(obj).parent().find('.hc_plus_disable').attr('class','hc_plus');
            }
        }
    }else{
        goodsnum = 1;
    }
    $(obj).siblings('input').val(goodsnum);
    compute_price();
}
//删除商品
function click_del_goods(obj){
    $.showConfirm('确认删除？',function(){
        del_goods(obj);
    },1,'hc');
}
function del_goods(obj){
    //Ajax 删除商品
    var querydata = new Object;
    querydata['id']=$(obj).parent().find("input[name='cart_id[]']").val();
    var del_num=parseInt($(obj).parent().find('.hc_num_box input.hc_minicart_num').attr('orv'));
    $.ajax({
        url: APP + "Cart/del_cart",
        type: "post",
        data: querydata,
        dataType: "json",
        success: function (ret) {
//                alert(JSON.stringify(ret));
            if(ret.status==1){
//                    window.location.reload();
                $(obj).parent().parent().remove();
                var ori_num=parseInt($('div.buy-dorpdown .buy-box-m em').html());
                $('div.buy-dorpdown .buy-box-m em').html(parseInt(ori_num-del_num));
                compute_price();
            }else $.showErr('删除失败',0,'hc');
        }
    });
    //End
}
/**
 * 批量删除
 */
function all_del(){
    $('.hc_cart_cur .hc_select_box').hasClass('hc_selected').each(function(){
        var cart_id=$(this).find('.hc_cart_del');
        del_goods(cart_id);
    });
}
/**
 * 选择全部
 */
function check_select(shop_id,cart_id) {
    shop_id = parseInt(shop_id);
    cart_id = parseInt(cart_id);
    if(shop_id>=0){
        if(cart_id>0){
            if ($("a.hc_select_box.goods_check[cart_id='" + cart_id + "']").hasClass('hc_selected') == true){
                $("a.hc_select_box.goods_check[cart_id='" + cart_id + "']").removeClass('hc_selected');
                if ($("#check_box_shop_id_" + shop_id).hasClass('hc_selected') == true) $("#check_box_shop_id_" + shop_id).removeClass('hc_selected');
                if ($("#miniCartSeltAll").hasClass('hc_selected') == true) $("#miniCartSeltAll").removeClass('hc_selected');
            } else {
                $("a.hc_select_box.goods_check[cart_id='" + cart_id + "']").addClass('hc_selected');
                //判断是否店铺下商品都选中了
                var is_goods_all = true;
                $("a.hc_select_box.goods_check[shop_id='" + shop_id + "']").each(function () {
                    if ($(this).hasClass('hc_selected') == false) is_goods_all = false;
                });
                if (is_goods_all == true){
                    if ($("#check_box_shop_id_" + shop_id).hasClass('hc_selected') == false) $("#check_box_shop_id_" + shop_id).addClass('hc_selected');
                } else{
                    if ($("#check_box_shop_id_" + shop_id).hasClass('hc_selected') == true) $("#check_box_shop_id_" + shop_id).removeClass('hc_selected');
                }
                /**************************************/
                /*这段和!cart_id>0的代码相似，但不可以合一起*/
                if ($("#check_box_shop_id_" + shop_id).hasClass('hc_selected') == true) {
                    //判断是否店铺都选中了
                    var is_all = true;
                    $("a.hc_select_box.shop_check").each(function () {
                        if ($(this).hasClass('hc_selected') == false) is_all = false;
                    });
                    if (is_all == true){
                        if ($("#miniCartSeltAll").hasClass('hc_selected') == false) $("#miniCartSeltAll").addClass('hc_selected');
                    } else{
                        if ($("#miniCartSeltAll").hasClass('hc_selected') == true) $("#miniCartSeltAll").removeClass('hc_selected');
                    }
                }
                /**************************************/
            }
        }else {
            if ($("#check_box_shop_id_" + shop_id).hasClass('hc_selected') == true) {
                $("#check_box_shop_id_" + shop_id).removeClass('hc_selected');
                $("a.hc_select_box.goods_check[shop_id='" + shop_id + "']").each(function () {
                    if($(this).hasClass('hc_selected') == true) $(this).removeClass('hc_selected');
                });
                if ($("#miniCartSeltAll").hasClass('hc_selected') == true) $("#miniCartSeltAll").removeClass('hc_selected');
            } else {
                $("#check_box_shop_id_" + shop_id).addClass('hc_selected');
                $("a.hc_select_box.goods_check[shop_id='" + shop_id + "']").each(function () {
                    if($(this).hasClass('hc_selected') == false) $(this).addClass('hc_selected');
                });
                //判断是否店铺都选中了
                var is_all = true;
                $("a.hc_select_box.shop_check").each(function () {
                    if($(this).hasClass('hc_selected') == false) is_all = false;
                });
                if (is_all == true){
                    if ($("#miniCartSeltAll").hasClass('hc_selected') == false) $("#miniCartSeltAll").addClass('hc_selected');
                } else{
                    if ($("#miniCartSeltAll").hasClass('hc_selected') == true) $("#miniCartSeltAll").removeClass('hc_selected');
                }
            }
        }
    }else{
        if ($("#miniCartSeltAll").hasClass('hc_selected') == true) {
            $("#miniCartSeltAll").removeClass('hc_selected');
            $("a.hc_select_box.shop_check").each(function() {
                if($(this).hasClass('hc_selected') == true) $(this).removeClass('hc_selected');
            });
            $("a.hc_select_box.goods_check").each(function() {
                if($(this).hasClass('hc_selected') == true) $(this).removeClass('hc_selected');
            });
        } else {
            $("#miniCartSeltAll").addClass('hc_selected');
            $("a.hc_select_box.shop_check").each(function() {
                if($(this).hasClass('hc_selected') == false) $(this).addClass('hc_selected');
            });
            $("a.hc_select_box.goods_check").each(function() {
                if($(this).hasClass('hc_selected') == false) $(this).addClass('hc_selected');
            });
        }
    }
    compute_price();
}

//提交购物车
function sub_cart(){
    var querydata = new Object;
    querydata['id'] = new Array();
    querydata['number'] = new Array();
    $('dd.hc_cart_cur').each(function() {
        if ($(this).find('.hc_select_box.goods_check').hasClass('hc_selected') == true){
            querydata['id'].push($(this).find('input[name="cart_id[]"]').val());
            querydata['number'].push($(this).find("input[type='text'].hc_minicart_num").val());
        }
    });
    $.ajax({
        url: APP + "Cart/sub_cart",
        type: "post",
        data: querydata,
        dataType: "json",
        success: function (ret) {
                //alert(JSON.stringify(ret));
            if(ret.status==1){
                window.location.href=ret.url
            }else if(ret.status==2) $.showErr(ret.msg,0,'hc');
            else $.showErr(ret.msg,0,'hc');
        }
    });
}