/**
 * [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-08-05 by OEdev $
*/
$(function(){
    getCheckedGoodsNums();
    //复选框全选
    $(document).on("click", "input[name='J_checkAll']", function() {
        $('input[type="checkbox"]').attr("checked",this.checked);
        if($(this).prop("checked")){
            $(".label_1").addClass("current");
        }else{
            $(".label_1").removeClass("current");
        }
        getCheckedGoodsNums();
    });
    //商家全选
    $(document).on("click", "input[name='J_checkShop']",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();
    });
    //商品全选
    $(document).on("click", "[f='J_checkGoods']", 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);
            $('input[name="J_checkAll"]').parent("label").addClass("current");
        }else{
            $('input[name="J_checkAll"]').attr("checked",false);
            $('input[name="J_checkAll"]').parent("label").removeClass("current");
        }
        getCheckedGoodsNums();
    });

    //编辑
    $(document).on("click", "[f='data-edit']", function(){
        $class = $('[f="data-goodslist"]').find("li").attr("class");
        if($class == 'current'){
            $('[f="data-goodslist"]').find("li").removeClass("current");
            $(this).html("编辑");
            $('[f="p_1"]').show();
            $('[f="p_2"]').hide();
        }else{
            $('[f="data-goodslist"]').find("li").addClass("current");
            $(this).html("完成");
            $('[f="p_1"]').hide();
            $('[f="p_2"]').show();
        }
    });

    //更新数量
    $(document).on("click", "[f='updatanums']", 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());
        $("[f='nums_"+$gid+"']").html($("[id='nums_"+$gid+"']").val());
        $.ajax({
            type: "POST",
            url: WAP_APPFILE  + "&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("检测失败，请检查网络！");
            }
        });
    });

    //取消操作
    $(document).on("click", "[f='close_var']", function(){
        $_box = $(this).attr("data-id");
        $("#varpop_shade").hide();
        $("#"+$_box).fadeOut(200);
    });

    //弹出取消操作
    $(document).on("click", "[f='data-delGoods']", function(){
        $_id = $(this).attr("data-id");
        $("#orderid").val($_id);
        $("#varpop_shade").show();
        tabPopMarginAuto("cancel_box");
        $("#cancel_box").fadeIn(200); //显示
    });

    //批量删除商品
    $(document).on("click", "[f='delAllGoods']", function(){
        $_box = $(this).attr("data-id");
        $cartid='';$shopid='';
        $("[f='J_checkGoods']:checked").each(function(){
            $cartid += $(this).attr("cartid")+',';
        });
        $.ajax({
            type: "POST",
            url: WAP_APPFILE + "&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("操作失败，请稍后重试！");
                    }
                }
                $("#varpop_shade").hide();
                $("#"+$_box).fadeOut(200);
                getCheckedGoodsNums();
            },
            error: function() {
                ToastShow("检测失败，请检查网络！");
            }
        });
    });

    //去结算
    $(document).on("click", '[f="data-submit"]', function(){
        $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 getCheckedGoodsNums(){
        $("input[type='checkbox']").each(function(){
            if($(this).prop("checked")){
                $(this).parent("label").addClass("current");
            }else{
                $(this).parent("label").removeClass("current");
            }
        });
        $nums = $("[f='J_checkGoods']:checked").length;
        $("[f='checkedGoodsNums']").html($nums);
        if($nums>0){
            $("[f='checkedGoodsNums']").parent("label").attr("f","data-submit");
        }
        totalPrice();
        checkGoods();
    }
    //获取选中商品总价 页面js计算
    function totalPrice(){
        $total = 0;
        $("[f='J_checkGoods']:checked").each(function(){
            $goodsid = $(this).attr("id");
            $priceid = $(this).attr("priceid");
            $total = parseFloat($total)+parseFloat($("[f='total_"+$goodsid+"_"+$priceid+"']").html());
        });
        $("[f='totalprice']").html("￥"+$total.toFixed(2));
    }

    //购物车没有商品时重载
    function checkGoods(){
        if($("[f='J_checkGoods']").length < 1){
            window.location.reload();
        }
    }

    //详情页商品加入购物车
    $('[f="oe_cart_buy_goods"]').click(function(){
        $priceid = $("[f='oe_priceid']").val();
        $goodsnum = $("[f='oe_prd_nm']").val();
        $("#priceid").val($priceid);
        $("#goodsnum").val($goodsnum);
        $("#myform").submit();
    });
});




