<%@ page language="java" contentType="application/x-javascript; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.qmth.qrzk.repository.utils.General"%>
<%@ taglib uri="/WEB-INF/taglib/c.tld" prefix="c"%>
<c:set var="ctx" value="<%=General.getBasePath(request)%>" scope="request"/>
function fileQueueError(file, errorCode, message) {
	try {
		var imageName = "<font color='red'>文件上传错误</font>";
		var errorName = "";
		if (errorCode === SWFUpload.errorCode_QUEUE_LIMIT_EXCEEDED) {
			errorName = "You have attempted to queue too many files.";
		}

		if (errorName !== "") {
			alert(errorName);
			return;
		}
		
		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			imageName = "<font color='red'>文件大小为0</font>";
			break;
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			imageName = "<font color='red'>文件大小超过限制</font>";
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
		default:
			alert(message);
			break;
		}
		if(file.type=="qmq"){
		
		}else{
			addAccessoryFileInfo(file,imageName);
		}
		

	} catch (ex) {
		this.debug(ex);
	}
}

/**
 * 当文件选择对话框关闭消失时，如果选择的文件成功加入上传队列，
 * 那么针对每个成功加入的文件都会触发一次该事件（N个文件成功加入队列，就触发N次此事件）。
 * @param {} file
 * id : string,			    // SWFUpload控制的文件的id,通过指定该id可启动此文件的上传、退出上传等
 * index : number,			// 文件在选定文件队列（包括出错、退出、排队的文件）中的索引，getFile可使用此索引
 * name : string,			// 文件名，不包括文件的路径。
 * size : number,			// 文件字节数
 * type : string,			// 客户端操作系统设置的文件类型
 * creationdate : Date,		// 文件的创建时间
 * modificationdate : Date,	// 文件的最后修改时间
 * filestatus : number		// 文件的当前状态，对应的状态代码可查看SWFUpload.FILE_STATUS }
 */
function AccessoryFileQueued(file){
	swfu.startUpload();
}

