<?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'] !='chef')
    {
            header('Location: ../admin/');
            exit;
    }
require_once 'database.php';
class new_category extends database
{
   

   	public function GetAllorders()
    {
        $sql = 'SELECT o.*, GROUP_CONCAT("<tr><td>",mi.name, "</td><td>", oi.quantity, "</td></tr>") AS items 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.order_status="Pending" AND  o.deleted_at IS NULL AND oi.deleted_at IS NULL GROUP BY o.id ORDER BY o.created_at';
        return $this->callQuery($sql);
      //  $where['order_status'] = 'Pending';
        //    return $this->fetchAll('orders',$where);
    }
    
    
    public function GetAllCompletedorders()
    {
        $sql = 'SELECT o.*, GROUP_CONCAT("<tr><td>",mi.name, "</td><td>", oi.quantity, "</td></tr>") AS items 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.order_status="Completed" AND  o.deleted_at IS NULL AND oi.deleted_at IS NULL GROUP BY o.id ORDER BY o.created_at LIMIT 0, 50';
        return $this->callQuery($sql);
      //  $where['order_status'] = 'Pending';
        //    return $this->fetchAll('orders',$where);
    }
   	public function OrderItems($id)
    {
        $where['order_id'] = $id;
        return $this->fetchAll('order_item',$where);
    }
    public function ItemData($id)
    {
        $where['id'] = $id;
        return $this->fetchRow('menu_item',$where);
    }
   
}
$cat = new new_category();
$completedOrders = $cat->GetAllCompletedorders();

 include 'header.php';
 ?>
<div class="ordes-content">
    <div class="container midle-order">
        <div class="row">
        
            <div class="col-md-12">
                
                <div class="order-wraper">
                	<div class="col-md-12">
                    	<h1 class="mt-3">New Orders</h1>
                    </div>
                  
                    <div class="col-sm-12">
                    	<div class="curent-user">
                        	<div class="row">
								
                                <div class="col-sm-12">
                                    <div class="curent-user-nav">
                                        <div class="row">
                                        <?php
											 $orders = $cat->GetAllorders();
											 foreach($orders as $u)
											 {
											  
											?>
											
											<div class="card col-12 col-md-6 col-lg-4">
											    <div class="card-body">
											        Order ID: <?php echo htmlspecialchars($u['id']);?>
											        <br/>
											        Date : <?php echo date('d M Y',strtotime($u['created_at']));?>
											        <br/>
											        Items and Quantity : 
											        <table class="table table-striped">
											            <tr>
											                <th>
											                    Item
											                </th>
											                <th>
											                    Quantity
											                </th>
											            </tr>
											            <?php echo  ($u['items']);?>
											        </table>
											        
											        
											       <div class="col-12 mt-3 mb-3">
											        Order Status: <?php echo htmlspecialchars($u['order_status']);?>
											        </div>
											        <?php
												    if($u['order_status'] != 'Completed')
												    {
												    ?>
												        <button id="" data-href="complete?id=<?php  echo htmlspecialchars($u['id']); ?>" class="btn btn-primary complete">COMPLETE</button>
												    <?php
												        
												    }
												    ?>
											    </div>
											</div>
											
											
											<?php
											 }
											 ?>
											 </div>
											 
											 
											 <h1 class="col-12 mt-5">
											     Completed Orders
											 </h1>
											 
											 <div class="row">
                                        <?php
											// $orders = $cat->GetAllorders();
											 foreach($completedOrders as $u)
											 {
											  
											?>
											
											<div class="card col-12 col-md-6 col-lg-4">
											    <div class="card-body">
											        Order ID: <?php echo htmlspecialchars($u['id']);?>
											        <br/>
											        Date : <?php echo date('d M Y',strtotime($u['created_at']));?>
											        <br/>
											        Items and Quantity : 
											        <table class="table table-striped">
											            <tr>
											                <th>
											                    Item
											                </th>
											                <th>
											                    Quantity
											                </th>
											            </tr>
											            <?php echo  ($u['items']);?>
											        </table>
											       
											        <div class="col-12 mt-3 mb-3">
											            Order Status: <?php echo htmlspecialchars($u['order_status']);?>
											        </div>
											        
											        <br/>
											        <?php
												    if($u['order_status'] != 'Completed')
												    {
												    ?>
												        <button id="" data-href="complete?id=<?php  echo htmlspecialchars($u['id']); ?>" class="btn btn-primary complete">COMPLETE</button>
												    <?php
												        
												    }
												    ?>
											    </div>
											</div>
											
											
											<?php
											 }
											 ?>
											 </div>
											 
                                        
									
                                    	
                                    </div>
                            	</div>
                                
                        	</div>                            
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
	</div>
</div>
    <script>
     $(document).ready(function(){
       
        
        $('.complete').click(function(){
          
           
                var href = $(this).attr('data-href');
                $.post(href, function(res){
                 //   alert("Completion Successful");
                   location.reload();
                },'json').fail(function(xhr){
                   alert(xhr.responseJSON);
                },'json');
            
        });
    });
	
    
       </script>

    <script>
     
    $(document).ready(function(){
        $('.activ-cat').removeClass('activ-cat');
        $('#new-menu').addClass('activ-cat');
    }); 
</script>
<?php include 'footer.php';?>