<script type="text/javascript">
(function(){
	<%
		local para = {
			network = {lan = {}},
			dev_feature = {lan_set = {}}
		}

		local result = tpl_get_data(para)
		local lan = result[K_MODULE].network.lan
		local reboot_confirm = result[K_MODULE].dev_feature.lan_set.reboot_confirm
	-%>

	var ip = '<%=lan.ip%>';
	var netmask = '<%=lan.netmask%>';
	var reboot_confirm = <%=reboot_confirm%>;

	$("#LanIp").val(ip);

	function checkLanIp(ip){
		var err = checkIp(ip);
		switch(err){
		case E_BLANK:
			return E_LAN_IP_BLANK;
		case E_INVIPFMT:
			return E_LAN_IP_FORMAT;
		case E_INVIP:
			return E_LAN_IP_ILLEGAL;
		}
		return E_NONE;
	}

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

		data.ip = $("#LanIp").val();

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

		if(reboot_confirm){
			showConfirm(label.lanSetTip, function(choose){
				if (choose){
					saveLan(para);
				}
			});
		}else{
			saveLan(para);
		}
	});

	function saveLan(para){
		showLoading(label.saving);
		apiSet(para, function(ret){
			var code = ret[ERR_CODE];
			handleError(code, showNote)?closeLoading():refreshLan(ret);
		});
	}

	function refreshLan(ret){
		var url = "http://" + $("#LanIp").val();
		if(reboot_confirm){
			$.setTimeout(function(){
				closeLoading();
				window.location.href = url;
			}, ROUTER.time.reboot);
		}else{
			var time = ret[K_MODULE].network.lan.wait_time;
			$.setTimeout(function(){
				closeLoading();
				window.location.href = url;
			}, time * 1000);
		}

		var connected = 0;
		var detect = new Detect({
			tick:3000,
			url:url + '/luci-static/images/phicomm.png'
		});
		stateman.on({
			'connected': function(state){
				if(++connected >= 5){
					console.log('detect connected, jumping');
					window.location.href = url;
				}
			},
			'disconnect': function(state){
				connected = 0;
			}
		});
		detect.start();
	}
})();
</script>
<div class="head-con">
	<a href="javascript:history.go(-1);" class="head-btn-l"><i class="head-btn icon icon_back"></i></a>
	<h1 class="head-title">{%label.lanSet%}</h1>
</div>
<div class="set-con below-head">
	<ul class="blank-bg"></ul>
	<ul class="set-con">
		<label class="desc-lbl">{%label.ipaddr%}</label>
		<li class="text-con">
			<input id="LanIp" class="text hover" maxLength="15" value="" />
		</li>
	</ul>
	<ul class="blank-bg"></ul>
	<div class="err-note hidden">
		<i class="err-icon icon_remind_line"></i>
		<p class="note-str"></p>
	</div>
	<div class="btn-con">
		<input id="Save" class="btn" type="button" value="{%btn.save%}" />
	</div>
</div>