<%@ 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" %>
<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 scoreId = 0;
	public int hasXihao = 0;
	public int isRndSj = 0;
	public static string Answer = string.Empty;
	public static string ZGTDFQK = string.Empty;
	public static DataRow drsj = null;
	public static DataRow drscore = null;
	protected void Page_Load(object sender, EventArgs e)
	{
		sjId = Utils.StrToInt(KSCMS.S("sjid"));
		scoreId = Utils.StrToInt(KSCMS.S("scoreid"));
		if (scoreId == 0)
		{
			Msg.AlertDoFun("出错啦3！", "history.back()");
			return;
		}
		isRndSj = Utils.StrToInt(KSCMS.S("isrndsj"));
		if (KSCMS.S("do").Equals("save"))
		{
			BtnSubmit_Click();
		}
		
		if (!IsPostBack)
		{
			initialList();
		}
	}
	public string GetParam(string Fields)
	{
		string result = string.Empty;
		if (drscore != null)
		{
			result = drscore[Fields].ToString();
		}
		return result;
	}

	private void initialList()
	{
		using (DataTable dtt = DataFactory.GetDataTable("SELECT Top 1 * From KS_SJ Where id=" + sjId))
		{
			if (dtt.Rows.Count > 0)
			{
				drsj = dtt.Rows[0];
			}
			else
			{
				Msg.AlertDoFun("出错啦1！", "history.back()");
			}
		}
		string sqlStr = string.Empty;

		sqlStr += " KS_SjScore a Where scoreid=" + scoreId;

		using (DataTable dt = DataFactory.GetDataTable("select top 1 a.* From" + sqlStr))
		{
			if (dt.Rows.Count > 0)
			{
				drscore = dt.Rows[0];
				Answer = drscore["answer"].ToString();
				ZGTDFQK = drscore["ZGTDFQK"].ToString();
			}
			else
			{
				Msg.AlertDoFun("出错啦！", "history.back()");
				return;
			}
		}

		if (drscore["ispg"].ToString() == "0")
		{
		//	this.TxtPJTeacher.Text = KSCMS.GetCookie("Admin", "AdminUser");
		//	this.TxtPJSJ.Text = DateTime.Now.ToString();
		}
	
	}
	protected void BtnSubmit_Click()
	{

		string ZGTDFQK = KSCMS.S("ZGTDFQK");
		string pjTeacher = KSCMS.S("TxtPJTeacher");
		if (string.IsNullOrEmpty(pjTeacher))
		{
			JS.AlertDoFun("评卷教师必须输入！", "history.back()");
		}
		float TotalScore = Utils.StrToFloat(KSCMS.S("TotalScore"));
		DateTime pjsj = DateTime.Now;
		if (Utils.IsDateTime( KSCMS.S("TxtPJSJ")))
		{
			pjsj = Convert.ToDateTime(KSCMS.S("TxtPJSJ"));
		}


		Hashtable ht = new Hashtable();
		ht.Add("ispg", 1);
		ht.Add("PgTeacher", pjTeacher);
		ht.Add("py",KSCMS.S("TxtPY"));
		ht.Add("PGSJ", pjsj);
		ht.Add("ZGTDFQK", ZGTDFQK);
		ht.Add("score", TotalScore);

		CommonData.Update("KS_SJScore", ht, "where scoreid=" + scoreId);
		Msg.Alert("恭喜，评卷成功！", BasicField.GetUserUrl("teacher/TcHWSjScore","sjid-"+sjId+""));
	//	JS.Message("恭喜，评卷成功！", "success.gif", "location.href='" + KSCMS.GetInstallDir() + MyCache.GetCacheConfig(4) + "/Exam/KS.ZGSJPGItem.Aspx?sjid=" + sjId + "';");
	}
	public static string GetUserAnswerList(int tmType, int tmid, string ckda, string tmfs)
	{
		#region 答案是否正确
		StringBuilder sb = new StringBuilder();
		string userAnswer = Kesion.Exam.Function.GetAnswer(Answer, tmid);

		string answer = ckda;
		if (tmType == 3)
		{
			userAnswer = userAnswer.Replace('|', ',');
		}
		else if (tmType == 4)
		{
			if (!string.IsNullOrEmpty(userAnswer))
			{
				if (userAnswer == "1")
				{
					userAnswer = "对";
				}
				else
				{
					userAnswer = "错";
				}
			}
			if (!string.IsNullOrEmpty(answer))
			{
				if (answer == "1")
				{
					answer = "对";
				}
				else
				{
					answer = "错";
				}
			}
		}
		string mydc = string.Empty;
		string mydf = "0";

		if (answer.IndexOf("※") == -1)
		{
            sb.AppendLine("<td style='text-align:left'>" + userAnswer + "</td><td style='text-align:left'>" + ckda + "</td>");

			if (tmType != 1)
			{
				if (Kesion.Exam.Function.CheckDa(userAnswer, answer))
				{
					sb.AppendLine(@"<td style='text-align:left'><img src=""" + MyCache.GetCacheConfig(5) + MyCache.GetCacheConfig(0) + @"sysimg/default/yes.gif""></td>");
                    sb.AppendLine(@"<td style='text-align:left;color:green;'><input type='hidden' name='score' value='" + tmfs.ToString() + "'/>" + tmfs.ToString() + "</td>");
				}
				else
				{
                    sb.AppendLine(@"<td style='text-align:left'><img src=""" + MyCache.GetCacheConfig(5) + MyCache.GetCacheConfig(0) + @"sysimg/default/no.gif""></td>");
                    sb.AppendLine(@"<td style='text-align:left;color:red;'><input type='hidden' name='score'  value='0'/>0</td>");
				}
			}
			else
			{
                sb.AppendLine(@"<td style='text-align:left'><img src=""" + MyCache.GetCacheConfig(5) + MyCache.GetCacheConfig(0) + @"sysimg/default/unknow.gif""></td>");
                sb.AppendLine(string.Format(@"<td style='text-align:left'><input onkeyup=""checkgfnum(this,{0})"" type='text' name='score' value='{2}' tmid='{1}' style='text-align:center;width:30px' class='textbox'/></td>", tmfs, tmid.ToString(), Kesion.Exam.Function.GetDTScore(ZGTDFQK, tmid)));

			}
		}
		else
		{
			sb.AppendLine("<td colspan=4 style='padding:10px;'>");

			string[] arr = string.Concat(userAnswer, "").Split('※');
			string[] ckarr = string.Concat(answer, "※※※※※※※※※").Split('※');
			sb.AppendFormat(@"<strong>【得分情况】</strong><br/><table border=""0"" class='anstable' cellspacing='1' cellpadding='1'><tr class='t'><td align=""center"">题号</td><td align=""center"">考生答案</td><td align=""center"">正确答案</td><td align=""center"">是否正确</td><td align=""center"">得分</td></tr>");
			int k = 0;

			foreach (string s in arr)
			{
				k++;
				mydf = (Utils.StrToFloat(tmfs.ToString()) / arr.Length).ToString();
				if (Kesion.Exam.Function.CheckDa(s, ckarr[k - 1]))
				{
					mydc = MyCache.GetCacheConfig(5) + MyCache.GetCacheConfig(0) + "sysimg/default/yes.gif";
					//  mydf =tmfs.ToString();
				}
				else
				{
					if (tmType == 1)
					{
						mydc = MyCache.GetCacheConfig(5) + MyCache.GetCacheConfig(0) + "sysimg/default/yb.gif";
					}
					else
					{
						mydc = MyCache.GetCacheConfig(5) + MyCache.GetCacheConfig(0) + "sysimg/default/no.gif";
					}
					mydf = "0";
				}

				sb.AppendFormat(@"<tr class='split'><td align=""center"">第{0}空</td><td>{1}</td><td>{2}</td><td style=""text-align:center""><img src=""{3}"" border=""0""></td><td align='center' style='color:{5}'>{4}分<input type='hidden' name='score' value='" + mydf.ToString() + "'/></td></tr>", k, s, ckarr[k - 1].Replace("|", "或"), mydc, mydf, mydf == "0" ? "red" : "green");
			}
			sb.AppendLine("</table>");
			sb.AppendLine("</td>");
		}
		return sb.ToString();
		#endregion
	}

