<%+cbi/valueheader%>
<%-
	local util = require "luci.util"
	local sys = require "luci.sys"
-%>
<script type="text/javascript">//<![CDATA[
<% if self.option == "dnscrypt_bin" then -%>
	function update_bin(obtn, dataname, surl)
	{
		obtn.disabled = true;
		ret = false;
		alert(dataname+":\n" + surl);
		murl = dataname;
		var osta = document.getElementById(dataname+'-status');	XHR.get('<%=luci.dispatcher.build_url("admin", "services", "dnscrypt-proxy","update_c")%>',
			{ set:"dnscrypt_bin", url:surl },
			function(x, rv) {
			if(x.readyState === 4 ) {
			if(x.status === 200) {try{res = JSON.parse(x.responseText);}catch(e){res=null;}
			if(res !==null && typeof(res) === "object") {osta.innerText = res.status; obtn.disabled = false; obtn.value = '<%= self.name or translate("Refresh Data")%>';obtn.onclick = function (){alert("<%:No new data!%>");}; return (rv.err === 0);}
			}}});
			return false;
	}

	function parse_gittag(gtag, obtn, osta, sprefix, scver)
	{
		ret = true;
		tagv = gtag.name;
		dlfile = 'dnscrypt-proxy-%s-%s.tar.gz'.format(sprefix, tagv);
		obtn.title = "";
		osta.innerHTML = '<font color="red">%s</font><br /><div class="cbi-section-descr"><pre>%s</pre></div>'.format(dlfile, gtag.body);
		if(scver === tagv) {obtn.value = '<%:No new data!%>';}
		else if(! gtag.assets) {osta.innerText = '<%:Refresh Error!%>'; ret = false;}
		else {
			_assets = gtag.assets;
			osta.innerText = dlfile;
			for (var idx = 0; idx < _assets.length; idx++) {
				var dls=_assets[idx];
				if(dls.name === dlfile) {
					obtn.value = '<%:Update%> ' + tagv;
					obtn.title = dls.browser_download_url;
					obtn.onclick = function() { update_bin(this,'<%=self.option%>',this.title); };
					obtn.disabled = false;
					osta.innerHTML = '<a href="%s">%s</a>(%00.1fMB)<br /><div class="cbi-section-descr"><pre>%s</pre></div>'.format(dls.browser_download_url, dlfile, dls.size/1024/1024, gtag.body);
					break;
				}
			}
		}
		return ret;
	}
<%- end %>
	function refresh_data_<%=self.option%>(btn,dataname)
	{
		btn.disabled = true;
		btn.value    = '<%:Refresh...%> ';
		murl=dataname;
		var s = document.getElementById(dataname+'-status');
		if (dataname === "dnscrypt_bin") {
			os = '<%=util.trim(sys.exec("uname")):lower()%>';
			arch = '<%=util.trim(sys.exec("uname -m"))%>';
			prefix = '%s_%s'.format(os, arch);
			cver = '<%=util.trim(sys.exec("/usr/sbin/dnscrypt-proxy -version"))%>';
			tag_url = 'https://api.github.com/repos/dnscrypt/dnscrypt-proxy/releases/latest';
			s.innerHTML = ' current: dnscrypt-proxy %s-%s.tar.gz <a href="%s">release checking...</a>'.format(prefix, cver, tag_url);
			XHR.get('<%=luci.dispatcher.build_url("admin", "services", "dnscrypt-proxy","refresh_c")%>',null, function(x,rv) {
			s.innerText = 'Test, %s readyState: %d, status: %d'.format(x.statusText, x.readyState, x.status);
			x.open('GET', tag_url, true);
			x.onreadystatechange = function() {
			if(x.readyState === 4 && x.status === 200) {
			try{res = JSON.parse(x.responseText);}catch(e){res=null;}
			if(res !==null && typeof(res) === "object") {parse_gittag(res, btn, s, prefix, cver);}
			} else {
			s.innerHTML = '<font color="red">ReleaseTag, %s readyState: %d, status: %d</font>'.format(x.statusText, x.readyState, x.status);
			btn.disabled = false;
			btn.value    = '<%= self.name or translate("Refresh Data")%>';
			}};
			x.send(null);
			});
			return false;
		} else {
		XHR.get('<%=luci.dispatcher.build_url("admin", "services", "dnscrypt-proxy","refresh_c")%>',
			{ set:murl },
			function(x, rv) {
			if(x.readyState === 4 ) {
			if(x.status === 200) {try{res = JSON.parse(x.responseText);}catch(e){res=null;}
			if(res !==null && typeof(res) === "object") {
			var s = document.getElementById(dataname+'-status'); 
			if (s) {
				if (res.ret=="0") s.innerHTML ="<font color='green'>"+"<%:No new data!%> "+"</font>";
				else if(res.ret=="-1") s.innerHTML ="<font color='red'>"+"<%:Refresh Error!%> "+"</font>";
				else	s.innerHTML ="<font color='green'>"+"<%:Refresh OK!%> "+"<%= self.ret or translate('Total Records:')%>"+res.ret+"</font>";
				btn.disabled = false;
				btn.value    = '<%= self.name or translate('Refresh Data')%>';}}
			}}});
			return false;
		}
	}
//]]></script>


<input type="button" class="cbi-button cbi-input-reload" value="<%= self.name or translate('Refresh Data')%> " onclick="return refresh_data_<%=self.option%>(this,'<%=self.option%>')" />
<span id="<%=self.option%>-status"><em><%=self.value%></em></span>

<%+cbi/valuefooter%>