<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="../common/init.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>审题任务批次详情</title>
<%@ include file="../common/top.jsp"%>
<link rel="stylesheet" href="${ctx}/resources/css/main.css" type="text/css" />
<link rel='stylesheet' href='${ctx}/resources/css/easyui.css' type='text/css' media='screen' />
<link rel='stylesheet' href='${ctx}/resources/css/easyui/icon.css' type='text/css' media='screen' />
<link rel='stylesheet' href='${ctx}/resources/css/user-center-style.css' type='text/css' media='screen' />
<style>
select{
	width: 120px;
}
</style>
<script type="text/javascript" src="${ctx}/resources/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/swfupload/swfupload.queue.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/swfupload/importTask.js"></script>
<script type="text/javascript">
	function loadDg(){
		$('#dg').datagrid({
        	url:'${ctx}/business/allAuditQuesTasks/${batchId}',
        	onCheck: function (rowIndex, rowData) {
    			if(rowData.id == null){
    				$('#dg').datagrid('uncheckRow',rowIndex);
    			}
            },
            onSelect: function (rowIndex, rowData) {
    			if(rowData.id == null){
    				$('#dg').datagrid('unselectRow',rowIndex);
    			}
            },
            onSelectAll: function (rows) {
            	$('#dg').datagrid('unselectRow',rows.length - 1);
            },
            onLoadSuccess:function(data){
            	$(":checkbox[name='id']").eq(data.total - 1).remove();
            }
        });
	}

	$(function(){
		$('#course').combogrid({
			filter: function(q, row){
				var opts = $(this).combogrid('options');
				//console.log(q,row,opts);
				return row.code.indexOf(q) >= 0 || row.name.indexOf(q) >= 0 || row.showName.indexOf(q) >= 0;
				//return row[opts.textField].indexOf(q) == 0;
			}
		});

		$('#formForLinkUser').submit(function(event){
	        event.preventDefault();
			var param = {"keyword":$("#keywordForSearchUser").val()};
	        param = $.extend(param, {page:1,rows:10});
			$('#allUserDg').datagrid({
			    url:"${ctx}/user/queryUsers",
	        	'queryParams':param
			});
			return false;
		});
		
		$("#saveLinkUserBtn").click(function(){
			saveLinkUsers();
	    	return false;
	    });
		
		$("#cancleLinkUserBtn").click(function(){
	    	$('#linkUserDiv').window('close');
	    	return false;
	    });
		
		$("#saveAddTaskBtn").click(function(){
			saveAddTask();
	    	return false;
	    });
		
		$("#cancleAddTaskBtn").click(function(){
	    	$('#addTaskDiv').window('close');
	    	return false;
	    });
		
		$("#toModifyBtn").click(function(){
			var rowData = $("#dg").datagrid('getSelected');
			if(rowData != null && rowData.id != null){
				openLinkUserWin(rowData);
			}else{
	    		$.messager.alert('提示','请选择一行修改任务单相关人员', 'error');
	    	}
		});
		
		$("#saveChange").on('click',function(){
			if($("#oldTeacher").val()==$("#newTeacher").val()){
				$.messager.alert('提示','不能修改为同一个人','error');
			}else{
				var obj={
					"id":$("#taskId").val(),
					"oldId":$("#oldTeacher").val(),
					"name":$("#oldTeacher").find("option:selected").text(),
					"newId":$("#newTeacher").val()
				}
				$.post('${ctx}/business/updatePaperTask',obj,function(callback){
					if(callback.flag=='success'){
						$.messager.alert('提示','修改成功','info');	
						$('#dg').datagrid('reload');
					}
				});
			}
			
		});
		
		$("#closeStatDiv").click(function(){
			$('#statDiv').window('close');
			return false;
	    });
		
		window.setTimeout(function(){loadDg();},200);
	});
	
	function openStatWin(){
		$(document).mask('正在查询统计数据，请稍等...');
		var param = {"batchId":${batchId}};
		$.post('${ctx}/business/statBatchAuditorJob',param,function(res){
			$(document).unmask();
			if(res.flag=='failed'){
	           $.messager.alert('提示', res.msg, 'error', function () {
	        	   //do nothing
	           });
	           return false;
			}else{
				$('#statDg').datagrid({data:res.auditorJob});
		    	$('#statDiv').window('open');
			}
		});
	}
	
	function saveLinkUsers(){
		var rows = $('#chosenUserDg').datagrid('getRows');
		if(rows.length == 0){
			$.messager.alert('提示','请至少选择一个审题教师', 'error');
			return false;
		}
		var arr = [];
		for(var i=0;i<rows.length;i++){
			arr[arr.length] = rows[i].id;
		}
		$(document).mask('正在保存中，请稍等...');
		var param = {'taskId':$('#taskIdForLinkUser').val(),'links':arr};
		$.ajax({
			type : "POST",
			url : "${ctx}/business/saveLinkUsers",
			data : param,
			success : function(callback) {
				$(document).unmask();
				if(callback.flag == 'success'){
					loadDg();
					$.messager.alert('提示', callback.msg, 'info', function () {
						$('#linkUserDiv').window('close');
	                });
				}else{
					$.messager.alert('提示', "更新失败，请刷新后操作!", 'error');
				}
			}
		});
	}
	
	function saveAddTask(){
		var selected = $('#course').combogrid('grid').datagrid('getSelected');
		if(selected == null){
			$.messager.alert('提示', "请选择课程!", 'error',function(){
				$('#course').combogrid('showPanel');
			});
			return false;
		}
		var courseId = selected.id;
		var rows = $('#dg').datagrid('getRows');
		for(var i=0;i<rows.length;i++){
			if(rows[i].courseId == courseId){
				$.messager.alert('提示', "该课程对应的审题任务已存在，请选择其他课程!", 'error',function(){
					$('#course').combogrid('showPanel');
				});
				return false;
			}
		}
		$(document).mask('正在保存中，请稍等...');
		var param = {'courseId':courseId,'batchId':${batchId}};
		$.ajax({
			type : "POST",
			url : "${ctx}/business/addAuditQuesTask",
			data : param,
			success : function(callback) {
				$(document).unmask();
				if(callback.flag == 'success'){
					loadDg();
					$.messager.alert('提示', callback.msg, 'info', function () {
						$('#addTaskDiv').window('close');
						openLinkUserWin(callback.task);
	                });
				}else{
					$.messager.alert('提示', "更新失败，请刷新后操作!", 'error');
				}
			}
		});
	}
	
	function openLinkUserWin(task){
		$("#taskIdForLinkUser").val(task.id);
		$("#keywordForSearchUser").val("");
		$("#formForLinkUser").submit();
		$('#chosenUserDg').datagrid({
		    url:"${ctx}/business/selectUsersByTaskId/" + task.id
		});
		$('#linkUserDiv').window('setTitle','选择审题教师-' + task.courseCode);
		$('#linkUserDiv').window('open');
	}
	
	function userToLeft(){
		var selectedRow = null;
		while((selectedRow = $("#chosenUserDg").datagrid('getSelected')) != null){
			var selectedIndex = $("#chosenUserDg").datagrid('getRowIndex',selectedRow);
			$("#chosenUserDg").datagrid('deleteRow',selectedIndex);
		}
	}

	function userToRight(){
		var rows1 = $("#allUserDg").datagrid('getSelections');
		
		var data= $("#chosenUserDg").datagrid("getData");
		var rows=data.rows;
		var id;
		var flag;
		for(var i=0;i<rows1.length;i++){
			id=rows1[i].id;
		}
		for(var i=0;i<rows.length;i++){
			if(rows[i].id==id){
				flag=true;
			}
		}
		if(!flag){
			for(var i=0;i<rows1.length;i++){
				var index = -1;
				var existed = ((index = $("#chosenUserDg").datagrid('getRowIndex',rows1[i])) >= 0);
				if(!existed){
		    		index = $('#chosenUserDg').datagrid('insertRow',{row:rows1[i]}).datagrid('getRows').length-1;
				}
				$('#chosenUserDg').datagrid('selectRow', index);
			}
			$("#allUserDg").datagrid('unselectAll');
		}
		
	}
	
	function delTasks(){
		var rows = $('#dg').datagrid('getSelections');
		if(rows.length == 0){
			$.messager.alert('提示','请至少选择一行', 'error');
			return false;
		}
		$.messager.confirm("操作提示", "您确定要删除选中的任务吗？", function (data) {
	        if (data) {
				var arr = [];
				for(var i=0;i<rows.length;i++){
					arr[arr.length] = rows[i].id;
				}
				$(document).mask('正在保存中，请稍等...');
				var param = {'taskIds':arr};
				$.ajax({
					type : "POST",
					url : "${ctx}/business/delAuditQuesTasks",
					data : param,
					success : function(callback) {
						$(document).unmask();
						if(callback.flag == 'success'){
							loadDg();
							$.messager.alert('提示', callback.msg, 'info', function () {
			                });
						}else{
							$.messager.alert('提示', "更新失败，请刷新后操作!", 'error');
						}
					}
				});
	        }
		});
	}
	
	function addTask(){
		$('#addTaskDiv').window('open');
	}
	
