<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/taglib/c.tld" prefix="c"%>
<%@ include file="../common/init.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ include file="../common/top.jsp"%>
<link rel="stylesheet" type="text/css" href="${ctx}/static/subjecthead/style/exam.css">
<script type="text/javascript" src="${ctx}/static/subjecthead/js/bootstrapSwitch.js"></script>
<script type="text/javascript" src="${ctx}/static/subjecthead/js/jquery.PrintArea.js"></script>
</head>
<body class="easyui-layout">
	<%@ include file="../common/header.jsp"%>
	<%@ include file="../common/menu.jsp"%>
	<div data-options="region:'center',title:'首页 - 评卷员管理'">
		<div id="tt" class="easyui-tabs" data-options="fit:true">
			<div title="评卷员管理">
				<div id="marker-list-div" class="easyui-layout"
					data-options="fit:true,border:false">
					<div data-options="region:'north',border:false" class="domain">
						<form id="search-form" class="form-horizontal">
								<div id="markerListCondition">
									题块： <select name="subjectiveBlockId" class="span2">
										<c:forEach items="${sList}" var="subjectiveBlock">
											<option value="${subjectiveBlock.id}">${subjectiveBlock.title}</option>
										</c:forEach>
									</select> 
									状态： <select name="markStage" class="span2">
										<option value="-1" selected="selected">请选择</option>
										<option value="0">未开始</option>
										<option value="1">样卷</option>
										<option value="2">试评</option>
										<option value="3">正评</option>
									</select>
									<button class="btn">
										<i class="icon-search"></i>&nbsp;查询
									</button>
									<div class="fr">
										<button id="printButton1" class="btn" type="button">打印	</button>
										<button type="button" class="btn" onclick="addMarker()">增加</button>
										<c:if test="${!flag}"><button type="button" class="btn btn-danger" onclick="deleteMarkers()">删除</button></c:if>
										<div class="btn-group" data-toggle="buttons-checkbox">
										    <button type="button" class="btn" onclick="samplePaper()">样卷</button>
										    <button type="button" class="btn" onclick="tryPaper()">试评</button>
										    <button type="button" class="btn" onclick="formallyPaper()">正评</button>
									    </div>
									</div>
								</div>
						</form>
					</div>
					<div id="table1" data-options="region:'center'" style="border-color: #ccc; border-left: none;">
						<table class="easyui-datagrid" id="dg"></table>
					</div>
				</div>
				<div id="marker-add-div" class="span10 detail-cont-right" style="display: none">
					<div class="detail-cont-right-content">
						<div class="tab-content">
							<div class="tab-pane active change" id="manual-div">
								<form id="add-marker-form"
									action="${ctx}/subjectheader/marker/addmarkers"
									method="post">
									<input type="hidden" name="classTypeSubjectId"
										value="${classTypeSubjectId}" /> <input type="hidden"
										name="examId" value="${examId}" /> <input type="hidden"
										name="markerType" value="0" />
									<ul class="fl">
										<li><span>代码前缀</span> <input id="codePrefix"
											name="codePrefix" class="input-large" type="text"
											onblur="queryStartValue()" /></li>
										<li><span>代码起始值</span> <input id="startValue"
											name="startValue" class="input-large" type="text" /></li>
										<li><span>评卷员个数</span> <input name="number"
											class="input-large" type="text" /></li>
										<li><span>代码位数</span> <input name="bits"
											class="input-large" type="text" /></li>
										<li><span> <label class="radio fr mr-t1">
													<input type="radio" name="passwordType" id="optionsRadios1"
													value="1" checked>统一密码
											</label>
										</span> <input name="password" class="input-large" type="text"
											placeholder="输入密码" /></li>
										<li><span> <label class="radio fr mr-t1">
													<input type="radio" name="passwordType" id="optionsRadios0"
													value="0">随机密码
											</label>
										</span> <input name="passwordLen" class="input-large" type="text"
											placeholder="密码长度" /></li>
										<li><span>是否允许登录</span> <select name="status" class="span1">
												<option value="1">是</option>
												<option value="0">否</option>
										</select></li>
										<div class="controls ml-left">
											<a class="btn btn-primary" id="manual-submit-button">保存</a> <a
												class="btn return-button">返回</a>
										</div>
									</ul>
								</form>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div title="评卷员划分" style="overflow: auto; padding: 20px;"> 
				<div class="subjecthead-cont2">
					<div class="subjecthead-cont2title">
						<div class="subjecthead-list">
							<div class="subjecthead-listtitle">未分组评卷员</div>
							<div class="subjecthead-listcontent">
								<ul id="selectL">
									<c:forEach items="${mListNoBlock}" var="marker">
										<li><label class="checkbox"><input
												type="checkbox" value="${marker.id}"> ${marker.name}</label></li>
									</c:forEach>
								</ul>
							</div>
						</div>
						<div class="subjecthead-listbtn">
							<a href="#" id="toright"><img src="${ctx}/static/subjecthead/images/right.png"></a>
							<a href="#" id="toleft"><img src="${ctx}/static/subjecthead/images/left.png"></a>
							<a href="#" id="alltoright"><img src="${ctx}/static/subjecthead/images/all-right.png"></a>
							<a href="#" id="alltoleft"><img src="${ctx}/static/subjecthead/images/all-left.png"></a>
						</div>
						<div class="subjecthead-list">
							<div class="subjecthead-listtitle">已分组评卷员</div>
							<div class="subjecthead-listcontent">
								<ul id="selectR">
									<li><select id="blockId" name="blockId"
										onchange="showMarker()">
											<c:forEach items="${sList}" var="subjectiveBlock">
												<option value="${subjectiveBlock.id}">${subjectiveBlock.title}</option>
											</c:forEach>
									</select></li>
								</ul>
							</div>
						</div>
					</div>
					<div class="subjecthead-cont2foot">
						<button class="btn btn-primary saveMarker" type="button">保存</button>
						<button class="btn cancalButton" type="button">取消</button>
					</div>
				</div>
			</div>
			
			<div title="题组长指定" style="overflow: auto; padding: 20px;"> 
				<div class="subjecthead-cont2">
					<div class="subjecthead-cont2title">
						<div class="subjecthead-list">
							<div class="subjecthead-listtitle">评卷员列表</div>
							<div class="subjecthead-listcontent">
								<ul id="selectTopicMarker">
									<li><select id="topicBlockId" name="topicBlockId"
										onchange="showTopicMarker()">
											<c:forEach items="${sList}" var="subjectiveBlock">
												<option value="${subjectiveBlock.id}">${subjectiveBlock.title}</option>
											</c:forEach>
									</select></li>
								</ul>
							</div>
							<div class="subjecthead-cont2foot">
								<button class="btn btn-primary saveTopicMarker" type="button">保存</button>
							</div>
						</div>
					</div>
				</div>
			</div>
			
		</div>
	</div>
