<style type="text/css">div.version-loading{margin:0 auto;min-height:100px}div.check-result{width:52px;height:52px;margin:25px auto;object-fit:contain;background:url(/luci-static/images/check_upgrade_success.png?_=20191016014108) no-repeat;background-size:contain}div.check-result-failed{background:url(/luci-static/images/check_upgrade_failed.png?_=20191016014108) no-repeat;background-size:contain}#CheckTip{margin:0 1em 15px}ul.set-margin{margin-bottom:30px}#CurSwVer{color:rgba(63,64,68,0.5)}#NewSwVer{color:rgba(63,64,68,0.5)}ul.ul-border{border-bottom:1px solid #DDD;margin:10px 50px 10px}p.upgrade-info{color:#767d82;font-size:14px;width:300px;margin:5px auto}p.upgrade-time{color:rgba(79,83,86,0.5);margin-bottom:20px}#NewSwLog{margin-top:20px}li.text-auto{width:146px}li.text-con span.select-auto{width:118px;padding:0 14px}ul.sel-opts-ul{width:100%}span.time-to{display:inline-block;vertical-align:middle;line-height:28px;font-size:14px;color:#4f5356;padding-left:2px;padding-right:2px}li.option span{padding:0 6px}li.multi-sel{height:36px}div.button-margin{margin:0}</style><%
	local para = {
		system = {upgrade = {}},
		device = {info = {}},
		dev_feature = {auto_upgrade = {}}
	}

	local result = tpl_get_data(para)
	local data = result[K_MODULE].system.upgrade
	local sw_ver = result[K_MODULE].device.info.sw_ver
	local timing_upgrade = result[K_MODULE].dev_feature.auto_upgrade.timing_upgrade
