/// <reference path="../../js/jquery-1.7.2.min.js" />

$(document).ready(function () {
    $("#CardContent").blur(function () {
        var a = $("#CardContent").val().split('\n');
        var b = a.length;
        if (a[a.length - 1]) {
            $("#Groupscount").html(b)
        } else {
            $("#Groupscount").html(b - 1)
        }
    });
    $("#CardContent").keyup(function () {
        var a = $("#CardContent").val().split('\n');
        var b = a.length;
        if (a[a.length - 1]) {
            $("#Groupscount").html(b)
        } else {
            $("#Groupscount").html(b - 1)
        }
    });
    $(".cardkind").addClass("onkind");
    $(".exchangebtn").live("click", function () {
        $obj = $(this).parent().parent().next();
        if ($obj.is(':hidden')) {
            $obj.show();
        }
        else {
            $obj.hide();
        }

    });
    $("#btnRec").click(function () {
        queryOrder();
    })
    queryOrder();
    $(".cardkind").click(function () {
        $(".mulcardkind").removeClass("onkind");
        $("#mulcard").hide();
        $("#siglecard").show();
        $(this).addClass("onkind");
        $("#subtype").val("0");
    });
    $(".mulcardkind").click(function () {
        $(".cardkind").removeClass("onkind");
        $("#siglecard").hide();
        $("#mulcard").show();
        $(this).show();
        $(this).addClass("onkind");
        $("#subtype").val("1");
    });
    $("dd a[name=channels]").click(function () {
        $("dd a[name=channels]").removeClass("card_click");
        $(this).addClass("card_click");
        $("#ctypename").html($(this).html());
        var channelcode = $(this).attr("id");
        $("#channelid").val(channelcode);
        if (channelcode != 'autocard') {
            $.post("AjaxCardFaceList.aspx", { channel: channelcode, t: Math.random() }, function (data) {
                $("#facelist").html(data);
            });
        }
        else {
            $("#facelist").html('<li><input id="CardMoney1" name="CardFaceList" value="5" type="radio"><label>5元</label></li><li><input id="CardMoney2" name="CardFaceList" value="10" type="radio"><label>10元</label></li><li><input id="CardMoney3" name="CardFaceList" value="15" type="radio"><label>15元</label></li><li><input id="CardMoney4" name="CardFaceList" value="20" type="radio"><label>20元</label></li><li><input id="CardMoney5" name="CardFaceList" value="25" type="radio"><label>25元</label></li><li><input id="CardMoney6" name="CardFaceList" value="30" type="radio"><label>30元</label></li><li><input id="CardMoney7" name="CardFaceList" value="50" type="radio"><label>50元</label></li><li><input id="CardMoney8" name="CardFaceList" value="100" checked type="radio"><label>100元</label></li><li><input id="CardMoney9" name="CardFaceList" value="200" type="radio"><label>200元</label></li><li><input id="CardMoney10" name="CardFaceList" value="300" type="radio"><label>300元</label></li><li><input id="CardMoney11" name="CardFaceList" value="500" type="radio"><label>500元</label></li><li><input id="CardMoney12" name="CardFaceList" value="1000" type="radio"><label>1000元</label></li>');
        }
    });
    $("#btnClear").click(function () {
        ClearGroupsinfo();
    });
    function ClearGroupsinfo() {
        var cardcontent = $("#CardContent").val();
        var cardLists = cardcontent.split('\n');
        var newContent = "";
        var n = 0;
        for (var i = 0; i < cardLists.length; i++) {
            if (n >= 100) {
                break;
            }
            var cardinfo = cardLists[i];
            cardinfo = cardinfo.replace(/\s+/g, ' ');
            var arrcard = cardinfo.split(' ');
            if (arrcard.length >= 2 && n <= 100) {
                if (arrcard[0].length > 0 && arrcard[1].length > 0) {
                    var card = arrcard[0] + " " + arrcard[1];
                    if (newContent.indexOf(card) < 0) {
                        if (newContent.length == 0) {
                            newContent += card;
                        } else {
                            newContent += "\n" + card;
                        }
                        n++;
                    }
                }
            }
        }
        $("#CardContent").val(newContent)
        $("#CardContent").blur();
    }
    $("#btnsub").click(function () {
        ClearGroupsinfo();
        var b = $("#CardContent").val();
        if (b == '') {
            $("#CardContent").focus();
            $("#Groupsinfo").attr("class", "tip_error");
            $("#Groupsinfo").html("请输入卡信息").show();
            return false
        };
        $(this).attr("disabled", true);
        var cardinfo = $("#CardContent").val();
        var cardlist = cardinfo.split('\n');
        $("#Groupsinfo").html("");
        $("#Groupsinfo").removeClass();
        for (var i = 0; i < cardlist.length; i++) {
            var n = i + 1;
            var cardmsg = "<span id='msg_" + n + "' class='tip_loading salesmsg'>第" + n + "张卡提交中</span>";
            $("#Groupsinfo").append(cardmsg);
            postcard(cardlist[i], n);
        }
        //$("#Groupsinfo").html("").hide();
        //$("#CardContent").val("");
        $("#btnsub").removeAttr("disabled");
        queryOrder();
    });
});
function postcard(cardinfo, nid) {
    var arrinfo = cardinfo.split(' ');
    var cardtype = $("#channelid").val();
    $.ajax({
        type: "post",
        url: "AjaxCardSale.aspx",
        dataType: "json",
        data: "cardinfo=" + cardinfo + "&cardmoney=" + $("input[name='CardFaceList']:checked").val() + "&cardtype=" + cardtype,
        success: function (data) {
            if (data.status == "true") {
                $("#msg_" + nid).removeClass("tip_loading");
                $("#msg_" + nid).addClass("min_tip_success");
                $("#msg_" + nid).html("第 " + nid + " 张卡 [" + arrinfo[0] + "] 售卡请求提交成功,卡种为" + data.msg);
            }
            else {
                $("#msg_" + nid).removeClass("tip_loading");
                $("#msg_" + nid).addClass("min_tip_error");
                $("#msg_" + nid).html("第 " + nid + " 张卡 [" + arrinfo[0] + "] 销卡失败:" + data.msg);
            }
        },
        error: function () {
            $("#msg_" + nid).removeClass("tip_loading");
            $("#msg_" + nid).addClass("min_tip_error");
            $("#msg_" + nid).html("第 " + nid + " 张卡 [" + arrinfo[0] + "] 提交出现错误");
        }
    });

}
function queryOrder() {
    $.get("AjaxCardList.aspx?t="+Math.random(), function (data) {
        $("#orderlist").html(data);
    })

}