</script>
</head>
<body>
	<%@ include file="../common/header.jsp"%>
	<div id="content" class="content">
        <%@ include file="../common/sub_menu.jsp"%>
	        <div id="page" class="page">
		   <div class="page-header">${taskBatch.name}-任务列表</div>
		   <div class="page-body">
		  	<div class="right" style="min-height:300px;position: relative;">
	             <div id="list-wrap posts" class="listview">
	                 <table id="dg" class="easyui-datagrid"
	                     data-options="fitColumns:true,singleSelect:false,striped:true,pagination:false,remoteSort:false,toolbar: '#tb'">
	                     <thead>
	                         <tr class="title">
	                         	<th data-options="field:'id',checkbox:true"></th>
	                            <th data-options="field:'courseCode',width:60,sortable:true">课程编码</th>
	                            <th data-options="field:'courseName',width:80,sortable:true">课程名称</th>
	                            <th data-options="field:'auditors',width:260,sortable:true">审题教师列表</th>
	                            <th data-options="field:'draftNum',width:60,sortable:true">草稿数量</th>
	                            <th data-options="field:'approvedNum',width:60,sortable:true">审核通过数量</th>
	                            <th data-options="field:'rejectedNum',width:60,sortable:true">审核拒绝数量</th>
	                            <th data-options="field:'statTotalNum',width:60,sortable:true">总题量</th>
	                         </tr>
	                     </thead>
	                   </table>
	               </div>
	            </div>
	            
	            <div id="tb" style="height:auto">
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" id="toModifyBtn">修改审题教师</a>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addTask()">添加任务单</a>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-no',plain:true" onclick="delTasks()">删除任务单</a>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-sum',plain:true" onclick="openStatWin()">统计分析</a>
				</div>
		</div>
		</div>
     </div>
     
