<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

session_start();
require_once "check_admin.php";
if(!isset($_SESSION['user_type']) || $_SESSION['user_type'] !='staff')
    {
            header('Location: ../admin/');
            exit;
    }
require_once 'database.php';

class listOrders extends database
{
    public function GetAllorders()
    {
        $sql = 'SELECT o.*, GROUP_CONCAT("<div >",mi.name, " ", "<br/> Quantity: " , oi.quantity, "<br/> Price:", oi.price, "</div>","<br/>") AS items, SUM(oi.price) AS total_price FROM orders o inner join order_item oi ON o.id=oi.order_id INNER JOIN menu_item mi ON oi.item_id=mi.id WHERE o.deleted_at IS NULL AND oi.deleted_at IS NULL GROUP BY o.id ORDER BY o.created_at DESC LIMIT 0, 150';
        return $this->callQuery($sql);
    }
}

$obj = new listOrders();

$orders = $obj->GetAllorders();

 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>list Orders</h1>
                    </div>
                    <div class="clearfix"></div>
                    <div class="col-sm-12">
                        <div class="curent-user-nav">
									<table class="table table-responsive"> 
											<tr>
												<th>Order ID</th>
                                                                                                <th>Table No</th>
												<th>Date</th>
												<th>Items & Quatity</th>
												<th>
												    Total Price
												</th>
											
											</tr>
											<?php
											
											 foreach($orders as $u)
											 {
											  
											?>
											<tr>
												<td><?php echo htmlspecialchars($u['id']);?></td>
                                                                                                <td><?php echo htmlspecialchars($u['table_no']);?></td>
												<td><?php echo date('d M Y',strtotime($u['created_at']));?></td>
												<td>
												   
												    <?php
												  /*  $ca = $cat->OrderItems($u['id']);
												     foreach($ca as $it)
												     {*/
												        //$item_name = $cat->ItemData($it['item_id']);
												    echo ($u['items']);
												  //   }
												    
												    ?>
												    
												</td>
												<td>
												    <?php echo htmlspecialchars($u['total_price']) ?>
												</td>
												
												</tr>
												<tr style="border-bottom: 2px solid black">
    											    <td colspan="2">Status : <?php echo htmlspecialchars($u['order_status']);?></td>
    												<td colspan="2">
    												    <a class="btn btn-primary" href="edit_restaurant_order.php?id=<?php  echo htmlspecialchars($u['id']); ?>">Edit</a>
    												    <button data-href="order_delete?id=<?php  echo htmlspecialchars($u['id']); ?>" class="btn btn-danger btn-remove">REMOVE</button>
    												    <?php
    												    if($u['order_status'] == 'Pending')
    												    {
    												    ?>
    												        <button id="" data-href="complete?id=<?php  echo htmlspecialchars($u['id']); ?>" class="btn btn-success complete">TO COMPLETE</button>
    												    <?php
    												        
    												    }
    												    if($u['order_status'] == 'Completed')
    												    {
    												    ?>
    												     <button id="" data-href="pending?id=<?php  echo htmlspecialchars($u['id']); ?>" class="btn btn-warning pending">TO PENDING</button>
    												     <?php
    												    }
    												    
    												    
    												    ?>
    												</td>
												</tr>
											</tr>
											<?php
											
											 }
											?>
											
                                </table>
                                    	
                                    </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 order?");
            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');
            }
        });
        
        $('.complete').click(function(){
          
           // var conf = confirm("Are you sure, you want to complete this order?");
        //    if(conf)
           // {
                var href = $(this).attr('data-href');
                $.post(href, function(res){
                    alert("Completion Successful");
                   location.reload();
                },'json').fail(function(xhr){
                   alert(xhr.responseJSON);
                },'json');
          //  }
        });
        
         $('.pending').click(function(){
          
          /*  var conf = confirm("Are you want to make this order pending?");
            if(conf)
            {*/
                var href = $(this).attr('data-href');
                $.post(href, function(res){
                    alert("Change to pending stage");
                   location.reload();
                },'json').fail(function(xhr){
                   alert(xhr.responseJSON);
                },'json');
           // }
        });
    });
	
    
    
    $(document).on('click','.btn-remove',function(){
       var parent=$(this).parents('tr').first();
       var conf = confirm("Are you want to delete this order?");
            if(conf)
            {
                var href = $(this).attr('data-href');
                $.post(href, function(res){
                    alert("Deleted Successfully");
                     $(parent).remove();
                   
                },'json').fail(function(xhr){
                   alert(xhr.responseJSON);
                },'json');
            }
       
      
    });
    </script>

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