<%@page import="com.qmth.qrzk.repository.utils.Consts"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../common/init.jsp"%>
<c:set var="skeletonStatus" value="<%=Consts.SkeletonStatus.dicMap%>" scope="request"/>
<c:set var="defaultSkeletonToApproveStatus" value="<%=Consts.SkeletonStatus.TOAPPROVE%>" scope="request"/>
<!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' />
<link rel='stylesheet' href='${ctx}/resources/css/user-center-style.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/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/jquery.alerts.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/swfupload/uploadHandlers.js"></script>

<%@ include file="../common/validation.jsp"%>
<title>学习中心</title>
</head>
<body>
 <%@ include file="../common/header.jsp"%>
    <div id="content" class="content">
        <%@ include file="../common/sub_menu.jsp"%>
        <div class="page" id="page" >
        <div class="page-header" >学习中心</div>
        <div class="page-body">
	        <div>
	            <form id="search-form" method="post" >
	            	<input type="hidden" name="categoryCode" id="categoryCode" value="${categoryCode }"/>
		            <input name="keyword" placeholder="课程代码或文件名称查询" style="width: 270px;"/>
		            <security:authorize ifAnyGranted="processManagement" >
		            	<input name="courseCode" value="" type="hidden" />
		            </security:authorize>
		            <security:authorize ifNotGranted="processManagement" >
		            	<input name="courseCode" value="1" type="hidden"/>
		            </security:authorize>
		            
		            <button id="searchBtn">查询</button>
	            </form>
			</div>
	        <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,striped:true,pagination:true,
	                     idField:'id',toolbar: '#tb'">
	                     <thead>
	                         <tr class="title">
	                         	 <th data-options="field:'id', checkbox:true"></th>
	                             <th data-options="field:'keyword',width:290">文件列表</th>
	                             <th data-options="field:'courseCode',width:290">课程代码</th>
	                         </tr>
	                     </thead>
	                   </table>
	                   <div id="tb" style="height:auto">
	                   		<security:authorize ifAnyGranted="processManagement" >
					       		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" id="uploadLearnCenterBtn">上传</a>
					       		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-no',plain:true" id="deleteLearnCenterBtn">删除</a>
					       	</security:authorize> 
					   </div>
	               </div>
	            </div>
	        </div>
        </div>
        
        <div id="uploadFileDiv" title="上传" class="easyui-window" data-options="modal:true,closed:true,iconCls:'icon-import'" style="width:500px;height:500px;padding:10px;overflow: scroll;">
        	<span>请输入课程代码：<input id="courseCodeUpload"style="width: 100px;"/>（为空表示所有用户均可查看。）</span>
        	<div style="margin-top:10px;">
	        	<span id="spanButtonPlaceholder"></span>
        	</div>
			<table style="bolder:1px solid;" cellpadding="5" width="380px;" id="uploadFileTable">
				
			</table>
		</div>
    </div>
</body>
</html>
<script language="javascript">
$(document).ready(function(){
	$('#search-form').submit(function(event){
        event.preventDefault(); 
        var param = $('#search-form').serializeJson();
        param = $.extend(param, {page:1,rows:10});
        $('#dg').datagrid({
        	'url':'${ctx}/learnCenter/queryLearnCenters',
        	'queryParams':param
        });
    });
    window.setTimeout(function(){$('#search-form').submit();},200);
    
    $("#dg").datagrid({
	    onDblClickRow: function (rowIndex, rowData) {
	    	window.open("${ctx}/attachment/download/" + rowData.attachmentId);
	    }
	});
    
    $("#deleteLearnCenterBtn").click(function(){
    	var ids = [];
    	var attIds = [];
    	var rows = $('#dg').datagrid('getSelections');
    	for(var i=0; i<rows.length; i++){
    		ids.push(rows[i].id);
    		attIds.push(rows[i].attachmentId)
    	}
    	
		var param ={'ids': ids.join(','), 'attIds': attIds.join(',')}; 	
    	
    	$(document).mask('正在处理，请稍等...');
    	
    	$.post('${ctx}/learnCenter/delete',param,function(res){
			$(document).unmask();
			$('#dg').datagrid('reload');
			if(res.flag=='failed'){
	           $.messager.alert('提示', res.msg, 'error', function () {
	        	   //do nothing
	           });
	           return false;
			}else{
			}
		});
    	
	});
    
    {//加载SwfUpload上传组件
		loadSwfUpload();
	}
	
	$("#uploadLearnCenterBtn").click(function(){
		$("#courseCodeUpload").val("");
		$("#uploadFileTable tr").remove();
		$("#uploadFileDiv").window('open'); 
	});
	
    var swfUpload = null;
	function loadSwfUpload() {
		swfUpload = new SWFUpload({
			 upload_url :"${ctx}/attachment/upload",
			 post_params: {"userId":"${sessionScope.web_session_user.id}"},
			 use_query_string : false,
			  
			// File Upload Settings
			file_size_limit : "50 MB",	// 50MB
			file_types : "*",
			file_types_description : "文件",
			file_upload_limit 	:	0,

			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,//选择好文件后提交
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,
			
			// Button Settings
			button_image_url : '${ctx}/resources/images/SmallSpyGlassWithTransperancy_17x18.png',
			button_placeholder_id : "spanButtonPlaceholder",
			button_width: 180,
			button_height: 18,
			button_text : '<span class="button">点击这里上传文件<span class="buttonSmall"></span></span>',
			button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 12px; } .buttonSmall { font-size: 10px; color: #ff0000;}',
			button_text_top_padding: 0,
			button_text_left_padding: 18,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor: SWFUpload.CURSOR.HAND,
			//button_action: SWFUpload.BUTTON_ACTION.SELECT_FILE,
			
			// Flash Settings
			flash_url : '${ctx}/resources/js/swfupload/swfupload.swf',

			custom_settings : {
				upload_target : "importDivFileProgressContainer"
			},
			// Debug Settings
			debug: false //是否显示调试窗口
		});
	};
	
	function uploadSuccess(file, serverData) {
		//$(document).unmask();
		serverData=eval('(' + serverData + ')');
		if(serverData.flag=='success'){
			//$("#uploadedFileName").text("已上传文件：" + serverData.att.originalName);
			//$("#uploadedAttId").val(serverData.att.id);
			
			$("#uploadFileTable").append("<tr><td>" + serverData.att.originalName + "</td></tr>");
			
			var categoryCode = $('#categoryCode').val();
			
			$.ajax({
				type : "POST",
				url : "${ctx}/learnCenter/uploadLearnCenter",
				data : JSON.stringify({'categoryCode': categoryCode, 'courseCode': $("#courseCodeUpload").val()
						, 'attachmentId': serverData.att.id, 'keyword': serverData.att.originalName}), 
				contentType : "application/json; charset=utf-8",
				dataType : "json",
				success : function(callback) {
					$(document).unmask();
					$('#dg').datagrid('reload');
					//$.messager.alert('提示', "保存成功!");
				}
			});
			
		}else{
			$.messager.alert('提示',serverData.msg,'error');
		}
	}
	
});
</script>
