<% 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 createdevice as a component, we break the automatic redirect
	if session.createdeviceresult and not session.createdeviceresult.errtxt then
		local tmp = session.createdeviceresult
		session.createdeviceresult = 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("overridedeviceparams") then io.write("overridedeviceparams") elseif viewlibrary.check_permission("editdeviceparams") then io.write("editdeviceparams") else io.write("editdevice") end %>?device_id=<%= tmp.value.device_id.value %>&redir=<%= html.html_escape(page_info.orig_action) %>");
<%
	end
%>

        $(document).ready(function() {
                $("#list").tablesorter({headers: {0:{sorter: false}}});
		$(".deletedevice").click(function(){ return confirm("Are you sure you want to delete this device?")});
        });
</script>

<% htmlviewfunctions.displaycommandresults({"deletedevice", "editdevice", "editdeviceparams", "overridedeviceparams"}, session) %>
<% htmlviewfunctions.displaycommandresults({"createdevice"}, session, true) %>

<% -- Determine all of the groups
local tmp = {}
for k,v in ipairs( view.value ) do
	for g,c in pairs(v) do
		if not tmp[g] then tmp[g] = true end
	end
end
local display = {}
for n in pairs(tmp) do
	if n ~= "device_id" then
		display[#display+1] = n
	end
end
table.sort(display)
%>

<h1>Devices</h1>
<DL>
<TABLE id="list" class="tablesorter"><THEAD>
	<TR style="background:#eee;font-weight:bold;">
		<TH>Action</TH>
		<TH>Device ID</TH>
	<% for i,n in ipairs(display) do %>
		<TH><%= string.gsub(n, "^.", string.upper) %>
	<% end %>
	</TR>		
</THEAD><TBODY>
<% for k,v in ipairs( view.value ) do %>
	<TR>
		<TD>
		<% if viewlibrary.check_permission("deletedevice") then %>
			<%= html.link{value=page_info.script..page_info.prefix..page_info.controller.."/deletedevice?submit=true&device_id="..v.device_id, label="Delete ", class="deletedevice"} %>
		<% end %>
		<% if viewlibrary.check_permission("editdevice") then %>
			<%= html.link{value=page_info.script..page_info.prefix..page_info.controller.."/editdevice?device_id="..v.device_id.."&redir="..page_info.orig_action, label="Edit "} %>
		<% end %>
		<% if viewlibrary.check_permission("overridedeviceparams") then %>
			<%= html.link{value=page_info.script..page_info.prefix..page_info.controller.."/overridedeviceparams?device_id="..v.device_id.."&redir="..page_info.orig_action, label="Params "} %>
		<% elseif viewlibrary.check_permission("editdeviceparams") then %>
			<%= html.link{value=page_info.script..page_info.prefix..page_info.controller.."/editdeviceparams?device_id="..v.device_id.."&redir="..page_info.orig_action, label="Params "} %>
		<% end %>
		<% if viewlibrary.check_permission("getdevicevalues") then %>
			<%= html.link{value=page_info.script..page_info.prefix..page_info.controller.."/getdevicevalues?submit=true&id=device_id&value="..v.device_id.."&viewtype=templated", label="View "} %>
		<% end %>
		</TD>
		<TD><%= html.html_escape(v.device_id) %></TD>
	<% for i,n in ipairs(display) do %>
		<TD><%= html.html_escape(v[n]) %></TD>
	<% end %>
	</TR>
<% end %>
</TBODY>
</TABLE>

<% if view.errtxt then %>
<p class="error"><%= html.html_escape(view.errtxt) %></p>
<% end %>
<% if #view.value == 0 then %>
<p>No devices found</p>
<% end %>
</DL>

<% if page_info.action == "listdevices" and viewlibrary and viewlibrary.dispatch_component and viewlibrary.check_permission("createdevice") then
        viewlibrary.dispatch_component("createdevice")
end %>
