<%@ 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"%>
				<div id="header-search-div">
				<form class="form-inline" action="${ctx}/admin/exam-param/header" 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="header-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="${headerList!=null}">
					<c:forEach var="subjectHeader" items="${headerList}">
					<div class="tab-list header-list" data-value="${subjectHeader.id}">
						<em class="list-num"><input class="header-checkbox" type="checkbox"></em> 
						<i class="list-text code-text">${subjectHeader.code}</i> 
						<i class="list-text name-text">${subjectHeader.name}</i>
						<i class="list-text password-text">${subjectHeader.password}</i> 
						<i class="list-text status-text" data-value="${subjectHeader.status}">
							<c:if test="${subjectHeader.status==1}">是</c:if>
							<c:if test="${subjectHeader.status==0}">否</c:if>
						</i>
						<p class="list-edit">
							<c:if test="${isFinish==false && isMarking==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 header-edit-list" data-value="${subjectHeader.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="${subjectHeader.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-success save-button">保存</a> 
							<a class="btn cancel-button">取消</a>
						</p>
					</div>
					</c:forEach>
					</c:if>
				</div>

				<div id="header-add-div" style="display:none">
		   		<form class="form-horizontal" id="add-header-form" action="${ctx}/admin/exam-param/header/add" method="post">
		   			<input type="hidden" name="examId" value="${examId}"/>
					<input type="hidden" name="schoolId" value="${schoolId}"/>
		   			<%@ include file="../../common/subjectClassTypeSelect2.jsp"%>
          			<div class="control-group">
            			<label class="control-label" for="">登录名</label>
            			<div class="input-prepend">
						  <span class="add-on">subject-</span>
						  <input class="info-input required-input" id="code" name="code" type="text" placeholder="登录名" onblur="loginNameCheck()">
						</div>
						<span class="help-inline"></span>
						<input type="hidden" name="codeCheck"/>
						<i id="message" style="display:none; color:#B94A48;">该科组长已存在！</i>
          			</div>
          			<div class="control-group">
            			<label class="control-label" for="">姓名</label>
            			<div class="controls">
            				<input type="text" name="name" class="info-input required-input"/>
            				<span class="help-inline"></span>
            			</div>
          			</div>
          			<div class="control-group">
            			<label class="control-label" for="">密码</label>
            			<div class="controls">
            				<input type="text" name="password" class="info-input required-input number-input"/>
            				<span class="help-inline"></span>
            			</div>
          			</div>
          			<div class="control-group">
            			<label class="control-label" for="">是否可登录</label>
            			<div class="controls">
            				<select name="status">
            					<option value="1">是</option>
            					<option value="0">否</option>
            				</select>
            			</div>
          			</div>
          			<div class="control-group">
          				<div class="controls">
          					<a class="btn btn-primary" id="submit-button">保存</a>
          					<a class="btn" id="return-button">返回</a>
          				</div>
          			</div>
        		</form>
        		</div>  
          	</div>
		</div>
	</div>
</body>
<script type="text/javascript">
$(document).ready(function() {
	$('.nav-tabs a').click(function (e) {
		e.preventDefault();
		$(this).tab('show');
	});
	$('.info-input').keyup(function(){
		validInfoInput($(this));
	});
	$('.delete-button').click(function(){
		del($(this).parent().parent().attr('data-value'));
	});
	$('.edit-button').live("click", function(){
		var headerDiv = $(this).parent().parent();
		var header = {
				id: headerDiv.attr('data-value'),
				name: headerDiv.find('.name-text').html(),
				password: headerDiv.find('.password-text').html(),
				status: headerDiv.find('.status-text').attr('data-value')==1
		};
		var editDiv = $('.header-edit-list[data-value="'+header.id+'"]');
		editDiv.find('.name-input').val(header.name);
		editDiv.find('.password-input').val(header.password);
		editDiv.find('.status-select').val(header.status);
		headerDiv.hide();
		editDiv.show();
	});
	$('.save-button').live("click", function(){
		var editDiv = $(this).parent().parent();
		var header = {
			id: editDiv.attr('data-value'),
			name: editDiv.find('.name-input').val(),
			password: editDiv.find('.password-input').val(),
			status: editDiv.find('.status-select').val()
		}
		if(header.password==''){
			alert("科组长密码不能为空！");
			return;
		}
		$.post('${ctx}/admin/exam-param/header/update.json', header, function(obj){
			if(obj.success){
				alert("保存成功");
				var headerDiv = $('.header-list[data-value="'+header.id+'"]');
				headerDiv.find('.name-text').html(header.name);
				headerDiv.find('.password-text').html(header.password);
				headerDiv.find('.status-text').html(header.status==1?'是':'否');
				headerDiv.find('.status-text').attr('data-value', header.status);
				editDiv.hide();
				headerDiv.show();
			}else{
				alert(obj.message);
			}
		});	
	});
	$('.cancel-button').live("click", function(){
		var id=$(this).parent().parent().attr('data-value');
		$('.header-list[data-value="'+id+'"]').show();
		$(this).parent().parent().hide();
	});
	$('#add-button').click(function(){
		initAddDiv();
		$('#header-search-div').hide();
		$('#header-list-div').hide();
		$('#header-add-div').show();
	});
	$('#return-button').click(function(){
		$('#header-search-div').show();
		$('#header-list-div').show();
		$('#header-add-div').hide();
	});
	$('#submit-button').click(function(){
		var flag = true;
		$('#add-header-form .info-input').each(function(index, obj){
			flag = validInfoInput($(obj)) && flag;
		});
		var codeCheck=$("input[name='codeCheck']").val();
		if(codeCheck=='false'){
			flag=false;	
		}
		if(flag){
			$('#add-header-form').submit();
		}
	});
	$('#multi-checkbox').click(function(){
		if($(this).attr('checked')){
			$('.header-list :checkbox').each(function(index, obj){
				$(obj).attr("checked","true");
			});
		}else{
			$('.header-list :checkbox').each(function(index, obj){
				$(obj).removeAttr("checked");
			});
		}
		toggleMultiControl();
	});
	$('.header-list :checkbox').click(toggleMultiControl);
	$('#multi-delete-button').click(function(){
		var ids = new Array();
		$('.header-list input:checked').each(function(index, obj){
			ids.push($(obj).parent().parent().attr('data-value'));
		});
		multiDel(ids);
	});
});

function loginNameCheck(){
	var code="subject-"+$("input[name='code']").val();
	var examId=$("input[name='examId']").val();
	var schoolId=$("input[name='schoolId']").val();
	var subjectId=$("select[name='subjectId']").val();
	//var classTypeValue=$("select[name='classTypeValue']").val();
	$.post('${ctx}/admin/exam-param/header/check.json',{examId:examId,schoolId:schoolId,subjectId:subjectId,code:code},function(obj){
		if(obj.flag){
			$("#message").hide();
			$("input[name='codeCheck']").attr("value",'true');
		}else{
			$("#message").show();
			$("input[name='codeCheck']").attr("value",'false');
		}
	});
}

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

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

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

function initAddDiv(){
	$('#add-header-form .info-input').each(function(){
		$(this).val('');
		$(this).parent().parent().removeClass('error');
		$(this).parent().find('span.help-inline').html('');
	});
}
</script>
</html>