<?php
session_start();
error_reporting(1);
ini_set('display_errors', 1);
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 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';
?>
            <!-- MAIN CONTENT-->
            <div class="main-content">
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="overview-wrap">
                                    <h2 class="title-1">edit orders</h2>
                                    
                                </div>
								<?php if($msg !=''){echo '<span class="col-md-6 alert alert-danger pull-left">'.$msg.'</span>';}?>
                            </div>
                        </div>
                        
                        <div class="row m-t-25 m-row">
					
                            <div class="col-lg-6">
                                <div class="card">
                                    <div class="card-header">
                                        <strong>ORDER: #<?php echo $od['id'];?></strong>
                                        
                                    </div>
                                    <div class="card-body card-block">
									<form method="post">
                                        <div class="form-group">
                                            <label for="Name" class=" form-control-label">Name</label>
                                            <input type="text" name="data[name]" id="Name" placeholder="Name" class="form-control" value="<?php echo htmlspecialchars($od['name']); ?>" />
                                        </div>
                                        <div class="form-group">
                                            <label for="Address" class=" form-control-label">Address</label>
                                            <input type="text" id="Address" name="data[address]" placeholder="Address" class="form-control" value="<?php echo htmlspecialchars($od['address']); ?>" />
                                        </div>
                                        <div class="form-group">
                                            <label for="PhoneNumber" class=" form-control-label">Phone Number</label>
                                            <input type="text" name="data[phone]" id="PhoneNumber" placeholder="Phone Number" class="form-control" value="<?php echo htmlspecialchars($od['phone']); ?>" />
                                        </div>
										<div class="form-group">
                                            <label for="Device" class="form-control-label">Device</label>
                                            <select id="Device" class="form-control"  name="data[device_id]">
												<option>--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="form-group">
											<label for="Door" class="form-control-label">Door Code</label>
											<input type="text"  name="data[door_code]" placeholder="Door Code" id="Door" class="form-control" value="<?php echo htmlspecialchars($od['door_code']); ?>" />
										</div>
										<div class="form-group">
											<label for="City" class="form-control-label">City</label>
											<input type="text"  name="data[city]"  placeholder="City" id="City" class="form-control" value="<?php echo htmlspecialchars($od['city']); ?>" />
										</div>
										<div class="form-group">
										<button type="submit" class="track-btn pull-right">Submit</button>
										<p><strong>Status:</strong> Active <span class="fa fa-check check"></span></p> 
										</div>
                                        
                                        </form>
                                    </div>
                                </div>
                            </div>
                          
                        </div>
                       <script>
							$(document).ready(function(){
								$('.active').removeClass('active');
								$('#orders-menu').addClass('active');
							}); 
						</script>              
                        
                       

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