<%+cbi/valueheader%>
<%local fs=require"nixio.fs"%>

<input type="button" class="cbi-button cbi-button-apply" id="apply_update_geoip" value="<%:Download%>" onclick=" return apply_update() "/>
<br /><br />
<textarea id="geoip_download" class="cbi-input-textarea" style="width: 100%;display:none" data-update="change" rows="10" cols="100" readonly="readonly" > </textarea>

<script type="text/javascript">//<![CDATA[

function apply_update(){
	XHR.get('<%=url([[admin]], [[services]], [[clash]], [[geoipupdate]])%>',null,function(x, data){}
		);
	poll_check();
	return
}

function poll_check(){
var lv = document.getElementById('geoip_download');
	lv.style.display="inline"
XHR.poll(2, '<%=url([[admin]], [[services]], [[clash]], [[check_geoip]])%>', null,
	function(x, data) {
		var lv = document.getElementById('geoip_download');
		if (x.responseText && lv) {
			lv.innerHTML += x.responseText;
			lv.scrollTop = lv.scrollHeight; 
		}
	}
);

}


XHR.poll(3, '<%=luci.dispatcher.build_url("admin", "services", "clash", "geo")%>', null, function(x, status) {
		if ( x && x.status == 200 ) {			
			if(status.geoipcheck==0||status.geoipcheck==2){
				hide();	
			}
			if(status.geoipcheck==1){
				var btn = document.getElementById('apply_update_geoip');
				btn.disabled = false;
				btn.value    = '<%:Download%>';
				poll_check();
				
			}

		}
});



//]]>

function hide(){
	$('#geoip_download').hide();
	var btn = document.getElementById('apply_update_geoip');
	btn.disabled = false;
	btn.value    = '<%:Download%>';
}



<% if fs.access("/var/run/geoip_update") then %>
var	btn = document.getElementById('apply_update_geoip');
	btn.disabled = true;
	btn.value    = '<%:Downloading...%>';
poll_check();
<%end%>


</script>

<%+cbi/valuefooter%>
