<% local form, 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">
	$(function(){
		$('input, select').first().focus();
		$('input, select').keydown(function(e){
			if (e.keyCode == 13) {
				$(this).parents('form').find('input.submit').click();
				return false;
			}
		});
		$("#list").tablesorter({headers: {0:{sorter: false}}, widgets: ['zebra']});
		$(".deletedevice").click(function(){ return confirm("Are you sure you want to delete this device?")});
	});
</script>

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

<%
-- This view is also used for getdevicevalues, and this is where the values are displayed
if form.value.values then
	local header_level = htmlviewfunctions.displaysectionstart(cfe({label="Device"}), page_info)
	htmlviewfunctions.displayinfo(form)
	htmlviewfunctions.displayitem(cfe({label="Device ID", readonly=true, value=form.value.result.value[1].device_id}), page_info)
	local header_level2 = htmlviewfunctions.displaysectionstart(cfe({label="Parameter Values"}), page_info, htmlviewfunctions.incrementheader(header_level))
	val = require("session").serialize("values", form.value.values.value)
	val = string.gsub(val, "[^\n]*%{%}\n", "")
	print("<pre>"..val.."</pre>")
	htmlviewfunctions.displaysectionend(header_level2)
	htmlviewfunctions.displaysectionend(header_level)
	form.value.values = nil
-- And here we display the list of devices in the result
elseif form.value.result then
	-- Determine all of the columns
	local tmp = {}
	for k,v in ipairs( form.value.result.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)

	local header_level = htmlviewfunctions.displaysectionstart(form.value.result, page_info)
%>
	<table id="list" class="tablesorter"><thead>
		<tr>
			<th>Action</th>
			<th>Device ID</th>
		<% for i,n in ipairs(display) do %>
			<th><%= html.html_escape(string.gsub(n, "^.", string.upper)) %>
		<% end %>
		</tr>		
	</thead><tbody>
	<% local device_id = cfe({ type="hidden", value="" }) %>
	<% local redir = cfe({ type="hidden", value=page_info.orig_action }) %>
	<% for k,v in ipairs( form.value.result.value ) do %>
		<tr>
			<td>
			<% device_id.value = v.device_id %>
			<% if viewlibrary.check_permission("editdevice") then %>
				<% htmlviewfunctions.displayitem(cfe({type="link", value={device_id=device_id, redir=redir}, label="", option="Edit", action="editdevice"}), page_info, -1) %>
			<% end %>
			<% if viewlibrary.check_permission("overridedeviceparams") then %>
				<% htmlviewfunctions.displayitem(cfe({type="link", value={device_id=device_id, redir=redir}, label="", option="Params", action="overridedeviceparams"}), page_info, -1) %>
			<% elseif viewlibrary.check_permission("editdeviceparams") then %>
				<% htmlviewfunctions.displayitem(cfe({type="link", value={device_id=device_id, redir=redir}, label="", option="Params", action="editdeviceparams"}), page_info, -1) %>
			<% end %>
			<% if viewlibrary.check_permission("getdevicevalues") then %>
				<% htmlviewfunctions.displayitem(cfe({type="form", value={value=device_id, id=cfe({type="hidden", value="device_id"}), viewtype=cfe({type="hidden", value="templated"})}, label="", option="View", action="getdevicevalues"}), page_info, -1) %>
			<% end %>
			<% if viewlibrary.check_permission("deletedevice") then %>
				<% htmlviewfunctions.displayitem(cfe({type="form", value={device_id=device_id}, label="", option="Delete", action="deletedevice", class="deletedevice"}), page_info, -1) %>
			<% 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 form.value.result.errtxt then %>
	<p class="error"><%= html.html_escape(form.value.result.errtxt) %></p>
	<% end %>
	<% if #form.value.result.value == 0 then %>
	<p>No devices found</p>
	<% end %>
	<% htmlviewfunctions.displaysectionend(header_level) %>

<% end %>

<%
	form.value.result = nil
	htmlviewfunctions.displayitem(form, page_info)
%>
