<%@ 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"%>
</head>
<body class="easyui-layout">
	<%@ include file="../../common/header.jsp"%>
	<%@ include file="../../leftMenu/exam-admin.jsp"%>
	<div data-options="region:'center',title:'首页 - 【考试管理】 - 评卷员管理'">
		<div class="easyui-layout" data-options="fit:true,border:false">
			<div data-options="region:'north',border:false" class="domain">
				<%-- <%@ include file="../../common/message.jsp"%> --%>
				<c:if test="${message!=null}">
					<div class="curr-info alert alert-error ">
						${message}
					</div>
				</c:if>
				<div id="marker-search-div">
					<form class="form-inline" action="${ctx}/admin/exam-param/marker" method="post" id="search-form">
						<%@ include file="../../common/subjectClassTypeSelect.jsp"%>
						<button type="submit" class="btn">
							<i class="icon-search"></i> 查询
						</button>
						<c:if test="${isFinish==false }">
						<a class="btn btn-success" id="add-button"><i class="icon-plus icon-white"></i> 添加 </a>
						</c:if>
					</form>
				</div>
			</div>

			<div class="border" data-options="region:'center'">
				<div class="table-container col6" id="marker-list-div">
					<div class="tab-header">
						<em class="list-num"><input type="checkbox" /></em> 
						<i class="list-text"><strong>登录名</strong></i> 
						<i class="list-text"><strong>姓名</strong></i> 
						<i class="list-text"><strong>密码</strong></i> 
						<i class="list-text"><strong>是否可登录</strong></i>
						<p class="list-edit">
							<strong>操作</strong>
						</p>
					</div>
					<c:if test="${markerList!=null}">
						<c:forEach var="marker" items="${markerList}">
							<div class="tab-list marker-list" data-value="${marker.id}">
								<em class="list-num"><input class="marker-checkbox" type="checkbox"> </em> 
								<i class="list-text code-text">${marker.code}</i> 
								<i class="list-text name-text">${marker.name}</i>
								<i class="list-text password-text">${marker.password}</i> 
								<i class="list-text status-text" data-value="${marker.status}"> 
									<c:if test="${marker.status==1}">是</c:if> 
									<c:if test="${marker.status==0}">否</c:if>
								</i>
								<p class="list-edit">
									<c:if test="${isMarking==false && isFinish==false}">
									<a class="btn btn-small btn-primary edit-button">编辑</a> 
									<a class="btn btn-small btn-danger delete-button">删除</a>
									</c:if>
								</p>
							</div>
							<div class="tab-edit marker-edit-list" data-value="${marker.id}" style="display: none">
								<em class="list-num-no"></em> 
								<i class="list-text">
									<input class="input-small uneditable-input code-input" type="text" value="${marker.code}" disabled />
								</i> 
								<i class="list-text">
									<input class="input-small name-input" type="text"> 
								</i> 
								<i class="list-text">
									<input class="input-small password-input" type="text"> 
								</i> 
								<i class="list-text">
									<select class="span1 status-select">
            							<option value="1">是</option>
            							<option value="0">否</option>
            						</select> 
								</i>
								<p class="list-edit">
									<a class="btn btn-small btn-success save-button">保存</a> 
									<a class="btn btn-small cancel-button">取消</a>
								</p>
							</div>
						</c:forEach>
					</c:if>
				</div>

				<div id="marker-add-div" style="display:none">
					<ul class="nav nav-tabs">
  						<li class="active"><a href="#manual-div" data-toggle="tab">批量创建</a></li>
  						<li><a href="#import-div" data-toggle="tab">EXCEL导入</a></li>
					</ul>
					<div class="tab-content">
						<div class="tab-pane active change" id="manual-div">
							<form id="add-marker-form" action="${ctx}/admin/exam-param/marker/addmarkers" method="post">
								<ul class="fl">
									<%@ include file="../../common/subjectClassTypeSelect3.jsp"%>
									<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">
											<option value="1">是</option>
											<option value="0">否</option>
										</select>
									</li>
								</ul>
								<div class="control-group mr-t mr-r">
									<div class="controls">
										<a class="btn btn-primary" id="manual-submit-button">保存</a> 
										<a class="btn return-button">返回</a>
									</div>
								</div>
							</form>
						</div>
						<div class="tab-pane leading-in" id="import-div">
							<form id="excel-import-form" class="form-horizontal" action="${ctx}/admin/exam-param/marker/excel-import" method="post" enctype="multipart/form-data">
								<div class="control-group">
									<label class="control-label" for="">模板文件</label>
									<div class="controls">
										<a class="btn btn-success" type="button" href="${properties['download.server']}/marker.xls" target="_blank">
											<i class="icon-arrow-down icon-white"></i>下载
										</a>
									</div>
								</div>
								<div class="control-group">
									<label class="control-label" for="">上传文件</label>
									<div class="controls">
										<input name="sourceFile" type="file"/>
									</div>
								</div>
								<div class="controls">
									<a class="btn btn-primary submit-button">提交</a>
									<a class="btn" href="${ctx}/admin/exam-param/marker">返回</a>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
