<%@ 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>
<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' />
<%@ include file="../common/top.jsp"%>
<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/jquery.mask.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/papers/batchPaper.js"></script>
<%@ include file="../common/validation.jsp"%>
<title>试卷检索</title>

<script language="javascript">
$(document).ready(function(){
    $('#search-form').submit(function(event){
        event.preventDefault(); 
        var param = $('#search-form').serializeJson();
        if(param.batchId==""){
        	param.batchId=0;
        }
        $('#dg').datagrid('load',param);
    });
    
    $('#batchId').combogrid({
		panelWidth: 150,
		   multiple: false,
		   idField: 'id',
		   textField: 'type',
		   url: '${ctx}/examPaper/listBatch',
		   editable:false,
		   method: 'get',
		   columns: [[
				{field:'code',title:'批次代码',width:55},
				{field:'type',title:'批次类型',width:65}
			]],
			fitColumns: true
	});
    
    $("#clearBtn").click(function(){
    	$("#code").val("");
    	$("#courseCode").val("");
    	$("#courseName").val("");
    	$("#bookName").val("");
    	$("#bookAuthor").val("");
    	$("#bookPress").val("");
    	$("#bookPressYear").val("");
    	
    	
    	$("input[name='isSeparatePaperUploaded']:first").attr("checked", true);
    	$("input[name='isSpecialAnswerSheet']:first").attr("checked", true);
    	$("input[name='status']:first").attr("checked", true);   
    });
    
    $("#searchBtn").click(function(){
    	$('#search-form').submit();
    });
});
function formatStatus(data){
	if(data=="DRAFT"){
		return "毛坯";
	}else if(data=="FORMAL"){
		return "清样";
	}else if(data=="USED"){
		return "启用";
	}else{
		return "";
	}
}

function downExcelInfo(url){
	var form=$("<form>");//定义一个form表单
   	form.attr("style","display:none");
   	form.attr("target","");
   	form.attr("method","post");
   	form.attr("enctype","multipart/form-data");
   	form.attr("action",url+"/dbfPaperInfo/downExcelTotalRepertory");
   	
   	$("body").append(form);//将表单放置在web中
   	form.submit();//表单提交 
}

function downPaper(url){
	var row = $('#dg').datagrid('getSelected');
	var flag=true;
    if (row){
    	var arr = new Array();
    	var rows = $('#dg').datagrid("getSelections");
		for(var i =0;i<rows.length;i++){
			var rr=rows[i];
			//是专卡
			if(rr.isSpecialAnswerSheet==1){
				if(rr.answerSheetPath!=null&&rr.standardAnswerPath!=null&&rr.paperPath!=null){
					arr.push(rr.id);
				}else{
					$.messager.alert('提示','下载试卷条件必须满足:1.无专卡的试卷 必须试卷和标答存在  2.有专卡的试卷必须试卷、标答和提卡存在','info');
					flag=false;
					break;
				}
			}else{
				if(rr.standardAnswerPath!=null&&rr.paperPath!=null){
					arr.push(rr.id);
				}else{
					$.messager.alert('提示','下载试卷条件必须满足:1.无专卡的试卷 必须试卷和标答存在  2.有专卡的试卷必须试卷、标答和提卡存在','info');
					flag=false;
					break;
				}
			}
		}
       	if(flag){
       		var form=$("<form>");//定义一个form表单
           	form.attr("style","display:none");
           	form.attr("target","");
           	form.attr("method","post");
           	form.attr("enctype","multipart/form-data");
           	form.attr("action",url+"/examPaper/downLoadPaper");
           	var input1=$("<input>");
           	input1.attr("type","hidden");
           	
           	var input2=$("<input>");
           	input2.attr("type","hidden");
           	input2.attr("name","array");
           	input2.attr("value",arr);
           	
           	$("body").append(form);//将表单放置在web中
           	form.append(input1);
           	form.append(input2);
           	form.submit();//表单提交 
       	}
    }else{
		$.messager.alert('提示','请选择需要下载的试卷进行下载操作');
	}
}

