<%@ 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" %>
	<style>
		.my-modal{
			width:450px;
		}
	</style>
	<script type="text/javascript">
	var mainN = 0;
	//显示modal框
	function showModal(val){
		var htmlval = "";
		if(val == '1'){
			htmlval = "<input type=\"hidden\" id=\"objective\" value='1' name=\"objective\"/>"
				+"<label>客观题型:</label>&nbsp;&nbsp;&nbsp;"
				+"<select class=\"input-samll\" style=\"width:180px;\" name=\"questionTypeName\" id=\"questionTypeName\" >"
				+"<option value=\"\" >请选择</option>"					
				+"<option value=\"单选\" >单选</option>"	
				+"<option value=\"多选\" >多选</option>"	
				+"<option value=\"判断\" >判断</option>"	
				+"</select>";
			
		}else{
			htmlval = "<input type=\"hidden\" id=\"objective\" value='0' name=\"objective\"/>"
				+"<label>主观题型:</label>&nbsp;&nbsp;&nbsp;"
				+"<input type=\"text\" style=\"width:170px;\" class=\"form-control\" id=\"questionTypeName\" name=\"questionTypeName\" />";
			$("#totalCount").attr("readonly",true);
			//主观题默认总数为30
			$("#totalCount").val(30);
		}
		$("#questionTypeContent").html(htmlval);
		$("#myModal").modal('show');
	}
	//修改
	function editObjtiveType(id,questionTypeName,mainNumber,totalCount,count,totalScore,intervalScore,objective){
			mainN = mainNumber;
			if(objective=='1'){
				htmlval = "<input type=\"hidden\" id=\"objective\" value='1' name=\"objective\"/>";
			}else{
				htmlval = "<input type=\"hidden\" id=\"objective\" value='0' name=\"objective\"/>";
				$("#totalCount").attr("readonly",true);
			}
			 htmlval += "<label>主观题型:</label>&nbsp;&nbsp;&nbsp;"
			+"<input type=\"text\" style=\"width:170px;\" class=\"form-control\" id=\"questionTypeName\" name=\"questionTypeName\" />";
			$("#questionTypeContent").html(htmlval);
			$("#myModal").modal('show');
			$("#id").val(id);
			$("#questionTypeName").val(questionTypeName).trigger("change");
			$("#questionTypeName").attr("readonly",true);
			$("#mainNumber").attr("readonly",true);
			$("#mainNumber").val(mainNumber);
			$("#totalCount").val(totalCount);
			$("#count").val(count);
			$("#totalScore").val(totalScore);
			$("#intervalScore").val(intervalScore);
	}
	//删除
	function deleteObjtiveType(id,subjectCode,objective){
		(objective == '1')?objective = true:objective=false;
		 bootbox.confirm({ 
			  className:"my-modal",
			  message: "确定删除该题型吗?", 
			  buttons: {
			        confirm: {
			            label: 'Yes',
			            className: 'btn-success'
			        },
			        cancel: {
			            label: 'No',
			            className: 'btn-danger'
			        }
			    },
			  callback: function(result){
					  if(result){
						  $.post('${ctx}/admin/exam-param/subjectPaper/delete',{id:id,objective:objective},function(obj){
								if(obj.success){
									window.location.href = "${ctx}/admin/exam-param/subjectPaper/examQuestionTypeEdit?subjectCode=" + subjectCode;
								}
							});
					  }
			  }
			})
	}
	</script>