<script type="text/javascript">
$(document).ready(function() {
	$('.nav-tabs a').click(function (e) {
		e.preventDefault();
		$(this).tab('show');
	});
	$('.delete-button').click(function(){
		del($(this).parent().parent().attr('data-value'));
	});
	$('.edit-button').live("click", function(){
		var markerDiv = $(this).parent().parent();
		var marker = {
				id: markerDiv.attr('data-value'),
				name: markerDiv.find('.name-text').html(),
				password: markerDiv.find('.password-text').html(),
				status: markerDiv.find('.status-text').attr('data-value')
		};
		var editDiv = $('.marker-edit-list[data-value="'+marker.id+'"]');
		editDiv.find('.name-input').val(marker.name);
		editDiv.find('.password-input').val(marker.password);
		editDiv.find('.status-select').val(marker.status);
		markerDiv.hide();
		editDiv.show();
	});
	$('.save-button').live("click", function(){
		var editDiv = $(this).parent().parent();
		var marker = {
			id: editDiv.attr('data-value'),
			name: editDiv.find('.name-input').val(),
			password: editDiv.find('.password-input').val(),
			status: editDiv.find('.status-select').val()
		}
		if(marker.password==''){
			alert("评卷员密码不能为空！");
			return;
		}
		$.post('${ctx}/admin/exam-param/marker/save.json', marker, function(obj){
			if(obj.success){
				alert("保存成功");
				var markerDiv = $('.marker-list[data-value="'+marker.id+'"]');
				markerDiv.find('.name-text').html(marker.name);
				markerDiv.find('.password-text').html(marker.password);
				markerDiv.find('.status-text').html(marker.status==1?'是':'否');
				markerDiv.find('.status-text').attr('data-value', marker.status);
				editDiv.hide();
				markerDiv.show();
			}else{
				alert(obj.message);
			}
		});	
	});
	$('.cancel-button').live("click", function(){
		var id=$(this).parent().parent().attr('data-value');
		$('.marker-list[data-value="'+id+'"]').show();
		$(this).parent().parent().hide();
	});
	$('#add-button').click(function(){
		initAddDiv();
		$('#marker-list-div').hide();
		$('#marker-search-div').hide();
		$('#marker-add-div').show();
	});
	$('.return-button').click(function(){
		$('#marker-list-div').show();
		$('#marker-search-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();
	});
	$('#multi-checkbox').click(function(){
		if($(this).attr('checked')){
			$('.marker-list :checkbox').each(function(index, obj){
				$(obj).attr("checked","true");
			});
		}else{
			$('.marker-list :checkbox').each(function(index, obj){
				$(obj).removeAttr("checked");
			});
		}
		toggleMultiControl();
	});
	$('.marker-list :checkbox').click(toggleMultiControl);
	$('#multi-delete-button').click(function(){
		var ids = new Array();
		$('.marker-list input:checked').each(function(index, obj){
			ids.push($(obj).parent().parent().attr('data-value'));
		});
		multiDel(ids);
	});
	$('#excel-import-form .submit-button').click(function(){
		var file = $('#excel-import-form input[name="sourceFile"]').val();
		if(file==undefined || file==''){
			alert("请选择上传文件");
			return;
		}
		if(!file.endWith('xls')){
			alert("上传文件扩展名不是xls");
			return;
		}
		$('#excel-import-form').submit();
	});
});

function toggleMultiControl(){
	if($('.marker-list input:checked').length>0){
		$('.multi-control').show();
	}else{
		$('.multi-control').hide();
	}
}

function del(markerId) {
	if (confirm('确定删除这个评卷员吗？')) {
		$.post('${ctx}/admin/exam-param/marker/delete.json', {markerId: markerId}, function(obj){
			if(obj.success){
				$('.marker-list[data-value="'+markerId+'"]').remove();
				$('.marker-edit-list[data-value="'+markerId+'"]').remove();
				alert("删除成功");
			}else{
				alert(obj.message);
			}
		});
	}
}

function multiDel(markerIds) {
	if(markerIds.length>0 && confirm('确定删除这些评卷员吗？')){
		$.post('${ctx}/admin/exam-param/marker/multi-delete.json', {markerIds: markerIds.join(',')}, function(obj){
			if(obj.count>0){
				$.each(obj.ids.split(','), function(index, id){
					$('.marker-list[data-value="'+id+'"]').remove();
					$('.marker-edit-list[data-value="'+id+'"]').remove();
				});
			}
		});
	}
}

function initAddDiv(){
	$('#add-marker-form :input').each(function(){
		if(!$(this).is(':hidden')){
			$(this).val('');
		}
	});
}

function queryStartValue(){
	var codePrefix=$('#codePrefix').val();
	var subjectId=$("#subjectId  option:selected").val();
	var classTypeValue = $("#classTypeValue  option:selected").val();
	var data={
			codePrefix:codePrefix,
			classTypeValue:classTypeValue,
			examId: ${examId},
			subjectId:subjectId
		};
	$.post('${ctx}/admin/exam-param/marker/query-start-value.json',data,function(res){
		$("#startValue").val(res+1);
	});
}
</script>
</html>