<?php
/*include_once "../ichat/admin/route.php";
require_once 'database.php';
if(isset($_SESSION[$elhash]) && $_SESSION[$elhash] != "") { 
    $random_hash = $_SESSION[$elhash];
} else {
    $random_hash = uniqid();
    $_SESSION[$elhash] = $random_hash;
}*/
session_start();
require_once "check_admin.php";
require_once 'database.php';
class new_user extends database
{
   public function index($data)
    {
		return $this->insert('user', $data);
	}
	 public function changes($data,$id)
    {
        $where['id'] = $id;
		return $this->update('user', $data, $where);
	}
	 public function check($username)
    {
        $where['username']=$username;
         return $this->fetchRow('user', $where);
    }
	 public function Currentuser()
    {
        $where[0]['user_type']['operator'] = '!=';
		$where[0]['user_type']['value'] = 'admin';
        return $this->fetchAll('user', $where, 'created_at', 'DESC');
    }
	 public function CurrentuserIP($id)
    {
        $where['user_id']=$id;
        return $this->fetchRow('user_log', $where);
    }
     public function e_User($id)
    {
        $where['id']=$id;
        return $this->fetchRow('user', $where);
    }
}
$cat = new new_user();
$msg="";
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}
$id="";
if(isset($_GET['id']))
{
 $id= $_GET['id'];
}
if(isset($_POST['data']))
{
	$data=$_POST['data'];
		if(trim($data['user_type']) == '')
	{
		$msg.= "Role is required<br/>";
	}
	if(trim($data['username']) == '')
	{
		$msg.= "Username is required<br/>";
	}
        if(trim($data['password']) == '')
	{
		$msg.= "Password is required<br/>";
	}
        else {
         $data['password']=password_hash($data['password'], PASSWORD_DEFAULT);
   
            }
           
        $res1 = $cat->check($data['username']);
        if($res1)
        {
            $msg.= "Username already exists.\n";
        } 

	if($msg=="" && $id=="")
	{
		 $res = $cat->index($data);
            if($res)
            {
                    header('Location:add-users.php?message=User Added Successfully');
            }
            else {
                $msg = 'Something Went Wrong';
            }
	}
	elseif($msg=="" && $id!="")
	{
	     $res = $cat->changes($data,$id);
            if($res)
            {
                    header('Location:add-users.php?message=User Updated Successfully');
            }
            else {
                $msg = 'Something Went Wrong';
            }
	}
}
 include 'header.php';
 ?>
<div class="ordes-content">
    <div class="container midle-order">
        <div class="row">
        	<?php include 'menu.php';?>
            <div class="col-md-9">
                <div class="order-wraper">
                	<div class="col-md-12">
                    	<h1>Add Account</h1>
						<?php if($msg !=''){echo '<span class="alert alert-danger pull-left">'.$msg.'</span>';}?>
                    </div>
                    <div class="clearfix"></div>
                    <div class="col-sm-12">
                    	<div class="order-contact">
                        	<div class="row edit-user-box">
                                <div class="col-sm-12">
                                    <div class="row">
										<?php
									$name="";
								
									$btn="ADD";
									$role="";
									if($id !="")
									{
									    $cate = $cat->e_User($id);
									    	$name= $cate['username'];
									    	$role= $cate['user_type'];
									    	$btn="UPDATE";
									}
									?>
									<form method="post">
									     <div class="col-md-12">
                                            <label>Role</label>
                                            <select type="text" name="data[user_type]">
                                                <option value="">----Select----</option>
                                               
                                                <option value="admin" <?php if($role == 'admin'){echo 'selected';}?>>Admin</option>
                                               <option value="chef" <?php if($role == 'chef'){echo 'selected';}?>>Chef</option>
                                               <option value="staff" <?php if($role == 'staff'){echo 'selected';}?>>Front Staff</option>
                                            </select>
                                        </div>
                                        <div class="col-md-12">
                                            <label>Username</label>
                                            <input type="text" name="data[username]" placeholder="Imp1337" value="<?php echo $name;?>" <?php if($name != ""){echo 'readonly';}?>/>
                                        </div>
                                        <div class="col-md-12">
                                            <label>Password</label>
                                            <input type="password" name="data[password]" placeholder="Password" />
                                        </div>
                                        <div class="col-md-12 botm-cont">
                                            <button type="submit" class="track-btn"><?php echo $btn;?></button>
                                        </div>
									</form>
                                    </div>
                            	</div>
                        	</div>                            
                        </div>
                    </div>
                </div>
                <div class="order-wraper">
                	<div class="col-md-12">
                    	<h1>Current User</h1>
                    </div>
                    <div class="clearfix"></div>
                    <div class="col-sm-12">
                    	<div class="curent-user">
                        	<div class="row">
								
                                <div class="col-sm-12">
                                    <div class="curent-user-nav">
									<table class="table table-responsive"> 
											<tr>
												<th>User names</th>
												<th>Last logged IP: 111111</th>
												<th>Actions</th>
											</tr>
											<?php
											 $user = $cat->Currentuser();
											 foreach($user as $u)
											 {
												$userip = $cat->CurrentuserIP($u['id']);
											?>
											<tr>
												<td><?php echo htmlspecialchars($u['username']);?></td>
												<td><?php echo htmlspecialchars($userip['ip']);?></td>
												<td><button id="remove" data-href="user-remove?id=<?php  echo htmlspecialchars($u['id']); ?>" class="track-btn">REMOVE</button>
												<a id="edit" href="add-users.php?id=<?php  echo htmlspecialchars($u['id']); ?>" class="track-btn bg-primary">EDIT</a></td>
											</tr>
											<?php
											
											 }
											?>
                                </table>
                                    	
                                    </div>
                            	</div>
                                
                        	</div>                            
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
	</div>
</div>
<script>
    $(document).ready(function(){
        $('#remove').click(function(){
            var parent = $(this).parents('tr').first();
            var conf = confirm("Are you sure you want to delete this user?");
            if(conf)
            {
                var href = $(this).attr('data-href');
                $.post(href, function(res){
                    alert("Deletion Successful");
                    $(parent).remove();
                },'json').fail(function(xhr){
                   alert(xhr.responseJSON);
                },'json');
            }
        });
    });
	
    </script>

    <script>
    $(document).ready(function(){
        $('.activ-cat').removeClass('activ-cat');
        $('#add-user-menu').addClass('activ-cat');
    }); 
</script>
<?php include 'footer.php';?>