<?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 edit_devices extends database
{
   public function index($data,$id)
    {
		$where['id']= $id;
		return $this->update('device',$data, $where);
	}
	public function Getdata($id)
    {
		$where['id']= $id;
		return $this->fetchRow('device', $where);
	}
}
$dev = new edit_devices();	
$msg="";
if(!isset($_GET['id']))
{
	  header('Location:devices.php');
}
$id=$_GET['id'];
if(isset($_POST['data']))
{
	$data=$_POST['data'];
	if(trim($data['name']) == '')
	{
		$msg.= "Device name is required<br/>";
	}
        if(trim($data['description']) == '')
	{
		$msg.= "Description is required<br/>";
	}
	if($msg=="")
	{
		 $res = $dev->index($data,$id);
            if($res)
            {
                    header('Location:devices.php?message=Device Updated Successfully');
            }
            else {
                $msg = 'Something Went Wrong';
            }
	}
}
 $res1 = $dev->Getdata($id);
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 Devices</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">
							
							<form method="post">
                            	<div class="col-md-12">
                                	<label>Device Name</label>
                                    <input type="text" name="data[name]" placeholder="Device Name" value="<?php echo htmlspecialchars($res1['name']);?>"/>
                                </div>
                            	<div class="col-md-12">
                                	<label>Description</label>
                                   <input name="data[description]" placeholder="Description" value="<?php echo htmlspecialchars($res1['description']);?>"/>
                                </div>
                            	
                            	
                                <div class="col-md-12 botm-cont">
                                	<button type="submit" class="track-btn">Update</button>
                                </div>
							</form>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
	</div>
</div>

<script>
    $(document).ready(function(){
        $('.activ-cat').removeClass('activ-cat');
        $('#device-menu').addClass('activ-cat');
    }); 
</script>

<?php include 'footer.php';?>