<% 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 getclassvalues, we need to overcome the automatic redirect
	if session.getclassvaluesresult then
		-- Use JavaScript to redirect to the getclassvalues page
%>
		window.location.assign("<%= html.html_escape(page_info.script..page_info.prefix..page_info.controller) %>/getclassvalues");
<%
	end
%>
        $(document).ready(function() {
                $("#list").tablesorter({headers: {0:{sorter: false}}});
		$(".deleteclass").click(function(){ return confirm("Are you sure you want to delete this class?")});
        });
</script>

<% htmlviewfunctions.displaycommandresults({"deleteclass", "editclass"}, session) %>
<% htmlviewfunctions.displaycommandresults({"createclass"}, session, true) %>

<h1>Classes</h1>
<DL>
<TABLE id="list" class="tablesorter"><THEAD>
	<TR style="background:#eee;font-weight:bold;">
		<TH>Action</TH>
		<TH>Class Group</TH>
		<TH>Label</TH>
		<TH>Sequence</TH>
	</TR>		
</THEAD><TBODY>
<% for k,v in ipairs( view.value ) do %>
	<TR>
		<TD>
		<% if viewlibrary.check_permission("deleteclass") then %>
			<%= html.link{value=page_info.script..page_info.prefix..page_info.controller.."/deleteclass?submit=true&class_id="..v.class_id, label="Delete ", class="deleteclass"} %>
		<% end %>
		<% if viewlibrary.check_permission("editclass") then %>
			<%= html.link{value=page_info.script..page_info.prefix..page_info.controller.."/editclass?class_id="..v.class_id.."&redir="..page_info.orig_action, label="Edit "} %>
		<% end %>
		<% if viewlibrary.check_permission("getclassvalues") then %>
			<%= html.link{value=page_info.script..page_info.prefix..page_info.controller.."/getclassvalues?submit=true&class_id="..v.class_id, label="View "} %>
		<% end %>
		</TD>
                <TD><%= html.html_escape(v.group) %></TD>
                <TD><%= html.html_escape(v.label) %></TD>
                <TD><%= html.html_escape(v.seq) %></TD>
	</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 classes found</p>
<% end %>

<% if viewlibrary and viewlibrary.dispatch_component and viewlibrary.check_permission("createclass") then
        viewlibrary.dispatch_component("createclass")
end %>
</DL>