<div style="display: none;">
     <div id="linkUserDiv" title="选择审题教师" class="easyui-window" data-options="modal:true,closed:true,minimizable:false,resizable:false,iconCls:'icon-import'" style="width:860px;height:380px;padding:5px;overflow-x: hidden;">
    	<div id="barcode-scan" style="font-size: 12px;">
    		<form action="#" id="formForLinkUser">
    			<input type="hidden" name="taskId" id="taskIdForLinkUser"/>
    			<table style="width: 830px;margin-top: 5px;" cellpadding="0" border="0" class="bookSelTable" align="center">
    				<tr>
    					<td>
							<div style="text-align: left;">
									<input id="keywordForSearchUser" placeholder="登录名或姓名"/>
									<button id="searchUserBtn">查询</button>
							</div>
							<table id="allUserDg" class="easyui-datagrid" style="width:450px;height:240px;" data-options="fitColumns:true,singleSelect:true,pagination:true,onClickRow:userToRight">
    							<thead>
							        <tr>
							        	<th data-options="field:'loginName',width:50">登录名</th>
							            <th data-options="field:'name',width:50">姓名</th>
							        </tr>   
    							</thead>
							</table>
						</td>
						<td valign="middle" align="center">
							<div style="margin-bottom: 30px;">
								<input type="button" value="&gt;" style="width: 30px;" id="addUserBtn"/>
							</div>
							<div>
								<input type="button" value="&lt;" style="width: 30px;" id="delUserBtn"/>
							</div>
						</td>
						
						<td align="right" valign="bottom">
							<table id="chosenUserDg" class="easyui-datagrid" style="width:320px;height:240px" data-options="fitColumns:true,singleSelect:true,onClickRow:userToLeft">
							    <thead>   
							        <tr>
							        	<th data-options="field:'loginName',width:50">登录名</th>
							            <th data-options="field:'name',width:50">姓名</th>
							        </tr>   
							    </thead>   
							</table>
						</td>
    				</tr>
    				<tr>
						<td colspan="3" style="padding-top: 10px;" align="center"><button style="margin-right: 50px;" id="saveLinkUserBtn">保存</button><button id="cancleLinkUserBtn">取消</button></td>
					</tr>
    			</table>
    		</form>
    	</div>
    </div>
    
    <div id="addTaskDiv" title="添加审题任务" class="easyui-window" data-options="modal:true,closed:true,minimizable:false,resizable:false,iconCls:'icon-import'" style="width:460px;height:220px;padding:5px;overflow-x: hidden;">
    	<div id="barcode-scan" style="font-size: 12px;">
    			<table style="width: 430px;margin-top: 20px;" cellpadding="0" border="0" class="bookSelTable" align="center">
    				<tr>
    					<td>请选择一个课程：</td>
    					<td>
    					<input id="course" name="course" class="easyui-combogrid" style="width:300px;" 
	                           data-options="url:'${ctx}/course/queryAllCourses',
		                                   method:'get',
		                                   panelHeight:'160',
		                                   idField: 'id',
		                                   textField: 'showName',
		                                   columns: [[
						                    {field:'code',title:'code',width:40},
						                    {field:'name',title:'课程名称',width:160}
						                    ]],
						                    fitColumns: true,
						                    editable:true          
		                                    "/>
    					</td>
    				</tr>
    				<tr>
						<td colspan="2" style="padding-top: 80px;" align="center"><button style="margin-right: 50px;" id="saveAddTaskBtn">保存</button><button id="cancleAddTaskBtn">取消</button></td>
					</tr>
    			</table>
    	</div>
    </div>
	
	<div id="statDiv" title="审核数据统计" class="easyui-window" data-options="modal:true,closed:true,maximizable:false,minimizable:false,resizable:false,iconCls:'icon-import'" style="width:520px;height:350px;padding:5px;overflow-x: hidden;">
        <div id="barcode-scan" style="font-size: 12px;margin-top: 10px;">
        	<div style="margin-top: 10px;margin-bottom: 10px;">
        		<table id="statDg" class="easyui-datagrid"
	                     data-options="fitColumns:true,singleSelect:true,striped:true,pagination:false">
	                     <thead>
	                         <tr class="title">
	                             <th data-options="field:'auditorName',width:60">审题教师</th>
	                             <th data-options="field:'approvedNum',width:100">审核通过的题量</th>
	                             <th data-options="field:'rejectedNum',width:100">审核拒绝的数量</th>
	                             <th data-options="field:'statTotalNum',width:100">总计</th>
	                         </tr>
	                     </thead>
        		</table>
        	</div>
        	<div style="text-align: center;"><button class="btn" id="closeStatDiv">确定</button></div>
        </div>
    </div>
</div>
</body>
</html>