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

<input type="button" class="cbi-button cbi-button-apply" id="apply_update_button" value="<%:Download%>" onclick=" return apply_update() "/>
<br /><br />
<textarea id="core_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]], [[doupdate]])%>',null,function(x, data){}
		);
	poll_check();
	return
}

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

}

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

		}
});



//]]>

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



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


</script>

<%+cbi/valuefooter%>
