<%@ 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;  //定义页面是否加载
	private int sjId = 0;
	public static StringBuilder sb = new StringBuilder();
	public static float totalzf = 0;
	protected void Page_Load(object sender, EventArgs e)
	{
		sjId = Utils.StrToInt(KSCMS.S("id"));
		if (!string.IsNullOrEmpty(KSCMS.S("score")))
		{
			doSave();
		}
		else
		{
			if (!IsPostBack)
			{
				totalzf = 0;
				sb.Remove(0, sb.Length);
				using (DataTable dt = DataFactory.GetDataTable("select top 1 * from ks_sj where Id=" + sjId))
				{
					if (dt.Rows.Count > 0)
					{
						DataRow dr = dt.Rows[0];
						sb.AppendLine(string.Format(@"<div style='font-weight:bold'>试卷名称：{0}</div>", dr["title"].ToString()));
						sb.AppendLine(string.Format(@"<div  class=""title"" style=""font-size:14px;border:0px #000 dashed; margin-top:10px;padding:6px;text-align:left"">各试题分数分布情况：</div>"));
						if (Utils.StrToInt(dr["dtfs"].ToString()) == 1)
						{
							checkZF();
						}
						else  // 随机组卷
						{
							checkRnd();
						}

						sb.AppendFormat(@"<div class=""attention totaltips"">本份试卷系统核算后的总分是 <span class='zf'>{0}</span> 分，您添加试卷时设置的总分为 <span class='score'>{1}</span> 分。", totalzf.ToString("#.##"), dr["Score"].ToString());
						if (totalzf == Utils.StrToFloat(dr["score"].ToString()))
						{
							sb.AppendLine(@"<div class=""success"">恭喜，总分检查相符！</div>");
						}
						else
						{
							sb.AppendLine(@"<div class=""error"">对不起，总分检查不相符！<br/><span>将总分设置为<input type='hidden' name='frame' value='0'/>   <input type='text' name='score' value='" + totalzf.ToString("#.##") + "' class='textbox' style='text-align:center' size=5/> <input type='submit' value='确定设置' class='button'></span></div>");
						}
						sb.AppendFormat("</div>");
					}
					else
					{
						Msg.AlertDoFun("对不起,试卷ID出错啦!", " top.location.reload();");
						isLoadPage = false;
						return;
					}
				}
			}
		}
	}
	private void doSave()
	{
		float score = Utils.StrToFloat(KSCMS.S("score"));
		Hashtable ht = new Hashtable();
		ht.Add("score", score);
		CommonData.Update("KS_SJ", ht, "where id=" + sjId);
		string url = BasicField.GetUserUrl("sj/TcSJCheckZF", "frame-no,id-" + sjId);
		Msg.AlertDoFun("恭喜，成功的修改了试卷总分！","location.href='"+url+"'");
		//isLoadPage = false;
		return;
	}
	/// <summary>
	/// 整份完整添加
	/// </summary>
	private void checkZF()
	{
		#region 整份完整添加
		using (DataTable dt = DataFactory.GetDataTable("SELECT * From [KS_SJTMType] Where SJID=" + sjId))
		{
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					StringBuilder ststr = new StringBuilder();
					ststr.Remove(0, ststr.Length);
					float totalfs = 0;
					using (DataTable dtm = DataFactory.GetDataTable("select * from KS_SJTK where typeid=" + dr["typeid"].ToString() + " order by orderid"))
					{
						if (dtm.Rows.Count > 0)
						{

							foreach (DataRow drr in dtm.Rows)
							{
								float dtfs = Utils.StrToFloat(drr["tmfs"].ToString());
								if (drr["tmtype"].ToString() == "5" && drr["answer"].ToString().IndexOf("※") != -1)
								{
									int knum = Utils.SplitString(drr["answer"].ToString(), "※").Length;
									dtfs = knum * dtfs;
									ststr.AppendFormat(@"<div>&nbsp;&nbsp;{0}、占<font color=green>{1}</font>分(共<font color=green>{2}</font>空)</div>", drr["orderid"].ToString(), dtfs.ToString(), knum);
								}
								else
								{
									ststr.AppendFormat(@"<div>&nbsp;&nbsp;{0}、占<font color=green>{1}</font>分</div>", drr["orderid"].ToString(), dtfs);
								}
								totalfs += dtfs;
							}
						}
					}
					totalzf += totalfs;
					sb.AppendFormat(@"<div class='kzf_tit'>第 {0} 大题、{1} 共计<font color=red>{2}</font> 分</div>", UtilsAutoID.CapitalFigures(Utils.StrToInt(dr["orderid"].ToString()) - 1), dr["typename"].ToString(), totalfs.ToString("#.##"));
					sb.AppendLine(ststr.ToString());
				}
			}
			else
			{
				sb.AppendLine("没有添加试题!");
			}
		}
		#endregion
	}
	/// <summary>
	/// 随机组卷添加
	/// </summary>
	private void checkRnd()
	{
		#region 随机组卷添加
		using (DataTable dt = DataFactory.GetDataTable("SELECT * From [KS_SJRndZJ] Where SJID=" + sjId))
		{
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					StringBuilder ststr = new StringBuilder();
					ststr.Remove(0, ststr.Length);
					float totalfs = 0;
					string tmIds = Utils.FilterIDs(dr["tmids"].ToString());
					if (!string.IsNullOrEmpty(tmIds))
					{
						int n = 0;
						using (DataTable dtm = DataFactory.GetDataTable("select * from KS_SJTK where tmid in(" + tmIds + ") order by orderid,tmid desc"))
						{
							if (dtm.Rows.Count > 0)
							{
								foreach (DataRow drr in dtm.Rows)
								{
									n++;
									float dtfs = Utils.StrToFloat(dr["fs"].ToString());
									if (drr["tmtype"].ToString() == "5" && drr["answer"].ToString().IndexOf("※") != -1)
									{
										int knum = Utils.SplitString(drr["answer"].ToString(), "※").Length;
										dtfs = knum * dtfs;
										ststr.AppendFormat(@"<div>&nbsp;&nbsp;{0}、占<font color=green>{1}</font>分(共<font color=green>{2}</font>空)</div>", n.ToString(), dtfs.ToString(), knum);
									}
									else
									{
										ststr.AppendFormat(@"<div>&nbsp;&nbsp;{0}、占<font color=green>{1}</font>分</div>", n.ToString(), dtfs);
									}
									totalfs += dtfs;
								}
							}

						}
					}
					totalzf += totalfs;
					sb.AppendFormat(@"<div class='kzf_tit'>第 {0} 大题、{1} (每小题<font color=brown>{3}</font>分，共计<font color=red>{2}</font> 分)</div>", UtilsAutoID.CapitalFigures(Utils.StrToInt(dr["orderid"].ToString()) - 1), dr["title"].ToString(), totalfs.ToString("#.##"), dr["fs"].ToString());
					sb.AppendLine(ststr.ToString());
				}
			}
			else
			{
				sb.AppendLine("没有添加试题!");
			}
		}
		#endregion
	}

