<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript" src="${ctx}/resources/js/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
function onUsedPapersTabSelected(title, index){
    if(index == 0){
        $('#usedPapersDG').datagrid('reload');
    }else if(index == 1){
        $('#dbfPaperInfoDG').datagrid('reload');
    }else if(index==2){
    	 $('#repertoryDg').datagrid('reload');
    }
}

function autoMatch(){
	$.messager.confirm("操作提示", "是否确定自动匹配试卷?", function (data) {
		if(data){
			matchPapers('${paperBatch.id}');
		}
	});
}
function confirmUsedPapers(){
	var row = $('#usedPapersDG').datagrid('getSelected');
	var flag=true;
    if (row){
    	var arr = new Array();
		var rows = $('#usedPapersDG').datagrid("getSelections");
		for(var i =0;i<rows.length;i++){
			var rr=rows[i];
			
			if(rr.status=="DRAFT"||rr.status=="USED"){
				$.messager.alert('提示','选中的试卷中必须都为清样试卷才可以进行启用操作!','error');
				flag=false;
				break;
			}
			if(rr.isOutBoundScanned!=0){
				arr.push(rr.id);
			}else{
				$.messager.alert('提示','存在未出库扫描状态的试卷!','error');
				flag=false;
				break;
			}
		}

    	if(flag){
    		var obj={
            		"batchId" : ${paperBatch.id},
            		"array" : arr
            	};
            	var PaperParams=JSON.stringify(obj);
            	
            	$.ajax({
        			type : "POST",
        			url : "${ctx}/examPaper/confirmUsedPapers",
        			data :  PaperParams ,
        			contentType : "application/json; charset=utf-8",
        			dataType : "json",
        			success : function(callback) {
        				callback=eval(callback);
        				if(callback.type=="success"){
        					$.messager.alert('提示','确认启用完成','info');
        					$('#usedPapersDG').datagrid('reload');
        					$('#usedPapersDG').datagrid('clearChecked');
        				}
        			}
        		});
    	}
	}else{
		$.messager.confirm("操作提示", "是否进行全部启用操作?", function (data) {
			if (data) {
				$(document).mask('正在处理该批次下所有确认启用，请稍等...');
				var obj={
	            		"batchId" : ${paperBatch.id},
	            	};
	            	var PaperParams=JSON.stringify(obj);
	            	
	            	$.ajax({
	        			type : "POST",
	        			url : "${ctx}/examPaper/confirmUsedPapersAll",
	        			data :  PaperParams ,
	        			contentType : "application/json; charset=utf-8",
	        			dataType : "json",
	        			success : function(callback) {
	        				$(document).unmask();
	        				callback=eval(callback);
	        				if(callback.type=="success"){
	        					$.messager.alert("操作提示", "该批次下所有试卷确认启用完成！","info");
	        					$('#usedPapersDG').datagrid('reload');
	        				}else{
	        					$.messager.alert("操作提示", "确认启用失败！该批次下存在未出库扫描的试卷!","error");
	        				}
	        			}
	        		});
			}
		});
	}
}

function load(){
	var obj={
			"batchId" :${paperBatch.id}
		};
	$.ajax({
		type : "POST",
		url : "${ctx}/examPaper/getTotalCount",
		data :  JSON.stringify(obj) ,
		contentType : "application/json; charset=utf-8",
		dataType : "json",
		success : function(callback) {
			callback=eval(callback);
			var Rhtml="该批次下：已上传 "+callback.paperCount+" 套试卷 、"+callback.standardAnswerPathCount+" 套答案、"+callback.answerSheetPathCount+" 套专卡、" + callback.separatePaperPath + " 套分卷。已有 " + callback.wPaperPath + " 套网评卷。已有 "+callback.isOutBoundScannedCount+" 套试卷出库扫描、已有 "+callback.isStorageScannedCount+" 套试卷回库扫描";
			$("#paperInfo").html(Rhtml);
		}
	});
}

function revertUsedPapersToFormal(){
	var row = $('#usedPapersDG').datagrid('getSelected');
	var flag=true;
    if (row){
    	var arr = new Array();
		var rows = $('#usedPapersDG').datagrid("getSelections");
		for(var i =0;i<rows.length;i++){
			var rr=rows[i];
			if(rr.status!="USED"){
				$.messager.alert('提示','选中的试卷中必须都为启用试卷才可以进行撤销启用操作!');
				flag=false;
				break;
			}
			
			if(rr.isStoreageScanned!=0){
				arr.push(rr.id);
			}else{
				$.messager.alert('提示','存在未回库扫描状态的试卷!');
				flag=false;
				break;
			}
		}

    	if(flag){
    		var obj={
            		"batchId" : ${paperBatch.id},
            		"array" : arr
            	};
            	var PaperParams=JSON.stringify(obj);
            	
            	$.ajax({
        			type : "POST",
        			url : "${ctx}/examPaper/revertUsedPapersToFormal",
        			data :  PaperParams ,
        			contentType : "application/json; charset=utf-8",
        			dataType : "json",
        			success : function(callback) {
        				callback=eval(callback);
        				if(callback.type=="success"){
        					$.messager.alert('提示','撤销启用完成');
        					$('#usedPapersDG').datagrid('reload');
        					$('#usedPapersDG').datagrid('clearChecked');
        				}
        			}
        		});
    	}
	}else{
		$.messager.alert('提示','请选择一行进行撤销启用操作');
	}
}

