<?php 
	include("includes/config.php");
	include("includes/func.php");
	
	if (($_SESSION['usertype'] != "Admin"))
	{
		redirect("login");
	}
	
	include("templates/header.php");
	
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

$_SESSION['PartNumber'] = $_POST['PartNumber'];

$partSearch = $_SESSION['PartNumber'];

// sets the amount of rows to display
$rowsPerPage = 25;

// sets the page number. ( 1 because by default first page is shown )
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
	if($pageNum == "0" || $pageNum == "" || !is_numeric($pageNum)){ $pageNum = 1; }
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

// creates a mysql query that selects all rows from the table
$queryTotalRows = mysql_query("SELECT * FROM users") or die ('Error: '.mysql_error ());

$num_rows = mysql_num_rows($queryTotalRows);

// how many pages we have
$maxPage = ceil($num_rows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];


// creating previous and next link plus the link to go straight to the first and last page
if ($pageNum > 1) {
  $page  = $pageNum - 1;
  $prev  = "<a href=\"$self?page=$page\" title='Previous'>&lt;".$Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Previous')."</a>";
   $first = "<a href=\"$self?page=1\" title='First'>&lt;&lt;</a> ";
} else {
  $prev  = "&lt;".$Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Previous'); // we're on page one, don't print previous link
  $first = "&lt;&lt;&nbsp;&nbsp;"; // nor the first page link
}

if ($pageNum < $maxPage){
  $page = $pageNum + 1;
  $next = "<a href=\"$self?page=$page\" title='Next'>".$Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Next')."&gt;</a> ";
  $last = "<a href=\"$self?page=$maxPage\" title='Last'>&gt;&gt;</a> ";
} else {
  $next = "&nbsp;".$Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Next')."&gt;"; // we're on the last page, don't print next link
  $last = "&nbsp;&nbsp;&gt;&gt;"; // nor the last page link
}

/*id 	firstname 	lastname 	email 	usertypeid 	username 	password 	delete*/
$sql = "select * from users ORDER BY `firstname` ASC LIMIT $offset, $rowsPerPage";
$res = mysql_query($sql) or die ('Error: '.mysql_error ());
if (mysql_num_rows($res) > 0)
	{
		$i=1;
		while($row = mysql_fetch_array($res)){
			
			$User_Type	=	'';
			
			if($i%2 == 0 )
			{
				$UsersHTML .= "<tr class='even'>";
			}else{
				$UsersHTML .= "<tr class='odd'>";
			}
			
			$UsersHTML .= "<td>$i</td>";
			$UsersHTML .= "<td>" . $row['firstname']." ". $row['lastname'] . "</td>";
			$UsersHTML .= "<td>" . $row['email'] . "</td>";
			
			if($row['usertypeid'] == '1')
			{
				$User_Type	=	'Admin';
			}else{
				$User_Type	=	'User';
			}
			
			$UsersHTML .= "<td>$User_Type</td>";
			
			$UsersHTML .= "<td>{$row['username']}</td>";
			
			$UsersHTML .= "<td align='center'>";
			
			$UsersHTML = $UsersHTML . "<a href='user_add_edit_delete.php?act=edit&uid=".$row['id']."'><img src='images/edit.png' alt='Edit' border='0'></a>&nbsp;";

			$UsersHTML = $UsersHTML .  "<a href='user_add_edit_delete.php?act=delete&uid=".$row['id']."' onclick='return confirm(\"Are you sure want to delete this user?\");'><img src='images/delete.png' alt='Delete' /></a>";
							  
			$UsersHTML = $UsersHTML .  "</td>";
			$UsersHTML .= "</tr>";
			$i ++;
		}
	}
else
	{
		$UsersHTML = "<tr align='center'><td colspan='6'>No Records found.</td></tr>";
		$TableSortHeader = '';
	}


// print the navigation link
if($UsersHTML != "") {
	$pagingTxt = $first . $prev ." $pageNum of $maxPage" . $next . $last ;
} else {
	$pagingTxt = '';
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


?>
<!-- Start main box area in which grid view display-->

<div id="mainPanel">
  <div id="datagrid">
	
    <h2>User Management</h2>
    <a href="user_add_edit_delete.php" class="form_button">Add New User</a>
    <div style="margin-bottom:5px;"></div>
    
    <br clear="all" />
    
     <div style="text-align:left; padding-bottom:5px; color:green;">
    	<?php
        if ($_SESSION['msg'] != '') { 
			    print $_SESSION['msg'];
                $_SESSION['msg'] = '';
        }
		?>
    </div>    
    
    <table id="tabling1" width="100%" border="0" cellspacing="0" cellpadding="0">
      
      <tr class="header">
        <td class="col1" width="3%"><span class="blue">#</span></td>
        <td class="col1" width="10%"><span class="blue"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Name'); ?></span></td>
        <td class="col1" width="20%"><span class="blue"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Email'); ?></span></td>
        <td class="col1" width="15%"><span class="blue"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Type'); ?></span></td>
        <td class="col1" width="15%"><span class="blue"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'User_Name'); ?></span></td>
        <td class="col1" width="3%" style="text-align:center"><span class="blue"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Action'); ?></span></td>
      </tr>

      <?php echo $UsersHTML; ?>
		
      <tr>
      	<td colspan="6" style="text-align:center"><?php echo $pagingTxt; ?></td>
      </tr>  
    </table>
        
  </div>
  <!-- Grid ends --> 
  
</div>
<!-- Maindata box ends -->
<?php include("templates/footer.php"); ?>