<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>考试管理</title>
	<meta name="decorator" content="default"/>
	<%@include file="/WEB-INF/views/include/head.jsp" %>
	 <script type="text/javascript">
        $(document).ready(function () {
        	$("#customize-div").hide();
            $("#inputForm").validate({
                submitHandler: function (form) {
                    loading('正在提交，请稍等...');
                    form.submit();
                },
                errorContainer: "#messageBox",
                errorPlacement: function (error, element) {
                    $("#messageBox").text("输入有误，请先更正。");
                    if (element.is(":checkbox") || element.is(":radio") || element.parent().is(".input-append")) {
                        error.appendTo(element.parent().parent());
                    } else {
                        error.insertAfter(element);
                    }
                }
            });
            $(".objectiveDiv").hide();
            $("#objective").change(function () {
                if ($("#objective").is(':checked')) {
                    $(".objectiveDiv").show();
                } else {
                    $("#answer").attr("value", "");
                    $("#paperType").attr("value", "");
                    $(".objectiveDiv").hide();
                }
            });
            $("[name='objectivePolicy']").change(function () {
                if($(this).val()=="4"){
                	$("#customize-div").show();
                	reShowBtn();
                }else{
                	$("#customize-div").hide();
                }
            });
            $("[name='type']").change(function () {
                if($(this).val()=="2"){
                	var tem=$("[name='objectivePolicy']").find("option[value='4']");
                	if(!tem[0]){
	                	$("[name='objectivePolicy']").append("<option value=\"4\">自定义</option>");
	                }
                }else{
                	var sle=$("[name='objectivePolicy']").val();
                	$("[name='objectivePolicy']").find("option[value='4']").remove();
                	if(sle=="4"){
                		$("[name='objectivePolicy']").val("1").trigger('change');
                	}
                }
            });
            $("[name='type']").trigger('change');
            $('#btnSubmit').click(function () {
                    var score = $('#interval-score-input').val();
                    var reg= /^-?\d+\.?\d{0,2}$/; 
                    if (score == '' || !reg.test(score) ||score<0) {
                    	alert('间隔分不能为空,必须大于0分且只支持两位小数');
                    	return false;
                    }
                    var total = $('#total-score-input').val();
                    if (total == '' || total<0) {
                    	alert('满分不能为空且大于0分');
                    	return false;
                    }
                    if(!buildParam()){
                    	return false;
                    }
                    $('#inputForm').submit();
            });
        });
        function editRow(btn){
        	var ob=$(btn);
        	if("-"==ob.val()){
        		ob.parent().remove();
        	}else{
        		var temrow=ob.parent().clone();
        		$(temrow).children("[name='answerCount']").val("");
        		$(temrow).children("[name='answerScore']").val("");
        		ob.parent().parent().append(temrow);
        	}
        	reShowBtn();
        }
        function reShowBtn(){
        	if($("#customize-div").children("div").size()==1){
        		$(".edit-button[value='-']").hide();
        	}else{
        		$(".edit-button[value='-']").show();
        	}
        	$(".edit-button[value='+']").hide();
        	$("#customize-div").children("div:last-child").children("input:last-child").show();
        }
        function checkParam(){
        	var countSet = new Set();
        	var regCount= /^[1-9]+\d*$/;
        	var regScore= /^-?\d+\.?\d{0,2}$/; 
        	var answerLength=$("[name='answer']").val().trim().length;
        	var maxScore=$('#total-score-input').val();
        	var hasMxScore=false;
        	try{
	        	var acArr=$("[name='answerCount']");
	        	var countSize=0;
	        	acArr.each(function(){
	        		countSize++;
	        		var tem=$(this).val();
	        		if(!tem||tem.trim()==""){
	        			throw new Error("自定义判分策略，答对个数不能为空"); 
	        		}
	        		tem=parseInt(tem.trim());
	        		if(!regCount.test(tem)){
	        			throw new Error("自定义判分策略，答对个数只能是正整数"); 
	        		}
	        		if(tem>answerLength){
	        			throw new Error("自定义判分策略，答对个数不能大于答案数量"); 
	        		}
	        		countSet.add(tem);
	        	});
	        	if(countSize!=countSet.size){
	        		throw new Error("自定义判分策略，答对个数有重复"); 
	        	}
	        	var asArr=$("[name='answerScore']");
	        	asArr.each(function(){
	        		var tem=$(this).val();
	        		if(!tem||tem.trim()==""){
	        			throw new Error("自定义判分策略，给分不能为空"); 
	        		}
	        		tem=parseFloat(tem.trim());
        		 	if (!regScore.test(tem) ||tem<=0) {
        		 		throw new Error("自定义判分策略，给分必须大于0分且只支持两位小数");
                 	}
        		 	if (tem>maxScore) {
        		 		throw new Error("自定义判分策略，给分不能大于小题满分");
                 	}
	        		if(maxScore==tem){
	        			hasMxScore=true;
	        		}
	        	});
	        	if(!hasMxScore){
        			throw new Error("自定义判分策略，小题满分未完成分配"); 
        		}
        		return true;
        	}catch(e){
        		alert(e.message);
        		return false;
        	}
        }
        function buildParam(){
        	if($("[name='objectivePolicy']").val()=="4"){
        		if(!checkParam()){
        			return false;
        		}
        		var arr=[];
        		$("#customize-div").children("div").each(function(){
        			var tem={};
        			tem['answerCount']=$(this).children("[name='answerCount']:first").val().trim();
        			tem['answerScore']=$(this).children("[name='answerScore']:first").val().trim();
        			arr.push(tem);
        		});
        		$("#customizePolicy").val(JSON.stringify(arr));
        	}else{
        		$("#customizePolicy").val("");
        	}
        	return true;
        }
    </script>
    <style type="text/css">
    .edit-button {
        width:26px !important;
        border-radius: 50%;
        border: 1px solid #ccc;
        margin-left:5px;
    }
    .customize-input{
    	width:30px;
    }
     .customize-row{
    	margin-bottom: 5px;
    }
    
    </style>
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active"><a href="javascript:" onclick="history.go(-1);">试题列表</a></li>
	</ul><br/>
	<form id="inputForm" action="${ctx}/admin/exam/paper/question/save" method="post" class="form-horizontal">
		<tags:message content="${message}"/>
		<input type="hidden" id="pageNumber" name="pageNumber" value="${query.pageNumber }"/>
	 	<input type="hidden" id="code" name="code" value="${query.code }"/>
	 	<input type="hidden" id="category" name="category" value="${query.category }"/>
	 	<input type="hidden" id="level" name="level" value="${query.level }"/>
	 	<input type="hidden" id="upload" name="upload" value="${upload}"/>
	 	<input type="hidden" id="totalScoreNotEqual" name="totalScoreNotEqual" value="${query.totalScoreNotEqual }"/>
	 	<input type="hidden" id="customizePolicy" name="customizePolicy"/>
	 	
	 	<input type="hidden" name="subjectCode" value="${subjectCode}"/>
		<div class="control-group">
			<label class="control-label">大题名称</label>
			<div class="controls">
			 <input name="mainTitle" value="${examQuestion.mainTitle }" type="text"  maxlength="32" class="required"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">大题昵称</label>
			<div class="controls">
			 <input name="name" value="${examQuestion.name }" type="text"  maxlength="32"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">大题号</label>
			<div class="controls">
			 <input name="mainNumber" value="${examQuestion.mainNumber }" type="number" htmlEscape="false" max="10000" min="1" class="required digits"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">小题号</label>
			<div class="controls">
			 <input name="subNumber" value="${examQuestion.subNumber }" type="number" htmlEscape="false" max="10000" min="1" class="required digits"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">满分</label>
			<div class="controls">
			 <input name="totalScore" value="${examQuestion.totalScore }" type="number" id="total-score-input" htmlEscape="false"  maxlength="10" class="required"/>
			</div>
		</div>
		<div class="control-group">
	        <label class="control-label">是否客观题</label>
	        <div class="controls">
	            <input type="checkbox" id="objective" name="objective">
	        </div>
    	</div>
			<div class="control-group">
	            <label class="control-label">间隔分</label>
	            <div class="controls">
	                <input type="number" name="intervalScore" class="required " id="interval-score-input" htmlEscape="false" maxlength="10" class="required "
	                       value="${examQuestion.intervalScore}"/>
	            </div>
	        </div>
        <div class="objectiveDiv">
        	<div class="control-group">
				<label class="control-label">试卷类型</label>
				<div class="controls">
				 <input name="paperType" value="${examQuestion.paperType }" type="text" class="required"/>
				</div>
			</div>
	        <div class="control-group">
				<label class="control-label">题型</label>
				<div class="controls">
					<select class="input-small" name="type">
	                <c:forEach items="${questionTypeList}" var="item">
	                	 <option value="${item.value}" <c:if test="${item.value==examQuestion.type.value}">selected</c:if>>${item.name}</option>
	                </c:forEach>
	            </select>
				</div>
			</div>
	        <div class="control-group">
	            <label class="control-label">答案</label>
	            <div class="controls">
	                <input type="text" name="answer" htmlEscape="false" maxlength="10" class="required"
	                       value="${examQuestion.answer}"/>
	            </div>
	        </div>
			<div class="control-group">
				<label class="control-label">判分策略</label>
				<div class="controls">
					<select class="input-small" name="objectivePolicy">
		                <c:forEach items="${objectivePolicyList}" var="item">
		                	 <option value="${item.value}" <c:if test="${item.value==examQuestion.objectivePolicy.value}">selected</c:if>>${item.name}</option>
		                </c:forEach>
		            </select>
				</div>
			</div>
			<div id="customize-div" class="control-group">
				<label class="control-label"></label>
				<div class="controls customize-row">
					答对
					<input type="text" name="answerCount" htmlEscape="false" maxlength="10" class="customize-input"
	                       value=""/>
	                       	个给
	                <input type="text" name="answerScore" htmlEscape="false" maxlength="10" class="customize-input"
	                       value=""/>
	                       	分
	                <input type="button" class="edit-button" value="-" onclick="editRow(this)"/>
	                <input type="button" class="edit-button" value="+" onclick="editRow(this)"/>
				</div>
			</div>
		</div>
		<div class="form-actions">
			<input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>
			&nbsp;
			<a href="javascript:" onclick="history.go(-1);"  class="btn"/>返回</a>
		</div>
	</form>
</body>
</html>