function downDBF(url){
	var options = $('#dg').datagrid('getPager').data("pagination").options;
	var total = options.total;
	
	var row = $('#dg').datagrid('getSelected');
    if (row){
    	var arr = new Array();
    	var rows = $('#dg').datagrid("getSelections");
		for(var i =0;i<rows.length;i++){
			var rr=rows[i];
			arr.push(rr.id);
		}
		var form=$("<form>");//定义一个form表单
       	form.attr("style","display:none");
       	form.attr("target","");
       	form.attr("method","post");
       	form.attr("enctype","multipart/form-data");
       	form.attr("action",url+"/examPaper/downLoadDBF");
       	var input1=$("<input>");
       	input1.attr("type","hidden");
       	
       	var input2=$("<input>");
       	input2.attr("type","hidden");
       	input2.attr("name","array");
       	input2.attr("value",arr);
       	
       	$("body").append(form);//将表单放置在web中
       	form.append(input1);
       	form.append(input2);
       	form.submit();//表单提交 
    }else{
    	$.messager.confirm("操作提示", "是否选择下载全部试卷信息?", function (data) {
    		if (data) {
    			if(total!=0){
    				var form=$("<form>");//定义一个form表单
			       	form.attr("style","display:none");
			       	form.attr("target","");
			       	form.attr("method","post");
			       	form.attr("enctype","multipart/form-data");
			       	form.attr("action",url+"/examPaper/downLoadDBF");
			       	var input1=$("<input>");
			       	input1.attr("type","hidden");
			       	
			       	var input2=$("<input>");
			       	input2.attr("type","hidden");
			       	input2.attr("name","rukuType");
			       	input2.attr("value",1);
			       	
			       	var input3=$("<input>");
			       	input3.attr("type","hidden");
			       	input3.attr("name","name");
			       	input3.attr("value",name);
			       	
			       	$("body").append(form);//将表单放置在web中
			       	form.append(input1);
			       	form.append(input2);
			       	form.append(input3);
			       	form.submit();//表单提交 
    			}else{
    				$.messager.alert('提示','该批次下无可下载的试卷信息!');
    			}
    		}
    	});
	}
}
function load(){
	var obj={
			"batchId" :""
		};
	$.ajax({
		type : "POST",
		url : "${ctx}/examPaper/getTotalPaper",
		data :  JSON.stringify(obj) ,
		contentType : "application/json; charset=utf-8",
		dataType : "json",
		success : function(callback) {
			callback=eval(callback);
			var Rhtml="统计:毛坯试卷信息一共有"+callback.DRAFT+"套、  清样试卷信息一共有"+callback.FORMAL+"套、 抽卷试卷信息一共有"+callback.USED+"套、 已上传了"+callback.paperPath+"套试卷、"+callback.answerPath+"套标答、"+callback.answerSheetPath+" 套专卡、" + callback.separatePaperPath + " 套分卷、" + callback.wPaperPath + " 套网评卷。";
			$("#paperInfo").html(Rhtml);
		}
	});
}
function DBClickCell(rowIndex, field, value){
	if(field=='bookName'||field=='bookAuthor'||field=='bookPress'||field=='bookPressYear'){
		$.messager.confirm("操作提示", "是否编辑选中行的信息", function (data) {
			if (data) {
				$('#dg').datagrid('rejectChanges');
				$('#dg').datagrid('selectRow', rowIndex).datagrid('beginEdit', rowIndex);
			}
		});
	}
}
function reject(){
	$('#dg').datagrid('rejectChanges');
}
var editIndex = undefined;
var index;

