<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/taglib/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/taglib/fmt.tld" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" style="display:none"
	aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h4 id="myModalLabel">历史考试</h4>
	</div>
	<div class="modal-body">
		<!-- <form class="form-inline mr-b" id="exam-search-form">
			<span class="help-inline">名称：</span>
			<input type="text" name="name"/>
			<a href="#" class="btn" id="history-search-button">
				<i class="icon-search"></i> 搜索
			</a>
		</form> -->
		<table class="table table-bordered table-hover" id="exam-history-table">
			<thead>
				<tr>
					<th>#</th>
					<th>名称</th>
					<th>学校</th>
					<th>考生</th>
					<th>评卷员</th>
					<th>科组长</th>
					<th>客观题</th>
					<th>主观题</th>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
		<div class="pagination" id="paginator">
			<ul id="page-ul">
			</ul>
		</div>
	</div>
	<div class="modal-footer">
		<a href="#" class="btn btn-primary" id="select-history-button">确定</a>
		<a href="#" class="btn" data-dismiss="modal" aria-hidden="true">取消</a>
	</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
	$('#myModal').modal({
		show : false
	}).css({
	    width:'800px',
	    'margin-left': function () {
	       return -($(this).width() / 2);
	   }
	}); 
	$('#history-search-button').click(function(){
		searchHistoryExam($('#exam-search-form input[name="name"]').val(),1,8);
	});
	$('#page-ul .page-link').live('click', function(){
		searchHistoryExam($('#exam-search-form input[name="name"]').val(),parseInt($(this).attr('data-number')),8);
	});
	$('#exam-history-table tbody tr').live('click', function(){
		$('#exam-history-table tbody tr.active').each(function(index, obj){
			$(obj).removeClass('active');
		});
		$(this).addClass('active');
	});
	$('#select-history-button').click(function(){
		var exam = $('#exam-history-table tbody tr.active')[0];
		if(exam==undefined){
			alert('请选择某次考试');
			return false;
		}
		if(confirm('确定要复制本次考试的参数吗？')){
			$('#sourceExamId').val($(exam).attr('data-id'));
			$('#copyHistory').val(true);
			$('#myModal').modal('hide');
			//var examTime=$("#examTime").datebox("getValue");
			//$('#examTime').datebox('setValue',examTime);
			submit();
		}
	});
});

function initExamHistory(){
	$('#sourceExamId').val('');
	$('#myModal').modal('show');
	$('#exam-search-form input[name="name"]').val('');
	searchHistoryExam('', 1, 8);
}

function searchHistoryExam(name, pageNumber, pageSize){
	var option = {statusValue:2};
	if(name!=undefined && name!=''){
		option.name=name;
	}
	if(pageNumber!=undefined && pageNumber>0){
		option.pageNumber=pageNumber;
	}
	if(pageSize!=undefined && pageSize>0){
		option.pageSize=pageSize;
	}
	$.post('${ctx}/admin/exam/page-list.json', option, function(result){
		$('#exam-history-table tbody').empty();
		$.each(result.examList, function(index, exam){
			$('#exam-history-table tbody').append($.nano($('#exam-tr-template').html(),exam));
		});
		buildPaginator(result);
	});
}

function buildPaginator(result){
	$('#page-ul').empty();
	if(result.totalPage>1){
		var slide=7;
		if(result.pageNumber>1){
			$('#page-ul').append($.nano($('#page-li-template').html(),{pageNumber:1, showNumber:'<<'}));
		}
		var halfSize = 3;
        var start = (result.pageNumber-halfSize)>1?(result.pageNumber-halfSize):1;
        var end = (start+6)<result.totalPage?(start+6):result.totalPage;
        for (var i = start; i <= end; i++) {
        	if(i==result.pageNumber){
        		$('#page-ul').append($.nano($('#page-li-active-template').html(),{showNumber:i}));
        	}else{
        		$('#page-ul').append($.nano($('#page-li-template').html(),{pageNumber:i, showNumber:i}));
        	}
        }
		if(result.pageNumber<result.totalPage){
			$('#page-ul').append($.nano($('#page-li-template').html(),{pageNumber:result.totalPage, showNumber:'>>'}));
		}
		$('#paginator').show();
	}else{
		$('#paginator').hide();
	}
}
</script>
<script type="template" id="exam-tr-template">
<tr data-id="{id}">
<td><input name="examinfo" type="radio"></td>
<td>{name}</td>
<td>{schoolCount}</td>
<td>{studentCount}</td>
<td>{markerCount}</td>
<td>{subjectHeaderCount}</td>
<td>{objectiveCount}</td>
<td>{subjectiveCount}</td>
</tr>
</script>
<script type="template" id="page-li-template">
<li><a href="#" class="page-link" data-number="{pageNumber}">{showNumber}</a></li>
</script>
<script type="template" id="page-li-active-template">
<li class="active"><a href="#">{showNumber}</a></li>
</script>