<%@ Control Language="C#" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.HtmlTags" %>
<%@ Import Namespace="Kesion.APPCode" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Kesion.Course" %>
<%@ Import Namespace="Kesion.Components" %>
<script runat="server" type="text/C#">
	dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息，
	private static dynamic __info = new System.Dynamic.ExpandoObject();   //初始化当前页信息
	bool isLoadPage = true;  //定义页面是否加载
	public int sjid = 0;
	public int typeid = 0;
	public int isRndSj = 0;
	public StringBuilder sb = new StringBuilder();
	protected void Page_Load(object sender, EventArgs e)
	{
		CheckUserLogin.Run();

		var tmtype = Utils.StrToInt(KSCMS.S("tmtype"));
		typeid = Utils.StrToInt(KSCMS.S("typeid"));
		sjid = Utils.StrToInt(KSCMS.S("sjid"));
		isRndSj = Utils.StrToInt(KSCMS.S("IsRndSj"));
		var num = Utils.StrToInt(KSCMS.S("num"), 1);
		var score = KSCMS.S("score");
		var xihao = Utils.StrToInt(KSCMS.S("xihao"), 0);
		var isrnd = Utils.StrToInt(KSCMS.S("isrnd"));
		string str = string.Empty;


		if (KSCMS.S("action").Equals("save"))
		{
			SaveChange();
		}
		for (int i = 1; i <= num; i++)
		{
			sb.AppendFormat(@"<table class=""border"" width='100%' border='0' align='center' cellpadding='0' cellspacing='0' >
              <tr> 
                <td colspan='2' align='left' style='text-align:left' height='30'>&nbsp;&nbsp;<font color='#993300'>第{0}小题<font color='#FF0000'> 
                    试卷题号:{1}</font> 
                    <input type='hidden' value='{1}' name='xihao{0}'></font></td>
              </tr>", i, xihao + i - 1);
			sb.AppendFormat(@"
              <tr> 
                <td width='109' class='lefttd' valign='top' nowrap style='line-height:35px'>题目类型：</td>
                <td  class='tag_left' style='line-height:35px'> &nbsp;  &nbsp;  &nbsp; {0}
				  题目难度 <select name='Difficulty{1}'>
				   {3}
				  </select>
                  分值： <input class='textbox nopadtext' name='tmfs{1}' style='width:50px' type='text' id='tmfs{1}' value='{2}' size='3'>
                  分 ", getTmtype(i, tmtype), i, score, getDifficulty(1));
			sb.AppendFormat(@"<label><input name='isrnd{0}'  type='checkbox' id='isrnd{0}' value='1'{1} class='checkbtn'>&nbsp;&nbsp;随机题</label>", i, isrnd == 1 ? " checked" : string.Empty);
			sb.AppendFormat(@"<label><input class='tlcheckbtn' type='checkbox' name='ismedia{0}' onClick=""if (this.checked){{jQuery('#media{0}').show()}}else{{jQuery('#media1').hide()}}"" value='1'>听力题</label> ", i);
			if (MyCache.GetSJCacheConfig(2) == "1")
			{
				sb.AppendFormat("<span>");
			}
			else
			{
				sb.AppendFormat(@"<span style=""display:none"">");
			}
			sb.AppendFormat(@"<div style='display:none'><input type='button' id='knowledgeBtn{0}' class='ty_button' value='选择知识点..' onclick='loadKnowledge({0});'></div>
                <span id='showKnowledge{0}'></span></span>", i);
			sb.AppendFormat(@"</td>
              </tr>	
			  
			  <tbody id='media{0}' style='display:none'>
			   <tr>
			     <td class='lefttd' width='109' valign='top' nowrap> <div align='right'>音频地址：</div></td>
                 <td class='tag_left'> <input type='text' class='textbox' style='float:left' name='mediaurl{0}' id='mediaurl{0}' size='40' /> <input type='button' name='Button' class='ty_button' onClick=""SelectUserFiles('mediaurl{0}',101);"" value='选择...'></td>
			   </tr>
			   <tr>
			     <td width='109' class='lefttd' valign='top' nowrap> <div align='right'>音频原文：</div></td>
                 <td class='tag_left'> <textarea name='mediatxt{0}' id='mediatxt{0}' class='textbox' cols='60' style='width:600px;float:left; padding:10px;height:100px'></textarea>
				<input type='button' class='ty_button' value='用编辑器' onClick=""replaceCk(this,'mediatxt{0}')""></td>
			   </tr>
			  </tbody>", i);

			sb.AppendFormat(@"
			  <tr style='display:none'> 
                <td height='35' class='lefttd' width='120' align='right'><div id='showwordTitle1'></div>题目标题：</td>
				<td class='tag_left'> 
				<span id='showuploadTitle1'></span>
				<textarea name='Title{0}' id='Title{0}' cols='60' class='textbox' style='width:600px;height:60px;padding:10px;overflow:hidden;'></textarea>
				<!--<input type='button' class='button' value='用编辑器' onClick=""replaceCk(this,'Title{0}')"">-->
				</td>
              </tr>
			  <tr> 
                <td height='35' class='lefttd' width='109' align='right'>
				<div id='showwordTmnr{0}'></div>
				题目内容：<br/>
				<font color=red>
				如选择题填写具体的选项,填空题在需要用户作答的地方放{{#answer}}标签,使用编辑器时按shift+Enter分段</font></td>
				<td class='tag_left'> 
				<span id='showuploadTmnr{0}'></span>
				<textarea name='Tmnr{0}' cols='60' id='Tmnr{0}' rows='5' style='width:622px;height:120px;'></textarea>
                {1}
				</td>
              </tr>", i, Kesion.APPCode.EditorAPI.EchoEditor("editor" + i, "Tmnr" + i, "Basic", 0, false, false, false, false, true));


			sb.AppendFormat(@"
              <tr> 
                <td class='lefttd' width='109' align='right' nowrap>
				参考答案：</td>
                <td class='tag_left radio_box' id='showanswer{1}'> {0}</td>
              </tr>", getAnswer(i, tmtype), i);

			sb.AppendFormat(@" <tr> 
                <td height='35' width='109' class='lefttd' align='right'>
				<div id='showwordDAJS1'></div>
				答案解释： </td>
				<td class='tag_left'>
				 <span id='showuploadDAJS1'></span>
				 <textarea name='DAJS{0}' id='DAJS{0}' cols='60' rows='3' class='textbox' style='width:600px;padding:10px;height:100px;float:left;'></textarea>
                   <input type='button' class='ty_button' value='用编辑器' onClick=""replaceCk(this,'DAJS{0}')""></td>
              </tr>
                <tr> 
                <td class='lefttd' height='35' width='109' align='right'>答案解释视频： </td>
				<td class='tag_left'> <input type='text' name='dajsmedia{0}' class='textbox' size='30'/><span class='tips'>输入视频地址即可。</span></td>
              </tr>	
            </table>
			<div class='blank10'></div>", i);
		}
	}
	private string getTmtype(int n, int tmtype)
	{
		string str = string.Format(@"<select name='tmtype{0}' id='tmtype{0}' onChange='settmtype(this.value,{0})'>", n);

		int i = 1;
		foreach (string s in Kesion.Exam.Function.TMTypeArr)
		{
			str += @" <option value='" + i + "'" + (tmtype == i ? " selected" : "") + ">" + s + "</option>";
			i++;
		}

		str += "</select>";
		return str;
	}
	private string getDifficulty(int select)
	{
		string str = string.Empty;
		int i = 0;
		foreach (string s in Kesion.Exam.Function.DifficultyArr)
		{
			str += @"<option value='" + i + "'" + (select == i ? " selected" : "") + ">" + s + "</option>";
			i++;
		}
		return str;
	}
	private string getAnswer(int n, int tmtype)
	{
		string str = string.Empty;
		switch (tmtype)
		{
			case 1:
				str = string.Format(@"<strong>参考答案：</strong><div class='clear blank10'></div><textarea ID='answer{0}' style=""height:100px;width:622px"" name='answer{0}'></textarea>
                    <br/><strong>关键字：</strong><div class='clear blank10'></div><textarea name=""keywords{0}"" class=""textbox"" style=""height:60px;width:600px;padding:10px;"" cols='60' rows='3'></textarea><br/>
Tips:主观题将根据设置的关键词估分,多个关键字请用英文逗号(,)隔开。
                     ", n);
				str += Kesion.APPCode.EditorAPI.EchoEditor("editoranswer" + n, "answer" + n, "Basic", 0, false, false, false, false, true);

				break;
			case 2:
				str = string.Format(@"选项个数：<select onchange='showansweroption(this.value,{0});' name='optionnum{0}'><option value='2'>2个</option>
                            <option value='3'>3个</option>
                            <option value='4' selected>4个</option>
                            <option value='5'>5个</option>
                            <option value='6'>6个</option>
                            <option value='7'>7个</option>
                            <option value='8'>8个</option>
                            <option value='9'>9个</option>
                     </select>
                     <br/>
                       <label><input type='radio' value='A' name='answer{0}'/>(A)</label>
                      <textarea {1} id='appans{0}1' name='appans{0}1'  name='appans{0}1'></textarea><br /></span>
                     <label id='l{0}2'><input type='radio' value='B' name='answer{0}'/>(B)</label>
                    <span id='a1{0}2'><textarea {1} id='appans{0}2'  name='appans{0}2'></textarea><br /></span>
                     <label id='l{0}3'><input type='radio' value='C' name='answer{0}'/>(C)</label>
                          <span id='a1{0}3'><textarea {1} id='appans{0}3'  name='appans{0}3'></textarea><br /></span>
                     <label id='l{0}4'><input type='radio' value='D' name='answer{0}'/>(D)</label>
                   <span id='a1{0}4' ><textarea {1}  id='appans{0}4'  name='appans{0}4'></textarea><br /></span>                    
                       <label id='l{0}5' style='display:none'><input type='radio' value='E' name='answer{0}'/>(E)</label>
                       <span id='a1{0}5'  style='display:none'><textarea {1}  id='appans{0}5'  name='appans{0}5'></textarea><br /></span>  
                     <label id='l{0}6' style='display:none'><input type='radio' value='F' name='answer{0}'/>(F)</label>
                     <span id='a1{0}6'  style='display:none'><textarea {1}  id='appans{0}6' name='appans{0}6'></textarea><br /></span>   
                   <label id='l{0}7' style='display:none'><input type='radio' value='G' name='answer{0}'/>(G)</label>
  <span id='a1{0}7'  style='display:none'><textarea {1}   id='appans{0}7' name='appans{0}7'></textarea><br /></span>                     
<label id='l{0}8' style='display:none'><input type='radio' value='H' name='answer{0}'/>(H)</label>
  <span id='a1{0}8'  style='display:none'><textarea {1}  id='appans{0}8'  name='appans{0}8'></textarea><br /></span>  
                     <label id='l{0}9' style='display:none'><input type='radio' value='I' name='answer{0}'/>(I)</label>
  <span id='a1{0}9'  style='display:none'><textarea {1}  id='appans{0}9' name='appans{0}9'></textarea><br /></span>  
                     ", n, " style='width:480px;height:80px' ");
				break;
			case 3:
				str = string.Format(@"选项个数：<select onchange='showansweroption(this.value,{0});' name='optionnum{0}'><option value='2'>2个</option>
                            <option value='3'>3个</option>
                            <option value='4' selected>4个</option>
                            <option value='5'>5个</option>
                            <option value='6'>6个</option>
                            <option value='7'>7个</option>
                            <option value='8'>8个</option>
                            <option value='9'>9个</option>
                     </select>
                    <br/>
                     <label><input type='checkbox' value='A' name='answer{0}'/>(A)</label>
                      <textarea {1}   id='appans{0}1' name='appans{0}1'></textarea><br /></span>                     
                      <label id='l{0}2'><input type='checkbox' value='B' name='answer{0}'/>(B)</label>
                     <span id='a1{0}2'><textarea {1} id='appans{0}2'  name='appans{0}2'></textarea><br /></span>
                     <label id='l{0}3'><input type='checkbox' value='C' name='answer{0}'/>(C)</label>
                      <span id='a1{0}3'><textarea {1}   id='appans{0}3' name='appans{0}3'></textarea><br /></span>
                     <label id='l{0}4'><input type='checkbox' value='D' name='answer{0}'/>(D)</label>
                       <span id='a1{0}4' ><textarea {1} id='appans{0}4'  name='appans{0}4'></textarea><br /></span>    
                     <label id='l{0}5' style='display:none'><input type='checkbox' value='E' name='answer{0}'/>(E)</label>
                     <span id='a1{0}5'  style='display:none'><textarea {1} id='appans{0}5'  name='appans{0}5'></textarea><br /></span>  
                     <label id='l{0}6' style='display:none'><input type='checkbox' value='F' name='answer{0}'/>(F)</label>
                      <span id='a1{0}6'  style='display:none'><textarea {1}   id='appans{0}6' name='appans{0}6'></textarea><br /></span> 
                     <label id='l{0}7' style='display:none'><input type='checkbox' value='G' name='answer{0}'/>(G)</label>
                     <span id='a1{0}7'  style='display:none'><textarea {1}  id='appans{0}7'  name='appans{0}7'></textarea><br /></span>      
                     <label id='l{0}8' style='display:none'><input type='checkbox' value='H' name='answer{0}'/>(H)</label>
                        <span id='a1{0}8'  style='display:none'><textarea {1}  id='appans{0}8'  name='appans{0}8'></textarea><br /></span>   
                     <label id='l{0}9' style='display:none'><input type='checkbox' value='I' name='answer{0}'/>(I)</label>
                    <span id='a1{0}9'  style='display:none'><textarea {1}  id='appans{0}9'  name='appans{0}9'></textarea><br /></span>  
                     ", n, " style='width:480px;height:80px' ");
				break;
			case 4:
				str = string.Format(@"<label><input type='radio' value='1' name='answer{0}'/>正确</label>&nbsp;<label><input type='radio' value='0' name='answer{0}'/>错误</label>", n);
				break;
			case 5:
				str = string.Format(@"<textarea ID='answer{0}' name='answer{0}' cols='60' rows='3'></textarea><br/>Tips：填空题如果有多个空格，各个空格的参考答案之间用※隔开,一个空有多个答案的，再用|线隔开。
<br/>如1|2※2※3
<br/>表示有三个空格，第一个空格的答案可以是1或是2,第二个空格的答案是2,第三个空格的答案是3 ", n);

				break;
		}
		return str;
	}
	protected void SaveChange()
	{
        Utils.TipsDisabled();  //检查是否演示状态
		var tmIds = string.Empty;
		var xihao = 0;
		var num = Utils.StrToInt(KSCMS.S("TxtNum"));
		for (var i = 1; i <= num; i++)
		{
            string appAnswer = string.Empty;
			var __info = new SJTKInfo();

			__info.TypeId = Utils.StrToInt(KSCMS.S("TxtTypeId"));
			__info.ClassId = Utils.StrToInt(KSCMS.S("TxtClassID"));
			__info.TmType = Utils.StrToInt(KSCMS.S("tmtype" + i));
		
            __info.Tmnr = KSCMS.S("tmnr" + i) + KSCMS.S("title" + i); ;
			__info.DaJS = KSCMS.S("dajs" + i);
			__info.Tmfs = Utils.StrToFloat(KSCMS.S("tmfs" + i), 0);
			__info.Answer = KSCMS.S("answer" + i);
			__info.OptionNum = Utils.StrToInt(KSCMS.S("optionnum" + i));
			__info.OrderID = Utils.StrToInt(KSCMS.S("xihao" + i));
			xihao = __info.OrderID;
			__info.IsRnd = Utils.StrToInt(KSCMS.S("isrnd" + i));
			__info.IsMedia = Utils.StrToInt(KSCMS.S("ismedia" + i));
			__info.MediaUrl = KSCMS.S("mediaurl" + i);
			__info.MediaTxt = KSCMS.S("MediaTxt" + i);
			__info.DajsMedia = KSCMS.S("dajsmedia" + i);
			__info.Difficulty = Utils.StrToInt(KSCMS.S("difficulty" + i));
			__info.KnowledgeId = Utils.StrToInt(KSCMS.S("knowledgeid" + i));
			__info.KeyWords = KSCMS.S("keywords" + i);

			SJManage.InsertSJTK(__info);
            int tmid =Utils.StrToInt(DataFactory.GetOnlyValue("Select Max(tmid) From KS_SJTK"));
			if (KSCMS.S("TxtIsRndSj") == "1")
			{
                tmIds += "," + tmid;
			}
            #region APP考题部份
            string apptmnr = string.Empty;
            for (int j = 1; j <= Utils.StrToInt(KSCMS.S("optionnum" + i), 2); j++)
            {
                int asciiCode = 64;
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                byte[] byteArray = new byte[] { (byte)(asciiCode + j) };
                string strCharacter = asciiEncoding.GetString(byteArray);

                if (!string.IsNullOrEmpty(KSCMS.S("appans" + i + j)))
                {
                    //  apptmnr += strCharacter + "." + KSCMS.S("appans" + i+j) + "<br />";
                    appAnswer += Utils.ChkSQL(KSCMS.S("appans" + i + j)) + "$$$$";
                }
            }
            #endregion


            DataFactory.ExecuteNonQuery("update ks_sjtk  set appanswer='" + appAnswer + "', SchoolID=" + new TemporaryVar().SchoolID + " where tmid=" + tmid);
		}
        
        
        
		var rurl = BasicField.GetUserUrl("sj/TcSJAddST", "sjid-" + KSCMS.S("TxtSJID") + "");
		Msg.AlertDoFun("试题添加成功!", "location.href='" + rurl + "';");
		isLoadPage = false;
		return;
		}

</script>
 <script>
 	function showansweroption(v, n) {
 		for (i = 2; i <= 9; i++) {
 		    jQuery("#l" + n + '' + i).hide();
 		    jQuery("#a1" + n + '' + i).hide();
 		}
 		for (var i = 2; i <= v; i++) {
 		    jQuery("#l" + n + '' + i).show();
 		    jQuery("#a1" + n + '' + i).show();
 		}
 	}

 
 	function replaceCk(obj, id) {
 		var um = UE.getEditor(id, { toolbars: [['fullscreen', 'source', '|', 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', '|', 'insertimage', 'wordimage', 'emotion']], wordCount: false, elementPathEnabled: false, initialFrameHeight: 120 });
 		obj.style.display = 'none';
 	}
 	function settmtype(v, n) {
 		
 		jQuery.get("/plus/exam/TcSjAjax.ashx", { action: "getanswer", num: n, tmtype: v }, function (v) {
 			jQuery('#showanswer' + n).html(unescape(v));
 		});
 	}
 	function loadKnowledge(n) {
 		var str = '<select name="KnowledgeID' + n + '" id="KnowledgeID' + n + '"><option value="0">===选择知识点===</option></select>';
 		jQuery("#showKnowledge" + n).html(str);
 		jQuery("#knowledgeBtn" + n).hide();
 		jQuery.get("/plus/exam/TcSjAjax.ashx", { action: "getknowledge", classid: 401 }, function (v) {
 			jQuery("#KnowledgeID" + n).append(v);
 		});
 	}
	 </script>
<%
    if (!isLoadPage) return;
	Kesion.APPCode.EditorAPI.EchoHead();
%>

	<form name="myform" action="<%=BasicField.GetUserUrl("sj/tcsjaddTm","do-"+KSCMS.S("do")) %>" method="post" id="Form1" >
        <div class="t_tybox">
         	<div class="tytitle clearfix"><h4 class="fl">添加题目</h4></div>
            <input type="hidden" name="action" value="save" />
            <!--<div id="manage_top" class="toptitle menu_top_fixed">添加题目</div>
            <div class="menu_top_fixed_height"></div> 
            <div class="blank10"></div>-->
            <span id="LabTMArea">
                <%=sb.ToString() %>
            </span>
            <input name="TxtClassID" type="text" value="<%=KSCMS.S("ClassId") %>" id="TxtClassID" style="display:none" />
            <input name="TxtSJID" type="text" value="<%=sjid.ToString() %>" id="TxtSJID" style="display:none" />
            <input name="TxtNum" type="text" value="<%=Utils.StrToInt(KSCMS.S("num"), 1).ToString() %>" id="TxtNum" style="display:none" />
            <input name="TxtTypeId" type="text" value="<%=typeid.ToString() %>" id="TxtTypeId" style="display:none" />
            <input name="TxtIsRndSj" type="text" value="<%=isRndSj.ToString() %>" id="TxtIsRndSj" style="display:none" />
            <div class="cz_btn" style="padding: 0px 0px 20px 160px;float: none;">
                <span><input type="submit" name="Button2" value="确定保存" onclick="return (CheckForm());" id="Button2" class="button" /></span>
                <span><input type="button" class="button" onclick="window.history.go(-1);"value=" 返 回 "></span>
            </div>	   
        </div>
    </form>
	<script>
		function CheckForm() {
			var tnum = jQuery("#TxtNum").val();
	
				for (var i = 1; i <= tnum; i++) {
					if (eval("editor" + i + ".getContent()") == '' || eval("editor" + i + ".getContent()") == undefined) {
						alert("第" + i + "题题目内容不能为空");
						return false;
					}
					var tmtype = jQuery("#tmtype" + i).val();
	
					if (tmtype == 2 || tmtype == 3) {
						var opnet = jQuery("#optionnum" + i).val();
						var latter = '';
						for (var j = 1; j <= opnet; j++) {
							if (jQuery("#appans" + i + j).val() == '' || jQuery("#appans" + i + j).val() == undefined) {
								alert("第" + i + "题答案内容不能为空");
								jQuery("#appans" + i + j).focus();
								return false;
							}
	
						}
						if ($("input[name='answer" + i + "']:checked").val() == '' || $("input[name='answer" + i + "']:checked").val() == undefined) {
							alert("第" + i + "题标准答必须选择!");
							return false;
						}
					}
	
				}
	
	
		}
    </script>
