define(function(require, exports, module) {
	require('datetimepicker');
	require('datetimepickerCn');
	var global = require("../../js/global");
	var jGlobal = require("./faultDiagGlobal");
	var upload = require("./upload");
	var curUploadParams = {};
	
	function operateStyle(value, row, index) {
	    if(row.operate==1){
			return '<div>' +
	    			'<div class="table-btn copy" style="float: left; width: 87px;">复制故障ID</div>' +
	    			'<div class="table-btn view" style="float: left; width: 50px; margin-left: 5px;">查看</div>' +
			   '</div>';
		}else if(row.operate==0){
			return '<div>' +
	    			'<div class="table-btn upload" style="float: left; width: 87px;" align="center">未上传</div>' +
	    			'<div class="table-btn view" style="float: left; width: 50px; margin-left: 5px; ">查看</div>' +
			   '</div>';
		}
	    
	}

	
	function uploadDiagLogInfo(row, index) {
		var path = global.nwPath.resolve(global.tftpOperateInstance.queryLogFilePath(), row.logId);
		jGlobal.faultDiag.diagSavePath = path;
		
		curUploadParams.deviceName = row.deviceName;
		curUploadParams.ruleName = row.ruleName;
		curUploadParams.logParams = {};
		curUploadParams.logParams.deviceIp = row.deviceIp;
		curUploadParams.logParams.deviceType = row.deviceType;
		
		$("#uploadModal").modal('show');
		$("#copyBtn").hide();
		$("#copyBtn").click(function () {
			copy(row,index);
		});
	}
	
	function viewDiagLogInfo(row, index) {
//		console.log(row);
	    var path = global.nwPath.resolve(global.tftpOperateInstance.queryLogFilePath(), row.logId);
	    global.jsUtil.openDir(path);
	}
	function copy(row,index) {
		function queryCloudIdCbk(bool, cloudId){
			if (bool != true) {
				alert(undefined, "读取ID失败！");
				return;
			}
			var e=$("#contents");;
			e.val(cloudId);
			e.show();
			e.select(); 
			document.execCommand("Copy"); 
			alert(undefined, "复制成功,故障ID:"+cloudId + '<br>您可将此ID粘贴到CSOS系统中');
			e.hide();
		}
		var diagnose1 = row.diagnoseId;
		global.operDbIns.queryCloudId(diagnose1,queryCloudIdCbk); 
	}
	function getDiagLogInfo() {		
		function querydbInfo(err, rows){
			if (err != null) {
				alert(undefined, "读取日志信息失败！");
				return;
			}
			var diagInfo = [];
			for (var i = 0; i < rows.length; i++) {
				var tmpDiagInfo = {};
				var diagnose = null;
				tmpDiagInfo.operate = rows[i].is_upload;
				tmpDiagInfo.logId = rows[i].log_id;
				tmpDiagInfo.diagData = rows[i].diag_time;
				tmpDiagInfo.deviceModel = rows[i].device_type;
				tmpDiagInfo.ipAddr = rows[i].device_ip;
				tmpDiagInfo.faultType = rows[i].sub_rule_name;
//				tmpDiagInfo.savePath = rows[i].savePath;
				tmpDiagInfo.deviceName = rows[i].device_name;
				tmpDiagInfo.deviceIp = rows[i].device_ip;
				tmpDiagInfo.deviceType = rows[i].device_type;
				tmpDiagInfo.ruleName = rows[i].sub_rule_name;
				tmpDiagInfo.diagnoseId = rows[i].diagnose_id;
				diagInfo.push(tmpDiagInfo);	
			}	
			window.operateStyle = operateStyle;
			$('#diagLogTable').bootstrapTable('load', diagInfo);
		}
		var params = {};
		params.pageNum = 1;
		params.pageSize = 2000;
		global.operDbIns.queryLogsByCondition(params,querydbInfo);
	}
	
	function init() {
		initWindowEvent();
		initPageStatistic();
		initVariable();
		initTable();
		initEvent();
		initDom();
		resize();
		initGlobalEvent();
		
		function initWindowEvent() {
			window.operateStyle = operateStyle;
			window.operateEvents = {
			    'click .upload': function (e, value, row, index) {
			    	uploadDiagLogInfo(row, index);
			    },
			    'click .view': function (e, value, row, index) {
			    	viewDiagLogInfo(row, index);
			    },
				'click .copy': function (e, value, row, index) {
			    	copy(row, index);
			    }
			};
		}
		function initPageStatistic() {
			window.statRecordEle("faultDiag#diagLog");
		}
		function initVariable() {
			curUploadParams = {};
		}
		function initTable() {
			$('#diagLogTable').bootstrapTable({
		        data: []
		    });
			
			$(".fixed-table-body").addClass('custom-scroll');
			$("#diagLogTable").show();
			
			getDiagLogInfo();
		}
		function initEvent() {
			$('#selectTimeModal').on('hidden.bs.modal', function() {
				$("#startTime").val("");
				$("#endTime").val("");
			});
			
			$("#selectTimeModal .btn-confirm").click(function() {
				var startTime = $("#startTime").val();
				var endTime = $("#endTime").val();				
//				console.log(startTime);
//				console.log(endTime);
				
				function queryLogsByTimeCb(err, rows) {
					if (err != null) {
						alert(undefined, "读取日志信息失败！");
						return;
					}
					
					var diagInfo = [];
					for (var i = 0; i < rows.length; i++) {
						var tmpDiagInfo = [];
						tmpDiagInfo.logId = rows[i].log_id;
						tmpDiagInfo.diagData = rows[i].diag_time;
						tmpDiagInfo.deviceModel = rows[i].device_type;
						tmpDiagInfo.ipAddr = rows[i].device_ip;
						tmpDiagInfo.faultType = rows[i].sub_rule_name;
//						tmpDiagInfo.savePath = rows[i].savePath;
						diagInfo.push(tmpDiagInfo);	
						tmpDiagInfo.operate = rows[i].is_upload;						
					}
					
					$('#diagLogTable').bootstrapTable('load', diagInfo);
				}
				var params = {};
				params.pageNum = 1;
				params.pageSize = 2000;
				params.startTime = startTime;
				params.endTime = endTime;
//				global.logDbIns.queryLogsByTime(params, queryLogsByTimeCb);
				global.operDbIns.queryLogsByCondition(params,queryLogsByTimeCb);
			});
			
			$('#uploadModal').on('show.bs.modal', function() {
				$('#uploadModal .modal-body').load("./faultDiagMenu/html/upload.html", function() {
					console.log(curUploadParams);
					
					var productType = global.tftpOperateInstance.checkProductType(curUploadParams.deviceName);
					var emailSubject = $("#uploadFaultId").text() + " + " + productType.deviceType + " + " + curUploadParams.ruleName + "。";					
					upload.setEmailSubject(emailSubject);
					upload.setUploadParams(curUploadParams);
				});
			});
			
			$('#uploadModal .claim>span').click(function() {
				$('#claimModal').modal('show');
			});
			
			$('#uploadModal').on('hide.bs.modal', function() {
					getDiagLogInfo();
					$("#showId").hide();
					$("#bugId").hide();
			});
		}
		function initDom() {
			$('#selectTimeModal').modal({
				backdrop: 'static',
				show: false
			});
			
			$('#diagLogStartTime').datetimepicker({
		        language: 'zh-CN',
		        weekStart: 1,
		        todayBtn:  1,
				autoclose: 1,
				todayHighlight: 1,
				startView: 2,
				minView: 2,
				forceParse: 0
		    });
			
			$('#diagLogEndTime').datetimepicker({
		        language: 'zh-CN',
		        weekStart: 1,
		        todayBtn:  1,
				autoclose: 1,
				todayHighlight: 1,
				startView: 2,
				minView: 2,
				forceParse: 0
		    });
		}
		function resize() {
			var client_h = document.documentElement.clientHeight;
			var head_h = $(".main_menu").height();
			var body_h = client_h - head_h;
			
			$('#diagLogTable').bootstrapTable('resetView', {height : body_h - 5});
		}
		function initGlobalEvent() {
			global.exitPageCallback.exitFun = function () {
				$('#selectTimeModal').unbind();
				$("#selectTimeModal .btn-confirm").unbind();
				$('#uploadModal').unbind();
				$('#uploadModal .claim>span').unbind();
				
				return true;
			}
		}
	}
	exports.init = init;
});