<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddQuestions.aspx.cs" Inherits="RM.Web.jiabi.AddQuestions" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>添加假币试题</title>
    <script src="/Themes/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="/Training/Learn/m/json2.js" type="text/javascript"></script>
    <link href="../Themes/Styles/Site.css" rel="stylesheet" type="text/css" />
    <style type="text/css">    
div, ul, li, dl, dt, dd, img, h1, h2 { margin: 0px; padding: 0px; list-style-type: none; border: 0; }
#quehtml{ padding:5px; }
.div_main{ padding:5px; }
.ques_Context{ border-bottom: 1px solid #ccc;  padding:2px;  }
.ques_Context ul { padding-left:10px; padding-top:2px; }
.ques_Context ul li{ padding-left:10px;}
    </style>

    <script type="text/javascript">

        var list = [];
        //添加题目
        function addque() {

        }
        //添加伪造币
        function addwzb() {
        /*
            haoma++;
            $.ajax({
                url: "/jiabi/AddQuestions.aspx",
                type: "post",
                data: { "action": "addwzb", a: Math.random() },
                dataType: "json",
                success: function (data) {
                    var qjson = data;
                    var qjson2 = '<%=InfoObj %>';
                    if (qjson != "" && qjson != undefined && qjson != null && qjson != '[]') {
                        if (qjson2 != "" && qjson2 != undefined && qjson2 != null && qjson2 != '[]'){
                            jsontohtml(eval(qjson), eval(qjson2));
                        } else {
                            jsontohtml(eval(qjson), "");
                        }
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("网络异常" + XMLHttpRequest.status.toString());
                }
            });   */
            addCurProp("addwzb", "1");
            
        }
        //添加变造币
        function addbzb() {
        /*
            haoma++;
            $.ajax({
                url: "/jiabi/AddQuestions.aspx",
                type: "post",
                data: { "action": "addbzb", a: Math.random() },
                dataType: "json",
                success: function (data) {
                    var qjson = data;
                    var qjson2 = '<%=InfoObj %>';
                    if(qjson != "" && qjson != undefined && qjson != null && qjson != '[]') {
                        if(qjson2 != "" && qjson2 != undefined && qjson2 != null && qjson2 != '[]') {
                            jsontohtml2(eval(qjson), eval(qjson2));
                        } else {
                            jsontohtml2(eval(qjson), "");
                        }
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("网络异常" + XMLHttpRequest.status.toString());
                    //showTipsMsg("网络异常" + XMLHttpRequest.status.toString(), 3000, 5);
                }
            });*/
            addCurProp("addbzbprop", "3");
            addCurProp("addbzb", "2");
        }

        //添加残币
        function addcb() {

        }

        //假币的特征和方式
        function addCurProp(actionStr, typeid) {            
            $.ajax({
                url: "/jiabi/AddQuestions.aspx",
                type: "post",
                data: { action: actionStr, tid: typeid, a: Math.random() },
                dataType: "json",
                success: function (data) {
                    var qjson = data;
                    var qjson2 = '<%=InfoObj %>';
                    if (qjson != "" && qjson != undefined && qjson != null && qjson != '[]') {
                        if (qjson2 != "" && qjson2 != undefined && qjson2 != null && qjson2 != '[]') {
                            if (typeid == "1") { jsontohtml(eval(qjson), eval(qjson2), typeid); }
                            if (typeid == "2") { jsontohtml2(eval(qjson), eval(qjson2)); }
                            if (typeid == "3") { jsontohtml(eval(qjson), eval(qjson2), typeid); }
                        } else {
                            if (typeid == "1") { jsontohtml(eval(qjson), "", typeid); }
                            if (typeid == "2") { jsontohtml2(eval(qjson), ""); }
                            if (typeid == "3") { jsontohtml(eval(qjson), "", typeid); }
                        }
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("网络异常" + XMLHttpRequest.status.toString());
                    //showTipsMsg("网络异常" + XMLHttpRequest.status.toString(), 3000, 5);
                }
            });
        }


        var queNum = 0;
        var haoma = 0;
        //将JSON转成html
        function jsontohtml(data, data2, typeid) {
            haoma++;
            if (data2.length > 0) {
                if (data.length > 0) {
                    var inputStr = "";
                    if (data2[0]["info_Num"]!=null && data2[0]["info_Num"]!=""){
                        inputStr = " value=\""+data2[0]["info_Num"]+"\" ";
                    }
                    var quehtml = "<div class=\"ques_Context\"><li>号码" + haoma + "<input type=\"text\" " + inputStr + " maxlength=\"10\" id=\"huobi_" + haoma + "\" /><input type=\"hidden\" value=\"" + typeid + "\" /></li>";
                    for (var i = 0; i < data.length; i++) {
                        quehtml += "<ul data=\"" + data[i].feat_id + "\" >" + data[i].feat_Name;
                        if (data[i].option.length > 0) {
                            for (var j = 0; j < data[i].option.length; j++) {
                                var checkstr = "";
                                for (var jj = 0; jj < data2.length; jj++) {
                                    if (data2[jj]["questions_answer"] == data[i].option[j]) {
                                        checkstr = " checked=\"checked\" ";
                                        break;
                                    }
                                }
                                quehtml += "<li><input type=\"checkbox\" onclick=\"checkBox(this);\" data=\"" + data[i].option[j] + "\" " + checkstr + " name=\"option_" + queNum + "\"/>" + data[i].option[j] + "</li>";
                            }
                            queNum++;
                        }
                        quehtml += "</ul>";
                    }
                    quehtml += "</div>";
                    $("#quehtml").append(quehtml);
                }
            } else {
                if (data.length > 0) {
                    var quehtml = "<div class=\"ques_Context\"><li>号码" + haoma + "<input type=\"text\" maxlength=\"10\" id=\"huobi_" + haoma + "\" /><input type=\"hidden\" value=\"" + typeid + "\" /></li>";
                    for (var i = 0; i < data.length; i++) {
                        quehtml += "<ul data=\"" + data[i].feat_id + "\" >" + data[i].feat_Name;
                        if (data[i].option.length > 0) {
                            for (var j = 0; j < data[i].option.length; j++) {
                                quehtml += "<li><input onclick=\"checkBox(this);\" type=\"checkbox\" data=\"" + data[i].option[j] + "\" name=\"option_" + queNum + "\"/>" + data[i].option[j] + "</li>";
                            }
                            queNum++;
                        }
                        quehtml += "</ul>";
                    }
                    quehtml += "</div>";
                    $("#quehtml").append(quehtml);
                }
            }
        }

        function jsontohtml2(data, data2) {            
            if (data2.length > 0) {
                if (data.length > 0) {
                    //var inputStr = "";
                    //if (data2[0]["info_Num"] != null && data2[0]["info_Num"] != "") {
                        //inputStr = " value=\"" + data2[0]["info_Num"] + "\" ";
                    //}
                    //var quehtml = "<div class=\"ques_Context\"><li>号码" + haoma + "<input type=\"text\" " + inputStr + " id=\"huobi_" + haoma + "\" /><input type=\"hidden\" value=\"2\" /></li>";
                    var quehtml = "<div class=\"ques_Context\"><li><input type=\"hidden\" value=\"2\" /></li>";  
                    for (var i = 0; i < data.length; i++) {
                        quehtml += "<ul data=\"" + data[i].feat_id + "\">" + data[i].feat_Name;
                        if (data[i].option.length > 0) {
                            var strque = "";
                            for (var j = 0; j < data[i].option.length; j++) {
                                var checkstr = "";
                                for (var jj = 0; jj < data2.length; jj++) {
                                    if (data2[jj]["questions_answer"].indexOf(data[i].option[j]) >= 0) {
                                        checkstr = " checked=\"checked\" ";
                                        break;
                                    }
                                }

                                quehtml += "<li><input type=\"checkbox\" data=\"" + data[i].option[j] + "\" " + checkstr + " onclick=\"checkBox(this);\" name=\"op_" + queNum + "\"/>" + data[i].option[j] + "</li>";
                            }
                            queNum++;
                        }
                        quehtml += "</ul>";
                    }
                    quehtml += "</div>";
                    $("#quehtml").append(quehtml);
                }
            } else {
                if (data.length > 0) {
                    //var quehtml = "<div class=\"ques_Context\"><li>号码" + haoma + "<input type=\"text\" id=\"huobi_" + haoma + "\" /><input type=\"hidden\" value=\"2\" /></li>";
                    var quehtml = "<div class=\"ques_Context\"><li><input type=\"hidden\" value=\"2\" /></li>";                    
                    for (var i = 0; i < data.length; i++) {
                        quehtml += "<ul data=\"" + data[i].feat_id + "\">" + data[i].feat_Name;
                        if (data[i].option.length > 0) {
                            var strque = "";
                            for (var j = 0; j < data[i].option.length; j++) {
                                quehtml += "<li><input type=\"checkbox\" data=\"" + data[i].option[j] + "\" onclick=\"checkBox(this);\" name=\"op_" + queNum + "\"/>" + data[i].option[j] + "</li>";
                            }
                            queNum++;
                        }
                        quehtml += "</ul>";
                    }
                    //quehtml += "</div>";
                    $("#quehtml").append(quehtml);
                }
            } 
        }

        //保存
        function save() {
            var queues = [];
            var num = 0;
            $(".ques_Context").each(function (e, e2) {
                var obj = {};
                //alert($(".ques_Context")[e].innerHTML);
                //获取所有的货币号码
                $(this).find("input[type='text']").each(function (j) {
                    //alert($(this).val());
                    obj.info_Num = $(this).val();
                    if (obj.info_Num.length == 0) {
                        alert("号码"+(e+1)+"不能为空");
                        num++;
                    }
                });

                $(this).find("input[type='hidden']").each(function (j) {
                    //alert($(this).val());
                    obj.cid = $(this).val();
                });

                obj.option = [];
                obj.qid = [];
                //获取所有的货币号码对应的特征
                $(this).find("ul").each(function (j) {
                    //alert($(this).attr("data"));                    
                    obj.qid.push($(this).attr("data"));
                    var flag = false;

                    $(this).children("li").children("input:checked").each(function () {
                        flag = true;
                        //alert($(this).attr("data"));
                        obj.option.push($(this).attr("data"));
                    });
                    if (flag == false) {
                        obj.option.push(""); //无选项时
                    }
                });
                queues.push(obj);
            });
            var json = JSON.stringify(queues);

            //alert(json);
            if (num == 0) {
                $.ajax({
                    url: "/jiabi/AddQuestions.aspx",
                    type: "post",
                    data: { "action": "save", array: json, infoid: "<%=infoid %>", a: Math.random() },
                    dataType: "json",
                    success: function (data) {
                        var qjson = data;
                        if (qjson > 0) {
                            alert("保存成功！");
                            window.location.href = "/jiabi/jblist.aspx";
                        } else {
                            alert("保存失败！");
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("网络异常" + XMLHttpRequest.status.toString());
                        //showTipsMsg("网络异常" + XMLHttpRequest.status.toString(), 3000, 5);
                    }
                });
            }
        }

        /*
        function toArray(list) {
                ///	<summary>
                /// 将Queue转换为原生数组
                ///	</summary>
                var _newList = list.slice(0);
                this.each(function (item, i) {
                    if (item.child && _newList[i].child) {
                        //_newList[i].child = Array.prototype.slice.call(item.child, 0);
                        //_newList[i].child.push(item.child);
                        _newList[i].child = Array.prototype.slice.call(item.child.toArray(), 0);
                    }
                });
                return _newList;
            }
*/
        window.onload = function () {
            if ("<%=cid %>" == "1" || "<%=cid %>" == "3") {
                //addwzb();
                addCurProp("addwzb", "<%=cid %>");
                if ("<%=cid %>" == "3") { addCurProp("addbzb", "2"); }
            }
            /*
            if ("<%=cid %>" == "2") {
                //addbzb();
                addCurProp("addbzb", "2");
            } */
        }


        function checkBox(obj) {
            //只有当选中的时候才会去掉其他已经勾选的checkbox，所以这里只判断选中的情况 
            if ($(obj).is(":checked")) {
                // 先把所有的checkbox 都设置为不选种
                $(obj).parent().parent().find("input").each(function (j) {
                    $(this).prop('checked', false);
                });
                // 把自己设置为选中
                $(obj).prop('checked', true);
            }
        }

    </script>
</head>
<body style="overflow:scroll;" >
  <div class="div_main">
  <% if (infoid == "0")
     { %>
    <input type="button" value="添加伪造币" onclick="addwzb()"  class="button" />
    <input type="button" value="添加变造币" onclick="addbzb()" class="button"/>    
    <input type="button" value="添加残币" onclick="addcb()" class="button" style="display:none;" />
    <%} %>
    <input type="button" value="保存" onclick="save()" class="button" />
    
    <form id="form1" runat="server">
    
    <input id="txtinfoid" type="hidden" value="0" runat="server" />

       <div id="quehtml" style="width:630px;" ></div>
        </form>
    </div>
</body>
</html>
