<% 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">
        $(document).ready(function() {
                $("#list").tablesorter({headers: {0:{sorter: false}}, widgets: ['zebra']});
		$(".deleteuser").click(function(){ return confirm("Are you sure you want to delete this user?")});
        });
</script>

<% htmlviewfunctions.displaycommandresults({"createuser", "deleteuser", "editusersettings"}, session) %>

<% local header_level = htmlviewfunctions.displaysectionstart(view, page_info) %>
<% local header_level2 = htmlviewfunctions.incrementheader(header_level) %>
<table id="list" class="tablesorter"><thead>
	<tr>
		<th>Action</th>
		<th>Extension</th>
		<th>First Name</th>
		<th>Last Name</th>
	</tr>		
</thead><tbody>
<% local username = cfe({ type="hidden", value="" }) %>
<% local redir = cfe({ type="hidden", value=page_info.orig_action }) %>
<% for k,v in ipairs( view.value ) do %>
	<tr>
		<td>
		<% username.value = v.username %>
		<% if viewlibrary.check_permission("editusersettings") then %>
			<% htmlviewfunctions.displayitem(cfe({type="link", value={username=username, redir=redir}, label="", option="Edit", action="editusersettings"}), page_info, -1) %>
		<% end %>
		<% if viewlibrary.check_permission("deleteuser") then %>
			<% htmlviewfunctions.displayitem(cfe({type="form", value={username=username}, label="", option="Delete", action="deleteuser", class="deleteuser" }), page_info, -1) %>
		<% end %>
		</td>
                <td><%= html.html_escape(v.username) %></td>
                <td><%= html.html_escape(v.firstname) %></td>
                <td><%= html.html_escape(v.lastname) %></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 users found</p>
<% end %>

<% if viewlibrary and viewlibrary.dispatch_component and viewlibrary.check_permission("createuser") then %>
<% htmlviewfunctions.displayitem(cfe({type="link", value={redir=redir}, label="Create New User", option="Create", action="createuser" }), page_info, header_level2) %>
<% end %>

<% if viewlibrary and viewlibrary.dispatch_component and viewlibrary.check_permission("createmanyusers") then %>
<% htmlviewfunctions.displayitem(cfe({type="link", value={redir=redir}, label="Create Multiple Users", option="Create", action="createmanyusers" }), page_info, header_level2) %>
<% end %>

<% htmlviewfunctions.displaysectionend(header_level) %>
