/**
 * [OEmarry] (C)2010-2099 OEMARRY.COM Inc.
 * Email:service@phpcoo.com,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2016-03-17 by OEdev $
*/

$(function(){
    getCheckedGoodsNums();
    //复选框全选
    $("input[name='J_checkAll']").click(function() {
        $('input[type="checkbox"]').attr("checked",this.checked);
        getCheckedGoodsNums();
    });
    //商家全选
    $("input[name='J_checkShop']").click(function() {
        $shopid = $(this).attr("id");
        $('input[name="J_checkGoods_'+$shopid+'"]').attr("checked",this.checked);
        if($("[f='J_checkGoods']").length == $("[f='J_checkGoods']:checked").length){
            $('input[name="J_checkAll"]').attr("checked",this.checked);
        }else{
            $('input[name="J_checkAll"]').attr("checked",false);
        }
        getCheckedGoodsNums();
    });
    //商品全选
    $("[f='J_checkGoods']").click(function(){
        $name = $(this).attr("name");
        $shopid = $(this).attr("shopid");
        if($("[name='"+$name+"']").length==$("[name='"+$name+"']:checked").length){
            $('#'+$shopid).attr("checked",this.checked);
        }else{
            $('#'+$shopid).attr("checked",false);
        }
        if($("[f='J_checkGoods']").length == $("[f='J_checkGoods']:checked").length){
            $('input[name="J_checkAll"]').attr("checked",this.checked);
        }else{
            $('input[name="J_checkAll"]').attr("checked",false);
        }
        getCheckedGoodsNums();
    });

    //更新数量
	$("[f='updatanums']").click(function(){
        $gid = $(this).attr("gid");
        $type = 1;
        $num = parseInt($("[id='nums_"+$gid+"']").val());
        if($(this).attr("act")=='+'){
            $("[id='nums_"+$gid+"']").val($num+1);
        }else{
            if($num>1){
                $type = 0;
                $("[id='nums_"+$gid+"']").val($num-1);
            }
        }
        $updatanums = parseInt($("[id='nums_"+$gid+"']").val());
        $.ajax({
            type: "POST",
            url: _ROOT_PATH + "index.php?c=cart&a=updatanums",
            cache: false,
            data: {gid:$gid, type:$type, nums:$updatanums },
            dataType: "json",
            success: function($data) {
                $json = eval($data);
                $response = $json.response;
                $result = $json.result;
                $info = $json.res;
                if ($response == "1") { //成功
                    $("[f='total_"+$gid+"']").html($info.total.toFixed(2));
                }
                else {
                    if ($result.length > 0) {
                        ToastShow($result)
                    }
                    else {
                        ToastShow("操作失败，请稍后重试！");
                    }
                }
                getCheckedGoodsNums();
            },
            error: function() {
                ToastShow("检测失败，请检查网络！");
            }
        });
    });

    //删除商品
    $("[f='delgoods']").click(function(){
        $id = $(this).attr("id");
        $shopid = $(this).attr("shopid");
        $.ajax({
            type: "POST",
            url: _ROOT_PATH + "index.php?c=cart&a=del",
            cache: false,
            data: {cartid:$id, shopid:$shopid},
            dataType: "json",
            success: function($data) {
                $json = eval($data);
                $response = $json.response;
                $result = $json.result;
                $flag = $json.flag;
                if ($response == "1") { //成功
                    if($flag > 0){
                        $("[f='shop_"+$shopid+"']").remove();
                    }else{
                        $("[f='goods_"+$id+"']").remove();
                    }
                }
                else {
                    if ($result.length > 0) {
                        ToastShow($result)
                    }
                    else {
                        ToastShow("操作失败，请稍后重试！");
                    }
                }
                getCheckedGoodsNums();
            },
            error: function() {
                ToastShow("检测失败，请检查网络！");
            }
        });
    });

    //移入收藏夹
    $("a[f='collectgoods']").bind("click", function(){ //收藏商品
        $goods_id = $(this).attr("data-id");
        $id = $(this).next("a").attr("id");
        $shopid = $(this).next("a").attr("shopid");
        $dd = $("[f='goods_"+$id+"']").siblings("dd");
        $.ajax({
            type: "POST",
            url: _ROOT_PATH + "index.php",
            cache: false,
            data: {c:"cart", a:"collectgoods", id:$goods_id, cartid:$id, r:get_rndnum(8)},
            dataType: "json",
            success: function($data) {
                $json = eval($data);
                $response = $json.response;
                $result = $json.result;
                if ($response == "1") { //成功
                    ToastShow("收藏成功");
                    if($dd.length>0){
                        $("[f='goods_"+$id+"']").remove();
                    }else{
                        $("[f='shop_"+$shopid+"']").remove();
                    }
                }else {
                    if ($result.length > 0) {
                        ToastShow($result)
                    }
                    else {
                        ToastShow("收藏失败");
                    }
                }
                getCheckedGoodsNums();
            },
            error: function() {
                ToastShow("收藏失败，请检查网络！");
            }
        });
    });

    //批量删除商品
    $("[f='delAllGoods']").click(function(){
        $cartid='';$shopid='';
        $("[f='J_checkGoods']:checked").each(function(){
            $cartid += $(this).attr("cartid")+',';
        });
        $.ajax({
            type: "POST",
            url: _ROOT_PATH + "index.php?c=cart&a=delall",
            cache: false,
            data: {cartlist:$cartid},
            dataType: "json",
            success: function($data) {
                $json = eval($data);
                $response = $json.response;
                $result = $json.result;
                if ($response == "1") { //成功
                    ToastShow($result);
                    $cartid ='';
                    $("[f='J_checkGoods']:checked").each(function(){
                        $cartid = $(this).attr("cartid");
                        $("[f='goods_"+$cartid+"']").remove();
                    });
                    $("[name='J_checkShop']:checked").each(function(){
                        $id = $(this).attr("id");
                        $("[f='shop_"+$id+"']").remove();
                    });

                }
                else {
                    if ($result.length > 0) {
                        ToastShow($result)
                    }
                    else {
                        ToastShow("操作失败，请稍后重试！");
                    }
                }
                getCheckedGoodsNums();
            },
            error: function() {
                ToastShow("检测失败，请检查网络！");
            }
        });
    });


    $("[f='confirm_order']").click(function(){
        submit();
    });


    $(".oe_cart_bottom_right_3").click(function(){
        submit();
    });
    //去结算
    function submit(){
        $info = '';
        if($("[f='J_checkGoods']:checked").length<1){
            return false;
        }
        $("[f='J_checkGoods']:checked").each(function(){
            $cartid = $(this).attr("cartid");
            $nums = $("[f='cart_"+$cartid+"']").val();
            $info += $cartid+"|"+$nums+"#";
        });
        $("[f='goodslist']").val($info);
        document.forms['myform'].submit();
    }

    //商品总数
    function getGoodsNums(){
        $nums = $("dd").length;
        $("[f='all_goods_nums']").html($nums);
    }

    //选中商品总数
    function getCheckedGoodsNums(){
        $nums = $("[f='J_checkGoods']:checked").length;
        if($nums > 0){
            $(".oe_cart_bottom_right_3").addClass("current");
            $("[f='confirm_order']").addClass("current");
        }else{
            $(".oe_cart_bottom_right_3").removeClass("current");
            $("[f='confirm_order']").removeClass("current");
        }
        $("[f='checkedGoodsNums']").html($nums);
        totalPrice();
        getGoodsNums();
        checkGoods();
    }
    //获取选中商品总价
    function totalPrice(){
        $total = 0;
        $("[f='J_checkGoods']:checked").each(function(){
            $goodsid = $(this).attr("id");
            $priceid = $(this).attr("priceid");
            $total += parseFloat($("[f='total_"+$goodsid+"_"+$priceid+"']").html());
        });
        $("[f='totalprice']").html("￥"+$total.toFixed(2));
        $("[f='totalprice_2']").html("￥"+$total.toFixed(2));
    }

    //获取选中商品总价
    function checkGoods(){
        if($("[f='J_checkGoods']").length < 1){
            window.location.reload();
        }
    }

});