<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../common/init.jsp"%>
<!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>
<link rel="stylesheet" href="${ctx}/resources/css/main.css" type="text/css" />
<link rel='stylesheet' href='${ctx}/resources/css/easyui.css' type='text/css' media='screen' />
<link rel='stylesheet' href='${ctx}/resources/css/easyui/icon.css' type='text/css' media='screen' />
<link rel='stylesheet' href='${ctx}/resources/css/user-center-style.css' type='text/css' media='screen' />

<link href="${ctx}/resources/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="${ctx}/resources/css/style.css" rel="stylesheet" type="text/css" />

<%@ include file="../common/top.jsp"%>
<script type="text/javascript" src="${ctx}/resources/js/jquery.easyui.min.js"></script>
<%@ include file="../common/validation.jsp"%>
</head>
<title>及格分数预估表</title>
<body>
    <div id="content" class="content" style="text-align:center;">
        <div class="page" id="page" style="margin-left:10px;text-align:center; width:100%;">
        	<div class="page-header" >
        		<div style="text-align:left; color:red;">${errorMsg }</div>
        		<c:if test="${createEstimate == true && canEdit == true}">
	        		<button id="createEstimateTableBtn">创建及格分数预估表</button>
	        	</c:if>
	        	<c:if test="${difficultyEstimates != null && canEdit == true}">
	        		<button id="editEstimateTableBtn">编辑</button>
	        		<button id="saveEstimateTableBtn">保存</button>
	        	</c:if>
			</div>
	        <div class="page-body" style="">
	        	<h3 style="align:center;">高等教育自学考试试卷及格分数预估表</h3>
	        	课程名称：${course.name }    课程代码：${course.code }   试卷代码：${paper.name }
	        	
	        	<table id="estimateTable" class="table table-bordered">
	        		<tr>
	        			<th style="text-align:center;">题型</th>
	        			<th style="text-align:center;">题号</th>
	        			<th style="text-align:center;">分值</th>
	        			<th style="text-align:center;">估计难度层次</th>
	        			<th style="text-align:center;">估计难度值</th>
	        			<th style="text-align:center;">估计平均分</th>
	        			<th>实测平均分</th>
	        			<th>实测难度值</th>
	        		</tr>
	        		<c:forEach var="list" items="${difficultyEstimates}">
	        			<c:forEach var="item" items="${list }" varStatus="status">
		        			<tr>
		        				<c:if test="${status.index == 0 }">
				        			<td rowspan="${fn:length(list)}" style="text-align:center;">${item.questionType}</td>
		        				</c:if>
			        			<td style="text-align:center;">${item.sortNo }</td>
			        			<td style="text-align:center;" columnName="questionScore">${item.questionScore }</td>
			        			<td style="text-align:center;" columnName="estimateDifficultyName">${item.estimateDifficultyName }</td>
			        			<td style="text-align:center;"><input type="hidden" value="${item.id }" columnName="estimateDifficultyId"/><input textType="estimateDifficultyValue" style="width:50px;" value="${item.estimateDifficultyValue }"  disabled="disabled"/></td>
			        			<td style="text-align:center;" columnName="averageScore">${item.averageScore }</td>
			        			<td></td>
			        			<td></td>
			        		</tr>
    	    			</c:forEach>
	        		</c:forEach>
	        		<tr>
	        			<td colspan='5' style="text-align:right;">合计：</td>
	        			<td id="totalAverageScore" style="text-align:center;"></td>
	        			<td></td>
	        			<td></td>
	        		</tr>
	        	</table>
		        	
	        	<table class="table">
	        		<tr><td colspan="2">注：及格分数预估表同试卷一起存档。</td></tr>
	        		<tr><td>填表人:  ${createdBy.name }</td><td>  填表日期：  <fmt:formatDate value="${updatedTime}" pattern="yyyy-MM-dd" /></td></tr>
	        		<tr><td colspan="2"></td></tr>
		        	<tr><th colspan="2" style="text-align:center;">填表说明</th></tr>
		        	<tr><td colspan="2">1. 估计难度值：以刚达标的应考者为群体样本，估计他们能正确回答一道试题的概率，即是该题的估计难度值(小数点后保留两位)，该值应落在该题的难度层次（A，B，C，D）的数值范围内。</td></tr>
		        	<tr><td colspan="2">2. 估计平均分：估计难度值乘以该题满分值，得到该题的估计平均分。</td></tr>
		        	<tr><td colspan="2">2*.前面两步也可简化为：直接估计刚达标群体（及格水平的学生）在每小题可能得到的分数为该题的估计平均分。</td></tr>
		        	<tr><td colspan="2">3. 试卷中所有试题的估计平均分之和为预估的试卷及格分数。</td></tr>
		        	<tr><td colspan="2">4. 如果预估的试卷及格分数在55-65分范围之外，则必须对有关试题进行调整。</td></tr>
		        	<tr><td colspan="2">5. 实测平均分和实测难度为试卷考后的统计数据，供作对比实验用，命题时不填写。</td></tr>
	        	</table>
	        </div>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function(){
	$('#createEstimateTableBtn').click(function(){
		$(document).mask('正在处理，请稍等...');
    	$.post('${ctx}/zujuan/createDifficultyEstimate/${paper.id}',null,function(res){
			$(document).unmask();
			if(res.flag=='failed'){
	           $.messager.alert('提示', res.msg, 'error', function () {
	        	   //do nothing
	           });
	           return false;
			}else{
				window.location.reload();
			}
		});
	});
	
	$("#editEstimateTableBtn").click(function(){
		$("[textType='estimateDifficultyValue']").each(function(){
			$(this).attr("disabled",false);
		});
		$('#saveEstimateTableBtn').show();
		$('#editEstimateTableBtn').hide();
		
	});
	
	$('#saveEstimateTableBtn').click(function(){
		//$(document).mask('正在处理，请稍等...');
		//验证估计难度值
		var array=new Array();
		var flag=true;
		var total=$("#totalAverageScore").html();
		$("[textType='estimateDifficultyValue']").each(function(){
			var val = $(this).val();	//估计难度值
			var estimateDifficultyName = $(this).parent().parent().children("[columnName='estimateDifficultyName']").html();	//对应的估计难度层次
			var estimateDifficultyId=$(this).parent().children("[columnName='estimateDifficultyId']").val();	//主键id
			
			if(!val || val < 0 || val > 1|| (estimateDifficultyName == 'A' && val < 0.7) || (estimateDifficultyName == 'B' && (val < 0.5 || val > 0.7)) 
						|| (estimateDifficultyName == 'C' && (val < 0.3 || val > 0.5))|| (estimateDifficultyName == 'D' && val > 0.3)){
					alert("输入错误。请按照对照难度层次填写难度值：A(0.7~1) B(0.5~0.7) C(0.3~0.5) D(0~0.3)");
					flag=false;
					return false;
			}
			var obj=estimateDifficultyId+"-"+val;
			array.push(obj);
		});
		if(total<55){
			alert("合计值小于55,请核实!");
			flag=false;
		}
		if(total>65){
			alert("合计值大于65,请核实!");
			flag=false;
		}
		if(flag){
			$.ajax({
				type : "POST",
				url : "${ctx}/zujuan/setEstimateDifficulty",
				data : "["+  array  +"]", 
				contentType : "application/json; charset=utf-8",
				dataType : "json",
				success : function(callback) {
					if(callback.flag=='success'){
						$("[textType='estimateDifficultyValue']").each(function(){
							$(this).attr("disabled",true);
						});
						$('#saveEstimateTableBtn').hide();
						$('#editEstimateTableBtn').show();
						alert(callback.msg);
					}else{
						alert(callback.msg);
					}
				}
			});  
		}
	});
	
	$("input").focus(function(){
	  $(this).css("background-color","#FFFF00");
	});
	
	$("[textType='estimateDifficultyValue']").each(function(){
		if(true){
    		$(this).blur(function(e){
    			$(this).css("background-color","#FFFFFF");
    			var val = $(this).val() - 0;
    			var estimateDifficultyName = $(this).parent().parent().children("[columnName='estimateDifficultyName']").html();
    			
    			if(!val || val < 0 || val > 1
    					|| (estimateDifficultyName == 'A' && val < 0.7) 
   						|| (estimateDifficultyName == 'B' && (val < 0.5 || val > 0.7)) 
   						|| (estimateDifficultyName == 'C' && (val < 0.3 || val > 0.5))
   						|| (estimateDifficultyName == 'D' && val > 0.3)){
   					alert("输入错误。请按照对照难度层次填写难度值：A(0.7~1) B(0.5~0.7) C(0.3~0.5) D(0~0.3)");
    				val = '';    				
    			}else{
    				val = val.toFixed(2);
    			}
    			
    			$(this).val(val);
    			calcStat($(this));
    		});
		}else{
			$(this).attr("readonly","readonly");
		}
	}); 
	
	function calcStat(cell){
		var questionScore = cell.parent().parent().children("[columnName='questionScore']").html();
		var averageScore = (questionScore * cell.val()).toFixed(2);
		cell.parent().parent().children("[columnName='averageScore']").html(averageScore);
		
		var totalAverageScore = 0;
		$("[columnName='averageScore']").each(function(){
			totalAverageScore = totalAverageScore + ($(this).html()-0);
		});
		
		$('#totalAverageScore').html(totalAverageScore.toFixed(2));
	}
	
	function calcStat(){
		$("[textType='estimateDifficultyValue']").each(function(){
			var questionScore = $(this).parent().parent().children("[columnName='questionScore']").html();
			var averageScore = (questionScore * $(this).val()).toFixed(2);
			$(this).parent().parent().children("[columnName='averageScore']").html(averageScore);
		});
		
		var totalAverageScore = 0;
		$("[columnName='averageScore']").each(function(){
			totalAverageScore = totalAverageScore + ($(this).html()-0);
		});
		
		$('#totalAverageScore').html(totalAverageScore.toFixed(2));
	}
	
	calcStat();
});
</script>
        