</script>
<%
    if (!isLoadPage) return;
%>
	<script>

		function showToggle(id) {
			if (jQuery('#xt' + id).html() == '') {
				jQuery('#xt' + id).append('<tr class="tdbg"><td height="30" align="center" colspan="4">本大题下还没有添加小题!</td></tr>');
			}
			var t = jQuery("#t" + id).html();
			if (t.indexOf('打开') != -1) {
				jQuery('#xt' + id).slideDown('fast');
				jQuery("#t" + id).html('<img border="0" src="../../admin/images/icon_del_over.png" align="absmiddle"/>关闭')
			} else {
				jQuery('#xt' + id).slideUp('fast');
				jQuery("#t" + id).html('<img border="0" src="../../admin/images/icon_add_normal.png" align="absmiddle"/>打开')
			}
		}
		jQuery(document).ready(function () {
			jQuery(".toggleArea").html('<img border="0" src="../../admin/images/icon_del_over.png" align="absmiddle"/>关闭');
			jQuery('.showArea').each(function () {
				if (jQuery(this).html() == '') {
					jQuery(this).append('<tr class="tdbg"><td height="30" align="center" colspan="4">本大题下还没有添加小题!</td></tr>');
				}
			});

		});
</script>
    	<script src="/ks_inc/DatePicker/WdatePicker.js"></script>
