<%@ 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"%>
<script type="text/javascript" src="${ctx}/static/admin/js/bootstrap-tab.js"></script>
</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">
				<p>
					<a href="${ctx}/admin/exam/list"><button
						class="btn fr" type="button">
						<i class="icon-hand-left "></i> 返回
					</button></a>
				</p>
			</div>
			<div class="border" data-options="region:'center'">
				<ul class="nav nav-tabs" id="myTab">
	              <li class="active"><a href="#home1">整体状态</a></li>
	              <li><a href="#home2">扫描/统分状态</a></li>
	              <li><a href="#home3">评卷状态</a></li>
	              <li><a href="#home4">考试状态</a></li>
	            </ul>
	            <div class="tab-content">
	            	<div class="tab-pane active" id="home1">
	            		<table class="table table-hover table-bordered">
							<thead>
								<tr>
									<th>名称</th>
									<th>数量</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
							  <c:if test="${web_user.roleMap['school_admin']!=null }">
							  	<tr>
								  	<td>学习中心</td>
								  	<td id="campusCount"></td>
								  	<td><button class="btn btn-small" type="button" onclick="toCampus()">进入</button></td>
								</tr>
							  </c:if>	
							  <tr>
							  	<td>考生</td>
							  	<td id="studentCount"></td>
							  	<td><button class="btn btn-small" type="button" onclick="toStudent()">进入</button></td>
							  </tr>
							  <tr>
							  	<td>评卷员</td>
							  	<td id="markerCount"></td>
							  	<td><button class="btn btn-small" type="button" onclick="toMarker()">进入</button></td>
							  </tr>
							  <tr>
							  	<td>科组长</td>
							  	<td id="subjectHeaderCount"></td>
							  	<td><button class="btn btn-small" type="button" onclick="toSubjectHeader()">进入</button></td>
							  </tr>
							  <tr>
							  	<td>客观题</td>
							  	<td id="objectiveCount"></td>
							  	<td><button class="btn btn-small" type="button" onclick="toObjective()">进入</button></td>
							  </tr>
							  <tr>
							  	<td>主观题</td>
							  	<td id="subjectiveCount"></td>
							  	<td><button class="btn btn-small" type="button" onclick="toSubjective()">进入</button></td>
							  </tr>
							</tbody>
						</table>
	            	</div>
	            	<div class="tab-pane" id="home2">
	            		<table class="table table-hover table-bordered">
							<thead>
								<tr>
									<th>科目</th>
									<th>客观题</th>
									<th>主观题</th>
									<th>科组长</th>
									<th>评卷员</th>
									<th>状态</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody id="scanAnalysis">
							</tbody>
						</table>
	            	</div>
	            	<div class="tab-pane" id="home3">
	            		<table class="table table-hover table-bordered">
							<thead>
								<tr>
									<th>科目</th>
									<th>阶段</th>
									<th>状态</th>
								</tr>
							</thead>
							<tbody id="markStatus">
							</tbody>
						</table>
	            	</div>
	            	<div class="tab-pane" id="home4">
	            		<table class="table table-hover table-bordered">
							<thead>
								<tr>
									<th>考试名称</th>
									<th>状态</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td id="examName"></td>
									<td id="examStatus"></td>
									<td><a class="btn btn-small btn-primary" href="${ctx }/admin/analysis/subject">❶科目参数设置</a>
										<a class="btn btn-small btn-info" href="${ctx }/admin/analysis/total">❷划线参数设置</a>
										<a id="exam-Status" class="btn btn-small btn-warning" href="javascript:void(0);" onclick="statistic()">❸统计分析</a>
										<a class="btn btn-small btn-danger" href="${ctx }/admin/analysis/total" onclick="endedExam()">❹结束考试</a>
									</td>
								<tr>
							</tbody>
						</table>
	            	</div>
	            </div>
			</div>
		</div>
	</div>