</script>
<%
    if (!isLoadPage) return;
%>
 <style>
.totaltips{padding:5px;margin:10px;font-size:12px;}
.totaltips .zf{color:Red}
.totaltips .score{color:green}
.totaltips .error{color:red;line-height:40px}
.totaltips .success{color:green;}
.totaltips .error span{color:#888}
.kzf_tit{height: 38px;
    line-height: 38px;
    margin-top: 20px;
    font-size: 14px;
    padding-left: 10px;
    color: #908b83;
    background: #faf6ef;
    border-radius: 3px;
    border: 1px solid #f0d5c1;
    margin-bottom: 20px;
}
.cz_btn .button {
    padding: 0px 20px;
    float: left;
    height: 30px;
    line-height: 30px;
    background: #5bc0de;
    border: 1px solid #5BC0DE;
    border-radius: 5px;
    color: #fff;
    font-size: 14px;
    margin-top: 1px;
}
</style>
 <form name="myform" action="<%=BasicField.GetUserUrl("SJ/TcSJCheckZF") %>" method="post" id="Form1" >
<%=sb.ToString() %>
<div class="cz_btn nofloat" style=" width:150px; margin:auto; background:none; border:0;"><span><input type="button" class="button" value="关闭窗口" onclick="closed();" accesskey="C"/></span><div style=" clear:both;"></div></div>
 <input type="hidden" name="id" value="<%=KSCMS.S("id") %>" />
      <input type="hidden" name="frame" value="1" />
     <script>
     	function closed() {
     		//alert(1);
     		//  box.close();
     		top.location.reload();
     	}
        </script>
		</form>