<style>
.anstable{border:#E5E5E5 1px solid;padding:1px;width:80%;}
.anstable split. td{background:#FAFAFA;font-size:12px;padding-left:12px;}
.anstable .t{background:#DBE9F6;height:28px;line-height:28px;}
.anstable .t td{font-size:12px;padding-left:12px;padding-right:12px;}
</style>
 <div class="t_tybox">
    	 
 <div class="tytitle"><h4>试卷批阅</h4></div>

   <form name="myform" action="<%=BasicField.GetUserUrl("teacher/TcClassSjPY","do-save") %>" method="post" id="myform" >

   <table width="98%" align="center" border="0" cellpadding="1" cellspacing="1" class="border">
<%
					 
    int type = Utils.StrToInt(DataFactory.GetOnlyValue("select top 1 dtfs from KS_Sj where id=" + sjId));
    
     string	sqlStr=string.Empty;
    string sqlStr2=string.Empty;
    if (type == 1)
    {
        sqlStr = "SELECT * From [KS_SJTMType] Where SJID=" + sjId + " order by orderid,typeid";

        using (DataTable dt = DataFactory.GetDataTable(sqlStr))
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    KSCMS.W(" <TR class=\"CTitle sort topbar\"><td align=\"center\" nowrap>" +
                        "<span style='cursor:pointer' id='t" + dr["typeid"].ToString() + "' class=\"toggleArea\" onclick=\"showToggle(" + dr["typeid"].ToString() + ");\"></span></td>" +
   " <td colspan=\"5\" style=\"text-align:left;padding-top:2px;padding-bottom:2px;padding-left:10px;\"> <strong>" +
   " 第" + UtilsAutoID.CapitalFigures(Utils.StrToInt(Convert.ToString(dr["orderid"].ToString()), 1) - 1) + "大题&nbsp;" + dr["typename"] + "" +
   " <div style=\"display:none\">" + dr["intro"] + " </div></strong></td><td style=\"text-align:center\"></td></TR>");
                    KSCMS.W(" <tbody id='xt" + dr["typeid"].ToString() + "' class=\"border\"> <tr class=\"bar\" align=\"center\">" +
" <td>题号</td><td>分数</td><td style=\"width:500px;\">题目内容</td><td>考生答案</td><td>正确答案</td><td>是否正确</td><td>得分</td></tr>");
                        sqlStr2 = "select * from KS_SJTK where typeid=" + dr["TypeID"].ToString() + " order by orderid,tmid ";
                    using (DataTable dts = DataFactory.GetDataTable(sqlStr2))
                    {
                        if (dts.Rows.Count > 0)
                        {
                            foreach (DataRow drs in dts.Rows)
                            {
                                KSCMS.W(" <tr class=\"tdbg\"> <td style=\"text-align:center\" width=\"48\"> <strong>" + drs["orderid"].ToString() + "</strong></td>" +
     "<td align=\"center\" style=\"font-weight:bold\" nowrap>" + drs["TMFS"].ToString() + "分</td>" +
     "<td style=\"text-align:left \" >" + drs["title"].ToString() + drs["TMnr"].ToString() + "</td>" + GetUserAnswerList(Convert.ToInt32(drs["TmType"]), Convert.ToInt32(drs["tmid"]), Convert.ToString(drs["answer"]), Convert.ToString(drs["tmfs"])) + " </tr>");
                            }
                        }
                    }
                    KSCMS.W("</tbody>  ");
                }
            }
            else
            {
                KSCMS.W("  <tr class=\"tdbg\" <td class=\"Ctd\" style=\"text-align:center\" colspan=\"4\"> </td> </tr> ");
            }
        }
    }
    else
    { 
   
        sqlStr = "SELECT * From [KS_SJRndZJ] Where SJID=" + sjId + " order by orderid,tmtype";
      
        using (DataTable dt = DataFactory.GetDataTable(sqlStr))
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    KSCMS.W(" <TR class=\"CTitle sort topbar\"><td align=\"center\" nowrap>" +
                        "<span style='cursor:pointer' id='t" + dr["tmtype"].ToString() + "' class=\"toggleArea\" onclick=\"showToggle(" + dr["tmtype"].ToString() + ");\"></span></td>" +
   " <td colspan=\"5\" style=\"text-align:left;padding-top:2px;padding-bottom:2px;padding-left:10px;\"> <strong>" +
   " 第" + UtilsAutoID.CapitalFigures(Utils.StrToInt(Convert.ToString(dr["orderid"].ToString()), 1) - 1) + "大题&nbsp;" + dr["title"] + "" +
   " <div style=\"display:none\">" + dr["intro"] + " </div></strong></td><td style=\"text-align:center\"></td></TR>");
                    KSCMS.W(" <tbody id='xt" + dr["tmtype"].ToString() + "' class=\"border\"> <tr class=\"bar\" align=\"center\">" +
" <td>题号</td><td>分数</td><td style=\"width:500px;\">题目内容</td><td>考生答案</td><td>正确答案</td><td>是否正确</td><td>得分</td></tr>");
                    sqlStr2 = "SELECT * From [KS_SJTK] Where tmid in (" + dr["tmids"].ToString() + ") order by orderid,tmid";
                    using (DataTable dts = DataFactory.GetDataTable(sqlStr2))
                    {
                        if (dts.Rows.Count > 0)
                        {
                            foreach (DataRow drs in dts.Rows)
                            {
                                KSCMS.W(" <tr class=\"tdbg\"> <td style=\"text-align:center\" width=\"48\"> <strong>" + drs["orderid"].ToString() + "</strong></td>" +
     "<td align=\"center\" style=\"font-weight:bold\" nowrap>" + drs["TMFS"].ToString() + "分</td>" +
     "<td style=\"text-align:left \" >" + drs["title"].ToString() + drs["TMnr"].ToString() + "</td>" + GetUserAnswerList(Convert.ToInt32(drs["TmType"]), Convert.ToInt32(drs["tmid"]), Convert.ToString(drs["answer"]), Convert.ToString(drs["tmfs"])) + " </tr>");
                            }
                        }
                    }
                    KSCMS.W("</tbody>  ");
                }
            }
            else
            {
                KSCMS.W("  <tr class=\"tdbg\" <td class=\"Ctd\" style=\"text-align:center\" colspan=\"4\"> </td> </tr> ");
            }
        }
    }
	 %>
         
           </table>

          <table width="99%" align="center" border="0" cellpadding="1" cellspacing="1" class="border"> 
          <tr>
            <td class="lefttd" height="30" align="right" style="width: 108px"><b>批卷教师：</b></td>
            <td class="righttd">
				    <input name="TxtPJTeacher" type="text" id="TxtPJTeacher" value="<%=(string.IsNullOrEmpty( GetParam("PgTeacher"))?__user.UserName: GetParam("PgTeacher")) %>" style="width:203px" class="textbox" />
            </td>
          </tr>

        <tr>
            <td align="right" class="lefttd" height="30" style="width: 108px">
                <strong>总&nbsp;得&nbsp;分：</strong></td>
            <td class="righttd" style="font-size:20px;font-weight:bold;"> &nbsp;<span id="totalscore" style="color:Red">0</span>分
             
             <script>

             	jQuery(document).ready(function() {
             		getScore();
             		jQuery("input[name=score]").keyup(function() {
             			getScore();
             		});
             	});
             	function getScore() {
             		var score = 0;
             		jQuery("input[name=score]").each(function() {

             			score += parseFloat(jQuery(this).val());
             		});
             		jQuery("#totalscore").html(score);
             		jQuery("#TotalScore").val(score);
             	}
             	//估分时，检查总分
             	function checkgfnum(obj, t) {
             		if (isNaN(obj.value)) {
             			obj.value = '0';
             			obj.focus();
             			window.KesionJS.Alert("分数必须输入数字! ");
             			return false;
             		}
             		if (obj.value > t) {
             			obj.value = t;
             			obj.focus();
             			window.KesionJS.Alert("此题总分 " + t + " 分，评分不能超过 " + t + " 分! ");
             			return false;
             		}
             	}
             	function check() {
             		var str = '';
             		jQuery("input[name=score]").each(function() {
             			if (jQuery(this).attr("tmid") != undefined) {
             				if (str=='')
             					str = jQuery(this).attr("tmid") + "|" + jQuery(this).val();
             				else
             					str +='$'+ jQuery(this).attr("tmid") + "|" + jQuery(this).val();
             			}
             		});
             		jQuery("#ZGTDFQK").val(str);
             		return true;
             	}
             </script>
            
                </td>
        </tr>
        <tr>
            <td class="lefttd" height="30" align="right" style="width: 108px"><b>教师评语：</b></td>
            <td class="righttd">   <textarea name="TxtPY" rows="2" cols="20" id="TxtPY" style="height:200px;width:80%;"><%=(string.IsNullOrEmpty( GetParam("py"))?"": GetParam("py")) %></textarea>
			<%Kesion.APPCode.EditorAPI.EchoHead(); %>
			    <% Kesion.APPCode.EditorAPI.EchoEditor("editor", "TxtPY", "Basic", 0, false, true, true, false);     %>
            </td>
          </tr>            

          <tr>
            <td class="lefttd" height="30" align="right" style="width: 108px"><b>批卷时间：</b></td>
            <td class="righttd">
				   <input name="TxtPJSJ" type="text" id="TxtPJSJ" value="<%=string.IsNullOrEmpty(GetParam("PGSJ"))?Utils.GetDateTime():GetParam("PGSJ") %>" class="textbox  Wdate" onclick="WdatePicker({dateFmt:&#39;yyyy-MM-dd HH:mm:ss&#39;,minDate:&#39;%y-%M-%d&#39;});" /> 
            </td>
          </tr>
          
        <tr>
            <td align="right" class="lefttd" height="50">
               </td>
            <td class="righttd">
            <input type="hidden" name="ZGTDFQK" id="ZGTDFQK" />
            <input type="hidden" name="TotalScore" id="TotalScore" />
			<input type="hidden" name="scoreid" value="<%=scoreId %>" />
			<input type="hidden" name="sjid" value="<%=sjId %>" />
             <%-- <asp:Button ID="BtnSubmit" Text="完成批改" OnClientClick="return(check())"   runat="server" CssClass="button" />--%>
			   <div class="cz_btn">
        <span><input type="submit" class="button" value="完成批改" onclick="return(check())"></span>
        <div class="clear"></div>
     </div>
             </td>
        </tr>
        </table>
           
		  
	 </form>

		</div>
    