<%+cbi/valueheader%>
<%local fs=require"nixio.fs"%>
<input type="button" class="btn cbi-button cbi-button-apply" id="apply_update_button" value="<%:Update core version%>" onclick=" return apply_update() "/>
<input type="button" class="btn cbi-button cbi-button-apply" id="apply_forceupdate_button" value="<%:Force update%>" onclick=" return apply_forceupdate()" style="display:none"/>
<% if self.showfastconfig then %>
<input type="button" class="btn cbi-button cbi-button-apply" id="to_configpage" value="<%:Fast config%>" onclick="location.href='<%=url([[admin]], [[services]], [[AdGuardHome]], [[manual]])%>'"/>
<%end%>
<div id="logview" style="display:none">
<input type="checkbox" id="reversetag" value="reverse" onclick=" return reverselog()" style="vertical-align:middle;height: auto;"><%:reverse%></input>
<textarea id="cbid.logview.1.conf" class="cbi-input-textarea" style="width: 100%;display:block;" data-update="change" rows="5" cols="60" readonly="readonly" > </textarea>
</div>
<script type="text/javascript">//<![CDATA[
var updatebtn = document.getElementById('apply_update_button');
var forceupdatebtn = document.getElementById('apply_forceupdate_button');
var islogreverse = false;
function apply_forceupdate(){
	XHR.get('<%=url([[admin]], [[services]], [[AdGuardHome]], [[doupdate]])%>',{ force: 1 },function(x, data){}
		);
	updatebtn.disabled = true;
	poll_check();
	return
}
function reverselog(){
	var lv = document.getElementById('cbid.logview.1.conf');
	lv.innerHTML=lv.innerHTML.split('\n').reverse().join('\n')
	if (islogreverse){
	islogreverse=false;
	}else{
	islogreverse=true;
	}
	return
}
function apply_update(){
	XHR.get('<%=url([[admin]], [[services]], [[AdGuardHome]], [[doupdate]])%>',null,function(x, data){}
		);
	updatebtn.disabled = true;
	updatebtn.value    = '<%:Check...%>';
	forceupdatebtn.style.display="inline"
	poll_check();
	return
}
function poll_check(){
var tag = document.getElementById('logview');
	tag.style.display="block"
XHR.poll(3, '<%=url([[admin]], [[services]], [[AdGuardHome]], [[check]])%>', null,
	function(x, data) {
		var lv = document.getElementById('cbid.logview.1.conf');
		if (x.responseText && lv) {
			if (x.responseText=="\u0000"){
				for(j = 0,len=this.XHR._q.length; j < len; j++) {
					if (this.XHR._q[j].url == '<%=url([[admin]], [[services]], [[AdGuardHome]], [[check]])%>'){
						this.XHR._q.splice(j,1);
						updatebtn.disabled = false;
						updatebtn.value    = '<%:Updated%>';
						break;
					}
				}
				return	
			}
			if (islogreverse){
				lv.innerHTML = x.responseText.split('\n').reverse().join('\n')+lv.innerHTML;
			}else{
				lv.innerHTML += x.responseText;
			}
		}
	}
);}
<% if fs.access("/var/run/update_core") then %>
	updatebtn.disabled = true;
	updatebtn.value    = '<%:Check...%>';
	forceupdatebtn.style.display="inline"
	poll_check();
<%elseif fs.access("/var/run/update_core_error") then %>
	poll_check();
<%end%>
//]]>
</script>
<%+cbi/valuefooter%>
