﻿$(document).ready(function() {
    //执行
});
var ProductMain = {
    GetProductMessage: function() {
        var productID = "";
        var PageIndex = 1;
        var PageMax = 10;
        $.ajax({
            type: "POST",
            url: webPath+"Ajax.aspx?action=GetProductMessage",
            data: { "productID": productID, "PageIndex": PageIndex, "PageMax": PageMax },
            dataType: "json",
            success: function(result) {
                if (result.returnID == 1) {
                    //结构改动暂停
                }
                else {
                }
            },
            error: function(xhr, msg, ex) { alert(ex); }
        });
    },
    AddToCart: function() {
        //检查库存
        var _sum = $("#buyprocount").val();
        var _allsum = $("#product_Storage").val();
        var _buysum = $("#buyprocount").val();
        var _productID = $("#product_ID").val();
        //判断是否是真实商品
        var _isTrue = true;
        var _trueProductID =  $("#product_trueId").val();        
        if (_allsum < _buysum) {
            //显示提示框
            $("#product_BuyTips_title").innerHtml = "错误提示";
            $("#product_BuyTips_body").innerHtml = "库存不足请重新选择购买数量";
            $("#product_BuyTips").show("slow"); setTimeout(function() { $("#product_BuyTips").hide("slow"); }, 4000);
            return false;
        }
        KLKM_Cart.addToCart(_productID, _sum, _isTrue, _trueProductID);
    }
};
var KLKM_Cart = {
    addToCart: function(productID, sum, isTrueProduct, tureproductID) {

        $.ajax({
            type: "POST",
            url: webPath+"Ajax.aspx?action=AddCart",
            data: { "productID": productID, "sum": sum, "isTrueProduct": isTrueProduct, "tureproductID": tureproductID },
            dataType: "json",
            success: function(result) {
                switch (result.Results) {
                    case 0:
                        $("#product_BuyTips_title").html("购物提示");
                        $("#product_BuyTips_body").html("请登录后再进行购物&nbsp;&nbsp;<a href='"+webPath+"sign.aspx'>>>马上去登录..</a>");
                        break;
                    case 1:
                        $("#product_BuyTips_title").html("购物提示");
                        $("#product_BuyTips_body").html("购买数量不能为空");
                        break;
                    case 2:
                        $("#product_BuyTips_title").html("购物提示");
                        $("#product_BuyTips_body").html("系统错误,请联系客服");
                        break;
                    case 3:
                        $("#product_BuyTips_title").html("购物提示");
                        $("#product_BuyTips_body").html("目前共选择1种商品 共" + result.totalnum + "件 合计￥" + result.totalprice + "");
                        var _money = result.AllMoney;
                        break;
                }
                $("#product_BuyTips").show("slow");
                setTimeout(function() { $("#product_BuyTips").hide("slow"); }, 4000);

            },
            error: function(xhr, msg, ex) { alert(ex); }
        });
    },
    delToCart: function() {
    },
    delAllCart: function() {
    },
    getCartInfo: function() {
    }

};
/*购物车页面操作**************************************************/
var Page_cart = {
    //增加减少
    addSingleProduct: function(id, type, productID, tureproductID) {
        var _coul = $("#cart_list_" + id + "").val();
        _coul = parseInt(_coul);
        if (type == 1) {
            if (_coul < 2) { alert("不能购买数量不能少于一件"); return false; }
        }
        var _olecoul = $("#cart_list_hid_" + id).val();
        _olecoul = parseInt(_olecoul);
        //        if (!PageCheckInput.IsNumber(_coul)) { alert("请输入数字"); $("#cart_list_" + id + "").focus(); return false; };
        //        if (_coul < 1) { alert("请输入输入大于0的数"); $("#cart_list_" + id + "").focus(); return false; }

        var isTrueProduct = 0;
        var sum = 1;
        //更新数量

        $.ajax({
            type: "POST",
            url: webPath+"Ajax.aspx?action=DelOneCart",
            data: { "productID": productID, "sum": sum, "tureproductID": tureproductID, "type": type },
            dataType: "json",
            success: function(result) {
                switch (result.Results) {
                    case 0:
                        alert("操作失败请联系客服人员");
                        return false;
                        break;
                    case 1:

                        break;
                    case 2:
                        alert("库存不足，请稍后购买");
                        return false;
                        break;
                }


                //返回
                var _productScore = 0; //新的总积分
                var _productUseScore = 0; //新的赠送总积分
                var _allProductPrice = 0; //总价格
                var _allyunfei = 0; //总运费
                var _allNewSore = 0;

                //更新总数量
                if (type == 0) {
                    //加
        
                    $("#cart_list_hid_" + id + "").text((PageConvert.ToInt(_coul, 0) + 1).toString()); //更新单个数量

                    $("#Scre_sum_" + id + "").text((PageConvert.ToInt(_coul, 0) + 1).toString()); //更新积分数量
                    $("#cart_listallprice_" + id + "").text((PageConvert.ToInt(_coul, 0) + 1) * PageConvert.ToInt($("#cart_listOneprice_" + id + "").val(), 0)); //更新小计
                    $("#cart_list_" + id + "").val((PageConvert.ToInt(_coul, 0) + 1).toString()); //更新单个数量
                    //更新总的数量
                    var _oldAllCount = PageConvert.ToInt($("#Page_allCount").text(), 0);
                    //$("#Page_allCount").text((_oldAllCount + 1).toString());
                    //更新赠送积分

                    var _singleSore = PageConvert.ToFloat($("#hid_Score_" + id + "").val(), 0); //单个积分多少
                    var _oldAllSores = PageConvert.ToFloat($("#Page_allSore").text(), 0);
                    $("#Page_allSore").text((_oldAllSores + _singleSore * 1).toString());


                    var _oldproductOldprice = 0;
                    var _oldAllprices = PageConvert.ToFloat($("#page_allPrice").text(), 0);
                    $("#page_allPrice").text((_oldAllprices + PageConvert.ToFloat($("#cart_listOneprice_" + id + "").val(), 0) * 1).toString()); //更新总费用
                    //更新总运费
                    //                    $("#page_allPrice").text(_allyunfei);

                }
                else {
                    $("#cart_list_hid_" + id + "").text((PageConvert.ToInt(_coul, 0) - 1).toString()); //更新单个数量                   
                    $("#Scre_sum_" + id + "").text((PageConvert.ToInt(_coul, 0) - 1).toString()); //更新积分数量

                    $("#cart_listallprice_" + id + "").text((PageConvert.ToInt(_coul, 0) - 1) * PageConvert.ToInt($("#cart_listOneprice_" + id + "").val(), 0)); //更新小计
                    $("#cart_list_" + id + "").val((PageConvert.ToInt(_coul, 0) - 1).toString()); //更新单个数量
                    //更新总的数量
                    var _oldAllCount = PageConvert.ToInt($("#Page_allCount").text(), 0);
                    //$("#Page_allCount").text((_oldAllCount - 1).toString());
                    //更新赠送积分

                    var _singleSore = PageConvert.ToFloat($("#hid_Score_" + id + "").val(), 0); //单个积分多少
                    var _oldAllSores = PageConvert.ToFloat($("#Page_allSore").text(), 0);
                    $("#Page_allSore").text((_oldAllSores - _singleSore * 1).toString());


                    var _oldproductOldprice = 0;
                    var _oldAllprices = PageConvert.ToFloat($("#page_allPrice").text(), 0);
                    $("#page_allPrice").text((_oldAllprices - PageConvert.ToFloat($("#cart_listOneprice_" + id + "").val(), 0) * 1).toString()); //更新总费用
                }



            },
            error: function(xhr, msg, ex) { alert(ex); }
        });
    },
    deloneSingleProduct: function(id, type, productID, tureproductID) {
        //删除一行
        $.ajax({
            type: "POST",
            url: webPath+"Ajax.aspx?action=DelOneCart",
            data: { "productID": productID, "sum": 0, "isTrueProduct": 0, "tureproductID": tureproductID, "type": 2 },
            dataType: "json",
            success: function(result) {
                switch (result.Results) {
                    case 0:
                        alert("删除失败");
                        break;
                    case 1:
                        location.href = webPath+"cart.aspx";
                        break;
                }
                //
            },
            error: function(xhr, msg, ex) { alert(ex); }
        });

    },
    delAllproduct: function() {
        //清空购物车
        $.ajax({
            type: "POST",
            url: webPath+"Ajax.aspx?action=DelAllCart",
            data: {},
            dataType: "json",
            success: function(result) {
                location.href = webPath+"cart.aspx";
            },
            error: function(xhr, msg, ex) { alert(ex); }
        });
    }
};