-%>
<script type="text/javascript">
(function(){
	var timingUpgrade = "<%=timing_upgrade%>";

	function startUpgrade(){
		var para = {
			system:{upgrade_info:{action:"start"}}
		};

		apiGet(para, function(){
			$.setTimeout(checkUpgrade, 3000);
		});
	}

	function checkUpgrade(){
		var para = {
			system:{upgrade_info:{action:"get"}}
		};

		apiGet(para, function(data){
			var info = data[K_MODULE]["system"]["upgrade_info"];
			var status = info.running_status;

			if ("2" == status){	// 升级检查完成
				$("#VersionChecking").hide();
				$(".inside-circle").hide();

				var code = parseInt(info.status_code, 10);
				if (1 == code){	// 有新版本
					$("#CheckResult").fadeIn(500).removeClass("check-result-failed");
					$("#CheckTip").text(label.hasNewSw);

					$("#NewSwVer").val(info.new_ver);
					$("#RealeaseTime").html(info.release_time);
					var log = unescape(info.release_log.replace(/#x/g,'%u').replace(/\n/g,'<br>'));
					$("#NewSwLog").html(log);
					$("#UpgradeCon").show();
					$("#UpgradeCheck").hide();
				}else{
					var state = false;
					switch(code){
					case 0:
					case 8804:
						state =true;
						errInfo = label.swIsNewest;
						break;
					case 8801:
						errInfo= label.serverBusy;
						break;
					case 8802:
						errInfo= label.checkFail;
						break;
					case 8803:
						errInfo= label.argsInvalid;
						break;
					default:
						errInfo= label.checkFail;
						break;
					}

					$("#CheckTip").text(errInfo);
					if (state){
						$("#CheckResult").fadeIn(500).removeClass("check-result-failed");
					}else{
						$("#CheckResult").fadeIn(500).addClass("check-result-failed");
					}

					$("#UpgradeCheck").show();
					$("#Check").val(btn.reCheckUpdate);
					$("#NewSwVer").val("");
					$("#UpgradeCon").hide();
				}

				closeLoading();
			}else{
				$.setTimeout(checkUpgrade, 3000);
			}
		});
	}

	$("#CurSwVer").val('<%=sw_ver%>');

	$("#Check").click(function(){
		$("#UpgradeCon").hide();
		showLoading(label.upgradeChecking);

		var para = {
			system:{upgrade_info:{action:"start"}}
		};

		apiGet(para, function(){
			$.setTimeout(checkUpgrade, 3000);
		});
	});

	$("#Upgrade").click(function(){
		showLoading(label.versionChecking);

		var para = {
			system:{upgrade_info:{action:"upgrade"}}
		};

		apiGet(para, function(ret){
			var code = ret[ERR_CODE];
			if (E_NONE == code){
				$.setTimeout(upgradeStatus, 1000);
			}
		});
	});

	var upgradeStatusHd = null;
	function upgradeStatus(){
		var para = {
			system:{upgrade_info:{action:"upgrade_status"}}
		};

		apiGet(para, function(ret){
			var info = ret[K_MODULE]["system"]["upgrade_info"];
			var status = info.running_status;
			var code = parseInt(info.status_code, 10);

			if (4 == status){
				if (0 == code){
					clearTimeout(upgradeStatusHd);
					showProgressBar(label.upgrading, label.upgradingTips, ROUTER.time.onlineUpgrade, function(){
						window.location.reload();
					});

					$.setTimeout(function(){
						// 检测LAN是否可以连接DUT
						lanDetecting(function(){
							window.location.reload();
						});
					}, ROUTER.time.detectDelay);
				}
			}else if(0 == status){
				clearTimeout(upgradeStatusHd);
				statusHandle(status, code);
			}else{
				switch(code){
				case 0:
					showStatus(status);
					upgradeStatusHd = $.setTimeout(upgradeStatus, 1000);
					break;
				default:
					clearTimeout(upgradeStatusHd);
					statusHandle(status, code);
					break;
				}
			}
		});
	}

	function showStatus(status){
		if(1 == status){
			showLoading(label.versionChecking);
		}else if(2 == status){
			showLoading(label.downloadingTip);
		}else if(3 == status){
			showLoading(label.upgradeChecking);
		}
	}

	function statusHandle(status, code){
		var noteStr = "";

		if(1 == status){
			switch(code){
			case 1://当前已经是最新版本
				noteStr = errStr.versionIsNewest;
				break;
			case 2://网络异常导致获取新版本失败
				noteStr = errStr.getNewVersionErr;
				break;
			default:
				noteStr = errStr.unknownErr + status + code;
				break;
			}
		}else if(2 == status){
			switch(code){
			case 1://有一个下载例程在进行
				noteStr = errStr.downloadUnderway;
				break;
			case 2://下载失败
				noteStr = errStr.downloadFail;
				break;
			default:
				noteStr = errStr.unknownErr + status + code;
				break;
			}
		}else if(3 == status){
			switch(code){
			case 1://不合法升级文件
				noteStr = errStr.upgradeFileErr;
				break;
			case 2://不匹配硬件设备
				noteStr = errStr.fwNoMatchHw;
				break;
			case 3://不匹配产品型号
				noteStr = errStr.fwNoMatchPrdct;
				break;
			case 4://当前版本不满足最低版本要求
				noteStr = errStr.fwVerTooLow;
				break;
			case 5://当前固件版本不支持版本降级
				noteStr = errStr.noAllowReduceVer;
				break;
			case 6://系统错误
				noteStr = errStr.systemErr;
				break;
			case 7://有其他升级程序正在运行
				noteStr = errStr.upgradeUnderway;
				break;
			case 8://校验信息获取失败
				noteStr = errStr.checkInfoFail;
				break;
			default:
				noteStr = errStr.unknownErr + status + code;
				break;
			}
		}else{
			noteStr = errStr.unknownErr + status + code;
		}

		showAlert(noteStr);
	}

	function getHour(time){
		var hour = parseInt(time / 3600);
		return hour;
	}

	function getMinute(time){
		var minute = parseInt(time % 3600);
		minute = parseInt(minute / 60);
		return minute;
	}

	function setTimeData(hour, minute){
		var time;
		time = hour * 3600;
		time += minute * 60;
		return time;
	}

	if (parseInt(timingUpgrade)){
		$("#TimingUpgrade").show();

		var mode = parseInt('<%=data.mode%>');

		initSwitch("Switch", mode, function(state){
			if (1 == state){
				$("#TimeSet").show();
			}else{
				$("#TimeSet").hide();
				closeNote();
			}
		});

		var hourList = [];
		var hour;
		for (var i = 0; i < 24; i++){
			if (i.toString().length == 1){
				hour = "0" + i;
			}else{
				hour = i;
			}
			hourList.push({name : hour, value : i});
		}

		var minList = [];
		var min, val;
		for (var i = 0; i < 12; i++){
			if (i < 2){
				min = "0" + i * 5;
			}else{
				min = i * 5;
			}
			val = i * 5;
			minList.push({name : min, value : val});
		}

		var upgradeTime = parseInt('<%=data.upgrade_time%>');

		initSelect("Hour", hourList, getHour(upgradeTime));
		initSelect("Minute", minList, getMinute(upgradeTime));

		$("#Save").click(function(){
			var para = {};
			var data = {};

			para.system = {};
			para.system.upgrade = data;

			data.mode = $("#Switch").attr("data-value");

			if(data.mode == 1){
				var hour = $("#Hour").attr("data-value");
				var minute = $("#Minute").attr("data-value");
				data.upgrade_time = setTimeData(hour, minute);
			}

			showLoading(label.saving);

			apiSet(para, function(ret){
				var code = ret[ERR_CODE];
				if (E_NONE == code){
					$.setTimeout(function(){
						closeLoading();
					}, ret[K_MODULE].system.upgrade.wait_time * 1000);
				}else{
					handleError(code);
					closeLoading();
				}
			});
		});
	}else{
		$("#TimingUpgrade").hide();
	}

	startUpgrade();
})();
</script>

<div class="help hidden">
	<ul class="help-content">
		<li class="help-head">
			<img class="app-icon" src="/luci-static/images/app-icon/upgrade.png?_=20191016014108" />
			<span class="help-title">{%label.autoUpgrade%}</span>
		</li>
		<li class="summary">{%helpStr.autoUpgrade%}</li>
		<li class="summary">{%helpStr.autoUpgradeTip1%}</li>
		<li class="summary">{%helpStr.autoUpgradeTip2%}</li>
		<li class="summary">{%helpStr.autoUpgradeTip%}</li>
	</ul>
</div>
<div class="head-con">
	<img class="app-icon" src="/luci-static/images/app-icon/upgrade.png?_=20191016014108" />
	<a href="javascript:history.go(-1);" class="head-btn-l">
		<i class="head-btn icon icon_back"></i>
		<span class="head-title">{%label.autoUpgrade%}</span>
	</a>
</div>
<div class="set-con below-head">
	<div class="loading version-loading">
		<div class="outer-circle" id="VersionChecking">
			<div class="inside-circle">
				<i class="circle"></i>
			</div>
		</div>
		<div class="check-result hidden" id="CheckResult"></div>
		<p class="loading-tip" id="CheckTip">{%label.versionChecking%}</p>
	</div>
	<div id="OnlineUpgrade">
		<ul class="set-con">
			<li class="text-con">
				<label class="desc-lbl">{%label.curVer%}</label>
				<input id="CurSwVer" class="text" value="" readonly />
			</li>
		</ul>
		<ul class="set-con set-margin">
			<li class="text-con">
				<label class="desc-lbl">{%label.newVer%}</label>
				<input id="NewSwVer" class="text" value="" readonly />
			</li>
		</ul>
		<div class="btn-con button-margin hidden" id="UpgradeCheck">
			<input id="Check" class="btn" type="button" value="{%btn.detectVer%}" />
		</div>
		<div id="UpgradeCon" class="hidden">
			<p class="upgrade-info upgrade-time"><span>{%label.newVerReleaseTime%}{%label.colon%}</span><span id="RealeaseTime"></span></p>
			<p class="upgrade-info"><span>{%label.newVerReleaseLog%}{%label.colon%}</span></p>
			<p class="upgrade-info" id="NewSwLog"></p>
			<div class="btn-con button-margin">
				<input id="Upgrade" class="btn" type="button" value="{%btn.upgradeNow%}" />
			</div>
		</div>
	</div>
	<div id="TimingUpgrade" class="hidden">
		<ul class="set-con ul-border"></ul>
		<ul class="set-con">
			<li class="text-con">
				<label class="desc-lbl">{%label.timeUpgrade%}</label>
				<span class="set-con-middle">
					<span id="Switch" class="switch">
						<i class="switch-circle icon_select_off"></i>
					</span>
				</span>
			</li>
		</ul>
		<ul id="TimeSet" class="set-con">
			<li class="text-con multi-sel">
				<label class="desc-lbl">{%label.upgradeTime%}</label>
				<li class="text-con text-auto">
					<span id="Hour" class="select select-auto">
						<span class="value sel-value set-min"></span>
						<i class="tip icon_list-expansion"></i>
					</span>
				</li>
				<span class="time-to">{%label.colon%}</span>
				<li class="text-con text-auto">
					<span id="Minute" class="select select-auto">
						<span class="value sel-value set-min"></span>
						<i class="tip icon_list-expansion"></i>
					</span>
				</li>
			</li>
			<div class="err-note hidden">
				<i class="err-icon icon_remind_line"></i>
				<p class="note-str"></p>
			</div>
		</ul>
		<div class="btn-con button-margin">
			<input id="Save" class="btn" type="button" value="{%btn.save%}" />
		</div>
	</div>
</div>