/* function downDBFInfo(url,batchId){
	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/downDBFRepertory");
   	
   	var input2=$("<input>");
   	input2.attr("type","hidden");
   	input2.attr("name","paperBatchId");
   	input2.attr("value",batchId);
   	
   	
   	$("body").append(form);//将表单放置在web中
   	form.append(input2);
   	form.submit();//表单提交 
} */

function downExcelInfo(url,batchId){
	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/downExcelRepertory/"+batchId);
   	
   	var input2=$("<input>");
   	input2.attr("type","hidden");
   	input2.attr("name","batchId");
   	input2.attr("value",batchId);
   	
   	
   	$("body").append(form);//将表单放置在web中
   	form.append(input2);
   	form.submit();//表单提交 
}
</script>
<div align="left" style="padding-bottom: 0px; color: red; font-size: 14px;" id="paperInfo"></div>
<div class="easyui-tabs" style="width:auto;height:auto;" data-options="onSelect:onUsedPapersTabSelected">
   <div title="启用卷管理" style="padding:10px">
       <table id="usedPapersDG" class="easyui-datagrid"
    data-options="
      url: '${ctx}/examPaper/queryPaper/${paperBatch.id}',
      fitColumns:true
      ,singleSelect:false
      ,striped:true
      ,pagination:true
      ,toolbar: '#tb2',
      pageNumber:1,
      pageSize:10,
   	  onLoadSuccess:load">
        <thead>
            <tr class="title">
                <th rowspan="2" data-options="field:'ck',checkbox:true"></th>
                <th rowspan="2" data-options="field:'code',width:80,editor:{type:'validatebox',options:{required:true}},styler:codeCellStyler">试卷编码</th>
                <th rowspan="2" data-options="field:'status',width:30 ,editable:false,formatter: formatStatus">状态</th>
                <th rowspan="2" data-options="field:'isSpecialAnswerSheet',width:30 ,editable:false,formatter: formatZK">专卡</th>
                <th rowspan="2" data-options="field:'courseCode',width:45">课程代码</th>
                <th rowspan="2" data-options="field:'courseName',width:170">课程名称</th>
                <th rowspan="2" data-options="field:'bookName',width:200,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:110,editor:{type:'validatebox',options:{required:true}}">出版社</th>
                <th rowspan="2" data-options="field:'bookPressYear',width:30,editor:{type:'validatebox',options:{required:true}}">版次</th>
                <th rowspan="2" data-options="field:'school',width:110,editor:{type:'validatebox',options:{required:true}}">学校</th>
                <th rowspan="2" data-options="field:'isOutBoundScanned',width:30,styler:cellStyler,formatter: formatBooleanData"><p>出库</p><p>扫描</p></th>
                <!-- <th rowspan="2" data-options="field:'updatedDT',width:50">试卷下载</th> -->
                <th rowspan="2" data-options="field:'isStorageScanned',width:30,styler:cellStyler,formatter: formatBooleanData"><p>回库</p><p>扫描</p></th>
            </tr>
        </thead>
      </table>
      
      <div id="tb2" style="height:auto">
          <div style="margin-bottom:5px">
               <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-add',plain:true" onclick="addPapers('${ctx}','${paperBatch.id}')">添加试卷</a>
               <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-remove',plain:true" onclick="removePaperFromPapersDG('usedPapersDG', '${paperBatch.id}')">移出试卷</a>
               <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-download',plain:true" onclick="downLoadDBF('ZJ','${ctx}','${paperBatch.id}')">下载试卷信息</a>
               <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-download',plain:true" onclick="downLoadPaper('CJ','${ctx}','${paperBatch.id}')">下载电子试卷</a>
               <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-print',plain:true" onclick="printBatchPaperInfo('${ctx}','${paperBatch.id}')">打印试卷信息</a>
               <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-barcode-scan',plain:true" onclick="barcodeScanStart('W4')">出库扫描</a>
               <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-barcode-scan',plain:true" onclick="barcodeScanStart('W3','回库扫描')">回库扫描</a>
               <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-confirm',plain:true" onclick="confirmUsedPapers()">确认启用</a>
               <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-no',plain:true" onclick="revertUsedPapersToFormal()">撤销启用</a>
            </div>
            
            <div>
             <form id="search-formUsedPapers" method="post" >
	               <span>课程代码:</span>
	               <input id="courseCode" name="courseCode"/>
	               <span>状态：</span>
	               <select id="status" name="status">
	                   <option value="0">全部</option>
	                   <option value="DRAFT">毛坯</option>
	                   <option value="FORMAL">清样</option>
	                   <option value="USED">启用</option>
	               </select>
	               <span>出库扫描：</span>
	               <select id="isOutBoundScanned" name="isOutBoundScanned">
	                   <option value="0">全部</option>
	                   <option value="1">已扫描</option>
	                   <option value="2">未扫描</option>
	               </select>
	               <a href="#" class="easyui-linkbutton" iconCls="icon-search" id="searchUsedPapers">查找</a>
	               <a href="#" class="easyui-linkbutton clearSearch" iconCls="icon-undo" >清空条件</a>
               </form>
           </div>
       </div>
   </div>
   
   <div title="抽卷" style="padding:10px">
       <table id="dbfPaperInfoDG" class="easyui-datagrid"
    data-options="
      url: '${ctx}/dbfPaperInfo/queryDbfPaperInfo/${paperBatch.id}',
      fitColumns:true
      ,singleSelect:false
      ,striped:true
      ,pagination:true
      ,onClickCell:onClickCell
      ,toolbar: '#tb',
      pageNumber:1,
      pageSize:10,
   	onLoadSuccess:load,onSelect:selectParentRows">
        <thead>
            <tr class="title">
                <th rowspan="2" data-options="field:'ck',checkbox:true"></th>
                <th rowspan="2" data-options="field:'courseCode',width:40">课程代码</th>
                <th rowspan="2" data-options="field:'courseName',width:170">课程名称</th>
                <th rowspan="2" data-options="field:'bookName',width:180">教材名称</th>
                <th rowspan="2" data-options="field:'bookAuthor',width:70">编者</th>
                <th rowspan="2" data-options="field:'bookPress',width:100">出版社</th>
                <th rowspan="2" data-options="field:'bookPressYear',width:20">版次</th>
                <th rowspan="2" data-options="field:'relatedPaperNum',width:50,styler:relatedPaperCountCellStyler">关联试卷数量</th>
                <th rowspan="2" data-options="field:'df',width:30,formatter: formatDFData">操作</th>
            </tr>
        </thead>
      </table>
      
      <div id="tb" style="height:auto">
          <div style="margin-bottom:5px">
              <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-remove',plain:true" onclick="removeDbfPaperInfo()" >移出批次</a>
              <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-import-plan',plain:true" onclick="$('#uploadDbfPaperInfo').window('open')">导入课程与教材信息</a>
              <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-search',plain:true" onclick="autoMatch()">自动匹配试卷</a>
           </div>
           <div>
                <form id="dbfPaperInfo-search-form" method="post" >
                   <span>课程:</span>
                   <input name="keyword" placeholder="课程名称或编号"/>
                   <span>关联试卷数量:</span>
                   <select id="relatedPaperNum" name="relatedPaperNum">
                      <option value="-1">全部</option>
                      <c:forEach var="obj" items="${relatedPaperNums }">
                          <option value="${obj }">${obj }</option>    
                      </c:forEach>
                   </select>
                   <a href="#" class="easyui-linkbutton" iconCls="icon-search" id="dbfPaperInfoSearchBtn">查找</a>
               </form>
           </div>
       </div>
   </div>
   
   <div title="库存统计" style="padding:10px">
       <table id="repertoryDg" class="easyui-datagrid"
	    data-options="
	      url: '${ctx}/dbfPaperInfo/queryRepertory/${paperBatch.id}',
	      fitColumns:true
	      ,singleSelect:false
	      ,striped:true
	      ,pagination:true
	      ,onClickCell:onClickCell,
	       toolbar: '#tb1',
	      pageNumber:1,
	      pageSize:10,
	   	  onLoadSuccess:load">
        <thead>
            <tr class="title">
                <th rowspan="2" data-options="field:'courseCode',width:30">课程代码</th>
                <th rowspan="2" data-options="field:'courseName',width:120">课程名称</th>
                <th rowspan="2" data-options="field:'bookName',width:120">教材名称</th>
                <th rowspan="2" data-options="field:'bookAuthor',width:60">编者</th>
                <th rowspan="2" data-options="field:'bookPress',width:120">出版社</th>
                <th rowspan="2" data-options="field:'bookPressYear',width:80">版次</th>
                <th rowspan="2" data-options="field:'isGet',width:30,formatter: formatData">是否抽到</th>
                <th rowspan="2" data-options="field:'residueFormal',width:60">剩余可抽卷数量</th>
            </tr>
        </thead>
      </table>
      
       <div id="tb1" style="height:auto">
          <div style="margin-bottom:5px">
              <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-download',plain:true" onclick="downExcelInfo('${ctx}','${paperBatch.id}')">下载Excel报表</a>
              <%-- <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-download',plain:true" onclick="downDBFInfo('${ctx}','${paperBatch.id}')">下载DBF报表</a> --%>
           </div>
       </div> 
   </div>
</div>
<%@ include file="./dbfPaperInfo.jsp"%>