<?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_orders extends database
	{
		public function index($data,$id)
		{
			$where['id']=$id;
			return $this->update('orders', $data, $where);
		}
		public function Device()
		{
		
			return $this->fetchAll('device');
		}
		public function getdata($id)
		{
			$where['id']=$id;
			return $this->fetchRow('orders',$where);
		}
	}
$add = new edit_orders();
$msg="";
if(!isset($_GET['id']))
{
	  header('Location:orders.php');
}
$id=$_GET['id'];
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}
if(isset($_POST['data']))
{
	$data=$_POST['data'];
	if(trim($data['name']) == '')
	{
		$msg.= "Name is required<br/>";
	}
        if(trim($data['address']) == '')
	{
		$msg.= "Address is required<br/>";
	}
	    if(trim($data['phone']) == '')
	{
		$msg.= "Phone Number is required<br/>";
	}
	    if(trim($data['door_code']) == '')
	{
		$msg.= "Door code is required<br/>";
	}
	    if(trim($data['city']) == '')
	{
		$msg.= "city is required<br/>";
	}
	if($msg=="")
	{
		$data['user_id']=$_SESSION['id'];
		 $res = $add->index($data,$id);
            if($res)
            {
                    header('Location:orders.php?message=Order Updated Successfully');
            }
            else {
                $msg = 'Something Went Wrong';
            }
	}
}
 $dev = $add->Device();
 $od = $add->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>Edit Orders</h1>
						<?php if($msg !=''){echo '<span class="alert alert-danger pull-left">'.$msg.'</span>';}?>
                    </div>
                    <div class="clearfix"></div>
                    <div class="col-sm-6">
                    	<div class="order-contact">
                        	<h2>ORDER: #<?php echo htmlspecialchars($od['id']);?></h2>
                            <div class="row">
                            	<form method="post">
                            	<div class="col-md-12">
                                	<label>Name</label>
                                    <input type="text" name="data[name]" placeholder="Name" value="<?php echo htmlspecialchars($od['name']);?>"/>
                                </div>
                            	<div class="col-md-12">
                                	<label>Address</label>
                                    <input type="text" name="data[address]" placeholder="Address" value="<?php echo htmlspecialchars($od['address']);?>"/>
                                </div>
                            	<div class="col-md-12">
                                	<label>Phone Number</label>
                                    <input type="text" name="data[phone]" placeholder="Phone Number" value="<?php echo htmlspecialchars($od['phone']);?>"/>
                                </div>
                            	<div class="col-md-12">
                                	<label>Device</label>
                                    <select name="data[device_id]">
                                    	<option value="">--Device--</option>
										<?php
										foreach($dev as $d)
										{
										?>
                                        <option value="<?php echo htmlspecialchars($d['id']); ?>" <?php if($od['device_id'] == $d['id']){echo 'selected';}?>><?php echo htmlspecialchars($d['name']); ?></option>
										<?php
										}
										?>
                                    </select>
                                </div>
                            	<div class="col-md-12">
                                	<label>Door Code</label>
                                    <input type="text" name="data[door_code]" placeholder="Door Code" value="<?php echo htmlspecialchars($od['door_code']);?>"/>
                                </div>
                            	<div class="col-md-12">
                                	<label>City</label>
                                    <input type="text" name="data[city]" placeholder="City" value="<?php echo htmlspecialchars($od['city']);?>"/>
                                </div>
                                <div class="col-md-12 botm-cont">
                                	<button type="submit" class="track-btn">Submit</button>
									<p><strong>Status:</strong> Active <span class="fa fa-check check"></span></p> 
                                </div>
								</form>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
	</div>
</div>

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

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