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

<script type="text/javascript" src="<%= html.html_escape(page_info.wwwprefix) %>/js/jquery-latest.js"></script>
<script type="text/javascript" src="<%= html.html_escape(page_info.wwwprefix) %>/js/jquery.tablesorter.js"></script>
<script type="text/javascript">
<% -- Since we're including a submission of searchdevices, we need to overcome the automatic redirect
	if session.searchdevicesresult then
		-- Use JavaScript to redirect to the searchdevices page
%>
		window.location.assign("<%= html.html_escape(page_info.script..page_info.prefix..page_info.controller) %>/searchdevices");
<%
	end
	
	-- Since we're including a submission of createdevicefromrequest, we need to overcome the automatic redirect
	if session.createdevicefromrequestresult and not session.createdevicefromrequestresult.errtxt then
		local tmp = session.createdevicefromrequestresult
		session.createdevicefromrequestresult = nil
		-- Use JavaScript to redirect to the edit page
%>
		window.location.assign("<%= html.html_escape(page_info.script..page_info.prefix..page_info.controller) %>/<% if viewlibrary.check_permission("editdevice") then io.write("editdevice") elseif viewlibrary.check_permission("overridedeviceparams") then io.write("overridedeviceparams") else io.write("editdeviceparams") end %>?device_id=<%= tmp.value.device_id.value %>&redir=<%= html.html_escape(page_info.orig_action) %>");
<%
	elseif session.createdevicefromrequestresult then
		-- Use JavaScript to redirect to the createdevicefromrequest page
%>
		window.location.assign("<%= html.html_escape(page_info.script..page_info.prefix..page_info.controller) %>/createdevicefromrequest");
<%
	end
%>

        $(document).ready(function() {
		$("#list").tablesorter({headers: {1:{sorter:'digit'}, 3:{sorter:'ipAddress'}}});
		$(".deleterequest").click(function(){ return confirm("Are you sure you want to delete this request?")});
        });
</script>

<% htmlviewfunctions.displaycommandresults({"deleterequest"}, session) %>

<h1>Requests</h1>
<DL>
<TABLE id="list" class="tablesorter"><THEAD>
	<TR style="background:#eee;font-weight:bold;">
		<TH>Action</TH>
		<TH>Timestamp</TH>
		<TH>MAC Address</TH>
		<TH>IP Address</TH>
		<TH>User Agent</TH>
	</TR>		
</THEAD><TBODY>
<% for k,v in ipairs( view.value ) do %>
	<TR>
		<TD>
		<% if viewlibrary.check_permission("deleterequest") then %>
			<%= html.link{value=page_info.script..page_info.prefix..page_info.controller.."/deleterequest?submit=true&mac="..v.mac, label="Delete ", class="deleterequest"} %>
		<% end %>
		<% if v.device_id then %>
			<% if viewlibrary.check_permission("searchdevices") then %>
				<%= html.link{value=page_info.script..page_info.prefix..page_info.controller.."/searchdevices?submit=true&id=device_id&value="..v.device_id.."&redir=searchdevices", label="Search "} %>
			<% end %>
		<% else %>
			<% if viewlibrary.check_permission("createdevicefromrequest") then %>
				<%= html.link{value=page_info.script..page_info.prefix..page_info.controller.."/createdevicefromrequest?submit=true&mac="..v.mac, label="Create "} %>
			<% end %>
		<% end %>
		</TD>
		<TD>
		<span style="display:none">
		<% local date = {}
		date.year, date.month, date.day, date.hour, date.min, date.sec, remainder = string.match(v.date, "(%d+)%-(%d+)%-(%d+) (%d+):(%d+):(%d+)")
		local rem = string.match(v.date, "%.%d+$")
		io.write((os.time(date) - 1318000000)..(rem or "")) %>
		</span>
		<%= html.html_escape(v.date) %>
		</TD>
		<TD><%= html.html_escape(v.mac) %></TD>
		<TD><%= html.html_escape(v.ip) %></TD>
		<TD><%= html.html_escape(v.agent) %></TD>
	</TR>
<% end %>
</TBODY>
</TABLE>
