<?php

$where = "WHERE uname != 'admin' AND id != '".$_SESSION['user_id']."'";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}
###########################################  STRAT FILTER CONITIONS    #####################################
$search 	= isset($_REQUEST['search']) ? $_REQUEST['search'] : "";
if($search) {
    $search  = ($search);
	$where	.= " AND ( uname LIKE '%$search%' OR surname LIKE '%$search%' OR email LIKE '%$search%') ";
    $next_page_condition.="&search=$search";
}

$group_id = isset($_REQUEST['group_id']) ? $_REQUEST['group_id'] : "";
if ($group_id) {
   // $group_id = mysqli_real_escape_string($conn, $group_id); // Prevent SQL Injection
    $where .= " AND group_id = '$group_id'";
    $next_page_condition .= "&group_id=$group_id";
}
###########################################  	END  FILTER CONITIONS 	#####################################
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "admin";
$paging_table_name	= $table_name;
$paging_query		= $where;
$page_header = "User Management";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where LIMIT $eu, $limit";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

#################################   DELETE    #########################################
// when we click on delete this area of php code will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete')
{
	$id = $_REQUEST['id'];
	$sql=mysqli_query($conn,"DELETE FROM $table_name WHERE id =$id ");
	//mysqli_query($conn,"DELETE FROM admin_module WHERE admin_id = $id ");
	header("location: $file_name?page=admin_view&message_del=Admin user deleted Successfully".$next_page_condition.$paging);
}
#################################  END DELETE    #########################################
?>
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><?=$page_header;?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<section class="content">
  <div class="container-fluid">
		<div class="row">
			<div class="col-lg-12">
				<div class="card">
				<!----------------------- Message ------------------------------->
				 <?php if(isset($_REQUEST['message'])) { ?>
						<script>
							$(document).ready(function(){
								Toastify({
									text: "<?php echo $_REQUEST['message']; ?>",duration: 4000,close: true,gravity: "top",position: 'right',backgroundColor: "#28a745",style: {borderBottom: "3px solid white",},
								}).showToast();
								<?php unset($_REQUEST['message']); ?>
							});
						</script>
					<?}?>
					<?php if(isset($_REQUEST['message_del'])) { ?>
						<script>
							$(document).ready(function(){
								Toastify({
									text: "<?php echo $_REQUEST['message_del']; ?>",duration: 4000, close: true,gravity: "top",position: 'right', backgroundColor: "#E24D4C",style: {borderBottom: "3px solid white"},
								}).showToast();
								<?php unset($_REQUEST['message_del']); ?>
							});
						</script>
					<?} ?>
				<!----------------------- End Message ------------------------------->
					<div class="card-body">
					<!-- search form -->
					<?php include_once('admin_search_options.inc.php');?>
					<!-- /.search form -->
						<form action="index?page=add_admin" method="post" name="form1" id="form1">
						<div class="card-footer">
						   <button type="submit" name="add" class="btn btn-primary">Add User</button>
						</div>
						<? if($result>0){?>
						 <table id="example1" class="table table-bordered table-striped">
							<thead>
							  <tr>
								<th>First Name</th>
								<th>Email</th>
								<th>Department</th>
								<th>Edit</th>
								<th>Delete</th>
							  </tr>
							</thead>
							<tbody>
								<?
									$question_no = $eu +1;
									while($row =mysqli_fetch_array($sql)){ 
								?>
							  <tr>
							    <td nowrap>
									<a href="index?page=admin_progress&from=view&user_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging;?>" class="text-primary"><?php echo ucfirst($row["uname"]);?></a>
								</td>
								<td><?php echo $row['email']; ?></td>
								<td><? echo utility::getTableFieldById("groups","group_name","id",$row['group_id']);?></td>
								<td><a href="index?page=add_admin&action=edit&id=<?=$row['id'].$next_page_condition.$paging;?>"> <i class="fa fa-fw fa-edit text-primary"></i></a></td>
								<td><a href="index?page=admin_view&action=delete&id=<?=$row['id'].$next_page_condition.$paging;?>"onclick="javascript:return confirm('Are you sure you want to delete?');"><i class="fa fa-fw fa-trash text-danger"></i> </a></td>
							  </tr>
								<? } ?>
								<!-- Paging starts here  -->
								<? 
									$next_page_condition .="&page=admin_view";
									include("paging/paging_row.inc.php") ?>
								<!-- Paging ends starts here  -->
							</tbody>
						 </table>
						 <? }else{ ?>
							No	agent found...
						 <? } ?>
					</div>
					<div class="card-footer hide">
					   <button type="submit" name="add" class="btn btn-primary">Add Admin User</button>
					</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</section>