<% local data, viewlibrary, page_info, session = ... 
htmlviewfunctions = require("htmlviewfunctions")
html = require("acf.html")
%>

<script type="text/javascript">
	if (typeof jQuery == 'undefined') {
		document.write('<script type="text/javascript" src="<%= html.html_escape(page_info.wwwprefix) %>/js/jquery-latest.js"><\/script>');
	}
</script>

<script type="text/javascript">
	if (typeof $.tablesorter == 'undefined') {
		document.write('<script type="text/javascript" src="<%= html.html_escape(page_info.wwwprefix) %>/js/jquery.tablesorter.js"><\/script>');
	}
</script>

<script type="text/javascript">
	$(document).ready(function() {
		$("#sources").tablesorter({headers: {0:{sorter: false}}, widgets: ['zebra']});
	});
</script>

<% htmlviewfunctions.displaycommandresults({"deletesource", "editsource", "testsource", "createsource", "importlogs"}, session) %>

<h1><%= html.html_escape(data.label) %></h1>
<DL>
<TABLE id="sources" class="tablesorter"><THEAD>
	<TR>
		<TH>Action</TH>
		<TH>Name</TH>
		<TH>Enabled</TH>
		<TH>Source</TH>
		<TH>Method</TH>
	</TR>		
</THEAD><TBODY>

<% for i,source in ipairs(data.value) do %>
	<TR>
		<TD>
		<%= html.link{value = "editsource?sourcename=" .. source.sourcename.."&redir="..page_info.orig_action, label="Edit "} %>
		<%= html.link{value = "deletesource?submit=true&sourcename=" .. source.sourcename, label="Delete "} %>
		<%= html.link{value = "testsource?submit=true&sourcename=" .. source.sourcename, label="Test "} %>
		</TD>
		<TD><%=  html.html_escape(source.sourcename) %></TD>
		<TD><%= html.html_escape(tostring(source.enabled)) %></TD>
		<TD><%= html.html_escape(source.source) %></TD>
		<TD><%= html.html_escape(source.method) %></TD>
	</TR>
<% end %>
</TBODY>
</TABLE>

<% if data.errtxt then %>
<p class='error'><%= html.html_escape(data.errtxt) %></p>
<% end %>
<% if #data.value == 0 then %>
<p>No sources found</p>
<% end %>


<form action="<%= html.html_escape(page_info.script .. page_info.prefix .. page_info.controller .. "/createsource") %>">
<DT>Create New Source</DT>
<input class="hidden" type="hidden"  name="redir"  value="<%= html.html_escape(page_info.orig_action) %>" >
<DD><input class="submit" type="submit" value="Create"></DD>
</form>

<form action="<%= html.html_escape(page_info.script .. page_info.prefix .. page_info.controller .. "/importlogs") %>">
<DT>Import Logs</DT>
<DD><input class="submit" type="submit" name="submit" value="Import"></DD>
</form>
</DL>
