<?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']))
{
	$successMsg=$_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="content-wrapper">
            <!-- Content -->

            <div class="container-xxl flex-grow-1 container-p-y">
              <h4 class="fw-bold py-3 mb-4">Add Account</h4>
                

              <!-- Basic Layout -->
              <div class="row">
						<?php if($msg !=''){echo '<span class="alert alert-danger col-12">'.$msg.'</span>';}?>
							<?php if(isset($successMsg) && $successMsg !=''){echo '<span class="alert alert-success col-12">'.$successMsg.'</span>';}?>
                    
                    <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="mb-3">
                                            <label class="form-label">Role</label>
                                            <select type="text" class="form-control" 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="mb-3">
                                            <label class="form-label">Username</label>
                                            <input type="text" class="form-control" name="data[username]" placeholder="Imp1337" value="<?php echo $name;?>" <?php if($name != ""){echo 'readonly';}?>/>
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Password</label>
                                            <input type="password" class="form-control" name="data[password]" placeholder="Password" />
                                        </div>
                                        <div class="col-md-12 botm-cont">
                                            <button type="submit" class="track-btn btn btn-primary"><?php echo $btn;?></button>
                                        </div>
									</form>
                                    </div>
                            	</div>
                        	</div>                            
                        </div>
                    </div>
               
                <div class="order-wraper mt-3">
                	<div class="col-md-12">
                    	<h4 class="fw-bold py-3 mb-4">Current Users</h4>
                    </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 btn btn-danger">REMOVE</button>
												<a id="edit" href="add-users.php?id=<?php  echo htmlspecialchars($u['id']); ?>" class="track-btn btn btn-primary">EDIT</a></td>
											</tr>
											<?php
											
											 }
											?>
                                </table>
                                    	
                                    </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(){
        $('.menu-item').removeClass('active');
        $('#add-user-menu').addClass('active');
    }); 
</script>
<?php include 'footer.php';?>