<?php
	$adminBtns .= '<a href="#" class="admin btn btn-default" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="new" data-editid=""><i class="fa fa-plus-square"></i> Add a NEW '. ucfirst($_REQUEST['type']) .'</a>';
	
	include('pages/inc/page-header.php');
	
	$query = "SELECT * FROM users AS u LEFT JOIN clients AS c ON (u.clientID = c.clientID) LEFT JOIN userAuthLevels AS ua on (u.userAuth = ua.authID) ORDER BY userLastName, userFirstName"; 
	$result = $con->query($query);
	if ($result) {
		$usersActive = ''; 
		$usersInactive = '';
		$tableFieldNames = '<th>Name</th>
							<th>Company</th>
							<th>Email</th>
							<th>Auth Level</th>
							<th>User Since</th>
							<th>Last Login</th>
							<th>Expires</th>
							<th>&nbsp;</th>';
		
		include('inc/data-tables/table-top.php');
		include('inc/data-tables/table-bottom.php');
		
		$sortColumns = 6;
		$noSortColumns = 7; // Column Number from left (column 1=0) 
		$scrollHeight = '500px';
		$tabTable = true;
		
		while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
			
			$adminLinks = ''; 
			
			if ($row['userStatus'] == 1) {
				$tableName = "usersActive";
				$adminLinks .= ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="delete" data-page="'. $_REQUEST['type'] .'" data-section="'. $tableName .'" data-editid="' . $row['userID'] . '" title="Delete '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-trash"></i></a> ';
			}
			else { 
				$tableName = "usersInactive";
				$adminLinks .= ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="react" data-page="'. $_REQUEST['type'] .'" data-section="'. $tableName .'" data-editid="' . $row['userID'] . '" title="Reactivate '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-refresh"></i></a> ';
			}
			
			$adminLinks = ' <a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="edit" data-page="'. $_REQUEST['type'] .'" data-section="'. $tableName .'" data-editid="' . $row['userID'] . '" title="Edit '. ucfirst($_REQUEST['type']) .'"><i class="fa fa-pencil-square-o"></i></a> '. $adminLinks;
			
			// User's Company Name or Abbreviation
			if (!empty($row['clientAbbr'])) 
				$client = $row['clientAbbr'];
			else
				$client = $row['clientName'];
			// User's Expiration Date
			if ($row['userExpireDate'] == "0000-00-00")
				$expDate = "Never";
			else
				$expDate = Date::convert($row['userExpireDate'], 'Y-m-d', 'M d, Y');
			// User's Last Login Date
			if ($row['userLastLogin'] == "0000-00-00")
				$lastLogin = "Never";
			else
				$lastLogin = Date::convert($row['userLastLogin'], 'Y-m-d', 'M d, Y');
				
			$$tableName .= '<tr id="record-'. $row['userID'] .'">
							<td><a href="#" class="admin" data-toggle="modal" data-target="#adminModal" data-admintype="'. $_REQUEST['type'] .'" data-adminact="edit" data-page="'. $_REQUEST['type'] .'" data-section="'. $tableName .'" data-editid="' . $row['userID'] . '">' . $row['userLastName'] . ', ' . $row['userFirstName'] . '</a></td>
							<td>' . $client . '</td>
							<td>' . $row['userEmail'] . '</td>
							<td>' . $row['authTitle'] . '</td>
							<td>' . Date::convert($row['userActiveDate'], 'Y-m-d', 'M d, Y') . '</td>
							<td>' . $lastLogin . '</td>
							<td>' . $expDate . '</td>
							<td class="adminActions">'. $adminLinks .'</td>
						</tr>';
			if (!empty($odd))
				$odd = '';
			else  
				$odd = true;
		}
		
		if (!empty($usersActive)) 
			$activeUsers = '<h3>Active Users</h3>'. $tableHeader .'<tbody id="'. $tableName .'">'. $usersActive . '</tbody></table>';
		else
			$activeUsers = '<h3>Active Users</h3><p class="center">There are currently no Active Users Listed</p>';
		
		if (!empty($usersInactive)) 
			$inactiveUsers = '<h3>Inactive Users</h3>'. $tableHeader .'<tbody id="'. $tableName .'">'. $usersInactive . '</tbody></table>';
		else
			$inactiveUsers = '<h3>Inactive Users</h3><p class="center">There are currently no Inactive Users Listed</p>';
		
		$content .= $activeUsers . $inactiveUsers;
	}
	include('inc/data-tables/dataTables.php');
?>