<%+header%>
<script type="text/javascript" src="<%=resource%>/cbi.js"></script>
<script type="text/javascript">//<![CDATA[

	XHR.get('<%=luci.dispatcher.build_url("admin", "services","log_info","logs_display_status")%>', null,
		function(x, rv)
		{
			//console.log("rv.validpath-2",rv.validpath)
			if (rv.validpath){
				var name = document.getElementById('fname_i');
				for(var i = 0; i < rv.numberoffiles; i++) {
					opt = document.createElement('option');
					opt.value = rv.filename[i];
					opt.innerHTML = rv.filename[i];
					name.appendChild(opt);
				}	

				//var name = document.getElementById('fserver_i');
				//for(var i = 0; i < rv.numberofservers; i++) {
				//	opt = document.createElement('option');
				//	opt.value = rv.servers[i];
				//	opt.innerHTML = rv.servers[i];
				//	name.appendChild(opt);
				//}

				var name = document.getElementById('syslog');
				var s = String.format("");
				for(var i = 0; i < rv.totallines; i++) {
					s += String.format('%s\n',rv.filedata[i] );
				}
				if (rv.totallines > 25) {
					name.rows=25;
				}else{
					name.rows=rv.totallines;
				}
				name.innerHTML = String.format('%s', s)
			}else{
				var valid = document.getElementById('validpath');
				valid.innerHTML=String.format("\"Write system log to file\" is not configured.");
			}
		}

		);
	function selectfiledisplay(){
		var name = document.getElementById('fname_i');
		//console.log("selectedIndex",name.selectedIndex)
		//console.log("value",name.value)
		XHR.get('<%=luci.dispatcher.build_url("admin", "services","log_info","logs_display_status")%>/' + name.selectedIndex, null,
			function(x, rv)
			{
				//console.log("rv.validpath-1",rv.validpath)
				if (rv.validpath){
					var name = document.getElementById('syslog');
					var s = String.format("");
					for(var i = 0; i < rv.totallines; i++) {
						s += String.format('%s\n',rv.filedata[i] )
					}
					if (rv.totallines > 25) {
						name.rows=25;
					}else{
						name.rows=rv.totallines;
					}
					name.innerHTML = String.format('%s', s);;
				}else{
					var valid = document.getElementById('validpath');
					valid.innerHTML=String.format("\"Write system log to file\" is not configured.");
				}
			}
			);
	}
//]]>
</script>

<h2><a id="content" name="content"></a></h2>
<span class="help-block">Logs can be configured from
	<a href="<%=url([[admin]], [[system]], [[system]])%>"><%:System >> System >> Logging%></a> page.<br></br>
</span>
<form method="post" action="<%=REQUEST_URI%>" enctype="multipart/form-data"> 
	<table>
		<tr>
			<% if validpath and validpath ~= 0 then %>
				<td>
					<span>Select File</span>
					<select class="cbi-section-create-name" name="filename" id="fname_i" onChange="selectfiledisplay();">
				</td>
				<td>
					<!--<span><%:Server to select%></span>
					<select class="cbi-section-create-name" name="uploadconfig" id="fserver_i">
					-->
					</td>
					<td>
						<input type="submit" class="cbi-button cbi-button-action important" name="Send" value="Download">
					</td>
				</tr>
				<tr>
					<% if status and status ~= 0 then %>
					<h3><b class="cbi-section-error"><%:File send failed %></b></h3>
					<% elseif status == 0 then %> 
					<h3><b  class="cbi-section-success"><%:File send success %></b></h3>
					<% end %>
				</tr>
			<% else %>
				<div id="validpath" class="cbi-section-error"></div>
			<% end %>
	</table>
</form>
<div id="content_syslog">
	<textarea readonly="readonly" wrap="off" id="syslog"></textarea>
</div> 

<%+footer%>
