<%@ page import="java.util.List" %>
<%@ page import="zall.bean.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.sql.Timestamp" %>
<%
User user = (User)session.getAttribute("user");
List<User> users = (List<User>)request.getAttribute("users");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd HH:MM");

String search = null;
if( request.getParameter("search") != null ){
	if( !request.getParameter("search").trim().isEmpty() && !request.getParameter("search").equalsIgnoreCase("To search, type and hit enter") )
		search = request.getParameter("search");
}
%><%!

	public void userHTML(JspWriter out, User currentUser, User printUser) throws IOException{
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd HH:MM");
	
		out.println("	<li class='comment' style='padding-bottom: 5px; padding-top: 5px;'>");
		out.println("			<div class='comment-author vcard'>");
		out.println("				<img src='img/default-user.png' class='photo avatar photo' height='80' width='80' /> ");
		  out.print("				<span class='fn n'>");
		if( printUser.getEmail().isEmpty() )
		out.println("&nbsp;");			
		else if( currentUser.canEdit( printUser ))
		out.println("<a href='profile?id="+printUser.getId()+"'>"+printUser.getEmail()+"</a>");
		else
		out.println( printUser.getEmail() );		
		out.println("					&nbsp;"+(printUser.getEmail().isEmpty() || printUser.isEmailVerified() ? "" : "(Unverified)"));
		out.println("				</span>");
		out.println("			</div>");
		
		out.println("			<div class='comment-content'>");
		out.println("				<table border=1 width='100%'>");
		out.println("					<tr>");
		out.println("						<td width='180px'><b>"+printUser.getName()+"</b></td>");
		if( printUser.getLoginDate().after(new Timestamp(System.currentTimeMillis()-60*60*1000) )){
        out.println("						<td width='180px'><b>Last login:</b> <span style='color: lightgreen;'>"+formatter.format( printUser.getLoginDate() )+"</span></td>");
		} else {
		out.println("						<td width='180px'><b>Last login:</b> "+formatter.format( printUser.getLoginDate() )+"</td>");
		}
		out.println("						<td>&nbsp;");
		out.println("						&nbsp;</td>");
		out.println("					</tr>");
		
		out.println("					<tr style='margin: 0px; padding:0px'>");		
		if( currentUser.isSuperUser() && !currentUser.equals(printUser) ){
			if( printUser.getEmail() != null && !printUser.getEmail().isEmpty() && !printUser.isEmailVerified() ){
		out.println("						<td>");
		out.println("							<form method='post' style='margin: 0px;'>");
		out.println("								<input type='hidden' name='action' value='sendverfemail' /> ");
		out.println("								<input type='hidden' name='id' value='"+printUser.getId()+"' />  ");
		out.println("								<input type='submit' value='Send VerfEmail' />");
		out.println("							</form>");
		out.println("						</td>");
			} else {
		out.println("						<td>&nbsp;</td>");
			}
		out.println("						<td align='right'>");
		out.println("							<form method='post' style='margin: 0px;'>");
		out.println("								<input type='hidden' name='action' value='moduserstat' /> ");
		out.println("								<input type='hidden' name='id' value='"+printUser.getId()+"' />  ");
		
			if( !printUser.isEnabled() )
		out.println("								<input type='submit' name='enable' value='Enable' />");
			else
		out.println("								<input type='submit' name='disable' value='Disable' />");
		
			if(printUser.isSuperUser())
		out.println("								<input type='submit' name='unadmin' value='UnAdmin' />");
			else
		out.println("								<input type='submit' name='setadmin' value='Admin' />");
		
		out.println("							</form>");
		out.println("						</td>");
		out.println("						<td>");
		out.println("							<form method='post' style='margin: 0px;'>");
		out.println("								<input type='hidden' name='action' value='rmuser' /> ");
		out.println("								<input type='hidden' name='id' value='"+printUser.getId()+"' />  ");
		out.println("								<input type='submit' value='Remove' />");
		out.println("							</form>");
		out.println("						</td>");
		
		} else{
		out.println("						<td align='right' colspan='3'>&nbsp;</td>");
		}							
		out.println("					</tr>");
		out.println("				</table>");
		out.println("			</div>");
		out.println("		</li>");
	}
%>

<div id="main">
	<div id="container">
		<div id="content">
			<div class="page type-page hentry">
				<h1 class="entry-title">Users</h1>
			</div><!-- #post -->
			<div id="comments">
				
				<a name="inactive"></a> 
				<div id="comments-list" class="comments">
					<h3>Inactive Users</h3>
					<ol>					
						<%
						for( User us : users ){ 
							if( !us.isEnabled() ){
								if( search == null ||
										us.getName().contains(search) ||
										us.getEmail().contains(search) ){
									userHTML(out, user, us);
								}
							}
						}
						%>						
					</ol>
				</div><!-- #comments-list .comments -->


				<a name="active"></a> 
				<div id="comments-list" class="comments">
					<h3>Active Users</h3>
					<ol>					
						<%
						for( User us : users ){ 
							if( us.isEnabled() && !us.isSuperUser() ){
								if( search == null ||
										us.getName().contains(search) ||
										us.getEmail().contains(search) ){
									userHTML(out, user, us);
								}
							}
						} 
						%>						
					</ol>
				</div><!-- #comments-list .comments -->
				
				
				<a name="admin"></a> 
				<div id="comments-list" class="comments">
					<h3>Admins</h3>
					<ol>					
						<% 
						for( User us : users ){ 
							if( us.isEnabled() && us.isSuperUser() ){
								if( search == null ||
										us.getName().contains(search) ||
										us.getEmail().contains(search) ){
									userHTML(out, user, us);
								}
							}
						} 
						%>						
					</ol>
				</div><!-- #comments-list .comments -->
				
			</div><!-- #comments -->
		</div><!-- #content -->
	</div><!-- #container -->


	<div class="aside main-aside">
		<ul class="xoxo">
			<li class="widgetcontainer widget_search">
				<h3 class="widgettitle"><label for="s">Search</label></h3>
			
				<form id="searchform" method="get">
					<div>
						<input id="searchinput" name="search" type="text" value="<%=( search!=null ? search : "To search, type and hit enter" ) %>" onfocus="if (this.value == 'To search, type and hit enter') {this.value = '';}" onblur="if (this.value == '') {this.value = 'To search, type and hit enter';}" size="32" tabindex="1" /> 
						<input type="submit" value="Search" tabindex="2" />
						<input type="submit" value="X" tabindex="3" onclick="jQuery('#searchinput').attr('value', ''); return 1;" />
					</div>
				</form>
			</li>
			<li class="widgetcontainer widget_pages">
				<h3 class="widgettitle">User Types</h3>
				<ul>
					<li class="page_item"><a href="#inactive">Inactive Users</a></li>
					<li class="page_item"><a href="#active"  >Active Users</a></li>
					<li class="page_item"><a href="#admin"   >Admins</a></li>
				</ul>
			</li>
		</ul>
	</div><!-- #primary .aside -->
</div><!-- #main -->
