<script type="text/javascript">
(function(){
	<%
		local para = {
			firewall = {remote_manager = {}}
		}

		local data = tpl_get_data(para, true)
	-%>
	var remoteInfo = <%=data%>;

	function init(result){
		var errNo = result[ERR_CODE];
		var ret;
		if (E_NONE == errNo){
			ret = result[K_MODULE];
		}else{
			console.log(result);
			return;
		}

		var remote = ret.firewall.remote_manager;
		initSwitch("RemoteSwitch", remote.enable, function(state){
			if (1 == state){
				$("#Param").show();
			}else{
				$("#Param").hide();
			}
		});
		$("#RemotePort").val(remote.port);
		$("#RemoteIp").val(remote.ip);
	}

	init(remoteInfo);

	function checkRemotePort(port){
		var err = checkNumNew(port, 1, 65535);
		switch(err){
		case E_BLANK:
			return E_REMOTE_PORT_BLANK;
		case E_NUMFORMAT:
			return E_REMOTE_PORT_ILLEGAL;
		case E_NUMRANGE:
			return E_REMOTE_PORT_RANGE;
		}
		return E_NONE;
	}

	function checkRemoteIp(ip){
		if ("255.255.255.255" == ip){
			return E_NONE;
		}

		var err = checkIp(ip);
		switch(err){
		case E_BLANK:
			return E_REMOTE_IP_BLANK;
		case E_INVIPFMT:
			return E_REMOTE_IP_FORMAT;
		case E_INVIP:
			return E_REMOTE_IP_ILLEGAL;
		}
		return E_NONE;
	}

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

		data.enable = $("#RemoteSwitch").attr("data-value");

		if (data.enable == "1"){
			data.ip = $("#RemoteIp").val();
			data.port = $("#RemotePort").val();

			if (handleError(checkRemotePort(data.port))){
				return;
			}

			if (handleError(checkRemoteIp(data.ip))){
				return;
			}
		}

		showLoading(label.saving);
		apiSet(para, function(ret){
			var code = ret[ERR_CODE];
			handleError(code, showNote)?closeLoading():closeLoading(3);
		});
	});
})();
</script>
<div class="help hidden">
	<ul class="help-content">
		<li class="help-head">
			<img class="app-icon" src="/luci-static/images/app-icon/remote.png?_=20191016014108" />
			<span class="help-title">{%label.remoteMng%}</span>
		</li>
		<li class="summary">{%helpStr.remoteMng%}</li>
		<li class="summary">{%helpStr.remoteMngTip%}</li>
	</ul>
</div>
<div class="head-con">
	<img class="app-icon" src="/luci-static/images/app-icon/remote.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.remoteMng%}</span>
	</a>
</div>
<div class="set-con below-head">
	<ul class="set-con">
		<li class="text-con">
			<label class="desc-lbl">{%label.remoteMng%}</label>
			<span class="set-con-middle">
				<span id="RemoteSwitch" class="switch">
					<i class="switch-circle icon_select_off"></i>
				</span>
			</span>
		</li>
	</ul>
	<div id="Param" class="hidden">
		<ul class="set-con">
			<li class="text-con">
				<label class="desc-lbl mng-prt">{%label.remoteMngPrt%}</label>
				<input id="RemotePort" class="text hover" maxLength="5" value="" />
			</li>
		</ul>
		<ul class="set-con">
			<li class="text-con">
				<label class="desc-lbl">{%label.remoteMngIp%}</label>
				<input id="RemoteIp" class="text hover" maxLength="15" value="" />
			</li>
		</ul>
		<div class="err-note hidden">
			<i class="err-icon icon_remind_line"></i>
			<p class="note-str"></p>
		</div>
	</div>
	<div class="btn-con">
		<input id="Save" class="btn" type="button" value="{%btn.save%}" />
	</div>
</div>