function accept(){
	
	var row = $('#dg').datagrid('getSelected');
	if(row==null){
		return;
	}
	
	
	var id=row.id;
	var bookName=$('#dg').datagrid('getEditor',{index:index,field:"bookName"});
	var bookAuthor=$('#dg').datagrid('getEditor',{index:index,field:"bookAuthor"});
	var bookPress=$('#dg').datagrid('getEditor',{index:index,field:"bookPress"});
	var bookPressYear=$('#dg').datagrid('getEditor',{index:index,field:"bookPressYear"});
	
	if($(bookName.target).val()==''||$(bookAuthor.target).val()==''||$(bookPress.target).val()==''||$(bookPressYear.target).val()==''){
		$.messager.alert('提示','修改后的信息不能存在空值的情况','error');
		return;
	}
	if($(bookPressYear.target).val().length>10){
		$.messager.alert('提示','修改的版次字段长度过长','error');
		return;
	}
	
	
	var obj={
			"id" :id,
			"bookName"	:$(bookName.target).val(),
			"bookAuthor":$(bookAuthor.target).val(),
			"bookPress"	:$(bookPress.target).val(),
			"bookPressYear"	:$(bookPressYear.target).val()
		};
	$.ajax({
		type : "POST",
		url : "${ctx}/examPaper/updatePaper",
		data :  JSON.stringify(obj) ,
		contentType : "application/json; charset=utf-8",
		dataType : "json",
		success : function(callback) {
			callback=eval(callback);
			if(callback.result=='success'){
				$('#dg').datagrid('reload');
				$.messager.alert('提示','修改成功','info');
			}else{
				$('#dg').datagrid('reload');
				$.messager.alert('提示','修改失败','error');
			}
		}
	});
}
function checkOver(){
	$.messager.confirm("操作提示", "是否确定结束检查?结束检查后将会删除不符合条件的试卷信息!", function (data) {
		if (data) {
			$(document).mask('正在结束检查，请稍等...');
			var obj={
            		"batchId" : "-1"
            	};
            	var PaperParams=JSON.stringify(obj);
            	
            	$.ajax({
        			type : "POST",
        			url : "${ctx}/examPaper/checkOver",
        			data :  PaperParams ,
        			contentType : "application/json; charset=utf-8",
        			dataType : "json",
        			success : function(callback) {
        				 $(document).unmask();
        				callback=eval(callback);
        				if(callback.type=="success"){
        					$.messager.alert('提示','结束检查完成');
        					$('#dg').datagrid('reload');
        				}else{
        					$.messager.alert('提示','结束检查失败');
        					$('#dg').datagrid('reload');
        				}
        			}
        		});
		}
	});
}
function selectRows(rowIndex, rowData){
	index=rowIndex;
}
function editInfo(){
	var row = $('#dg').datagrid('getSelected');
    if (row){
    	$('#dg').datagrid('rejectChanges');
		$('#dg').datagrid('selectRow', index).datagrid('beginEdit', index);
    }
    else{
    	$.messager.alert('提示','请选择需要修改的试卷信息','error');
    }
}
</script>
</head>
<body>
<div class="container-fluid">
	<%@ include file="../common/header.jsp"%>
	<div class="row-fluid">
		<%@ include file="../common/sub_menu.jsp"%>
		
		<div class="span10">
			<ul class="breadcrumb">
	            <li class="active">试卷检索</li>
            </ul>
			<div class="change">
				<form id="search-form" method="post">
		              	<ul>
			               	<li><span>试卷编号</span><input class="input-medium" type="text" name="code"  id="code" placeholder="试卷编号"></li>
			               	<!-- 
							<li><span>课程代码</span><input class="input-medium" type="text" name="courseCode" placeholder="课程代码" id="courseCode"/></li>
							<li><span>课程名称</span><input class="input-medium" type="text" name="courseName" placeholder="课程名称" id="courseName"/></li>
							<li><span>教材名称</span><input class="input-medium" type="text" name="bookName" placeholder="教材名称" id="bookName"/></li>
							<li><span>编者</span><input class="input-medium" type="text" name="bookAuthor" placeholder="编者" id="bookAuthor"/></li>
							<li><span>出版社</span><input class="input-medium" type="text" name="bookPress" placeholder="编者" id="bookPress"/></li>
			               	 -->
							
							<!-- 
							<li><span>批次查询</span><select id="batchId" name="batchId" class="easyui-combogrid"></select></li>
							<li><span>版次</span><input class="input-medium" type="text" name="bookPressYear" placeholder="编者" id="bookPressYear"/></li>
							<li><span>专卡</span>
								<select name="isSpecialAnswerSheet" class="select-medium">
									<option value="2">全部</option>
									<option value="1">有</option>
									<option value="0">不详</option>
								</select>
							</li>
							<li><span>网评卷</span>
								<select name="isSeparatePaperUploaded" class="select-medium">
									<option value="2">全部</option>
									<option value="1">有</option>
									<option value="0">无</option>
								</select>
							</li>
							 -->
							<li><span>试卷状态</span>
								<select name="status" class="select-medium">
									<option value="0">全部</option>
									<option value="DRAFT">毛坯</option>
									<option value="FORMAL">清样</option>
									<option value="USED">抽卷</option>
								</select>
							</li>
							<li>
								<!-- 
								<a id="clearBtn" href="#" type="submit" class="btn"><i class=" icon-trash"></i> 清空条件</a>
								 -->
								<a id="searchBtn" href="#" type="submit" class="btn"><i class="icon-search"></i> 查询</a>
							</li>
						</ul>
					</form>
				</div>
				<div style="clear:both;"></div>

				<div style="margin-left:10px; color: #bf290b; font-size: 14px; font-weight:bold;" id="paperInfo"></div>
				
				 <div title="试卷检索信息" style="padding:10px" >
				 	<table id="dg" class="easyui-datagrid"
						data-options="
                     url: '${ctx}/examPaper/paperSearch',
                     fitColumns:true,singleSelect:false,striped:true,pagination:true,selectOnCheck:true,checkOnSelect:true,
				      pageNumber:1,
				      pageSize:10,
     					  onLoadSuccess:load,
     					  toolbar: '#tb',
     					  onDblClickCell:DBClickCell,
     					  onSelect:selectRows">
						<thead>
							<tr class="title">
								<th rowspan="2" data-options="field:'ck',checkbox:true"></th>
								<th rowspan="2" data-options="field:'code',width:90">试卷编号</th>
								<th rowspan="2" data-options="field:'status',width:30,formatter: formatStatus">状态</th>
								<th rowspan="2" data-options="field:'isSpecialAnswerSheet',width:30 ,formatter:formatZK">专卡</th>
								<th rowspan="2" data-options="field:'courseCode',width:50">课程代码</th>
								<th rowspan="2" data-options="field:'courseName',width:150">课程名称</th>
								<th rowspan="2" data-options="field:'bookName',width:150,editor:{
                                   type:'validatebox',
                                   options:{
                                       required:true
                                       }
                                   }">教材名称</th>
								<th rowspan="2" data-options="field:'bookAuthor',width:80,editor:{
                                   type:'validatebox',
                                   options:{
                                       required:true
                                       }
                                   }">编者</th>
								<th rowspan="2" data-options="field:'bookPress',width:100,editor:{
                                   type:'validatebox',
                                   options:{
                                       required:true
                                       }
                                   }">出版社</th>
								<th rowspan="2" data-options="field:'bookPressYear',width:40,editor:{
                                   type:'validatebox',
                                   options:{
                                       required:true
                                       }
                                   }">版次</th>
								<th colspan="5">电子试卷</th>
							</tr>
							<tr>
					          <th data-options="field:'paperPath',width:30,styler:cellStyler,formatter: formatBooleanData">合卷</th>
					          <th data-options="field:'standardAnswerPath',width:30,styler:cellStyler,formatter: formatBooleanData">标答</th>
					          <th data-options="field:'separatePaperPath',width:30,styler:separatePaperCellStylerRuku,formatter:formatSeparentPaperData">分卷</th>
					          <th data-options="field:'wPaperPath',width:30,styler:separatePaperCellStylerRuku,formatter:formatSeparentPaperData">网评卷</th>
					          <th data-options="field:'answerSheetPath',width:30,styler:answerSheetDataCellStyler,formatter: formatAnswerSheetData">专卡</th>
					      </tr>
						</thead>
					</table>
					<div id="tb" style="height: auto">
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-download',plain:true" onclick="downDBF('${ctx}')">下载DBF</a> 
						 <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-edit',plain:true" onclick="editInfo()">编辑</a>
						 <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-save',plain:true" onclick="accept()">保存修改</a>
						 <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-cancel',plain:true" onclick="reject()">撤销编辑</a>
						 <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-confirm',plain:true" onclick="checkOver()">结束检查</a>
					</div>
				 </div>
		</div>
	</div>
</div>
<%@ include file="../common/footer.jsp"%>
</body>
</html>