define(function(require, exports, module) {
	require('countdown');
	var global = require("../../js/global");
	var jGlobal = require("./oneUpgradeGlobal");
	
	/* 定时重启 */
	var scheRebootDev = [];			 // 存储需要定时重启的设备
	var scheRebootTimeout = false;   // 定时时间到，置为true
	var scheRebootCopyDone = false;  // 下发完成，置为true
	
	/* 初始化热补丁变量 */
	var hotFixEnd = false;
	var hotFixStep = 10;
	var hotFixIndex = hotFixStep - 1;
	var hotFixUpgradeFlag = false;	//热补丁升级是否完成
	var bigVerUpgradeFlag = false;  //大版本升级是否完成
	var hotFixList = [];
 	var patchUpgradeStatistic = {}; //热补丁升级统计数据
	
	//热补丁升级对象
    //var hotFixUpgrade = window.require("../../common/js/hotFixUpgrade");
    var	hotFixUpgradeIns = new global.hotFixUpgrade(global.devicesIns);
	
	function devNameSty(value, row, index) {
		return '<div id=' + row.deviceName.replace(/\./g, '_') + '>' + value + '</div>';
	}
	
	function upgradeVersionStyle(value, row, index) {		
//		if (row.upgradeVersion == undefined || row.upgradeVersion == "") {
//			return;
//		}
//		
//		var tmp_upgradeVersion = row.upgradeVersion.split('\\');
//		return tmp_upgradeVersion[tmp_upgradeVersion.length - 1];
		
		return value;
	}
	
	function upgradeStatusStyle(value, row, index) {
		if (row.upgradeStatus == undefined || row.upgradeStatus == "" || row.upgradeStatus == "-") {
			return;
		}

		if (row.upgradeStatus == "升级") {
			return '<div class="table-btn upgrade" style="width: 50px;">升级</div>';
		} else if (row.upgradeStatus.indexOf("失败") != -1) { //(@???有失败的时候都加上红色)
			return '<span style="color:#f5224c;">' + row.upgradeStatus + '</span>';
		} else if (row.upgradeStatus == "升级成功") {
			return '<span style="color:#0cc757;">' + row.upgradeStatus + '</span>';
		} else {
			return '<span>' + row.upgradeStatus + '</span>';
		}
	}
	
//	function safeUpgrade() {
//		var selectDevList = $("#selectDeviceTable").bootstrapTable('getSelections');
//	    if (selectDevList.length == 0) {
//	        alert(undefined, "请至少选择一台设备.");
//	        return;
//	    }
//	    
//	    //初始化页面
//	    updateDomBeforeUpgrade();
//	    
//	    //初始化变量
//		jGlobal.oneUpgrade.curUpgradeTactic = "safeUpgrade";
//		jGlobal.oneUpgrade.safeUpgradeIndex = 0;
//		jGlobal.oneUpgrade.upgradeStatistic = {};
//	    
//	    //升级前检测
//		jGlobal.oneUpgrade.uploadId = global.uuid.v4(); //上传统计信息用
//	    if (checkBeforeUpgrade(selectDevList) == false) {
//	    	return;
//	    }
//		
//		//升级第一台设备,存储云端信息
//		var params = [];
//		var tmp_params = {};
//		tmp_params.deviceName = jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.safeUpgradeIndex].deviceName;
//		tmp_params.fileName = jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.safeUpgradeIndex].upgradeVersioPath;
//		params.push(tmp_params);
//		global.oneUpgradeIns.copyBinToDeviceInBatch(params);
//		
//		for (var i = 0; i < jGlobal.oneUpgrade.batchUpgradeDevList.length; i++) {
//			var id = "#" + jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName.replace(/\./g, '_');
//			var index = $(id).parent().parent().attr('data-index');
//			
//			if (i == 0) {
//				$("#selectDeviceTable").bootstrapTable('updateRow', {
//					index: index, 
//					row: {
//						upgradeStatus: "正在下发…"
//					}
//				});
//				
//				//存储云端信息
//				var uploadInfo = getUploadBasicInfo(jGlobal.oneUpgrade.batchUpgradeDevList[0]);
//				uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');			
//				jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[0].deviceName] = uploadInfo;
//			} else {
//				$("#selectDeviceTable").bootstrapTable('updateRow', {
//					index: index, 
//					row: {
//						upgradeStatus: "待升级…"
//					}
//				});
//			}
//		}		
//	}
//	
//	function fastUpgrade() {
//		var selectDevList = $("#selectDeviceTable").bootstrapTable('getSelections');
//	    if (selectDevList.length == 0) {
//	        alert(undefined, "请至少选择一台设备.");
//	        return;
//	    }
//	    
//	    //初始化页面
//	    updateDomBeforeUpgrade();
//	    
//	    //初始化变量
//		jGlobal.oneUpgrade.curUpgradeTactic = "fastUpgrade";
//		jGlobal.oneUpgrade.fastUpgradeEnd = false;
//		jGlobal.oneUpgrade.fastUpgradeFirstDev = true;
//		jGlobal.oneUpgrade.fastUpgradeIndex = jGlobal.oneUpgrade.fastUpgradeStep;
//		jGlobal.oneUpgrade.upgradeStatistic = {};
//	    
//	    //升级前检测
//		jGlobal.oneUpgrade.uploadId = global.uuid.v4(); //上传统计信息用
//	    if (checkBeforeUpgrade(selectDevList) == false) {
//	    	return;
//	    }
//		
//		//升级第一台设备,存储云端信息
//		var params = [];
//		var tmp_params = {};
//		tmp_params.deviceName = jGlobal.oneUpgrade.batchUpgradeDevList[0].deviceName;
//		tmp_params.fileName = jGlobal.oneUpgrade.batchUpgradeDevList[0].upgradeVersioPath;
//		params.push(tmp_params);
//		global.oneUpgradeIns.copyBinToDeviceInBatch(params);
//		
//		for (var i = 0; i < jGlobal.oneUpgrade.batchUpgradeDevList.length; i++) {
//			var id = "#" + jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName.replace(/\./g, '_');
//			var index = $(id).parent().parent().attr('data-index');
//			
//			if (i == 0) {
//				$("#selectDeviceTable").bootstrapTable('updateRow', {
//					index: index, 
//					row: {
//						upgradeStatus: "正在下发…"
//					}
//				});
//				
//				//存储云端信息
//				var uploadInfo = getUploadBasicInfo(jGlobal.oneUpgrade.batchUpgradeDevList[0]);
//				uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');			
//				jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[0].deviceName] = uploadInfo;
//			} else {
//				$("#selectDeviceTable").bootstrapTable('updateRow', {
//					index: index, 
//					row: {
//						upgradeStatus: "待升级…"
//					}
//				});
//			}
//		}	
//	}
	
	function speedUpgrade(upgradeWay) {
		var selectDevList = $("#selectDeviceTable").bootstrapTable('getSelections');
	    if (selectDevList.length == 0) {
	        alert(undefined, "请至少选择一台设备.");
	        return;
	    }
	    
	    //提取出升级大版本和升级热补丁
	    var bigVerList = [];
	    	hotFixList = [];
		for (var i = 0; i < selectDevList.length; i++) {
			if (selectDevList[i].upgradeVersion != "补丁版本") {
				bigVerList.push(selectDevList[i]);
			} else {
				hotFixList.push(selectDevList[i]);
			}
		}
	    
        //初始化页面
	    updateDomBeforeUpgrade();
	    
	    //初始化变量
        jGlobal.oneUpgrade.batchUpgradeDevList = [];
		jGlobal.oneUpgrade.batchUpgradeResult = {};
		jGlobal.oneUpgrade.countSuccessUpgrade = 0;
		jGlobal.oneUpgrade.countFailUpgrade = 0;
	    
	    //初始化升级大版本所需变量
		jGlobal.oneUpgrade.curUpgradeTactic = upgradeWay;
		jGlobal.oneUpgrade.speedUpgradeEnd = false;
		jGlobal.oneUpgrade.speedUpgradeIndex = jGlobal.oneUpgrade.speedUpgradeStep - 1;
		jGlobal.oneUpgrade.upgradeStatistic = {};
        
        if (upgradeWay == 'speedUpgrade') {
            statRecordEle("oneUpgrade#upgrading#speedUpgrade");
        } else if (upgradeWay == 'onlyCopy') {
            statRecordEle("oneUpgrade#upgrading#onlyCopy");
        }
        
	    //升级前检测
        jGlobal.oneUpgrade.uploadId = global.uuid.v4(); //上传统计信息用
        hotFixUpgradeFlag = false;
    	bigVerUpgradeFlag = false;
    	var bigVerResult = checkBeforeUpgrade(bigVerList);
        if (bigVerResult == false && hotFixList.length != 0) {
        	hotFixUpgradeFlag = false;
        	bigVerUpgradeFlag = true;
        } else if (bigVerResult == true && hotFixList.length == 0) {
        	hotFixUpgradeFlag = true;
        	bigVerUpgradeFlag = false;
        } else if (bigVerResult == false && hotFixList.length == 0) {
        	updateDomAfterUpgrade();
        	return;
	    }
	    
		//大版本升级前10台设备
		var bigVerParams = [];
		if (jGlobal.oneUpgrade.batchUpgradeDevList.length > jGlobal.oneUpgrade.speedUpgradeIndex) {
			for (var i = (jGlobal.oneUpgrade.speedUpgradeIndex - jGlobal.oneUpgrade.speedUpgradeStep + 1); i <= jGlobal.oneUpgrade.speedUpgradeIndex; i++) {
				var tmp_params = {};
				tmp_params.deviceName = jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName;
				tmp_params.fileName = jGlobal.oneUpgrade.batchUpgradeDevList[i].upgradeVersioPath;
				bigVerParams.push(tmp_params);
			}		
//			speedUpgradeIndex += speedUpgradeStep;
		} else {
			if (jGlobal.oneUpgrade.speedUpgradeEnd == false) {
				for (var i = (jGlobal.oneUpgrade.speedUpgradeIndex - jGlobal.oneUpgrade.speedUpgradeStep + 1); i < jGlobal.oneUpgrade.batchUpgradeDevList.length; i++) {
					var tmp_params = {};
					tmp_params.deviceName = jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName;
					tmp_params.fileName = jGlobal.oneUpgrade.batchUpgradeDevList[i].upgradeVersioPath;
					bigVerParams.push(tmp_params);
				}
				jGlobal.oneUpgrade.speedUpgradeIndex = jGlobal.oneUpgrade.batchUpgradeDevList.length - 1;
				jGlobal.oneUpgrade.speedUpgradeEnd = true;
			}
		}
		console.error("大版本升级队列： ", bigVerParams);
		if (bigVerParams.length > 0) {
			global.oneUpgradeIns.copyBinToDeviceInBatch(bigVerParams);
		}
		
		//初始化升级热补丁所需变量
		hotFixEnd = false;
		hotFixIndex = hotFixStep - 1;
		patchUpgradeStatistic = {};
		
		//热补丁升级前10台设备
		var hotFixParams = [];
		if (hotFixList.length > hotFixIndex) {
			for (var i = (hotFixIndex - hotFixStep + 1); i <= hotFixIndex; i++) {
				var tmp_params = {};
				tmp_params.deviceName = hotFixList[i].deviceName;
				tmp_params.commonHotFix = hotFixList[i].commonHotFix;
				tmp_params.upgradeHotFix = hotFixList[i].upgradeHotFix;
				hotFixParams.push(tmp_params);
			}
		} else {
			if (hotFixEnd == false) {
				for (var i = (hotFixIndex - hotFixStep + 1); i < hotFixList.length; i++) {
					var tmp_params = {};
					tmp_params.deviceName = hotFixList[i].deviceName;
					tmp_params.commonHotFix = hotFixList[i].commonHotFix;
					tmp_params.upgradeHotFix = hotFixList[i].upgradeHotFix;
					hotFixParams.push(tmp_params);
				}
				hotFixIndex = hotFixList.length - 1;
				hotFixEnd = true;
			}
		}
		console.error("升级补丁队列: ", hotFixParams);
		if (hotFixParams.length > 0) {
			hotFixUpgradeIns.hotFixForUpgrade(hotFixParams);
		}

		//大版本升级刷新表格,存储云端信息
		for (var i = 0; i < jGlobal.oneUpgrade.batchUpgradeDevList.length; i++) {
			var id = "#" + jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName.replace(/\./g, '_');
			var index = $(id).parent().parent().attr('data-index');

			if (i >= 0 && i < jGlobal.oneUpgrade.speedUpgradeStep) {
				$("#selectDeviceTable").bootstrapTable('updateRow', {
					index: index, 
					row: {
						upgradeStatus: "正在下发…"
					}
				});
				
				//存储云端信息
				var uploadInfo = getUploadBasicInfo(jGlobal.oneUpgrade.batchUpgradeDevList[i]);
				uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');			
				jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName] = uploadInfo;
			} else {
				$("#selectDeviceTable").bootstrapTable('updateRow', {
					index: index, 
					row: {
						upgradeStatus: "待升级…"
					}
				});
			}
		}
		
		//补丁升级刷新表格,存储云端信息
		for (var i = 0; i < hotFixList.length; i++) {
			var id = "#" + hotFixList[i].deviceName.replace(/\./g, '_');
			var index = $(id).parent().parent().attr('data-index');

			if (i >= 0 && i < hotFixStep) {
				$("#selectDeviceTable").bootstrapTable('updateRow', {
					index: index, 
					row: {
						upgradeStatus: "正在升级…"
					}
				});
				
				//存储云端信息 @???				
				var uploadInfo = getUploadBasicInfo(hotFixList[i]);
				var patchArray = hotFixList[i].upgradeHotFix.concat(hotFixList[i].commonHotFix); //待升级的补丁队列(包括升级框架和普通补丁)
				uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
				uploadInfo.patchArray = patchArray;
				patchUpgradeStatistic[hotFixList[i].deviceName] = uploadInfo;
			} else {
				$("#selectDeviceTable").bootstrapTable('updateRow', {
					index: index, 
					row: {
						upgradeStatus: "待升级…"
					}
				});
			}
		}
	}
	
	//定时时间到，重启升级队列数组中的所有设备
	function scheduleReboot() {
		var params = [];
		for (var i = 0; i < scheRebootDev.length; i++) {
			//重启设备
    		var params_tmp = {};
    		params_tmp.deviceName = scheRebootDev[i];
    		params.push(params_tmp);
			
			//刷新表格
			var id = "#" + scheRebootDev[i].replace(/\./g, '_');
			var index = $(id).parent().parent().attr('data-index');
			
			$("#selectDeviceTable").bootstrapTable('updateRow', {
				index: index, 
				row: {
					upgradeStatus: "正在升级…"
				}
			});
		}
		global.oneUpgradeIns.upgradeDevciesInBatch(params);
	}
	
	function checkBeforeUpgrade(selectDevList) {		
		if (selectDevList.length == 0) {
			return false;
		}
		
	    for (var i = 0; i < selectDevList.length; i++) {
	    	var id = "#" + selectDevList[i].deviceName.replace(/\./g, '_');
			var index = $(id).parent().parent().attr('data-index');
			
	    	if (selectDevList[i].upgradeVersion != undefined && selectDevList[i].upgradeVersion != "") {
	    		if (selectDevList[i].softVer == selectDevList[i].upgradeVersion) {
	    			jGlobal.oneUpgrade.batchUpgradeResult[selectDevList[i].deviceName] = "升级成功";
	    			jGlobal.oneUpgrade.countSuccessUpgrade++;
	    			
	    			$("#selectDeviceTable").bootstrapTable('updateRow', {
	    				index: index, 
	    				row: {
	    					upgradeStatus: "升级成功：已是最新版本"
	    				}
	    			});
	    			
	    			//存储云端信息
	    			var uploadInfo = getUploadBasicInfo(selectDevList[i]);
	    			uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
	    			uploadInfo.upgradeEndTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
	    			uploadInfo.upgradeFailReason = "";
	    			uploadInfo.upgradeResult = "已是最新版本"; 			
	    			jGlobal.oneUpgrade.upgradeStatistic[selectDevList[i].deviceName] = uploadInfo;
	    			
	    			continue;
	    		}

	    		if (global.oneUpgradeIns.checkIfCanUpgrade(selectDevList[i].deviceName) == true) {
	    			jGlobal.oneUpgrade.batchUpgradeResult[selectDevList[i].deviceName] = "未升级";
	    			jGlobal.oneUpgrade.batchUpgradeDevList.push(selectDevList[i]);
	    		} else {
	    			jGlobal.oneUpgrade.batchUpgradeResult[selectDevList[i].deviceName] = "升级失败";
	    			jGlobal.oneUpgrade.countFailUpgrade++;
	    			
	    			var failMsg = '';
	    			var devInfo = global.devicesIns.getDeviceInfoByDeviceName(selectDevList[i].deviceName);
	    			if (devInfo.deviceType.match(/Wireless/i) != null) {
	    				failMsg = '升级失败：请返回选择无线升级进行升级';
	    			} else {
	    				failMsg = '升级失败：不支持工具升级';
	    			}
	    			
	    			$("#selectDeviceTable").bootstrapTable('updateRow', {
	    				index: index, 
	    				row: {
	    					upgradeStatus: failMsg
	    				}
	    			});
	    			
	    			//存储云端信息
	    			var uploadInfo = getUploadBasicInfo(selectDevList[i]);
	    			uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
	    			uploadInfo.upgradeEndTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
	    			uploadInfo.upgradeFailReason = "";
	    			uploadInfo.upgradeResult = failMsg;
	    			jGlobal.oneUpgrade.upgradeStatistic[selectDevList[i].deviceName] = uploadInfo;
	    		}
	    	} else {
	    		jGlobal.oneUpgrade.batchUpgradeResult[selectDevList[i].deviceName] = "未升级";
	    	}
	    }
	    
	    //若本次升级的设备均已是最新版本或不支持升级,本次升级结束,上传至云端.
	    jGlobal.oneUpgrade.batchUpgradeCount = jGlobal.oneUpgrade.batchUpgradeDevList.length;
	    if (jGlobal.oneUpgrade.batchUpgradeDevList.length == 0) {
	    	statInfoIns.sendUpgradeInfo(jGlobal.oneUpgrade.upgradeStatistic);
	    	return false;
	    }
	    
	    return true;
	}
	
	function getUploadBasicInfo(uploadDevInfo) {
		var uploadBasicInfo = {};
		uploadBasicInfo.deviceName = uploadDevInfo.deviceName;
		uploadBasicInfo.deviceIp = uploadDevInfo.host;
		uploadBasicInfo.deviceSn = uploadDevInfo.seriaNo;
		uploadBasicInfo.deviceModel = uploadDevInfo.deviceModel;
		uploadBasicInfo.hardVer = uploadDevInfo.hardVer;
		uploadBasicInfo.softVer = uploadDevInfo.softVer;
		uploadBasicInfo.upgradeVersion = uploadDevInfo.upgradeVersion;
		uploadBasicInfo.fileMd5 = uploadDevInfo.fileMd5;
		uploadBasicInfo.upgradeTactic = jGlobal.oneUpgrade.curUpgradeTactic;
        uploadBasicInfo.uploadId = jGlobal.oneUpgrade.uploadId;

        // 获取到额外的信息
        var params = global.devicesIns.getDeviceInfoByDeviceName(uploadDevInfo.deviceName);
        uploadBasicInfo.customer = params.customer;
        uploadBasicInfo.subGroup = params.subGroup;
        uploadBasicInfo.otherInfo = global.devicesIns.getStoreData(params);

		return uploadBasicInfo;
	}
	
	/* 大版本升级完一台后升级下一台 */
	function speedUpgradeNextDev() {
		if (jGlobal.oneUpgrade.speedUpgradeIndex < (jGlobal.oneUpgrade.batchUpgradeDevList.length - 1)) {
			jGlobal.oneUpgrade.speedUpgradeIndex++;
			var params = [];
			var tmp_params = {};
			tmp_params.deviceName = jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.speedUpgradeIndex].deviceName;
			tmp_params.fileName = jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.speedUpgradeIndex].upgradeVersioPath;
			params.push(tmp_params);
			global.oneUpgradeIns.copyBinToDeviceInBatch(params);
			
			//更新表格
			var id = "#" + jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.speedUpgradeIndex].deviceName.replace(/\./g, '_');
			var index = $(id).parent().parent().attr('data-index');		
			$("#selectDeviceTable").bootstrapTable('updateRow', {
				index: index,
				row: {
					upgradeStatus: "正在下发…"
				}
			});
			
			//存储云端信息
			var uploadInfo = getUploadBasicInfo(jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.speedUpgradeIndex]);
			uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');			
			jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.speedUpgradeIndex].deviceName] = uploadInfo;
		}
	}
	
	/* 热补丁升级完一台后升级下一台 */
	function hotFixNextDev() {
		if (hotFixIndex < (hotFixList.length - 1)) {
			hotFixIndex++;
			var params = [];
			var tmp_params = {};
			tmp_params.deviceName = hotFixList[hotFixIndex].deviceName;
			tmp_params.commonHotFix = hotFixList[hotFixIndex].commonHotFix;
			tmp_params.upgradeHotFix = hotFixList[hotFixIndex].upgradeHotFix;
			params.push(tmp_params);
			hotFixUpgradeIns.hotFixForUpgrade(params);
			
			//更新表格
			var id = "#" + hotFixList[hotFixIndex].deviceName.replace(/\./g, '_');
			var index = $(id).parent().parent().attr('data-index');		
			$("#selectDeviceTable").bootstrapTable('updateRow', {
				index: index,
				row: {
					upgradeStatus: "正在下发…"
				}
			});
			
			//存储云端信息@???
			var uploadInfo = getUploadBasicInfo(hotFixList[hotFixIndex]);
			var patchArray = hotFixList[hotFixIndex].upgradeHotFix.concat(hotFixList[hotFixIndex].commonHotFix); //待升级的补丁队列(包括升级框架和普通补丁)
			uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
			uploadInfo.patchArray = patchArray;
			patchUpgradeStatistic[hotFixList[hotFixIndex].deviceName] = uploadInfo;
		}
	}

