<?php // Administration Bootstrap 

	//session_start();
	chdir('../');
	
	include('includes/include_all.inc.php');
	
	//echo BOTTOM;
	//Create User object
	$obj_users = new Users();
	
	if(!$obj_users->checkUserLoginOrNot()){
	    header('Location: '.BASEURL.'login.php');
	} 
	
	if($_SESSION['user_type']!='super' && $_SESSION['user_type']!='admin'){ 
	    session_destroy();
	    header('Location: '.BASEURL.'login.php');
	}
	
	if($_SESSION['user_type'] == 'super') {
	    $usertype = 'super';
	} else {
	    $usertype = 'admin';
	}
	
	$groupWhere = "";
	
	if($usertype == 'admin'){
	    $groupWhere = " WHERE group_name = '".$_SESSION['user_group']."' ";
	}
	
	if (isset($_GET["groupnamesort"]) && !empty($_GET["groupnamesort"])){
		if ($_GET["groupnamesort"] === "ORDER BY group_name asc") {
		     $groupNameSort = "ORDER BY group_name asc";
		     $groupNameSortNext = "ORDER BY group_name desc";
		} else {
		     $groupNameSort = "ORDER BY group_name desc";
		     $groupNameSortNext = "ORDER BY group_name asc";
		}
	}
	else {
	      $groupNameSort = "";
	      $groupNameSortNext = "ORDER BY group_name desc";
	}
	
	if (isset($_GET["institutionnamesort"]) && !empty($_GET["institutionnamesort"])){
		if ($_GET["institutionnamesort"] === "ORDER BY group_institution_name asc") {
		     $institutionNameSort = "ORDER BY group_institution_name asc";
		     $institutionNameSortNext = "ORDER BY group_institution_name desc";
		} else {
		     $institutionNameSort = "ORDER BY group_institution_name desc";
		     $institutionNameSortNext = "ORDER BY group_institution_name asc";
		}
	}
	else {
	      $institutionNameSort = "";
	      $institutionNameSortNext = "ORDER BY group_institution_name desc";
	}
	
	if (isset($_GET["adminnamesort"]) && !empty($_GET["adminnamesort"])){
		if ($_GET["adminnamesort"] === "ORDER BY group_admin asc") {
		     $adminNameSort = "ORDER BY group_admin asc";
		     $adminNameSortNext = "ORDER BY group_admin desc";
		} else {
		     $adminNameSort = "ORDER BY group_admin desc";
		     $adminNameSortNext = "ORDER BY group_admin asc";
		}
	}
	else {
	      $adminNameSort = "";
	      $adminNameSortNext = "ORDER BY group_admin desc";
	}
	
	
	
	
	if (isset($_GET["adminemailsort"]) && !empty($_GET["adminemailsort"])){
		if ($_GET["adminemailsort"] === "ORDER BY group_email asc") {
		     $adminEmailSort = "ORDER BY group_email asc";
		     $adminEmailSortNext = "ORDER BY group_email desc";
		} else {
		     $adminEmailSort = "ORDER BY group_email desc";
		     $adminEmailSortNext = "ORDER BY group_email asc";
		}
	}
	else {
	      $adminEmailSort = "";
	      $adminEmailSortNext = "ORDER BY group_email desc";
	}
	
	
	
	
	
	
	if  (!isset($_GET["groupnamesort"]) && !isset($_GET["institutionnamesort"]) && !isset($_GET["adminnamesort"])) {
	      $groupNameSort = "ORDER BY group_name asc";
	      $groupNameSortNext = "ORDER BY group_name desc";
	}
	
	include ($DOCUMENT_ROOT. 'admin/header.php');
?>
<html>
<head>
	<title>Admin Home | Anesoft Anesthesia Simulator</title>
	
	 <script type="text/javascript">
		function confirmDeleteGeneric(group) { alert(group);
      
		    var retval=confirm("Are You Certain?");
	      
		    return retval;
		}
		
		function sort() {
      
		    alert("Big Dog");
	      
		    return false;
		}
	 </script>
</head>