</body>
<script type="text/javascript">
$(document).ready(function() {
	var url='${ctx}/admin/exam/gettotalstatus.json';
    $.ajax( {
    url : url,
    type : "POST", 
    data: null,
    success : function(val) {
    	if($("#campusCount").length>0){
    		$("#campusCount").html(val.campusCount);
    	}
    	$("#studentCount").html(val.studentCount);
    	$("#markerCount").html(val.markerCount);
    	$("#subjectHeaderCount").html(val.subjectHeaderCount);
    	$("#subjectiveCount").html(val.subjectiveCount);
    	$("#objectiveCount").html(val.objectiveCount);
    	if(val.examStatus==0){
    		$("#exam-Status").html("❸统计分析");
    		$("#exam-Status").attr("onclick","statistic()");
    		$("#exam-Status").addClass("btn-warning");
    	}else if(val.examStatus==1){
    		$("#exam-Status").html("❸统计分析中");
    		$("#exam-Status").removeClass("btn-warning");
    		$("#exam-Status").addClass("btn-inverse");
    		$("#exam-Status").removeAttr("onclick");
    	}else if(val.examStatus==2){
    		$("#exam-Status").html("❸统计分析结束");
    		$("#exam-Status").removeClass("btn-warning");
    		$("#exam-Status").addClass("btn-inverse");
    		$("#exam-Status").removeAttr("onclick");
    	}
    	
    },
	error:function(e){
    	alert('通信发生错误，稍等片刻再重新提交！');
    }
    })
});
$('#myTab a').click(function (e) {
  e.preventDefault();
  $(this).tab('show');
  if($(this).attr("href")=="#home1"){
	  	$("#studentCount").html("");
	  	$("#markerCount").html("");
	  	$("#subjectHeaderCount").html("");
	  	$("#subjectiveCount").html("");
	  	$("#objectiveCount").html("");
	  var url='${ctx}/admin/exam/gettotalstatus.json';
	    $.ajax( {
	    url : url,
	    type : "POST", 
	    data: null,
	    success : function(val) {
	    	$("#studentCount").html(val.studentCount);
	    	$("#markerCount").html(val.markerCount);
	    	$("#subjectHeaderCount").html(val.subjectHeaderCount);
	    	$("#subjectiveCount").html(val.subjectiveCount);
	    	$("#objectiveCount").html(val.objectiveCount);
	    	
	    },
		error:function(e){
	    	alert('通信发生错误，稍等片刻再重新提交！');
	    }
	    })
  }else if($(this).attr("href")=="#home2"){
	  getMarkSubject();
  }else if($(this).attr("href")=="#home3"){
	  $("#markStatus").html("");
	  var url='${ctx}/admin/exam/getmarkstatus.json';
	    $.ajax( {
	    url : url,
	    type : "POST", 
	    data: null,
	    success : function(val) {
	    	var str = "";
	    	var endMark = "评卷结束";
	    	for(var i=0;i<val.length;i++){
	    		str +="<tr>";
	    		str += "<td>"+val[i].subjectName+"</td>";
	    		str += "<td>"+val[i].stage+"</td>";
	    		str += "<td>"+val[i].status+"</td>";
	    		str +="</tr>";
	    		if(val[i].status != endMark){
	    			alert("评卷还未结束");
	    			return false;
	    		}
	    	}
	    	$("#markStatus").html(str);
	    },
		error:function(e){
	    	alert('通信发生错误，稍等片刻再重新提交！');
	    }
	    })
  }else{
	  var url='${ctx}/admin/exam/getexaminfo.json';
	    $.ajax( {
	    url : url,
	    type : "POST", 
	    data: null,
	    success : function(val) {
	    	$("#examName").html(val.name);
	    	if(val.status == "START"){
	    		$("#examStatus").html("未结束");
	    	}else if(val.status == "STATISTIC"){
	    		$("#examStatus").html("分析统计中");
	    	}else{
	    		$("#examStatus").html("已结束");
	    	}
	    },
		error:function(e){
	    	alert('通信发生错误，稍等片刻再重新提交！');
	    }
	    })
  }
});
function startMark(subjectId,rootSubjectId,classType){
	var data ={
			subjectId:subjectId,
			rootSubjectId:rootSubjectId,
			classType:classType
	}
	var url='${ctx}/admin/exam/startmark.json';
    $.ajax({
    url : url,
    type : "POST", 
    data: data,
    success : function(result) {
    	if(result.success){
    		getMarkSubject();
    	}else{
    		//alert("还有小科目没有设置科组长，请先为其设置科组长后再开始考试！");
    		alert(result.message);
    		return false;
    	}
    	
   }
	})
}
function startAnalysis(subjectId,classType){
	var data ={
			subjectId:subjectId,
			classType:classType
	}
	var url='${ctx}/admin/exam/startanalysis.json';
    $.ajax( {
    url : url,
    type : "POST", 
    data: data,
    success : function(result) {
    	getMarkSubject();
	}
    })
} 
function endedScan(subjectId,classType){
	var data ={
			subjectId:subjectId,
			classType:classType
	}
	var url='${ctx}/admin/exam/endedscan.json';
    $.ajax({
    url : url,
    type : "POST", 
    data: data,
    success : function(result) {
    	getMarkSubject();
   },
  	error:function(e){
      	alert('通信发生错误，稍等片刻再重新提交！');
      }
	})
}

