<?php session_start();
include("../includes/func.php");
include("../includes/connection.php"); 
?>
<?php if (empty($_SESSION["admin"])) redirectS("index.php?e=1");
//Single Delete
if (!empty($_GET["delid"])) {
	mysql_query("delete from user_master where `user_id`=" . $_GET["delid"]);
	redirectS($_SERVER["HTTP_REFERER"]);
}

$pg = new paging;
$pg->setpages("SELECT count(*) from user_master where user_type in (1,2,3)");
if ($_GET["sort"]!='')
	$sort=$_GET["sort"];
else
	$sort="user_id";
	
if ($_GET["sortorder"]!='')
	$sortorder=$_GET["sortorder"];
else
	$sortorder="desc";
$sql="SELECT * from user_master where user_type in (1,2,3) order by ".$sort." ".$sortorder;
$sql .=$pg->getlimit();


if ($_GET["what"]!='')
{
	$sql="Select * from user_master where " . $_GET["criteria"] . " like '%" . $_GET["what"] . "%' and  user_type in (1,2,3) order by user_id desc";
	$_SESSION["errorMessage"]="Filtered Results";
}
$cmd = mysql_query($sql);
?>
<?php include("includes/header.php"); ?>
<??>

<table width="98%" height="100" border="0" align="center" cellpadding="5" cellspacing="5">
	<tr>
		<td height="50"><span style="font-weight: bold">User Management </span> [<a href="#" onClick="openWin('userPopup.php', 'width=450, height=330');" class="BlackAri12B">Add New User</a>] &nbsp; [<a href="#search" class="BlackAri12B">Search</a>]&nbsp; [<a href="#sort" class="BlackAri12B">Sort</a>]
	      <hr size="1">  </td>
	</tr>
	
	<?php
	//if any records retrieved
	if (mysql_num_rows($cmd) != 0) {
	?>
	
	<tr>
		<td class="BlackAri12B" colspan=2><table width="100%" border="0" cellpadding="2" cellspacing="1" class="bgDarkGray">
            <tr class="bgLightGray">
                <td width="5%">&nbsp;</td>
				<td width="20%" class="BlackAri12B">User Name</td>
                <td width="20%" class="BlackAri12B">Login Name</td>
                <td width="20%" class="BlackAri12B">User Type</td>
				<td width="5%" class="BlackAri12B">Status</td>
				<td width="40%" class="BlackAri12B">email</td>
				<td width="40%" class="BlackAri12B">Date</td>
			</tr>
			<?php getErr("6"); ?>
            <tr>
                <td colspan="7" height="10" class="bgWhite"></td>
            </tr>
			<?php
			//loop thru records and display category listing
			while ($rs = mysql_fetch_array($cmd)) {
				if ($bgclass == "bgExtraLightGray")
					$bgclass = "bgWhite";
				else
					$bgclass = "bgExtraLightGray";
					$category_parent='';
				switch($rs["user_type"]){
					case 1:
						$user_type="<b>Super admin</b>";
						break;
					case 2:
						$user_type="NewsLetter";
						break;
					case 3:
						$user_type="Customer";
						break;
			}
			?>
			
            <tr class="<?php echo($bgclass); ?>">
                <td><table border="0" align="center">
                    <tr>
                        <td><a href="#" onClick="openWin('userPopup.php?user_id=<?php echo($rs["user_id"]); ?>', 'width=450, height=330');"><img src="../images/icons/view.gif" width="16" height="16" border="0" alt="Edit Record"></a></td>
                        <td><a href="admin_users.php?delid=<?php echo($rs["user_id"]); ?>" onClick="return window.confirm('Are you sure you want to delete this record.');"><img 
						src="../images/icons/delete.gif" width="16" height="16" border="0" alt="Delete Record"></a></td>
					 </tr>
                </table></td>
				<td class="BlackAri12" valign=top width="20%"><small><?php echo($rs["last_name"]); ?>&nbsp;<?php echo($rs["first_name"]); ?> <br>
				<a href="mailto:<?php echo($rs["email"]); ?>"><font color=blue><small><?php echo($rs["email"]); ?></small></font></a></small></td>
                <td class="BlackAri12" valign=top width="20%">&nbsp;<small><?php echo($rs["user_name"]); ?></small>
				</td>
				<td class="BlackAri12" width="20%">&nbsp;<small><?php echo($user_type); ?></small></td>
				<td align="center" width="20%"><?php stsfx($rs["active"]); ?></td>
				<td align="center" width="10%" class="BlackAri12" ><small><?php echo $rs["email"]; ?></small></td>
				<td align="center" width="10%" class="BlackAri12" ><small><?php echo $rs["user_date"]; ?></small></td>
            </tr>
			<?php } ?>
            <?if ($_GET["what"]=='')
				{?>
					<tr class="bgGray">
					   <td colspan="9" align="center" class="BlackAri12">
							<?php $pg->showPagingw("", "&ps=".$_GET["ps"]."&sort=".$_GET["sort"]."&sortorder=".$_GET["sortorder"]); ?>
					  </td>
					</tr>
			<?}?>
        </table></td>
	</tr>
	
	<?php } else { ?>
	<tr>
		<td align="center" class="err">No User found.</td>
	</tr>
	<?php } ?>
	<tr class="bgExtraLightGray">
		<td height="50" align="center" class="label">
		<?$sql="SELECT count(*) from user_master where user_type=3";
	$cmd = mysql_query($sql);
	$rs = mysql_fetch_array($cmd);
	echo $rs[0]." Total Registered Users.<br>";
	$sql="SELECT count(*) from user_master where user_type=1 or  user_type=2";
	$cmd = mysql_query($sql);
	$rs = mysql_fetch_array($cmd);
	echo $rs[0]." Total admin Users.";?>
	</td>
	</tr>
	<tr>
		<td height="50" align="center">&nbsp;</td>
	</tr>
	 <tr class="bgLightGray"><form name=search action="" method=get>
     <td width="100%" colspan=7 class="BlackAri12B">Search Users <a name=search></a>:
	
<input type=text name=what size=15 value="<?=$_GET["what"];?>">
<select name=criteria>
<option value=first_name>First Name</option>
<option value=email>User Name/Email</option>
</select>
<input type=submit name=search value=Search>
</form>
</td></tr>
<tr class="bgLightGray"><form name=search action="" method=get>
<td width="100%" colspan=7 class="BlackAri12B">Sort Users:<a name=sort></a><br>
No Of Users Per Page:	
<input type=text name=ps size=3 value="<?=$_GET["ps"];?>"><br>
Sort By:
<select name=sort>
<option value=first_name>First Name</option>
<option value=email>User Name/Email</option>
<option value=user_type>User Type</option>
</select>
<select name=sortorder>
<option value=desc>Decreasing</option>
<option value=asc>Increasing</option>

</select>
<input type=submit name=sortme value="Sort Users">
</form>
</td></tr>
</table>
</form>

<?php include("includes/footer.php"); ?>
