<% local view, 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">
<% -- 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'}}, widgets: ['zebra']});
		$(".deleterequest").click(function(){ return confirm("Are you sure you want to delete this request?")});
        });
</script>

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

<% local header_level = htmlviewfunctions.displaysectionstart(cfe({label="Requests"}), page_info) %>
<table id="list" class="tablesorter"><thead>
	<tr>
		<th>Action</th>
		<th>Timestamp</th>
		<th>MAC Address</th>
		<th>IP Address</th>
		<th>User Agent</th>
	</tr>		
</thead><tbody>
<% local mac = cfe({ type="hidden", value="" }) %>
<% local redir = cfe({ type="hidden", value="searchdevices" }) %>
<% for k,v in ipairs( view.value ) do %>
	<tr>
		<td>
		<% mac.value = v.mac %>
		<% if viewlibrary.check_permission("deleterequest") then %>
			<% htmlviewfunctions.displayitem(cfe({type="form", value={mac=mac}, label="", option="Delete", action="deleterequest", class="deleterequest" }), page_info, -1) %>
		<% end %>
		<% if v.device_id then %>
			<% if viewlibrary.check_permission("searchdevices") then %>
				<% htmlviewfunctions.displayitem(cfe({type="form", value={id=cfe({type="hidden", value="device_id"}), value=cfe({type="hidden", value=v.device_id}), redir=redir}, label="", option="Search", action="searchdevices" }), page_info, -1) %>
			<% end %>
		<% else %>
			<% if viewlibrary.check_permission("createdevicefromrequest") then %>
				<% htmlviewfunctions.displayitem(cfe({type="form", value={mac=mac}, label="", option="Create", action="createdevicefromrequest" }), page_info, -1) %>
			<% end %>
		<% end %>
		</td>
		<td>
		<span class="hide">
		<% 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>
<% htmlviewfunctions.displaysectionend(header_level) %>