function proceedScan(subjectId,classType){
	var data ={
			subjectId:subjectId,
			classType:classType
	}
	var url='${ctx}/admin/exam/proceedscan.json';
    $.ajax({
    url : url,
    type : "POST", 
    data: data,
    success : function(result) {
    	getMarkSubject();
   },
  	error:function(e){
      	alert('通信发生错误，稍等片刻再重新提交！');
      }
	})
}
function toCampus(){
	window.location.href="${ctx }/admin/exam-param/campus";
}
function toStudent(){
	window.location.href="${ctx }/admin/exam-param/student";
}
function toMarker(){
	window.location.href="${ctx }/admin/exam-param/marker";
}
function toSubjectHeader(){
	window.location.href="${ctx }/admin/exam-param/header";
}
function toObjective(){
	window.location.href="${ctx }/admin/exam-param/objective";
}
function toSubjective(){
	window.location.href="${ctx }/admin/exam-param/subjective";
}

function getMarkSubject(){
	$("#scanAnalysis").html("");
	  var url='${ctx}/admin/exam/getsubjectstatus.json';
	    $.ajax( {
	    url : url,
	    type : "POST", 
	    data: null,
	    success : function(val) {
	    	var str = "";
	    	for(var i=0;i<val.length;i++){
	    		if(val[i].hasLeaf){
	    			str +="<tr class='info font-w'>";
	    			var subjectName = "";
	    			if(val[i].classType==1){
	    				subjectName += "文科";
	    			}
	    			if(val[i].classType==2){
	    				subjectName += "理科";
	    			}
	    			subjectName += val[i].subjectName;
		    		str += "<td>"+subjectName+"</td>";
		    		if(val[i].hasObjective){
		    			str += "<td><img src='${ctx}/static/admin/images/ok.png'></td>";
		    		}else{
		    			str += "<td></td>";
		    		}
		    		if(val[i].hasSubjective){
		    			str += "<td><img src='${ctx}/static/admin/images/ok.png'></td>";
		    		}else{
		    			str += "<td></td>";
		    		}
		    		str += "<td>-</td>";
		    		str += "<td>-</td>";
		    		str += "<td>"+val[i].status+"</td>"
		    		if(val[i].status =="扫描上传" || val[i].status=="扫描结束"){
		    			if(val[i].hasSubjective&&val[i].hasSubjectHeader&&val[i].markSubjectStatus==0){
		    				if(val[i].status =="扫描上传"){
		    					str+="<td><button class='btn btn-small btn-danger' type='button' onclick='endedScan("+val[i].subjectId+","+val[i].classType+")'>结束扫描</button></td>";
		    					str += "<div id='demo' class='collapse in'> … </div>";
		    				}else{
		    					str+="<td><button class='btn btn-small btn-primary' type='button' onclick='proceedScan("+val[i].subjectId+","+val[i].classType+")'>继续扫描</button></td>";
		    				}		
		    			}else if(val[i].hasSubjective&&val[i].hasSubjectHeader&&val[i].markSubjectStatus==1){
		    				if(val[i].status =="扫描上传"){
		    					str+="<td><button class='btn btn-small btn-danger' type='button' onclick='endedScan("+val[i].subjectId+","+val[i].classType+")'>结束扫描</button></td>";
		    				}else{
		    					str += "<td><button class='btn btn-small btn-primary' type='button' onclick='proceedScan("+val[i].subjectId+","+val[i].classType+")'>继续扫描</button></td>";
		    				}
		    			}else if(val[i].hasObjective&&val[i].hasSubjective&&val[i].hasSubjectHeader&&val[i].markSubjectStatus==2){
		    				str+="<td><button class='btn btn-small btn-primary' type='button' onclick='startAnalysis("+val[i].subjectId+","+val[i].classType+")'>开始统分</button></td>";
		    			}else{
		    				str += "<td></td>";
		    			}
		    		}else{
		    			str += "<td></td>";
		    		}	
		    		str +="</tr>";
		    		for(var k=0;k<val[i].subList.length;k++){
		    			str += "<tr class='warning'>";
		    			str += "<td align='right'>"+val[i].subList[k].subjectName+"</td>";
		    			str += "<td>-</td>";
		    			str += "<td>-</td>";
			    		if(val[i].subList[k].hasSubjectHeader){
			    			str += "<td><img src='${ctx}/static/admin/images/ok.png'></td>";
			    		}else{
			    			str += "<td></td>";
			    		}
			    		if(val[i].subList[k].hasMarker){
			    			str += "<td><img src='${ctx}/static/admin/images/ok.png'></td>";
			    		}else{
			    			str += "<td></td>";
			    		}
			    		str += "<td>-</td>";
		    			if(val[i].hasSubjective&&val[i].subList[k].hasSubjectHeader&&val[i].subList[k].markSubjectStatus==0){
		    				str+="<td><button class='btn btn-primary' type='button' onclick='startMark("+val[i].subList[k].subjectId+","+val[i].subjectId+","+val[i].subList[k].classType+")'>开始评卷</button></td>";
		    			}else{
		    				str += "<td></td>";
		    			}
		    			str += "</tr>";
		    		}
	    		}else{
	    			str +="<tr class='info font-w'>";
	    			var subjectName = "";
	    			if(val[i].classType==1){
	    				subjectName += "文科";
	    			}
	    			if(val[i].classType==2){
	    				subjectName += "理科";
	    			}
	    			subjectName += val[i].subjectName;
		    		str += "<td>"+subjectName+"</td>";
		    		if(val[i].hasObjective){
		    			str += "<td><img src='${ctx}/static/admin/images/ok.png'></td>";
		    		}else{
		    			str += "<td></td>";
		    		}
		    		if(val[i].hasSubjective){
		    			str += "<td><img src='${ctx}/static/admin/images/ok.png'></td>";
		    		}else{
		    			str += "<td></td>";
		    		}
		    		if(val[i].hasSubjectHeader){
		    			str += "<td><img src='${ctx}/static/admin/images/ok.png'></td>";
		    		}else{
		    			str += "<td></td>";
		    		}
		    		if(val[i].hasMarker){
		    			str += "<td><img src='${ctx}/static/admin/images/ok.png'></td>";
		    		}else{
		    			str += "<td></td>";
		    		}
		    		str += "<td>"+val[i].status+"</td>"
		    		if(val[i].status =="扫描上传" || val[i].status=="扫描结束"){
		    			if(val[i].hasSubjective&&val[i].hasSubjectHeader&&val[i].markSubjectStatus==0){
		    				if(val[i].status =="扫描上传"){
		    					str+="<td><button class='btn btn-danger' type='button' onclick='endedScan("+val[i].subjectId+","+val[i].classType+")'>结束扫描</button>&nbsp;&nbsp;<button class='btn btn-primary' type='button' onclick='startMark("+val[i].subjectId+","+val[i].subjectId+","+val[i].classType+")'>开始评卷</button></td>";
		    				}else{
		    					str+="<td><button class='btn btn-primary' type='button' onclick='proceedScan("+val[i].subjectId+","+val[i].classType+")'>继续扫描</button>&nbsp;&nbsp;<button class='btn btn-primary' type='button' onclick='startMark("+val[i].subjectId+","+val[i].subjectId+","+val[i].classType+")'>开始评卷</button></td>";
		    				}
		    			}else if(val[i].hasSubjective&&val[i].hasSubjectHeader&&val[i].markSubjectStatus==1){
		    				if(val[i].status =="扫描上传"){
		    					str+="<td><button class='btn btn-danger' type='button' onclick='endedScan("+val[i].subjectId+","+val[i].classType+")'>结束扫描</button></td>";
		    				}else{
		    					str += "<td><button class='btn btn-primary' type='button' onclick='proceedScan("+val[i].subjectId+","+val[i].classType+")'>继续扫描</button></td>";
		    				}
		    			}else if(val[i].hasObjective&&val[i].hasSubjective&&val[i].hasSubjectHeader&&val[i].markSubjectStatus==2){
		    				str+="<td><button class='btn btn-primary' type='button' onclick='startAnalysis("+val[i].subjectId+","+val[i].classType+")'>开始统分</button></td>";
		    			}else{
		    				str += "<td></td>";
		    			}
		    		}else{
		    			str += "<td></td>";
		    		}
		    		
		    		str +="</tr>";
	    		}
	    	}
	    	$("#scanAnalysis").html(str);
	    	
	    },
		error:function(e){
	    	alert('通信发生错误，稍等片刻再重新提交！');
	    }
	    })
}