</body>
<script type="text/javascript">
	$(document).ready(function() {
		
		$('#dg').datagrid({
			url:'${ctx}/subjectheader/marker/pagelist.json',
			fit:true,
			border:false,
			fitColumns:true,
			striped:true,
			pagination:true,
			columns:[[{
				field:'id',
				width:'100',
				checkbox:true
			},{
				title:'姓名',
				field:'name',
				width:'150'
			},{
				title:'CODE',
				field:'code',
				width:'150'
			},{
				title:'密码',
				field:'password',
				width:"150"
			},{
				title:'状态',
				field:'markStage',
				width:'150',
				formatter:function(value, row, index) {
					var str = "";
					if (row.markStage == "WAITING") {
						str = "未开始"
					} else if (row.markStage == "EXAMPLE") {
						str = "样卷"
					} else if(row.markStage == "TEST"){
						str = "试评"
					} else if(row.markStage == "FORMAL"){
						str="正评"
					}
					return str;
				}
			},{
				title:'是否可登录',
				field:'status',
				width:'150',
				formatter:function(value,row,index){
					var str = "";
					if (row.status == 0) {
						str = "否"
					} else if(row.status == 1){
						str = "是"
					}
					return str;
				}
			},{
				title:'操作',
				field:'operate',
				width:'150',
				formatter:function(value, row, index) {
					if(${flag}){
						
					}else{
						return '<button class="btn btn-info" onclick="edit('
						+ row.id
						+ ',this)">修改</button>&nbsp;&nbsp;<button class="btn btn-danger" onclick="del('
						+ row.id + ')">删除</button>';
					}
		}
			}]]
		}); 
		
		$('#search-form').submit();
		showMarker();
		showNoBlockMarker();
		showTopicMarker();
		
		/*评卷员划分*/
		var leftSel = $("#selectL");
		var rightSel = $("#selectR");
		$("#toright").bind("click",function(){
			var subjectiveBlockId=$('#blockId').val();
			leftSel.find("input[type=checkbox]:checked").each(function(){
				$(this).parent().parent().remove().appendTo(rightSel);
				//$.post('${ctx}/subjectheader/marker/add',{subjectiveBlockId:subjectiveBlockId,markerId:this.value},function(res){});
			});
		});
		$("#toleft").bind("click",function(){		
			rightSel.find("input[type=checkbox]:checked").each(function(){
				$(this).parent().parent().remove().appendTo(leftSel);
				//$.post('${ctx}/subjectheader/marker/remove',{markerId:this.value},function(){});
			});
		});
		
		$('#alltoright').bind("click",function(){
			var subjectiveBlockId=$('#blockId').val();
			leftSel.find("li").each(function(){
				$(this).remove().appendTo(rightSel);
				var markerId=$(this).find("input").val();
				//$.post('${ctx}/subjectheader/marker/add',{subjectiveBlockId:subjectiveBlockId,markerId:markerId},function(res){});
			});
		});
		
		$('#alltoleft').bind("click",function(){
			var subjectiveBlockId=$('#blockId').val();
			rightSel.find("li:gt(0)").each(function(){
				$(this).remove().appendTo(leftSel);
				var markerId=$(this).find("input").val();
				//$.post('${ctx}/subjectheader/marker/remove',{markerId:markerId},function(){});
			});
		});
		
		$('.saveMarker').click(function(){
			var saveIds=new Array();
			var removeIds=new Array();
			//var topicMarkerIds=new Array();
			var subjectiveBlockId=$('#blockId').val();
			rightSel.find("li:gt(0)").each(function(index, obj){
				saveIds.push($(this).find("input").val());
			});
			/* rightSel.find("input[name=topicMarker]:checked").each(function(index, obj){
				topicMarkerIds.push($(this).val());//题组长id
			}); */
			leftSel.find("li").each(function(index, obj){
				removeIds.push($(this).find("input").val());
			});
			//alert(saveIds);
			//alert(topicMarkerIds);
			/* if(topicMarkerIds.length==0){
				topicMarkerIds.push(0);
			} */
			//alert(topicMarkerIds);
 			saveMarker(subjectiveBlockId,saveIds);
 			//saveTopicMarker(subjectiveBlockId,topicMarkerIds);
 			removeMarker(removeIds);
 			$('.subjecthead-cont2title').find('input[type=checkbox]:checked').removeAttr("checked");
 			alert("保存成功");
		});
		
		
		var topicSel = $("#selectTopicMarker");
		$('.saveTopicMarker').click(function(){
			var topicMarkerIds=new Array();
			var subjectiveBlockId=$('#topicBlockId').val();
			topicSel.find("input[type=checkbox]:checked").each(function(index, obj){
				topicMarkerIds.push($(this).val());
			});
			if(topicMarkerIds.length==0){
				topicMarkerIds.push(0);
			} 
 			saveTopicMarker(subjectiveBlockId,topicMarkerIds);
 			alert("保存成功");
		});
		
		$('.cancalButton').click(function(){
			showNoBlockMarker();
			showMarker();
		})
		
		$('.return-button').click(function(){
			$('#marker-list-div').show();
			$('#marker-add-div').hide();
		});
		$('#manual-submit-button').click(function(){
			var passwordType = $('#add-marker-form :input[name=passwordType]:checked').val();
			var password = $('#add-marker-form :input[name=password]').val();
			var passwordLength = $('#add-marker-form :input[name=passwordLen]').val();
			if(passwordType==1 && password==''){
				alert("统一密码不能为空");
				return false;
			}
			if(passwordType==0 && (passwordLength=='' || passwordLength<1)){
				alert("随机密码长度不能小于1");
				return false;
			}
			$('#add-marker-form').submit();
		});
	});
	
	$("#printButton1").click(function() {
		$("#table1").printArea();
	});

	function showMarker(){
		var val=new Array();
		$('#selectL').find('input').each(function(){
			var id=$(this).val();
			val.push(id);
		});
		var subjectiveBlockId=$('#blockId').val();
		var selectR=$('#selectR');
		$('#selectR').find('li:gt(0)').remove();
		$.post('${ctx}/subjectheader/marker/block',{subjectiveBlockId:subjectiveBlockId},function(res){
			$.each(res,function(index,obj){
				var option='<li><label class="checkbox"><input type="checkbox" value="'+obj.id+'"> '+obj.name+'</label></li>';
				var flag=true;
				for(var i=0;i<val.length;i++){
					if(obj.id==val[i]){
						flag=false;
					}
				}
				if(flag){
					selectR.append(option);
				}
			});
		});
	}
	function showNoBlockMarker(){
		var selectL=$('#selectL');
		$('#selectL').find('li').remove();
		$.post('${ctx}/subjectheader/marker/noblock',function(res){
			$.each(res,function(index,obj){
				var option='<li><label class="checkbox"><input type="checkbox" value="'+obj.id+'"> '+obj.name+'</label></li>';
				selectL.append(option);
			});
		});
	}
	
	function showTopicMarker(){
		var val=new Array();
		var subjectiveBlockId=$('#topicBlockId').val();
		var topicSel = $("#selectTopicMarker");
		$('#selectTopicMarker').find('li:gt(0)').remove();
		$.post('${ctx}/subjectheader/marker/block',{subjectiveBlockId:subjectiveBlockId},function(res){
			$.each(res,function(index,obj){
				var option;
				if(obj.isTrue==true){
					option='<li><label class="checkbox"><input type="checkbox" checked="checked" value="'+obj.id+'"> '+obj.name+'</label></li>';
				}else{
					option='<li><label class="checkbox"><input type="checkbox" value="'+obj.id+'"> '+obj.name+'</label></li>';
				}
				topicSel.append(option);
			});
		});
	}
	
	function saveMarker(subjectiveBlockId,ids){
		if(ids.length>0){
			$.post('${ctx}/subjectheader/marker/add',{subjectiveBlockId:subjectiveBlockId,markerIds:ids.join(',')},function(res){});
		}
	}
	
	function saveTopicMarker(subjectiveBlockId,ids){
		if(ids.length>0){
			$.post('${ctx}/subjectheader/marker/addtopicmarkers',{subjectiveBlockId:subjectiveBlockId,topicMarkerIds:ids.join(',')},function(res){});
		}
	}
	
	function removeMarker(ids){
		if(ids.length>0){
			$.post('${ctx}/subjectheader/marker/remove',{markerIds:ids.join(',')},function(res){});
		}
	}
	
	function edit(markerId,obj) {
		var td = $(obj).parent().parent().parent().find("div");

		var str = $(td[2]).html();
		$(td[2]).html("<input type='text' value="+str+">");
		
		var str = $(td[3]).html();
		$(td[3]).html("<input type='text' value="+str+">");
		
		var str = $(td[4]).html();
		var sel='<select name="markStage" style="width:100px;"><option value="0"';
		if(str=='未开始'){
			sel=sel+' selected="selected"';
		}
		sel=sel+'>未开始</option><option value="1"';
		if(str=='样卷'){
			sel=sel+' selected="selected"';
		}
		sel=sel+'>样卷</option><option value="2"';
		if(str=='试评'){
			sel=sel+' selected="selected"';
		}
		sel=sel+'>试评</option><option value="3"';
		if(str=='正评'){
			sel=sel+' selected="selected"';
		}
		sel=sel+'>正评</option></select>';
		$(td[4]).html(sel);
		
		var str = $(td[5]).html();
		var sel='<select name="status" style="width:100px;"><option value="0"';
		if(str=='否'){
			sel=sel+' selected="selected"';
		}
		sel=sel+'>否</option><option value="1"';
		if(str=='是'){
			sel=sel+' selected="selected"';
		}
		sel=sel+'>是</option></select>';
		$(td[5]).html(sel);

		var btn=$(td[6]).find("button");
		$(td[6]).html('');
		var btnStr='<div style="height:auto;" class="datagrid-cell datagrid-cell-c1-operate">'
			+'<button class="btn btn-info" onclick="saveM('
			+markerId+',this)">保存</button>&nbsp;&nbsp;'
			+'<button class="btn btn-danger" onclick="cancelSave()">取消</button></div>';
		$(td[6]).append(btnStr);
	}
	function del(markerId) {
		$.messager.confirm('删除', '确定删除这个评卷员吗?', function(r) {
			if (r) {
				$.post('${ctx}/subjectheader/marker/delete',{markerId:markerId},function(obj){
					if(obj.success){
						$.messager.confirm('信息','删除成功!');
						$('#dg').datagrid('load');
					}
				});
			}
		});
	}
	
	function saveM(markerId,obj){
		var td=$(obj).parent().parent().parent().parent().find('div');
		var name=$(td[2]).find('input').val();
		var password=$(td[3]).find('input').val();
		var markStage=$(td[4]).find('select').val();
		var status=$(td[5]).find('select').val();
		$.post('${ctx}/subjectheader/marker/save',{id:markerId,name:name,password:password,status:status,markStageValue:markStage},function(obj){
			if(obj.success){
				alert("修改成功");
				$('#dg').datagrid('load');
			}else{
				alert(obj.message);
			}
			
		});
	}
	
	function cancelSave(){
		$('#dg').datagrid('load');
	}

	$('#search-form').submit(function(event) {
		event.preventDefault();
		var query = $('#search-form').serializeJson();
		$('#dg').datagrid('load', query);
	});
	
	function queryStartValue(){
		var codePrefix=$('#codePrefix').val();
		$.post('${ctx}/subjectheader/marker/query-start-value.json',{codePrefix:codePrefix, subjectHeaderId: '${subjectHeaderId}'},function(res){
			$("#startValue").val(res+1);
		});
	}
	
	function addMarker(){
		$('#marker-list-div').hide();
		$('#marker-add-div').show();
	}
	
	function deleteMarkers(){
		var rows=$('#dg').datagrid('getSelections');
		if(rows.length>0){
			$.messager.confirm('请确认','确定删除这些评卷员吗?',function(r){
				if(r){
					var ids=[];
					for(var i=0;i<rows.length;i++){
						ids.push(rows[i].id);
					}
					$.post('${ctx}/subjectheader/marker/multi-delete',{markerIds:ids.join(',')},function(obj){
						if(obj.success){
							$.messager.confirm('信息','删除成功!');
							$('#dg').datagrid('load');
						}
					});
				}
			});
		}else{
			$.messager.alert('提示','请选择要删除的评卷员!','error');
		}
	}
	
	function samplePaper(){
		var rows=$('#dg').datagrid('getSelections');
		if(rows.length>0){
			$.messager.confirm('请确认','确定修改这些评卷员的状态吗?',function(r){
				if(r){
					var ids=[];
					for(var i=0;i<rows.length;i++){
						ids.push(rows[i].id);
					}
					$.post('${ctx}/subjectheader/marker/editStatus',{markerIds:ids.join(','),markStage:1},function(obj){
						if(obj.success){
							$.messager.confirm('信息','修改成功!');
							$('#dg').datagrid('load');
						}
					});
				}
			});
		}else{
			$.messager.alert('提示','请选择要修改的评卷员!','error');
		}
	}
	
	function tryPaper(){
		var rows=$('#dg').datagrid('getSelections');
		if(rows.length>0){
			$.messager.confirm('请确认','确定修改这些评卷员的状态吗?',function(r){
				if(r){
					var ids=[];
					for(var i=0;i<rows.length;i++){
						ids.push(rows[i].id);
					}
					$.post('${ctx}/subjectheader/marker/editStatus',{markerIds:ids.join(','),markStage:2},function(obj){
						if(obj.success){
							$.messager.confirm('信息','修改成功!');
							$('#dg').datagrid('load');
						}
					});
				}
			});
		}else{
			$.messager.alert('提示','请选择要修改的评卷员!','error');
		}
	}
	
	function formallyPaper(){
		var rows=$('#dg').datagrid('getSelections');
		if(rows.length>0){
			$.messager.confirm('请确认','确定修改这些评卷员的状态吗?',function(r){
				if(r){
					var ids=[];
					for(var i=0;i<rows.length;i++){
						ids.push(rows[i].id);
					}
					$.post('${ctx}/subjectheader/marker/editStatus',{markerIds:ids.join(','),markStage:3},function(obj){
						if(obj.success){
							$.messager.confirm('信息','修改成功!');
							$('#dg').datagrid('load');
						}
					});
				}
			});
		}else{
			$.messager.alert('提示','请选择要修改的评卷员!','error');
		}
	}
	
</script>
</html>