﻿//添加到收藏夹
function AddProductToWishList(ProductSysNo, obj) {
    var data = "";
    if (ProductSysNo != "")
        data += "&productSysNo=" + ProductSysNo;
    var url = "/Services/WishList.aspx";
    var pars = "opt=add" + data;
    var loadingCode = "objLoading('" + obj + "')";
    var completeCode = "objLoadingClose('" + obj + "')";
    AjaxXML(url, pars, loadingCode, completeCode, function(xml) {
        $(xml).find("ROOT").each(function(i) {
            var _result = $(this).children("RESULT").text();
            if (_result == 0) {
                AlertPosObj(obj, true, '收藏成功<a href=/customer/wishlist.aspx>[查看]</a>', 0, 120, 20, 'bottom');
            }
            else if (_result != 0) {
                $(this).find("ERRORLIST").each(function(i) {
                    var _error = $(this).text();
                    if (_result == 1) {//未登录判断
                        //OpenLoginInThis(_error);
                        loginThenEvent = "AddProductToWishList(" + ProductSysNo + ",'" + obj + "');"
                        ShowLogin();
                    }
                    else
                    { AlertPosObj(obj, false, _error, 0, 120, 20, 'bottom'); }

                });
            }
        });
    });
}
//删除
function ConfirmDelWishItem(obj, ProductSysNo) {
    delConfirm(obj, '确认删除该记录？', 110, 36,-110,0);
    var rtnFlag = false;
    $('#Yes_Del').click(function() {
        DelWishListItem(obj, ProductSysNo);
        $('#Yes_Del').unbind('click');
    });
    $('#No_Del').click(function() {
        AlertPosClose('#mesAlertPos');
        $('#No_Del').unbind('click');
    });
}
function DelWishListItem(obj, ProductSysNo) {
    var data = "";
    if (ProductSysNo != "")
        data += "&productSysNo=" + ProductSysNo;
    var url = "/Services/WishList.aspx";
    var pars = "opt=del" + data;
    var loadingCode = "objLoading('#WishList')";
    var completeCode = "objLoadingClose('#WishList')";
    AjaxXML(url, pars, loadingCode, completeCode, function(xml) {
        $(xml).find("ROOT").each(function(i) {
            var _result = $(this).children("RESULT").text();
            if (_result == 0) {
                AlertPosObj(obj, true, '删除成功', 1000, 100, 20, 'bottom');
                GetWishList("#WishList", "#WishContent");
            }
            else if (_result != 0) {
                $(this).find("ERRORLIST").each(function(i) {
                    var _error = $(this).text();
                    AlertPosObj(obj, false, _error, 3000, 100, 20, 'bottom')
                });
            }
        });
    });

}
//清空收藏夹
function ConfirmClearWish(obj, ProductSysNo) {
    delConfirm(obj, '确认要清空[收藏夹]？', 130, 36,-80,0);
    var rtnFlag = false;
    $('#Yes_Del').click(function() {
        ClearWish(obj);
        $('#Yes_Del').unbind('click');
    });
    $('#No_Del').click(function() {
        AlertPosClose('#mesAlertPos');
        $('#No_Del').unbind('click');
    });
}
function ClearWish(obj) {
    var url = "/Services/WishList.aspx";
    var pars = "opt=clear";
    var loadingCode = "objLoading('#WishList')";
    var completeCode = "objLoadingClose('#WishList')";
    AjaxXML(url, pars, loadingCode, completeCode, function(xml) {
        $(xml).find("ROOT").each(function(i) {
            var _result = $(this).children("RESULT").text();
            if (_result == 0) {
                AlertPosObj(obj, true, '清空成功', 1000, 100, 20, 'bottom');
                GetWishList("#WishList", "#WishContent");
            }
            else if (_result != 0) {
                $(this).find("ERRORLIST").each(function(i) {
                    var _error = $(this).text();
                    AlertPosObj(obj, false, _error, 1000, 100, 20, 'bottom');
                });
            }
        });
    });
}
//获取收藏夹列表
function GetWishList(obj, objLoading) {
    var url = "/Services/WishList.aspx";
    var pars = "opt=get";
    var loadingCode = "objLoading('" + objLoading + "')";
    var completeCode = "objLoadingClose('" + objLoading + "')";
    AjaxJson(url, pars, loadingCode, completeCode, function(json) {
        var cnt = json.totalCount;
        $(obj).empty();
        if (parseInt(cnt) > 0) {
            $("#DataCnt").html(cnt);
            $.each(json.data, function(i, item) {
                FormatWishList(obj, this);
            });
        } else
            DataGridNoData(obj, '您还没有收藏任何商品！', 5);
    });
}
//格式化wishItem列表
function FormatWishList(obj, item) {
    var _SysNo = item.SysNo;
    var _CustomerSysNo = item.CustomerSysNo;
    var _ProductSysNo = item.ProductSysNo;
    var _CreateTime = item.CreateTime;
    var _ProductID = item.ProductID;
    var _StyleSysNo = item.StyleSysNo;
    var _StyleID = item.StyleID;
    var _RefProductSysNo = item.RefProductSysNo;
    var _C3Sysno = item.C3SysNo;
    var _ProductName = item.ProductName;
    var _BasicPrice = item.BasicPrice;
    var _CurrentPrice = item.CurrentPrice;
    var _ID = "wishItem_" + _SysNo;
    var _DO = "<button onclick=\"ConfirmDelWishItem(this," + _ProductSysNo + ")\" class=\"btn-gray-z\">删除</button>";
    var ItemModel = "<tr id=\"" + _ID + "\" class=\"wishItem\">"
                    + "<td>" + _ProductID + "</td>"
                    + "<td><a href=\"javascript:GetProductDetailUrl(" + _ProductSysNo + ")\">" + _ProductName + "</a></td>"
                    + "<td class=\"word_pink\">￥" + _CurrentPrice + "</td>"
                    + "<td>" + _CreateTime + "</td>"
                    + "<td class=\"do\">" + _DO + "</td>"
                    + "</tr>";
    $(obj).append(ItemModel);
}