//	function safeUpgradeShowSelect(msg, res) {
//		alert(undefined, msg, function() {
//			//升级下一台设备
//			jGlobal.oneUpgrade.safeUpgradeIndex++;
//			
//			if (jGlobal.oneUpgrade.batchUpgradeCount == jGlobal.oneUpgrade.safeUpgradeIndex) {
//	    		alert(undefined, "安全升级全部完成！");
//
//	    		sortAfterUpgrade();
////	    		showUpgradeResult();
//	    		updateDomAfterUpgrade();
//	    		
//	    		//上传云端信息
//	    		var curIndex = jGlobal.oneUpgrade.safeUpgradeIndex - 1;
//	    		var curDeviceName = jGlobal.oneUpgrade.batchUpgradeDevList[curIndex].deviceName;
//	    		jGlobal.oneUpgrade.upgradeStatistic[curDeviceName].upgradeEndTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
//	    		if (msg.indexOf("成功") != -1) {
//	    			jGlobal.oneUpgrade.upgradeStatistic[curDeviceName].upgradeFailReason = "";
//	    			jGlobal.oneUpgrade.upgradeStatistic[curDeviceName].upgradeResult = "升级成功";
//	    		} else {
//	    			jGlobal.oneUpgrade.upgradeStatistic[curDeviceName].upgradeFailReason = res.reason;
//	    			jGlobal.oneUpgrade.upgradeStatistic[curDeviceName].upgradeResult = "升级失败";
//	    		}
//	    		if (isEmptyObject(jGlobal.oneUpgrade.upgradeStatistic) == false) {
//	    	    	//@???
//	    			statInfoIns.sendUpgradeInfo(jGlobal.oneUpgrade.upgradeStatistic);
//	    	    }
//	    		
//	    	    return;
//	    	}
//			
//			var params = [];
//			var tmp_params = {};
//			tmp_params.deviceName = jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.safeUpgradeIndex].deviceName;
//			tmp_params.fileName = jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.safeUpgradeIndex].upgradeVersioPath;
//			params.push(tmp_params);
//			global.oneUpgradeIns.copyBinToDeviceInBatch(params);
//			
//			var id_tmp = "#" + jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.safeUpgradeIndex].deviceName.replace(/\./g, '_');
//			var index_tmp = $(id_tmp).parent().parent().attr('data-index');
//			$("#selectDeviceTable").bootstrapTable('updateRow', {
//				index: index_tmp, 
//				row: {
//					upgradeStatus: "正在下发…"
//				}
//			});
//			
//			//存储云端信息
//			var uploadInfo = getUploadBasicInfo(jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.safeUpgradeIndex]);
//			uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');			
//			jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[jGlobal.oneUpgrade.safeUpgradeIndex].deviceName] = uploadInfo;
//		}, true, function() {
//			for (var i = (jGlobal.oneUpgrade.safeUpgradeIndex + 1); i < jGlobal.oneUpgrade.batchUpgradeDevList.length; i++) {
//				var id_tmp = "#" + jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName.replace(/\./g, '_');
//				var index_tmp = $(id_tmp).parent().parent().attr('data-index');
//				$("#selectDeviceTable").bootstrapTable('updateRow', {
//					index: index_tmp, 
//					row: {
//						upgradeStatus: "升级"
//					}
//				});
//			}
//			
//			sortAfterUpgrade();
////			showUpgradeResult();
//			updateDomAfterUpgrade();
//			
//			//上传云端信息
//			var curIndex = jGlobal.oneUpgrade.safeUpgradeIndex;
//			var curDeviceName = jGlobal.oneUpgrade.batchUpgradeDevList[curIndex].deviceName;
//			jGlobal.oneUpgrade.upgradeStatistic[curDeviceName].upgradeEndTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
//			if (msg.indexOf("成功") != -1) {
//				jGlobal.oneUpgrade.upgradeStatistic[curDeviceName].upgradeFailReason = "";
//				jGlobal.oneUpgrade.upgradeStatistic[curDeviceName].upgradeResult = "升级成功";
//			} else {
//				jGlobal.oneUpgrade.upgradeStatistic[curDeviceName].upgradeFailReason = res.reason;
//				jGlobal.oneUpgrade.upgradeStatistic[curDeviceName].upgradeResult = "升级失败";
//			}
//			if (isEmptyObject(jGlobal.oneUpgrade.upgradeStatistic) == false) {
//		    	//@???
//				statInfoIns.sendUpgradeInfo(jGlobal.oneUpgrade.upgradeStatistic);
//		    }
//		});
//	}
//
//	function fastUpgradeShowSelect(msg, res) {
//		alert(undefined, msg, function() {		
//			if (jGlobal.oneUpgrade.batchUpgradeCount == 0) { //快速升级只选了一台设备的情况	    	    
//	    	    return;
//	    	}
//			
//			var params = [];
//	    	if (jGlobal.oneUpgrade.batchUpgradeDevList.length - 1 > jGlobal.oneUpgrade.fastUpgradeIndex) {
//	    		for (var i = (jGlobal.oneUpgrade.fastUpgradeIndex - jGlobal.oneUpgrade.fastUpgradeStep + 1); i <= jGlobal.oneUpgrade.fastUpgradeIndex; i++) {
//	    			var id = "#" + jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName.replace(/\./g, '_');
//	        		var index = $(id).parent().parent().attr('data-index');
//	        		
//					var tmp_params = {};
//					tmp_params.deviceName = jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName;
//					tmp_params.fileName = jGlobal.oneUpgrade.batchUpgradeDevList[i].upgradeVersioPath;
//					params.push(tmp_params);
//					
//					$("#selectDeviceTable").bootstrapTable('updateRow', {
//						index: index, 
//						row: {
//							upgradeStatus: "正在下发…"
//						}
//					});
//					
//					//存储云端信息
//					var uploadInfo = getUploadBasicInfo(jGlobal.oneUpgrade.batchUpgradeDevList[i]);
//					uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');			
//					jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName] = uploadInfo;
//	    		}
//	    		jGlobal.oneUpgrade.fastUpgradeIndex += jGlobal.oneUpgrade.fastUpgradeStep;
//	    	} else {
//	    		if (jGlobal.oneUpgrade.fastUpgradeEnd == false) {
//	    			for (var i = (jGlobal.oneUpgrade.fastUpgradeIndex - jGlobal.oneUpgrade.fastUpgradeStep + 1); i < jGlobal.oneUpgrade.batchUpgradeDevList.length; i++) {
//	    				var id = "#" + jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName.replace(/\./g, '_');
//	            		var index = $(id).parent().parent().attr('data-index');
//
//						var tmp_params = {};
//						tmp_params.deviceName = jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName;
//						tmp_params.fileName = jGlobal.oneUpgrade.batchUpgradeDevList[i].upgradeVersioPath;
//						params.push(tmp_params);
//						
//						$("#selectDeviceTable").bootstrapTable('updateRow', {
//							index: index, 
//							row: {
//								upgradeStatus: "正在下发…"
//							}
//						});
//						
//						//存储云端信息
//						var uploadInfo = getUploadBasicInfo(jGlobal.oneUpgrade.batchUpgradeDevList[i]);
//						uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');			
//						jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName] = uploadInfo;
//	    			}
//	    			jGlobal.oneUpgrade.fastUpgradeEnd = true;
//	    		}
//	    	}
//	    	
//	    	global.oneUpgradeIns.copyBinToDeviceInBatch(params);
//	    	jGlobal.oneUpgrade.fastUpgradeFirstDev = false;
//		}, true, function() {
//			for (var i = 1; i < jGlobal.oneUpgrade.batchUpgradeDevList.length; i++) {
//				var id_tmp = "#" + jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName.replace(/\./g, '_');
//				var index_tmp = $(id_tmp).parent().parent().attr('data-index');
//				$("#selectDeviceTable").bootstrapTable('updateRow', {
//					index: index_tmp, 
//					row: {
//						upgradeStatus: "升级"
//					}
//				});
//			}
//			
//			sortAfterUpgrade();
////			showUpgradeResult();
//			updateDomAfterUpgrade();
//			
//			//上传云端信息
//			jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[0].deviceName].upgradeEndTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
//			if (msg.indexOf("成功") != -1) {
//				jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[0].deviceName].upgradeFailReason = "";
//				jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[0].deviceName].upgradeResult = "升级成功";
//			} else {
//				jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[0].deviceName].upgradeFailReason = res.reason;
//				jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[0].deviceName].upgradeResult = "升级失败";
//			}
//			if (isEmptyObject(jGlobal.oneUpgrade.upgradeStatistic) == false) {
//		    	//@???
//				statInfoIns.sendUpgradeInfo(jGlobal.oneUpgrade.upgradeStatistic);
//		    }
//		});
//	}
	
	function sortAfterUpgrade() {
		var devInfoAfterUpgrade = $("#selectDeviceTable").bootstrapTable('getData', false);
		
		var tmp_noUpgrade = [];
		var tmp_beUpgrade = [];
		var tmp_failUpgrade = [];
		var tmp_successUpgrade = [];
		for (var i = 0; i < devInfoAfterUpgrade.length; i++) {
			if (devInfoAfterUpgrade[i].upgradeStatus != '-' && devInfoAfterUpgrade[i].upgradeStatus != undefined) {
				if (devInfoAfterUpgrade[i].upgradeStatus.indexOf("成功") != -1) {
					tmp_successUpgrade.push(devInfoAfterUpgrade[i]);
				} else if (devInfoAfterUpgrade[i].upgradeStatus.indexOf("失败") != -1) {
					tmp_failUpgrade.push(devInfoAfterUpgrade[i]);
				} else {
					tmp_beUpgrade.push(devInfoAfterUpgrade[i]);
				}
			} else {
				tmp_noUpgrade.push(devInfoAfterUpgrade[i]);
			}
		}
		
		var devInfoAfterSort = tmp_noUpgrade.concat(tmp_beUpgrade, tmp_failUpgrade, tmp_successUpgrade);
		
		$('#selectDeviceTable').bootstrapTable('load', devInfoAfterSort);
//		return devInfoAfterSort;
	}
	
	function updateDomBeforeUpgrade() {
		/* 隐藏操作按钮 */
		$("#upgradeLabel").hide();
		$("#upgradeBack").hide();
		
		/* 更新步骤进度 */
		$(".curStepText div:last-child").removeClass('strongCurStepDiv');
		$(".curStepIcon div:nth-last-child(2)").removeClass('strongStepLine');
		$(".curStepIcon div:nth-last-child(1) img").attr('src', './image/step-icon-off.png');
		
		/* 显示升级成功和失败的台数 */
		$("#upgradeResult").hide();
		
		/* 禁止点击菜单切换到其他页面 */
		global.isLoadOnUpgrading = true;
	}
	
	function updateDomAfterUpgrade() {
		/* 显示操作按钮 */
		$("#upgradeLabel").show();
		$("#upgradeBack").show();
		
		/* 更新步骤进度 */
		$(".curStepText div:last-child").addClass('strongCurStepDiv');
		$(".curStepIcon div:nth-last-child(2)").addClass('strongStepLine');
		$(".curStepIcon div:nth-last-child(1) img").attr('src', './image/step-icon-on.png');
		
		/* 显示升级成功和失败的台数 */
		showUpgradeResult();
		
		/* 允许点击菜单切换到其他页面 */
		global.isLoadOnUpgrading = false;
	}
	
	function showUpgradeResult() {
//		if (jGlobal.oneUpgrade.curUpgradeTactic == "safeUpgrade") {
//			$("#upgradeTactic").text("安全升级");
//		} else if (jGlobal.oneUpgrade.curUpgradeTactic == "fastUpgrade") {
//			$("#upgradeTactic").text("快速升级");
//		} else if (jGlobal.oneUpgrade.curUpgradeTactic == "speedUpgrade") {
////			$("#upgradeTactic").text("极速升级");
//			$("#upgradeTactic").text("即刻升级");
//		} else if (jGlobal.oneUpgrade.curUpgradeTactic == "scheduleReboot") {
//			$("#upgradeTactic").text("定时重启");
//		} else if (jGlobal.oneUpgrade.curUpgradeTactic == "scheduleUpgrade") {
//			$("#upgradeTactic").text("定时升级");
//		}
		
		var success = jGlobal.oneUpgrade.countSuccessUpgrade;
		var fail = jGlobal.oneUpgrade.countFailUpgrade;
		var msg = "升级完成。成功：" + success + "台；失败：" + fail + "台。";
		
		$("#successNum").text(success);
		$("#failNum").text(fail);
		$("#upgradeResult").show();
		
		alert(undefined, msg);
	}
	
	function scheduleRebootDone() {
		alert(undefined, "定时重启全部完成！");

		sortAfterUpgrade();
//		showUpgradeResult();
		updateDomAfterUpgrade();
		
		//上传统计信息至云端
		if (isEmptyObject(jGlobal.oneUpgrade.upgradeStatistic) == false) {
	    	//@???
//			console.log('upgradeStatistic:!!!!!' + JSON.stringify(jGlobal.oneUpgrade.upgradeStatistic));
			statInfoIns.sendUpgradeInfo(jGlobal.oneUpgrade.upgradeStatistic);
	    }
	}
	
	function init() {
		initWindowEvent();
		initPageStatistic();
		removeGlobalEvent();
		initVariable();
		initTable();
		initEvent();
		initDom();
		resize();
		initGlobalEvent();
		
		function initWindowEvent() {
			window.devNameSty = devNameSty;
			window.upgradeVersionStyle = upgradeVersionStyle;
			window.upgradeStatusStyle = upgradeStatusStyle;
			window.upgradeStatusEvents = {
			    'click .upgrade': function (e, value, row, index) {
//			        upgradeCurDevice(row, index);
			    }
			};
//			window.safeUpgrade = safeUpgrade;
//			window.fastUpgrade = fastUpgrade;
			window.speedUpgrade = speedUpgrade;
		}
		function initPageStatistic() {
			statRecordEle("oneUpgrade#upgrading");
		}
		function initVariable() {
			scheRebootDev = [];
			scheRebootTimeout = false;
			scheRebootCopyDone = false;
			
			hotFixEnd = false;
			hotFixStep = 10;
			hotFixIndex = hotFixStep - 1;
			hotFixList = [];
			
			hotFixUpgradeFlag = false;
			bigVerUpgradeFlag = false;
			
			patchUpgradeStatistic = {};
			
			hotFixUpgradeIns = new global.hotFixUpgrade(global.devicesIns);
		}
		function removeGlobalEvent() {
			global.oneUpgradeIns.removeAllListeners("operResult");
			global.oneUpgradeIns.removeAllListeners("operFinish");
		}
		function initTable() {
			$('#selectDeviceTable').bootstrapTable({
		        data: []
		    });
			
			$(".fixed-table-body").addClass('custom-scroll');
			$("#selectDeviceTable").show();
			
			//再次读取设备信息,防止首次添加的设备这边还是没有信息的情况			
			var selectDevInfos = [];
			for (var i = 0; i < jGlobal.oneUpgrade.selectedDevice.length; i++) {
				var selectDevInfo = global.devicesIns.getDeviceInfoByDeviceName(jGlobal.oneUpgrade.selectedDevice[i].deviceName);
				selectDevInfos.push(selectDevInfo);
			}
			console.error(selectDevInfos);
			
			//加载大版本数据
		    for (var i = 0; i < selectDevInfos.length; i++) {
		    	if (jGlobal.oneUpgrade.multiDevUpgradeVersion[selectDevInfos[i].deviceName] != "" && jGlobal.oneUpgrade.multiDevUpgradeVersion[selectDevInfos[i].deviceName] != undefined) {
		    		selectDevInfos[i].upgradeVersion = jGlobal.oneUpgrade.multiDevUpgradeVersion[selectDevInfos[i].deviceName].versionCode;
		    		selectDevInfos[i].upgradeVersioPath = global.nwPath.resolve(global.oneUpgradeIns.getRootDir(), jGlobal.oneUpgrade.multiDevUpgradeVersion[selectDevInfos[i].deviceName].versionName);
		    		selectDevInfos[i].fileMd5 = jGlobal.oneUpgrade.multiDevUpgradeVersion[selectDevInfos[i].deviceName].fileMd5;
		    		selectDevInfos[i].upgradeStatus = "-";
		    	}
		    }
		    
		    //加载热补丁数据
		    var multiDevHotFix = jGlobal.oneUpgrade.multiDevHotFix;
			for (var i = 0; i < selectDevInfos.length; i++) {
				if (multiDevHotFix[selectDevInfos[i].deviceName] != undefined && multiDevHotFix[selectDevInfos[i].deviceName].length != 0) {
					selectDevInfos[i].upgradeVersion = "补丁版本";
					selectDevInfos[i].upgradeVersioPath = [];
					selectDevInfos[i].upgradeHotFix = [];
		    		selectDevInfos[i].commonHotFix = [];
		    		var patchArray = multiDevHotFix[selectDevInfos[i].deviceName];
					for (var j = 0; j < patchArray.length; j++) {
						if (patchArray[j].isUpgradeFrame) { //升级补丁
							var tmp = {};
							tmp.name = patchArray[j].name;
							if (patchArray[j].compileVer.match('cm') != null) {
								tmp.type = 'cm';
							} else if (patchArray[j].compileVer.match('lc') != null) {
								tmp.type = 'lc';
							} else if (patchArray[j].compileVer.match('fw') != null) {
								tmp.type = 'fw';
							} else {
								tmp.type = 'cm';
							}
							selectDevInfos[i].upgradeHotFix.push(tmp);
						} else { //普通补丁
							selectDevInfos[i].commonHotFix.push(patchArray[j].name);
						}
		    		}
		    		selectDevInfos[i].upgradeStatus = "-";
				}
			}
		    
		    //剔除没有版本的数据
		    var upgradeDevice = [];
		    for (var i = 0; i < selectDevInfos.length; i++) {
		    	if (selectDevInfos[i].upgradeVersion != '' && selectDevInfos[i].upgradeVersion != '-' && selectDevInfos[i].upgradeVersion != null) {
		    		upgradeDevice.push(selectDevInfos[i]);
		    	}
		    }
		    $('#selectDeviceTable').bootstrapTable('load', upgradeDevice);
		}
		function initEvent() {
			$("#upgradeBack").click(function() {
				$(".main_content").load("./oneUpgrade/html/oneUpgradeMenu.html");
			});
			
			$("#upgradeLabel").click(function() {
		    	if ($(".btn-upgradeToggle").is(':visible') == false) {
		    		$(".btn-upgradeToggle").show();
		    	} else {
		    		$(".btn-upgradeToggle").hide();
		    	}
		    	
		    	return false;
 		    });
//		    $(".btn-upgradeWay").click(function() {
//		    	$(".btn-upgradeToggle").hide();
//		    });
		    $("body").click(function() {
		    	$(".btn-upgradeToggle").hide();
		    });
		    
		    global.oneUpgradeIns.on("operResult", function(res) {
		    	var id = "#" + res.deviceName.replace(/\./g, '_');
		    	var index = $(id).parent().parent().attr('data-index');
		    	
		    	if (res.process == "copyBatch") {
		    		console.log("---------------------大版本单台下发完成-------------------------");
		        	console.log(res);
		        	
		        	if (res.result == "成功") {
		        		if (jGlobal.oneUpgrade.curUpgradeTactic == "speedUpgrade") {		  //极速升级
		        			$("#selectDeviceTable").bootstrapTable('updateRow', {
			    				index: index, 
			    				row: {
			    					upgradeStatus: "正在升级…"
			    				}
			    			});
		        			
		        			var params = [];
			        		var params_tmp = {};
			        		params_tmp.deviceName = res.deviceName;
			        		params.push(params_tmp);
			        		global.oneUpgradeIns.upgradeDevciesInBatch(params); //一台下发成功，马上升级该台
			        		
		        			speedUpgradeNextDev(); //对于极速升级，某台下发成功，马上下发下一台升级
		        		} else if (jGlobal.oneUpgrade.curUpgradeTactic == "scheduleReboot") { //定时重启
		        			$("#selectDeviceTable").bootstrapTable('updateRow', {
			    				index: index, 
			    				row: {
			    					upgradeStatus: "下发成功"
			    				}
			    			});
		        			
		        			scheRebootDev.push(res.deviceName);		        			
		        			speedUpgradeNextDev(); //对于定时重启，某台下发成功，马上下发下一台升级
		        		} else if (jGlobal.oneUpgrade.curUpgradeTactic == "onlyCopy") { //仅下发
		        			$("#selectDeviceTable").bootstrapTable('updateRow', {
			    				index: index, 
			    				row: {
			    					upgradeStatus: "下发成功"
			    				}
			    			});
		        			
		        			//存储云端信息			
			        		jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeEndTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
			        		jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeFailReason = "";
			        		jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeResult = "下发成功";
		        			
		        			jGlobal.oneUpgrade.countSuccessUpgrade++;		        			
		        			speedUpgradeNextDev();
		        		}
		        	} else if (res.result == "失败") {
		        		$("#selectDeviceTable").bootstrapTable('updateRow', {
		    				index: index, 
		    				row: {
		    					upgradeStatus: "下发失败：" + res.reason
		    				}
		    			});
		        		
		        		//存储云端信息			
		        		jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeEndTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
		        		jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeFailReason = res.reason;
		    			jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeResult = "升级失败";
		        		
		        		if (jGlobal.oneUpgrade.curUpgradeTactic == "speedUpgrade") {			//极速升级
		        			jGlobal.oneUpgrade.batchUpgradeResult[res.deviceName] = "升级失败";
		        			jGlobal.oneUpgrade.countFailUpgrade++;
		        			jGlobal.oneUpgrade.batchUpgradeCount--;
		            		
		            		speedUpgradeNextDev(); //某台升级失败，马上下发下一台升级
		            	} else if (jGlobal.oneUpgrade.curUpgradeTactic == "scheduleReboot") {   //定时重启
		            		jGlobal.oneUpgrade.batchUpgradeResult[res.deviceName] = "升级失败";
		        			jGlobal.oneUpgrade.countFailUpgrade++;
		        			jGlobal.oneUpgrade.batchUpgradeCount--;
		            		
		        			speedUpgradeNextDev(); //某台升级失败，马上下发下一台升级
		        		} else if (jGlobal.oneUpgrade.curUpgradeTactic == "onlyCopy") { //仅重启
		        			jGlobal.oneUpgrade.batchUpgradeResult[res.deviceName] = "升级失败";
		        			jGlobal.oneUpgrade.countFailUpgrade++;
		        			jGlobal.oneUpgrade.batchUpgradeCount--;
		        			
		        			speedUpgradeNextDev();
		        		}
//		        		else if (jGlobal.oneUpgrade.curUpgradeTactic == "safeUpgrade") {		//安全升级
//		            		jGlobal.oneUpgrade.batchUpgradeResult[res.deviceName] = "升级失败";
//		            		jGlobal.oneUpgrade.countFailUpgrade++;
//		            		
//		            		var msg = "下发失败！失败原因：" + res.reason + "。是否继续升级下一台设备？";
//		            		safeUpgradeShowSelect(msg, res);
//		            	} else if (jGlobal.oneUpgrade.curUpgradeTactic == "fastUpgrade") {		//快速升级
//		            		jGlobal.oneUpgrade.batchUpgradeResult[res.deviceName] = "升级失败";
//		            		jGlobal.oneUpgrade.countFailUpgrade++;            		
//		            		jGlobal.oneUpgrade.batchUpgradeCount--;
//		            		
//		            		if (jGlobal.oneUpgrade.fastUpgradeFirstDev == true) {	//快速升级，对于第一台设备下发失败才提示用户是否进行接下去的升级
//		            			var msg = "下发失败！失败原因：" + res.reason + "。是否继续升级其余设备？";
//		            			fastUpgradeShowSelect(msg, res);
//		            		}
//		            	} else if (jGlobal.oneUpgrade.curUpgradeTactic == "singleUpgrade") {	//单台升级
//		            		jGlobal.oneUpgrade.singleUpgradeCount--;
//		            	}
		        	}
		    	} else if (res.process == "upgradeBatch") {
		    		console.log("---------------------大版本单台升级完成-------------------------");
		        	console.log(res);
		        	
		        	if (res.result == "成功") {
		        		jGlobal.oneUpgrade.batchUpgradeResult[res.deviceName] = "升级成功";
		        		jGlobal.oneUpgrade.countSuccessUpgrade++;
		        		
		        		$("#selectDeviceTable").bootstrapTable('updateRow', {
		    				index: index,
		    				row: {
		    					upgradeStatus: "升级成功"
		    				}
		    			});
		        		
		        		//存储云端信息			
		        		jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeEndTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
		        		jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeFailReason = "";
		        		jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeResult = "升级成功";
		        	} else if (res.result == "失败") {
		        		jGlobal.oneUpgrade.batchUpgradeResult[res.deviceName] = "升级失败";
		        		jGlobal.oneUpgrade.countFailUpgrade++;
		        		
		        		$("#selectDeviceTable").bootstrapTable('updateRow', {
		    				index: index, 
		    				row: {
		    					upgradeStatus: "升级失败：" + res.reason
		    				}
		    			});
		        		
		        		//存储云端信息			
		        		jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeEndTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
		        		jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeFailReason = res.reason;
		        		jGlobal.oneUpgrade.upgradeStatistic[res.deviceName].upgradeResult = "升级失败";
		        	}
		        	
		        	if (jGlobal.oneUpgrade.curUpgradeTactic == "speedUpgrade") {			//极速升级
		        		jGlobal.oneUpgrade.batchUpgradeCount--;
		        		speedUpgradeNextDev(); //某台升级完成，马上下发下一台升级
		        	} else if (jGlobal.oneUpgrade.curUpgradeTactic == "scheduleReboot") {   //定时重启
		        		jGlobal.oneUpgrade.batchUpgradeCount--;
		        	}
//		        	else if (jGlobal.oneUpgrade.curUpgradeTactic == "safeUpgrade") {		//安全升级,在此处提示用户是否升级下一台设备
//		        		if (res.result == "成功") {
//		        			var msg = "升级成功！是否继续升级下一台设备？";
//		        		} else if (res.result == "失败") {
//		        			var msg = "升级失败！失败原因：" + res.reason + "。是否继续升级下一台设备？";
//		        		}        		
//		        		safeUpgradeShowSelect(msg, res);
//		        	} else if (jGlobal.oneUpgrade.curUpgradeTactic == "fastUpgrade") {		//快速升级,在此处提示用户是否升级接下去的设备
//		        		jGlobal.oneUpgrade.batchUpgradeCount--;
//		        		if (jGlobal.oneUpgrade.fastUpgradeFirstDev == true) {				//快速升级，对于第一台设备下发失败才提示用户是否进行接下去的升级
//		        			if (res.result == "成功") {
//		            			var msg = "升级成功！是否继续升级其余设备？";
//		            		} else if (res.result == "失败") {
//		            			var msg = "升级失败！失败原因：" + res.reason + "。是否继续升级其余设备？";
//		            		}
//		        			fastUpgradeShowSelect(msg, res);
//		        		}
//		        	} else if (jGlobal.oneUpgrade.curUpgradeTactic == "singleUpgrade") {	//单台升级
//		        		jGlobal.oneUpgrade.singleUpgradeCount--;
//		        	}
		    	}
		    });
		    
		    global.oneUpgradeIns.on("operFinish", function(res) {
		    	bigVerUpgradeFlag = true;
		    	
		    	if (res.process == "copyBatch") {
		    		console.log("----------------------大版本全部下发完成------------------------");
		    		
		    		if (jGlobal.oneUpgrade.curUpgradeTactic == "scheduleReboot") {   //定时重启
		    			scheRebootCopyDone = true;
		    			
		    			if (scheRebootDev.length == 0) { //下发全部失败的情况
		    				$("#cancel").trigger('click');
		    				$('#scheduleModal').modal('hide');
		    				scheduleRebootDone();
		    				return;
		    			}
		    			
		    			if (scheRebootTimeout == true) { //下发完成，定时时间到，重启升级设备
							$('#scheduleModal').modal('hide');
							scheduleReboot();
						}
		    		} else if (jGlobal.oneUpgrade.curUpgradeTactic == "onlyCopy") { //仅下发
		    			alert(undefined, "全部下发完成！");

			    		sortAfterUpgrade();
//			    		showUpgradeResult();
			    		updateDomAfterUpgrade();
			    		
			    		//上传统计信息至云端
			    		if (isEmptyObject(jGlobal.oneUpgrade.upgradeStatistic) == false) {
			    	    	//@???
			    			console.log('upgradeStatistic:!!!!!' + JSON.stringify(jGlobal.oneUpgrade.upgradeStatistic));
			    			statInfoIns.sendUpgradeInfo(jGlobal.oneUpgrade.upgradeStatistic);
			    	    }
		    		}
		    	} else if (res.process == "upgradeBatch") {
		    		console.log("---------------------大版本全部升级完成-------------------------");
		    		
		    		if (jGlobal.oneUpgrade.curUpgradeTactic == "scheduleReboot") {   //定时重启
		    			scheduleRebootDone();
		    		}
		    	}
		        	
		    	if (jGlobal.oneUpgrade.curUpgradeTactic == "speedUpgrade") {			// 极速升级
		        	if (jGlobal.oneUpgrade.speedUpgradeIndex == (jGlobal.oneUpgrade.batchUpgradeDevList.length - 1)) {
			    		console.log("大版本升级全部完成！");

			    		if (hotFixUpgradeFlag && bigVerUpgradeFlag) {
			    			sortAfterUpgrade();
			    			updateDomAfterUpgrade();
			    		}
			    		
			    		//上传统计信息至云端
			    		if (isEmptyObject(jGlobal.oneUpgrade.upgradeStatistic) == false) {
			    	    	//@???
//			    			console.log('upgradeStatistic:!!!!!' + JSON.stringify(jGlobal.oneUpgrade.upgradeStatistic));
			    			statInfoIns.sendUpgradeInfo(jGlobal.oneUpgrade.upgradeStatistic);
			    	    }
		        	}
		    	}
//		    	else if (jGlobal.oneUpgrade.curUpgradeTactic == "safeUpgrade") {		//安全升级
//		    		
//		    	} else if (jGlobal.oneUpgrade.curUpgradeTactic == "fastUpgrade") {		//快速升级
//		    		if (jGlobal.oneUpgrade.fastUpgradeFirstDev == false) {				//快速升级，第一台升级完成由用户确认后升级接下去的5台设备，5台设备都升级完成后，进入此处继续升级接下去的5台设备
//		    			var params = [];
//		            	if (jGlobal.oneUpgrade.batchUpgradeDevList.length - 1 > jGlobal.oneUpgrade.fastUpgradeIndex) {
//		            		for (var i = (jGlobal.oneUpgrade.fastUpgradeIndex - jGlobal.oneUpgrade.fastUpgradeStep + 1); i <= jGlobal.oneUpgrade.fastUpgradeIndex; i++) {
//		            			var id = "#" + jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName.replace(/\./g, '_');
//		                		var index = $(id).parent().parent().attr('data-index');
//		                		
//								var tmp_params = {};
//								tmp_params.deviceName = jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName;
//								tmp_params.fileName = jGlobal.oneUpgrade.batchUpgradeDevList[i].upgradeVersioPath;
//								params.push(tmp_params);
//								
//								$("#selectDeviceTable").bootstrapTable('updateRow', {
//									index: index, 
//									row: {
//										upgradeStatus: "正在下发…"
//									}
//								});
//								
//								//存储云端信息
//								var uploadInfo = getUploadBasicInfo(jGlobal.oneUpgrade.batchUpgradeDevList[i]);
//								uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');			
//								jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName] = uploadInfo;
//		            		}
//		            		jGlobal.oneUpgrade.fastUpgradeIndex += jGlobal.oneUpgrade.fastUpgradeStep;
//		            	} else {
//		            		if (jGlobal.oneUpgrade.fastUpgradeEnd == false) {
//		            			for (var i = (jGlobal.oneUpgrade.fastUpgradeIndex - jGlobal.oneUpgrade.fastUpgradeStep + 1); i < jGlobal.oneUpgrade.batchUpgradeDevList.length; i++) {
//		            				var id = "#" + jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName.replace(/\./g, '_');
//		                    		var index = $(id).parent().parent().attr('data-index');
//
//									var tmp_params = {};
//									tmp_params.deviceName = jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName;
//									tmp_params.fileName = jGlobal.oneUpgrade.batchUpgradeDevList[i].upgradeVersioPath;
//									params.push(tmp_params);
//									
//									$("#selectDeviceTable").bootstrapTable('updateRow', {
//										index: index, 
//										row: {
//											upgradeStatus: "正在下发…"
//										}
//									});
//									
//									//存储云端信息
//									var uploadInfo = getUploadBasicInfo(jGlobal.oneUpgrade.batchUpgradeDevList[i]);
//									uploadInfo.upgradeStartTime = global.moment().format('YYYY-MM-DD HH:mm:ss');			
//									jGlobal.oneUpgrade.upgradeStatistic[jGlobal.oneUpgrade.batchUpgradeDevList[i].deviceName] = uploadInfo;
//		            			}			
//		            			jGlobal.oneUpgrade.fastUpgradeEnd = true;
//		            		}
//		            	}
//		            	
//		            	global.oneUpgradeIns.copyBinToDeviceInBatch(params);                	
//		    		}
//
//		    		if (jGlobal.oneUpgrade.batchUpgradeCount == 0) {
//		        		alert(undefined, "快速升级全部完成！");
//
//		        		sortAfterUpgrade();
////		        		showUpgradeResult();
//		        		updateDomAfterUpgrade();
//		        		
//		        		//上传统计信息至云端
//			    		if (isEmptyObject(jGlobal.oneUpgrade.upgradeStatistic) == false) {
//			    	    	//@???
//			    			statInfoIns.sendUpgradeInfo(jGlobal.oneUpgrade.upgradeStatistic);
//			    	    }
//		        	}
//		    	} else if (jGlobal.oneUpgrade.curUpgradeTactic == "singleUpgrade") {	//单台升级
//		    		if (jGlobal.oneUpgrade.singleUpgradeCount == 0) { //单台升级完成		        		       		
//		        		//上传统计信息至云端
//		        		if (isEmptyObject(jGlobal.oneUpgrade.upgradeStatistic) == false) {
//			    	    	//@???
//		        			statInfoIns.sendUpgradeInfo(jGlobal.oneUpgrade.upgradeStatistic);
//			    	    }
//		        		jGlobal.oneUpgrade.singleUpgradeOnce = true;
//		        	}
//		    	}
		    });
		    
		    hotFixUpgradeIns.on("hotFixResult", function(res) {
		    	console.log("---------------------热补丁单台升级完成-------------------------");
		    	console.log(res);
		    	
		    	var id = "#" + res.deviceName.replace(/\./g, '_');
		    	var index = $(id).parent().parent().attr('data-index');
		    	
		    	if (res.result == "success") {
		    		// jGlobal.oneUpgrade.batchUpgradeResult[res.deviceName] = "升级成功";
		    		jGlobal.oneUpgrade.countSuccessUpgrade++;
		    		
		    		$("#selectDeviceTable").bootstrapTable('updateRow', {
		    			index: index,
		    			row: {
		    				upgradeStatus: "升级成功"
		    			}
		    		});
		    		
		    		//存储云端信息
		    		patchUpgradeStatistic[res.deviceName].upgradeEndTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
		    		patchUpgradeStatistic[res.deviceName].upgradeFailReason = "";
		    		patchUpgradeStatistic[res.deviceName].upgradeResult = "升级成功";
		    	} else if (res.result == "fail") {
		    		// jGlobal.oneUpgrade.batchUpgradeResult[res.deviceName] = "升级失败";
		    		jGlobal.oneUpgrade.countFailUpgrade++;
		    		
		    		$("#selectDeviceTable").bootstrapTable('updateRow', {
		    			index: index, 
		    			row: {
		    				upgradeStatus: "升级失败：" + res.reason
		    			}
		    		});
		    		
		    		//存储云端信息
		    		patchUpgradeStatistic[res.deviceName].upgradeEndTime = global.moment().format('YYYY-MM-DD HH:mm:ss');
		    		patchUpgradeStatistic[res.deviceName].upgradeFailReason = res.reason;
		    		if (res.failPatch != undefined) {
		    			patchUpgradeStatistic[res.deviceName].failPatch = res.failPatch;
		    		} else {
		    			patchUpgradeStatistic[res.deviceName].failPatch = patchUpgradeStatistic[res.deviceName].patchArray;
		    		}
		    		patchUpgradeStatistic[res.deviceName].upgradeResult = "升级失败";
		    	}
		    	
		    	hotFixNextDev(); //某台升级完成，马上升级下一台
		    });

		    hotFixUpgradeIns.on("hotFixFinish", function() {		    	
		    	console.log("---------------------热补丁全部升级完成-------------------------");
		    		
		    	if (jGlobal.oneUpgrade.speedUpgradeIndex == (jGlobal.oneUpgrade.batchUpgradeDevList.length - 1)) {
		    		console.log("热补丁升级全部完成！");

		    		hotFixUpgradeFlag = true;
		    		if (hotFixUpgradeFlag && bigVerUpgradeFlag) {
		    			sortAfterUpgrade();
			    		updateDomAfterUpgrade();
		    		}
		    		
		    		//上传统计信息至云端
		    		if (isEmptyObject(patchUpgradeStatistic) == false) {
//		    			console.log('upgradeStatistic:!!!!!' + JSON.stringify(patchUpgradeStatistic));
		    			statInfoIns.sendUpgradeInfo(patchUpgradeStatistic);
		    		}
		    	}
		    });
		    
            $("#scheduleReboot").click(function () {
                statRecordEle("oneUpgrade#upgrading#scheduleReboot");
		    	$("#scheduleModal .modal-title").text("定时重启");
		    });
		    
            $("#scheduleUpgrade").click(function () {
                statRecordEle("oneUpgrade#upgrading#scheduleUpgrade");
		    	$("#scheduleModal .modal-title").text("定时升级");
		    });
		    
		    // 定时重启、定时升级对话框
		    $('#scheduleModal').on('show.bs.modal', function() {
				$("#hour").val("");
				$("#minite").val("");				
				$("#cancel").hide();
		    	$("#scheduleModal .close").show();
		    	$("#confirm").show();
			});
		    
		    $("#confirm").click(function() {
		    	//输入合法性检查
		    	var hour = $("#hour").val();
		    	var minite = $("#minite").val();
		    	
		    	if (hour == "" && minite == "") {
		    		alert(undefined, '请输入重启时间！');
		    		return;
		    	}
		    	if (hour != "") {
		    		var ex = /^\d+$/;
		    		if (!ex.test(hour)) {
		    		    alert(undefined, '请输入整数！');
		    		    return;
		    		}
		    	}
		    	if (minite != "") {
		    		var ex = /^\d+$/;
		    		if (!ex.test(minite)) {
		    		    alert(undefined, '请输入整数！');
		    		    return;
		    		}
		    	}
		    	if (hour < 0 || minite < 0 || minite > 59) {
		    		alert(undefined, '输入不合法！请重新输入。');
	    		    return;
		    	}
		    	if (hour >= 24) {
		    		alert(undefined, '定时不能超过24小时！');
	    		    return;
		    	}
		    	
		    	// 布局样式
		    	$("#scheduleModal .close").hide();
		    	$("#confirm").hide();
		    	$("#cancel").show();
		    	
		    	// 倒计时
		    	if (hour == "") hour = 0;
		    	if (minite == "") minite = 0;
		    	
		    	var now = new Date();
				var countTo = (hour * 60 * 60 * 1000) + (minite * 60 * 1000) + now.valueOf();

				$('.timer').countdown(countTo, function(event) {
					var $this = $(this);
					switch(event.type) {
						case "seconds":
						case "minutes":
						case "hours":
						case "daysLeft":
							$this.find('span.' + event.type).html(event.value);
							break;
						case "finished":
							if ($("#scheduleModal .modal-title").text() == "定时重启") {
								scheRebootTimeout = true;
								if (scheRebootCopyDone == true) { //定时时间到，且下发完成，重启升级设备
									$('#scheduleModal').modal('hide');
									scheduleReboot();
								} else { //定时时间到，但下发未完成，提示用户等下发完成后重启升级设备
									alert(undefined, "定时时间到，等待下发完成后立即重启！");
								}
							} else if ($("#scheduleModal .modal-title").text() == "定时升级") {
								$('#scheduleModal').modal('hide');
								speedUpgrade('speedUpgrade');
							}
							break;
					}
				});
				
				if ($("#scheduleModal .modal-title").text() == "定时重启") {
					// 下发设备，与极速升级(即刻升级)一致
					scheRebootDev = [];
					scheRebootTimeout = false;
					scheRebootCopyDone = false;
					speedUpgrade('scheduleReboot');
				}
		    });
		    
		    $("#cancel").click(function() {
		    	// 布局样式
		    	$("#cancel").hide();
		    	$("#scheduleModal .close").show();
		    	$("#confirm").show();
		    	
		    	// 没有提供取消接口，人为将定时器置为0
		    	$('.timer').countdown(0, function(event) {
					var $this = $(this);
					switch(event.type) {
						case "seconds":
						case "minutes":
						case "hours":
						case "daysLeft":
							$this.find('span.' + event.type).html(event.value);
							break;
						case "finished":
							break;
					}
				});
		    });
		}
		function initDom() {			
			$('#scheduleModal').modal({
				backdrop: 'static',
				show: false
			});
			
			//升级中含有热补丁升级,升级策略剔除定时重启和仅下发			
			var flag = false;
			var multiDevHotFix = jGlobal.oneUpgrade.multiDevHotFix;
			for (var key in multiDevHotFix) {
				if (multiDevHotFix[key].length != 0) {
					flag = true;
					break;
				}
			}
			if (flag) {
				$("#scheduleReboot").hide();
				$("#onlyCopy").hide();
				$(".btn-upgradeToggle").height(80);
			}
		}
		function resize() {
			var client_h = document.documentElement.clientHeight;
			var head_h = $(".main_menu").height();
			var body_h = client_h - head_h;
			
			$('#selectDeviceTable').bootstrapTable('resetView', {height : body_h - 55 - 65});
		}
		function initGlobalEvent() {
			global.exitPageCallback.exitFun = function () {
				if ($("#upgradeBack").is(":hidden")) {					
					alert(undefined, "设备正在升级中，请不要离开当前页面!");
					return false;
				} else {					
					return true;
				}				
			}
		}
	}
	exports.init = init;
});