<?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 devices extends database
{
   public function index()
    {
		return $this->fetchAll('device');
	}
}
$msg='';
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}
$dev = new devices();	
$res=$dev->index();
 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>All Devices</h1>
						<a class="btn btn-success pull-right" href="add-devices.php">Add Device</a>
						<?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">
						   	
                                <div class="col-sm-12">
								
                                    <div class="curent-user-nav">
									
                                    	<table class="table table-responsive"> 
											<tr>
												<th>Device</th>
												<th>Description</th>
												<th>Actions</th>
											</tr>
											<?php
											foreach($res as $d)
											{
											?>
											<tr>
												<td><?php echo htmlspecialchars($d['name']);?></td>
												<td><?php echo htmlspecialchars($d['description']);?></td>
												<td><a id="edit" href="edit-devices?id=<?php  echo htmlspecialchars($d['id']); ?>" class="btn btn-primary"><i class="fa fa-pencil"></i></a> <a data-href="device-remove?id=<?php  echo htmlspecialchars($d['id']); ?>" class="remove btn btn-warning">Remove</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 device?");
            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');
        $('#device-menu').addClass('activ-cat');
    }); 
</script>
<?php include 'footer.php';?>