<%@ 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" %>
	<%@include file="/WEB-INF/views/include/dialog.jsp" %>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/admin/exam-param/marker">评卷员列表</a></li>
		<li><a href="${ctx}/admin/exam-param/marker/form">添加评卷员</a></li>
		<li class="active"><a href="${ctx}/admin/exam-param/marker/assign">分配评卷员</a></li>
	</ul>
	<tags:message content="${message}"/>
	<form id="searchForm"  action="${ctx}/admin/exam-param/marker/assigned" method="post" class="breadcrumb form-search">
		<div>
			<label>科目：</label>
			<select id="subjectCode" name="subjectCode" class="" onchange="loadBlock()" style="width: 280px;">
				<option value="">请选择...</option>
				<c:forEach items="${subjectList }" var="subject">
					<option value="${subject.code }" <c:if test="${subject.code eq subjectCode }">selected</c:if>>${subject.code}-${subject.name }</option>
				</c:forEach>
			</select>
			<label>大题：</label>
			<span id="selectDiv"></span>
			&nbsp;<input id="btnSubmit" class="btn btn-primary" type="button" value="查询" onclick="goSearch()"/>
		</div>
	</form>
	<ul class="nav nav-tabs">
		<li><a href="javascript:assigned();">已分配列表</a></li>
		<li class="active"><a href="javascript:toAssign();">未分配列表</a></li>
	</ul>
	<span id="assignButton" style="display: none; float: left;margin:0 10px 10px 0 "><button class="btn btn-primary" onclick="distributions()">分配</button></span>
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead>
			<tr>
				<th width="5%">选择</th>
				<th>评卷员</th>
				<th>代码</th>
				<th>密码</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		<c:forEach items="${markerList}" var="obj">
			<tr>
				<td><input type="checkbox" id="markerIds" name="markerIds" onclick="assigns()" value="${obj.id }"/></td>
				<td>${obj.name }</td>
				<td>${obj.code }</td>
				<td>${obj.password }</td>
				<td>
					<a href="javascript:void(0);" onclick="distribution(${obj.id })">分配</a>
				</td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<div class="pagination">${query}</div>
<script type="text/javascript">
var code = '${subjectCode}';
if(code.length>0){
	loadBlock();
}

function loadBlock(){
	var subjectCode = $("#subjectCode").val();
	var data = {
		subjectCode : subjectCode
	}
	var blockId = '${blockId}';
	var selectDiv = "<select id='blockId' name='blockId' class='input-small'>";
	selectDiv += "<option value=''>请选择...</option>";
	 $.post("${ctx}/admin/exam-param/marker/loadoption",data,function(result){
		 $(result).each(function(i,obj){
			 if(blockId.length>0){
				 if(blockId==obj.id){
					 selectDiv += "<option value='"+obj.id+"' selected>"+obj.title+"</option>";
				 }else{
					 selectDiv += "<option value='"+obj.id+"'>"+obj.title+"</option>";
				 }
			 }else{
				 selectDiv += "<option value='"+obj.id+"'>"+obj.title+"</option>";
			 }
		})
		selectDiv +="</select>";
		$("#selectDiv").html("");
		$("#selectDiv").html(selectDiv);
	  });
	 
}

function goSearch(){
	var subjectCode = $("#subjectCode").val();
	var blockId = $("#blockId").val();
	if(subjectCode.length==0){
		$.jBox.info("请先选择科目及大题，然后再查询");
		return false;
	}
	if(blockId.length==0){
		$.jBox.info("请先选择科目及大题，然后再查询");
		return false;
	}
	$("#searchForm").submit();
}
function assigned(){
	var subjectCode = $("#subjectCode").val();
	var blockId = $("#blockId").val();
	if(subjectCode.length==0){
		$.jBox.info("请先选择科目及大题，然后再查询");
		return false;
	}
	if(blockId.length==0){
		$.jBox.info("请先选择科目及大题，然后再查询");
		return false;
	}
	$("#searchForm").attr("action","${ctx}/admin/exam-param/marker/assigned");
	$("#searchForm").submit();
}
function toAssign(){
	var subjectCode = $("#subjectCode").val();
	var blockId = $("#blockId").val();
	if(subjectCode.length==0){
		$.jBox.info("请先选择科目及大题，然后再查询");
		return false;
	}
	if(blockId.length==0){
		$.jBox.info("请先选择科目及大题，然后再查询");
		return false;
	}
	$("#searchForm").attr("action","${ctx}/admin/exam-param/marker/toassign");
	$("#searchForm").submit();
}

function distribution(markerId){
	var blockId = $("#blockId").val();
	var subjectCode = $("#subjectCode").val();
	var url = '${ctx}/admin/exam-param/marker/distribution';
	var data = {
		blockId : blockId,
		markerId : markerId,
		subjectCode : subjectCode
	}
	$.post(url,data,function(result){
		if(result==1){
    		$.jBox.info("分配成功！");
            $("#searchForm").attr("action","${ctx}/admin/exam-param/marker/toassign");
           	$("#searchForm").submit();
    	}else{
    		$.jBox.info("分配失败！");
    	}
	  });
}

function assigns(){
	if($('input[name="markerIds"]:checked').length>0){
		$("#assignButton").show();
	}else{
		$("#assignButton").hide();
	}
	
}

function distributions(){
	var markerIds = "";
	$('input[name="markerIds"]:checked').each(function(){
		markerIds += $(this).val()+";";
	})
	markerIds = markerIds.substring(0,markerIds.length-1);
	var blockId = $("#blockId").val();
	var subjectCode = $("#subjectCode").val();
	var url = '${ctx}/admin/exam-param/marker/distributions';
	var data = {
		blockId : blockId,
		markerIds : markerIds,
		subjectCode : subjectCode
	}
	$.post(url,data,function(result){
		if(result==1){
    		$.jBox.info("分配成功！");
            $("#searchForm").attr("action","${ctx}/admin/exam-param/marker/toassign");
           	$("#searchForm").submit();
    	}else{
    		$.jBox.info("分配失败！");
    	}
	  });
}

</script>	
</body>
</html>