<%+cbi/valueheader%>
<input type="checkbox" name="NAME" value="reverse" onclick=" return reverselog()" style="vertical-align:middle;height: auto;" checked><%:reverse%></input>
<%if self.timereplace then%>
<input type="checkbox" name="NAME" value="localtime" onclick=" return chlogtime()" style="vertical-align:middle;height: auto;" checked><%:localtime%></input><br>
<%end%>
<textarea id="cbid.logview.1.conf" class="cbi-input-textarea" style="width: 100%;display:inline" data-update="change" rows="32" cols="60" readonly="readonly" > </textarea>
<input type="button" class="cbi-button cbi-button-apply" id="apply_update_button" value="<%:dellog%>" onclick=" return apply_del_log() "/>
<input type="button" class="cbi-button cbi-button-apply" value="<%:download log%>" style=" display:inline;" onclick=" return download_log()" />
<script type="text/javascript">//<![CDATA[
var islogreverse = true;
var isutc2local = <%=tostring(self.timereplace)%>;
function createAndDownloadFile(fileName, content) {
    var aTag = document.createElement('a');
    var blob = new Blob([content]);
    aTag.download = fileName;
    aTag.href = URL.createObjectURL(blob);
    aTag.click();
    URL.revokeObjectURL(blob);
}
function download_log(){
	var lv = document.getElementById('cbid.logview.1.conf');
	var dt = new Date();
	var timestamp = (dt.getMonth()+1)+"-"+dt.getDate()+"-"+dt.getHours()+"_"+dt.getMinutes();
	createAndDownloadFile("AdGuardHome"+timestamp+".log",lv.innerHTML)
	return
}
function apply_del_log(){
	XHR.get('<%=url([[admin]], [[services]], [[AdGuardHome]], [[dodellog]])%>',null,function(x, data){
	var lv = document.getElementById('cbid.logview.1.conf');
		lv.innerHTML="";
	}
		);
	return
}
function chlogtime(){
	var lv = document.getElementById('cbid.logview.1.conf');
	if (isutc2local){
	lv.innerHTML=line_toUTC(lv.innerHTML).join('\n');
	isutc2local=false;
	}else{
	lv.innerHTML=line_tolocal(lv.innerHTML).join('\n');
	isutc2local=true;
	}
	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 p(s) {
        return s < 10 ? '0' + s: s;
}
function line_tolocal(str){
var strt=new Array();
str.trim().split('\n').forEach(function(v, i) {
var dt = new Date(v.substring(0,19)+" UTC");
if (dt != "Invalid Date"){
  strt[i]=dt.getFullYear()+"/"+p(dt.getMonth()+1)+"/"+p(dt.getDate())+" "+p(dt.getHours())+":"+p(dt.getMinutes())+":"+p(dt.getSeconds())+v.substring(19);
}else{
  strt[i]=v;}})
return strt
}
function line_toUTC(str){
var strt=new Array();
str.trim().split('\n').forEach(function(v, i) {
var dt = new Date(v.substring(0,19))
if (dt != "Invalid Date"){
  strt[i]=dt.getUTCFullYear()+"/"+p(dt.getUTCMonth()+1)+"/"+p(dt.getUTCDate())+" "+p(dt.getUTCHours())+":"+p(dt.getUTCMinutes())+":"+p(dt.getUTCSeconds())+v.substring(19);
}else{
  strt[i]=v;}})
return strt
}
function poll_check(){
XHR.poll(3, '<%=url([[admin]], [[services]], [[AdGuardHome]], [[getlog]])%>', null,
	function(x, data) {
		var lv = document.getElementById('cbid.logview.1.conf');
		if (x.responseText && lv) {
			if (isutc2local)
			{
				var lines=line_tolocal(x.responseText);
				if (islogreverse){
					lv.innerHTML = lines.reverse().join('\n')+lv.innerHTML;
				}else{
					lv.innerHTML += lines.join('\n');
				}
			}else{
				if (islogreverse){
					lv.innerHTML = x.responseText.split('\n').reverse().join('\n')+lv.innerHTML;
				}else{
					lv.innerHTML += x.responseText;
				}
			}
		}
	}
);}
<%if self.pollcheck then%>
poll_check();
<%else%>
var lv = document.getElementById('cbid.logview.1.conf');
lv.innerHTML="<%:Please add log path in config to enable log%>"
<%end%>
//]]>
</script>	
<%+cbi/valuefooter%>