function statistic(){
	var url='${ctx}/admin/exam/statistic.json';
    $.ajax({
    url : url,
    type : "POST", 
    data: null,
    success : function(val) {
    	getMarkSubject();
    	if(val.examStatus==0){
    		$("#exam-Status").html("❸统计分析");
    		$("#exam-Status").attr("onclick","statistic()");
    		$("#exam-Status").addClass("btn-warning");
    	}else if(val.examStatus==1){
    		$("#exam-Status").html("❸统计分析中");
    		$("#exam-Status").removeClass("btn-warning");
    		$("#exam-Status").addClass("btn-inverse");
    		$("#exam-Status").removeAttr("onclick");
    	}else if(val.examStatus==2){
    		$("#exam-Status").html("❸统计分析结束");
    		$("#exam-Status").removeClass("btn-warning");
    		$("#exam-Status").addClass("btn-inverse");
    		$("#exam-Status").removeAttr("onclick");
    	}
   },
  	error:function(e){
      	alert('通信发生错误，稍等片刻再重新提交！');
    	}
	})
}
function endedExam(){
	 var url='${ctx}/admin/exam/getexaminfo.json';
	    $.ajax( {
	    url : url,
	    type : "POST", 
	    data: null,
	    success : function(val) {
	    	if(val.status == "STATISTIC"){
	    		alert("当前考试正在统计分析中，不能结束考试！");
	    		return false;
	    	}else if(val.status == "ENDED"){
	    		alert("当前考试已结束！");
	    		return false;
	    	}else{
	    		finished();
	    	}
	    },
		error:function(e){
	    	alert('通信发生错误，稍等片刻再重新提交！');
	    }
	    })
}

function finished(){
	var url='${ctx}/admin/exam/finished.json';
    $.ajax( {
    url : url,
    type : "POST", 
    data: null,
    success : function(val) {
    	var url='${ctx}/admin/exam/getexaminfo.json';
	    $.ajax( {
	    url : url,
	    type : "POST", 
	    data: null,
	    success : function(val) {
	    	$("#examName").html(val.name);
	    	if(val.status == "START"){
	    		$("#examStatus").html("未结束");
	    	}else if(val.status == "STATISTIC"){
	    		$("#examStatus").html("分析统计中");
	    	}else{
	    		$("#examStatus").html("已结束");
	    	}
	    },
		error:function(e){
	    	alert('通信发生错误，稍等片刻再重新提交！');
	    }
	    })
    },
	error:function(e){
    	alert('通信发生错误，稍等片刻再重新提交！');
    }
    })
} 
</script>
</html>
