<?php
session_start();
require_once "check_admin.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;
}*/

class Orders extends database
	{
		public function index($page)
		{
			$start = $page - 1;
			return $this->fetchAll('orders', null, 'created_at', 'DESC', ($start * 10), 10);
		}

		public function Device()
		{
			return $this->fetchAll('device');
		}
	}
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}

$page = 1;
if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 1)
{
    $page = htmlspecialchars($_GET['page']);
}

$order = new Orders();
$res = $order->index($page);
$dev = $order->Device();
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>Orders</h1>
							<?php if($msg !=''){echo '<span class="alert alert-danger pull-left">'.$msg.'</span>';}?>
                    </div>
                    <div class="clearfix"></div>
					<?php
					foreach($res as $od)
					{
					?>
                    <div class="col-sm-6 order-container">
                    	<div class="order-contact">
                        	<h2>ORDER: #<?php echo $od['id'];?></h2>
                            <div class="row">
                            	<div class="col-md-12">
                                	<label>Name</label>
                                    <input type="text" placeholder="Name" value="<?php echo htmlspecialchars($od['name']); ?>" readonly />
                                </div>
                            	<div class="col-md-12">
                                	<label>Address</label>
                                    <input type="text" placeholder="Address"  value="<?php echo htmlspecialchars($od['address']); ?>" readonly />
                                </div>
                            	<div class="col-md-12">
                                	<label>Phone Number</label>
                                    <input type="text" placeholder="Phone Number" value="<?php echo htmlspecialchars($od['phone']); ?>" readonly />
                                </div>
                            	<div class="col-md-12">
                                	<label>Device</label>
                                    <select disabled="true" >
                                    	<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="col-md-12">
                                	<label>Door Code</label>
                                    <input type="text" placeholder="Door Code"  value="<?php echo htmlspecialchars($od['door_code']); ?>" readonly />
                                </div>
                            	<div class="col-md-12">
                                	<label>City</label>
                                    <input type="text" placeholder="City"  value="<?php echo htmlspecialchars($od['city']); ?>" readonly />
                                </div>
                                <div class="col-md-12 botm-cont">
                                    <button type="button" class="btn btn-danger pull-left btn-delete" data-id="<?php echo htmlspecialchars($od['id'])?>">Delete</button>
                                	<a href="edit-tracking.php?id=<?php echo $od['id'];?>" class="track-btn">EDIT TRACKING</a>
                                	<a href="edit-orders.php?id=<?php echo $od['id'];?>" class="edit-btn">Edit</a>
                                    <p><strong>Status:</strong> <?php if($od['status'] ==1){echo ' Active';} ?></p>
                                </div>
                            </div>
                        </div>
                    </div>
					<?php
					}
					?>
                    <div class="pull-right col-xs-12 top-margin">
                         <?php
                        if($page > 1)
                        {
                        ?>
                        <a href="?page=<?php echo htmlspecialchars($page) - 1 ?>" class="track-btn">
                            Previous
                        </a>
                        <?php
                        }
                        if(count($res) == 10)
                        {
                        ?>
                            <a href="?page=<?php echo htmlspecialchars($page) + 1 ?>" class="track-btn">
                                Next
                            </a>
                        <?php
                        }
                        ?>
                    </div>
                </div>
                
            </div>
        </div>
	</div>
</div>

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

    $(document).on('click', '.btn-delete', function(res){
         var conf = confirm("Are you sure you want to delete this order?");
            if(!conf)
            {
                return;
            }
        $(this).attr('disabled', 'disabled').text('Deleting......');

        var current = $(this);
        var id = $(this).attr('data-id');
        var parent = $(this).parents('.order-container').first();
        $.post('order_delete', {id:id}, function(){
            alert('Order Deleted Successfully');
            $(parent).remove();
        },'json').fail(function(xhr){
           alert(xhr.responseJSON);
           $(current).removeAttr('disabled').text('Delete');
        },'json');
    });
</script>

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