<body>
<?php include ($DOCUMENT_ROOT. 'admin/topmenu.php'); ?>
		<h1>Anesoft CaseLogs Admin Tool</h1>
		
		<div>
			<ul id="links">
				<li style="background-color:white;"><a href="#">Groups</a></li>
				<li><a href="users.php">Users</a></li>
				<li><a href="cases.php">Cases</a></li>
				<?php
				      if ($usertype == 'super') {
					    echo "<li><a href='newgroup.php'>New Group</a></li>";
				      }
				?>
			</ul>
		</div>
			
		<p class="clearfloat spacer">
		  
		<table class="home-table"  style="width:70%;">
			<tr>
			    <th style="width:20%;">Group Name&nbsp;&nbsp;<a class="sort"  href="index.php?groupnamesort=<?= $groupNameSortNext ?>" >Sort</a></th>
			    <th style="width:25%;">Institution Name&nbsp;&nbsp;<a class="sort" href="index.php?institutionnamesort=<?= $institutionNameSortNext ?>" >Sort</a></th>
			    <th style="width:25%;">Institution Address</th>
			    <th style="width:15%;">GroupPassword</th>
			    <th style="width:10%;text-align:center;">Date Created</th>
			    <th style="width:5%;text-align:center;">Users</th>
			</tr>
			
			<?php
				 $groups = "SELECT 
								    id, 
								    group_name,
								    group_admin, 
								    group_email, 
								    group_status,
								    group_notes ,
								    group_pass,
								    group_pass_clear,
								    group_institution_name,
								    group_institution_address,
								    group_registered,
								    (SELECT COUNT(*) 
									    FROM wp_users  AS innertable
									    WHERE innertable.user_group = outertable.group_name) AS numusers
						    FROM wp_groups AS outertable
						    $groupWhere
						    $groupNameSort  $institutionNameSort ";		 
						    //echo $groups; exit;
				 $result = mysql_query($groups);
				 while ($oneRow = mysql_fetch_assoc($result)) {
			 ?>
				       <tr>
					    <td>
						<?= $oneRow['group_name']; ?>
					    </td>
					    <td>
						 <?= $oneRow['group_institution_name']; ?>
					    </td>
					    <td>
						 <?= nl2br(urldecode($oneRow['group_institution_address'])); ?>
					    </td>
					    <td>
						 <?= $oneRow['group_pass_clear']; ?>
					    </td>
					    <td style="text-align:center;">
						 <?= $oneRow['group_registered']; ?>
					    </td>
					    <td style="text-align:center;">
						 <?= $oneRow['numusers']; ?>
					    </td>
				      </tr>
			<?php 
			      } 
			?>
		</table>
				
		<table class="home-table"  style="width:70%;">
			<tr>
			    <th style="width:30%;">Group Name</th>
			    <th style="width:29%;">Admin Name&nbsp;&nbsp;<a class="sort" href="index.php?adminnamesort=<?= $adminNameSortNext ?>" >Sort</a></th>
			    <th style="width:28%;">Admin Email&nbsp;&nbsp;<a class="sort" href="index.php?adminemailsort=<?= $adminEmailSortNext ?>" >Sort</a></th>
			    <th style="width:6%;text-align:center;">Edit</th>
			    <?php
				  if ($usertype == 'super') {
					echo "<th style='width:6%;text-align:center;'>Delete</th>";
				  }
			    ?>
			</tr>
			
			<?php
				//  if ($result) {
				//	  mysql_data_seek($result, 0);
				//  }
				 $groups = "SELECT 
								    id, 
								    group_name,
								    group_admin, 
								    group_email, 
								    group_status,
								    group_notes ,
								    group_pass,
								    group_pass_clear,
								    group_institution_name,
								    group_institution_address,
								    group_registered,
								    (SELECT COUNT(*) 
									    FROM wp_users  AS innertable
									    WHERE innertable.user_group = outertable.group_name) AS numusers
						    FROM wp_groups AS outertable
						    $groupWhere
						    $adminNameSort  $adminEmailSort";		 
						    //echo $groups; exit;
				 $result = mysql_query($groups);
				  
				 while ($oneRow = mysql_fetch_assoc($result)) {
			 ?>
				       <tr>
					    <td>
						<?= $oneRow['group_name']; ?>
					    </td>
					    
					    <td>
						 <?= $oneRow['group_admin']; ?>
					    </td>
					    
					    <td>
						 <?= $oneRow['group_email']; ?>
					    </td>
					    
					    <td style="text-align:center;">
						<?
						      $groupId = $oneRow["id"];

						      echo "<a style='text-decoration: underline;' href='newgroup.php?id=$groupId' >Edit</a>";
						?>
					    </td>
						  <?
							if ($usertype == 'super') {
							      $groupId = $oneRow["id"];
							      $groupname = "'".$oneRow["group_name"]."'";
							      
							      echo "<td style='text-align:center;'>";
								    echo "<a style='text-decoration: underline;' href='deletegroup.php?id=$groupId&groupname=$groupname' onclick='javascript: return confirmDeleteGeneric($groupname)'>Delete</a>";
							      echo "</td>";
							}
						  ?>
				      </tr>
			<?php 
			      } 
			?>
		</table>
		      
	<?php include ($DOCUMENT_ROOT. 'admin/footer.php'); ?>
</body>
</html>