function ImportFileQueued(file){
	swfuImport.startUpload();
}
function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesQueued > 0) {
			document.getElementById('btnCancel').disabled = "";
			//this.startUpload();
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadProgress(file, bytesLoaded) {
	try {
		var percent = Math.ceil((bytesLoaded / file.size) * 100);

		var progress = new FileProgress(file,  this.customSettings.upload_target);
		progress.setProgress(percent);
		if (percent === 100) {
			progress.setStatus("");//正在创建缩略图...
			progress.toggleCancel(false, this);
		} else {
			progress.setStatus("正在上传...");
			progress.toggleCancel(true, this);
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		addAccessoryFileInfo(file,"上传成功");
		setTimeout("display()",2000);
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadImportFileSuccess(file, serverData){
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		
		var obj = JSON.parse(serverData);
		if(obj.type=="success"){
			addFileInfo(file,obj.uuid,"导入文件成功");
		}else if(obj.type=="fail"){
			addFileInfo(file,obj.uuid,"未导入,文件已被导入过，再次导入可能会产生重复试题。");
			$("#"+file.id+"_again").show();
		}
		$("#"+file.id+" td:nth-child(2)").html(obj.uuid);
		setTimeout("display()",2000);
		
	} catch (ex) {
		this.debug(ex);
	}
}

function addFileInfo(file,uuid,status){
	//用表格显示
	var infoTable="";
	var str="";
	infoTable=document.getElementById("importTable");
	str="importTable";
	var row = infoTable.insertRow();
	row.id = file.id;
	var col1 = row.insertCell();
	var col2 = row.insertCell();
	var col3= row.insertCell();
	var col4 = row.insertCell();
	var col5 = row.insertCell();

	col1.innerHTML = file.name;
	col2.innerHTML=uuid;
	col3.innerHTML=file.size;
	if(status!=null&&status!=""){
		col4.innerHTML="<font color='red'>"+status+"</font>";
	}else{
		col4.innerHTML="";
	}
	col5.innerHTML = "<a href='javascript:void(0)' id=\""+file.id+"_again\" style=\"display: none\" onclick=\"aginImport('"+file.id+"')\">再次导入</a>&nbsp;&nbsp;&nbsp;<a href='javascript:deleteFile(\""+file.id+"\",\""+str+"\")'>删除</a>";
	col1.style.width="150";
	col2.style.width="150";
	col3.style.width="150";
	col4.style.width="150";
	col5.style.width="150";
}
function addAccessoryFileInfo(file,status){
	//用表格显示
	var infoTable="";
	var str="";
	infoTable=document.getElementById("infoTable");
	str="infoTable";
	var row = infoTable.insertRow();
	row.id = file.id;
	var col1 = row.insertCell();
	var col2 = row.insertCell();
	var col3= row.insertCell();
	var col4 = row.insertCell();

	col1.innerHTML = file.name;
	col2.innerHTML=file.size;
	if(status!=null&&status!=""){
		col3.innerHTML="<font color='red'>"+status+"</font>";
	}else{
		col3.innerHTML="";
	}
	col4.innerHTML="<a href='javascript:deleteFile(\""+file.id+"\",\""+str+"\")'>删除</a>";
	//col5.innerHTML = "<a href='javascript:void(0)' id=\""+fileid+"_again\" style=\"display: none\" onclick=\"aginImport('"+file.id+"')\">再次导入</a>&nbsp;&nbsp;&nbsp;<a href='javascript:deleteFile(\""+fileid+"\",\""+str+"\")'>删除</a>";
	col1.style.width="150";
	col2.style.width="150";
	col3.style.width="150";
	col4.style.width="150";
	//col5.style.width="150";
}

function viewImportHistory(){
	window.location.href="${ctx}/historylist";
}

function aginImport(fileid){
	var name=$("#"+fileid+" td:nth-child(1)").html();
	var obj={
			"id":$("#importBy").val(),
			"name":name
	};
	var User=JSON.stringify(obj);
	
	$.ajax({
		type : "POST",
		url : "${ctx}/againImportQuestion",
		data : User,
		dataType: "json",
		contentType : "application/json; charset=utf-8",
		success : function(callback) {
			callback = eval(callback);;
			if(callback.type=="success"){
				$("#"+fileid+" td:nth-child(4)").html("<font color='red'>导入文件成功</font>");
			}
			$("#"+fileid+"_again").hide();
		}
	});
}

function cancelUpload(str){
	var infoTable="";
	if(str=="importTable"){
		infoTable = document.getElementById("importTable");
	}else if(str=="importTable"){
		infoTable = document.getElementById("infoTable"); 
	}
	var rows = infoTable.rows;
	var ids = new Array();
	if(rows==null){
		return false;
	}
	for(var i=1;i<rows.length;i++){
		ids[i] = rows[i].id;
	}
	for(var i=1;i<ids.length;i++){
		deleteFile(ids[i],str);
	}
}


function deleteFile(fileId,str){
	var name="";
	if(str=="importTable"){
		name=$("#importTable tr:eq(1) td:eq(0)").html();
	}else if(str=="infoTable"){
		name=$("#infoTable tr:eq(1) td:eq(0)").html();
	}
	var obj = {
			"name" : name
		};
	var fileName = JSON.stringify(obj);
	$.ajax({
		type : "POST",
		url : "${ctx}/deleteFile",
		data : fileName,
		dataType: "json",
		contentType : "application/json; charset=utf-8",
		success : function(callback) {
		}
	});
	
	//用表格显示
	var	infoTable = document.getElementById(str);
	var row = document.getElementById(fileId);
	infoTable.deleteRow(row.rowIndex);
	if(str=="importTable"){
		swfuImport.cancelUpload(fileId,false);
	}else if(str=="infoTable"){
		swfu.cancelUpload(fileId,false);
	}
	
}

function uploadComplete(file) {
	try {
		/*  I want the next upload to continue automatically so I'll call startUpload here */
		if (this.getStats().files_queued > 0) {
			this.startUpload();
		} else {
			var progress = new FileProgress(file,  this.customSettings.upload_target);
			progress.setComplete();
			progress.setStatus("<font color='red'>文件上传完毕!</b></font>");
			progress.toggleCancel(false);
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function display(){
	$("#divFileProgressContainer").hide();
}

function uploadError(file, errorCode, message) {
	var imageName =  "<font color='red'>文件上传出错!</font>";
	var progress;
	try {
		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			try {
				progress = new FileProgress(file,  this.customSettings.upload_target);
				progress.setCancelled();
				progress.setStatus("<font color='red'>取消上传!</font>");
				progress.toggleCancel(false);
			}
			catch (ex1) {
				this.debug(ex1);
			}
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			try {
				progress = new FileProgress(file,  this.customSettings.upload_target);
				progress.setCancelled();
				progress.setStatus("<font color='red'>停止上传!</font>");
				progress.toggleCancel(true);
			}
			catch (ex2) {
				this.debug(ex2);
			}
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			imageName = "<font color='red'>文件大小超过限制!</font>";
			break;
		default:
			alert(message);
			break;
		}
		addFileInfo(file.id,imageName);
	} catch (ex3) {
		this.debug(ex3);
	}

}


function addImage(src) {
	var newImg = document.createElement("img");
	newImg.style.margin = "5px";

	document.getElementById("thumbnails").appendChild(newImg);
	if (newImg.filters) {
		try {
			newImg.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 0;
		} catch (e) {
			// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
			newImg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + 0 + ')';
		}
	} else {
		newImg.style.opacity = 0;
	}

	newImg.onload = function () {
		fadeIn(newImg, 0);
	};
	newImg.src = src;
}

function fadeIn(element, opacity) {
	var reduceOpacityBy = 5;
	var rate = 30;	// 15 fps


	if (opacity < 100) {
		opacity += reduceOpacityBy;
		if (opacity > 100) {
			opacity = 100;
		}

		if (element.filters) {
			try {
				element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';
			}
		} else {
			element.style.opacity = opacity / 100;
		}
	}

	if (opacity < 100) {
		setTimeout(function () {
			fadeIn(element, opacity);
		}, rate);
	}
}



/* ******************************************
 *	FileProgress Object
 *	Control object for displaying file info
 * ****************************************** */

function FileProgress(file, targetID) {
	this.fileProgressID = "divFileProgress";

	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;

		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";

		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));

		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode("上传文件: "+file.name));

		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";

		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";

		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);

		this.fileProgressWrapper.appendChild(this.fileProgressElement);
		document.getElementById(targetID).style.height = "55px";
		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
		fadeIn(this.fileProgressWrapper, 0);

	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
		this.fileProgressElement.childNodes[1].firstChild.nodeValue = "上传文件: "+file.name;
	}

	this.height = this.fileProgressWrapper.offsetHeight;

}
FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = percentage + "%";
};
FileProgress.prototype.setComplete = function () {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setError = function () {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setCancelled = function () {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setStatus = function (status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};

FileProgress.prototype.toggleCancel = function (show, swfuploadInstance) {
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (swfuploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].onclick = function () {
			swfuploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};
