<style type="text/css">input.input-file{position:absolute;height:34px;line-height:34px;width:364px;left:0;opacity:0;filter:alpha(opacity=0);z-index:100;cursor:pointer}i.icon_file{color:#fe9818;display:inline-block;font-size:22px;position:absolute;width:22px;height:34px;line-height:34px;left:310px;cursor:pointer}span.scan-file{color:#fe9818;display:inline-block;font-size:14px;position:absolute;width:40px;height:34px;line-height:34px;left:330px;cursor:pointer}.download-firmware{color:#4f5356;font-size:14px;margin:20px 50px 10px;text-align:center;height:34px;line-height:34px}a.firmware-link{color:#fe9818}div.scan-con{height:34px;line-height:34px;vertical-align:middle;font-size:14px;text-align:left;margin-top:-34px;margin-left:300px}#UpgradeFile{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}</style>
<%
	local para = {
		device = {info = {}}
	}

	local result = tpl_get_data(para)
	local data = result[K_MODULE].device.info
-%>
<script type="text/javascript">
(function(){
	//$("#CurSwVer").val('<%=data.sw_ver%>');
	$("#CurSwVer").val('<%=data.build_time%>');
	$("#SwURL").attr("href",ROUTER.swURL);
	var action  = $.orgURL('/system/upgrade');
	$("#UploadFw").attr('action', action);

	// 上传升级
	function setFileVal(obj, objId) {
		var p = obj.value;
		var lastindex = p.lastIndexOf("\\");
		$("#"+objId).val((p.substring(lastindex+1)));
		$("#"+objId).attr("title", p.substring(lastindex+1));
		$(obj).attr("title", p.substring(lastindex+1));
		$("#ImgName").val(p.substring(lastindex+1));
		$("#Step").val("1");
	}

	function nameTooLenHadl(fileName){
		var shortName;

		if (fileName.length >= 23){
			shortName = fileName.substr(0, 20) + "...";
		}else{
			shortName = fileName;
		}

		return shortName;
	}

	$("#ScanFile").change(function(){
		setFileVal(this,"UpgradeFile");
	});

	$("#MnulUpgrade").click(function() {
		var str = $("#ScanFile").val();
		var lastindex = str.lastIndexOf(".");

		if (OS.iPad){
			showAlert(label.sysLimitTip);
			return;
		}

		if (str.length <= 0) {
			showAlert(errStr.upgradeFileEmpty);
			return;
		}
		if (str.substring(lastindex + 1) != "bin") {
			showAlert(errStr.upgradeFileForm);
			return;
		}

		showLoading(label.uploadingTip);

		$("#UploadIf").one("load", function(){
			iFrmOnload("UploadIf", function(data){
				var code = data[ERR_CODE];
				switch(code){
					case E_NONE:
						$.setTimeout(upgradeStatus, 1000);
						break;
					case E_NOMEM:
						showAlert(errStr.systemErr);
						break;
					default:
						handleError(code, showAlert);
				}
			});
		});

		var formObj = document.uploadFw;
		formObj.submit();
		formObj.reset();
	});

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

		apiGet(para, function(ret){
			var info = ret[K_MODULE]["system"]["upgrade_status"];
			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.manualUpgrade, function(){
						window.location.reload();
					});
				}
			}else if(0 == status){
				clearTimeout(upgradeStatusHd);
				statusHandle(status, code);
			}else{
				switch(code){
				case 0:
					showLoading(label.upgradeChecking);
					upgradeStatusHd = $.setTimeout(upgradeStatus, 1000);
					break;
				default:
					clearTimeout(upgradeStatusHd);
					statusHandle(status, code);
					break;
				}
			}
		});
	}

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

		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);
	}

})();
</script>
<div class="help hidden">
	<ul class="help-content">
		<li class="help-head">
			<img class="app-icon" src="/luci-static/images/app-icon/upload.png?_=20191016014108" />
			<span class="help-title">{%label.manualUpgrade%}</span>
		</li>
		<li class="summary">{%helpStr.manualUpgrade%}</li>
		<li class="summary">{%helpStr.manualUpgradeTip%}</li>
	</ul>
</div>
<div class="head-con">
	<img class="app-icon" src="/luci-static/images/app-icon/upload.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.manualUpgrade%}</span>
	</a>
</div>
<div class="set-con below-head">
	<ul class="set-con">
		<li class="text-con">
			<label class="desc-lbl">{%label.curVer%}</label>
			<input id="CurSwVer" class="text hover" readonly="readonly" value="" />
		</li>
	</ul>
	<iframe id="UploadIf" name="uploadIf" class="hidden"></iframe>
	<form name="uploadFw" id="UploadFw" action="system/upgrade" method="post" enctype="multipart/form-data" target="uploadIf">
		<ul class="set-con">
			<li class="text-con">
				<label class="desc-lbl">{%label.upgradeFile%}</label>
				<input type="text" id="UpgradeFile" class="text hover" placeholder="{%label.fwFileTip%}" readonly="readonly" value="" />
				<div class="scan-con">
					<input type="file" class="input-file" id="ScanFile" name="file" />
					<i class="icon_file"></i>
					<span class="scan-file">{%label.scan%}</span>
				</div>
			</li>
		</ul>
		<input type="hidden" name="step" id="Step" value="0" />
		<input type="hidden" name="filename" id="ImgName" />
	</form>
	<div class="err-note hidden">
		<i class="err-icon icon_remind_line"></i>
		<p class="note-str"></p>
	</div>
	<div class="download-firmware">
		<label>{%label.downloadTip%}</label>
		<a href="" id="SwURL" target="_blank" class="firmware-link">
			<span>{%label.downloadFirmware%}</span>
		</a>
	</div>
	<div class="btn-con">
		<input id="MnulUpgrade" class="btn" type="button" value="{%btn.mnulUpgrade%}" />
	</div>
</div>