</head>
<body>
	<div class="breadcrumb form-search" >
		<input type="hidden" id="pageNumber" name="pageNumber" value="${query.pageNumber }"/>
		<input type="hidden" id="pageSize" name="pageSize" value="${query.pageSize }"/>
		<div>
			<label>科目</label>
			&nbsp;&nbsp;&nbsp;<input maxlength="200" value="${subjectCode}-${subjectName}" readonly/>
			<button class="btn btn-primary btn-lg" onclick="showModal('1');">客观题型</button>
			<button class="btn btn-primary btn-lg" onclick="showModal('0');">主观题型</button>
			<a href="${ctx}/admin/exam-param/subjectPaper" style="float:right;" class="btn btn-primary">返回</a>
		</div>
		
		<!--客观题modal框 -->
		<form method="post" action="${ctx}/admin/exam-param/subjectPaper/save" class="form-horizontal" id="myForm">
		<input type="hidden" id="code" name="code" value="${subjectCode}"/>
		<input type="hidden" id="id" name="id"/>
		<div class="modal hide fade in" id="myModal" style="width: 720px" role="dialog"  aria-labelledby="myModalLabel" aria-hidden="true">
		    <div class="modal-dialog">
		        <div class="modal-content">
			            <div class="modal-header">
			                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
			                <h4 class="modal-title" id="myModalLabel">客观(主观)题型</h4>
			            </div>
			            <div class="modal-body" style="height:120px;">
			            <form class="form-horizontal">
				            	<div class="form-group" id="questionTypeContent" style="margin-top:10px;float:left">
					            </div>
					            <div class="form-group" style="margin-top:10px;margin-left:320px;">
					                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>大题号:</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					                <input type="text" style="width:100px;" class="form-control" id="mainNumber" name="mainNumber" />
					            </div>
					            <div class="form-group" style="margin-top:10px;float:left">
					                &nbsp;&nbsp;&nbsp;&nbsp;<label>总个数:</label>&nbsp;
					                <input type="text" style="width:170px;" class="form-control" id="totalCount" name="totalCount" />
					            </div>
					            <div class="form-group" style="margin-top:10px;margin-left:320px;">
					                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>有效个数:</label>&nbsp;
					                <input type="text" style="width:100px;" class="form-control" id="count" name="count" />
					            </div>
					            <div class="form-group" style="margin-top:10px;float:left">
					                &nbsp;&nbsp;&nbsp;&nbsp;<label>总分:</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					                <input type="text" style="width:170px;" class="form-control" id="totalScore" name="totalScore"/>
					            </div>
					            <div class="form-group" style="margin-top:10px;margin-left:320px;">
					                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>间 隔 分:</label>&nbsp;&nbsp;
					                <input type="text" style="width:100px;" class="form-control" id="intervalScore" name="intervalScore" />
					            </div>
					    </form>
						</div>
				        <div class="modal-footer">
								 <button type="submit" class="btn btn-info">保存</button>&nbsp;
					             <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				        </div>
				    </div>
				   </div>
				</div>				   
		</form>
		<!-- end -->
	</div>
		<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead>
			<tr>
				<th>题型</th>
				<th>个数</th>
				<th>总分</th>
				<th>间隔分</th>
				<th>操作</th>
				<th>详细配置</th>
			</tr>
		</thead>
		<tbody>
		<c:forEach items="${query.result}" var="subjectPaper">
			<tr>
				<td>
					${subjectPaper.questionTypeName}
				</td>
				<td>${subjectPaper.count}</td>
				<td>${subjectPaper.totalScore}</td>
				<td>${subjectPaper.intervalScore}</td>
				<td>
					<a onclick="editObjtiveType('${subjectPaper.id}','${subjectPaper.questionTypeName}','${subjectPaper.mainNumber}','${subjectPaper.totalCount}','${subjectPaper.count}','${subjectPaper.totalScore}','${subjectPaper.intervalScore}','${subjectPaper.objective}');">修改</a>&nbsp;
					<a onclick="deleteObjtiveType('${subjectPaper.id}','${subjectCode}','${subjectPaper.objective}')">删除</a></td>
				<td><a href="${ctx}/admin/exam-param/subjectPaper/editPaperDetail?id=${subjectPaper.id}&subjectCode=${subjectCode}&objective=${subjectPaper.objective}" class="btn btn-primary">编辑</a></td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<div class="pagination">${query}</div>
			
<script type="text/javascript">
	$(document).ready(function() {
		//当模态框隐藏后重置模态框中内容
		jQuery.validator.addMethod("validationCount", function(value, element) { 
			var countVal = $("#totalCount").val();
		    return this.optional(element) || (eval(value) <= eval(countVal));
		}, "有效数小于总数");
		jQuery.validator.addMethod("validationMainNumber", function(value, element) { 
			var mainNumber = $("#mainNumber").val();
			var objective = $("#objective").val();//1:客观题 ；0：主观题
			var objectiveNumber = '${objectiveNumber}';
			var subjectiveNumber = '${subjectiveNumber}';
			var a = objective == '1'?'${objectiveNumber}':'${subjectiveNumber}';
			var edit = $("#questionTypeName").attr("readonly");
			var arr = a.split(";");
			var flag =true;
			for(var i=0;i<arr.length;i++){
				if(arr[i]==mainNumber){
					flag = false;
					break;
				}
			}
			if(edit && mainN == mainNumber){//表示修改的情况下除去旧的大题号
				flag = true;
			}
		    return this.optional(element) || flag;
		}, "已存在！");
		
		var validator = $("#myForm").validate({
	        rules: {
			   questionTypeName:{
				   required: true
			   },
			   	mainNumber: {
				    required: true,
				    digits:true,
				    validationMainNumber:true
			   },
			   totalCount: {
				    required: true,
				    digits:true
			   },
			   count: {
				    required: true,
				    digits:true,
				    validationCount:true
			   },
			   totalScore: {
				    required: true,
				    number:true,
				    min:1
			   },
			   intervalScore: {
				    required: true,
				    number:true
			   }
			  },
			  messages: {
				  questionTypeName: "请输入题型",
				  mainNumber: {
				    required: "请输入大题号",
				    digits: "请输入整数"
				   },
				   totalCount: {
					   required: "请输入总数",
					    digits: "请输入整数"
				   },
				   count: {
					   required: "请输入有效个数",
					    digits: "请输入整数"
				   },
				   totalScore: {
					   required: "请输入总分",
					   number: "请输入合法的数字",
					   min:"分数不能小于1"
				   },
				   intervalScore: {
					   required: "请输入间隔分",
					   number: "请输入合法的数字"
				   }
				  }
	    });
		$('#myModal').on('hidden.bs.modal', function () {
			validator.resetForm();
			$("#questionTypeName").val('').trigger("change");
			$("#questionTypeName").attr("readonly",false);
			$("#totalCount").attr("readonly",false);
			$("#mainNumber").attr("readonly",false);
			$("#mainNumber").val('');
			$("#totalCount").val('');
			$("#count").val('');
			$("#totalScore").val('');
			$("#intervalScore").val('');
			});
	});
</script>	
</body>
</html>
