<?php
session_start();
error_reporting(1);
ini_set('display_errors', 1);
session_start();
require_once "../config.php";
include_once "../ichat/admin/route.php";
if ($session->is_logged_in() != true ) {
        redirect_to(WEB_LINK.'/login');
} else {
    $current_user = User::get_specific_id($session->admin_id);
    $current_user->set_online();
    $group = $current_user->prvlg_group;
}


require_once("../ichat/admin/assets/includes/Library/SessManager.php");
require_once("../ichat/admin/assets/includes/Library/User.php");
$session = new  SessManager();

//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 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, 'cl_created_at', 'DESC');
    }
	 public function CurrentuserIP($id)
    {
        $where['user_id']=$id;
        return $this->fetchRow('user_log', $where);
    }
}
$cat = new new_user();
$msg="";
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}
if(isset($_POST['data']))
{
	$data=$_POST['data'];
	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=="")
	{
		 $res = $cat->index($data);
            if($res)
            {
                    header('Location:add-users.php?message=User Added 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">
									
									<form method="post">
                                        <div class="col-md-12">
                                            <label>Username</label>
                                            <input type="text" name="data[username]" placeholder="Imp1337" />
                                        </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">ADD USER</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></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';?>