<?php
session_start();
require_once '../includes/config.php';
require_once '../includes/database.php';
require_once '../includes/check_session.php';
require_once '../includes/auth.php';

$auth = array('bar');
$authRes = checkAuth($auth, $user);
if(!$authRes)
{
    header('Location: ../index.php');
    exit;
}


include_once '../includes/check_paid.php';


class new_category extends database
{
    public function GetAllorders($companyID, $chefOrder)
    {
        if($chefOrder == 2)
        {
            $sql = 'SELECT o.*, oi.cl_id AS order_item_id,oi.cl_status AS item_status, GROUP_CONCAT("<tr><td>",mi.cl_name, "</td><td>", oi.cl_quantity, "</td></tr>") AS items FROM tb_order o inner join tb_order_item oi ON o.cl_id=oi.cl_order_id INNER JOIN tb_menu mi ON oi.cl_menu_id=mi.cl_id 
            WHERE oi.cl_status="Pending" AND  mi.cl_type="Drinks"  AND  o.deleted_at IS NULL AND oi.deleted_at IS NULL AND o.cl_company_id=? GROUP BY oi.cl_id ORDER BY o.created_at DESC';
        }
        else
        {
            $sql = 'SELECT o.*, oi.cl_id AS order_item_id,oi.cl_status AS item_status, GROUP_CONCAT("<tr><td>",mi.cl_name, "</td><td>", oi.cl_quantity, "</td></tr>") AS items FROM tb_order o inner join tb_order_item oi ON o.cl_id=oi.cl_order_id INNER JOIN tb_menu mi ON oi.cl_menu_id=mi.cl_id 
            WHERE oi.cl_status="Pending" AND  o.deleted_at IS NULL AND oi.deleted_at IS NULL AND o.cl_company_id=? GROUP BY oi.cl_id ORDER BY o.created_at DESC';
        }
        return $this->callQuery($sql, [$companyID]);
      //  $where['order_status'] = 'Pending';
        //    return $this->fetchAll('orders',$where);
    }
    
    
    public function GetAllCompletedorders($companyID, $chefOrder)
    {
        if($chefOrder == 2)
        {
            $sql = 'SELECT o.*, oi.cl_id AS order_item_id,oi.cl_status AS item_status, GROUP_CONCAT("<tr><td>",mi.cl_name, "</td><td>", oi.cl_quantity, "</td></tr>") AS items FROM tb_order o inner join tb_order_item oi ON o.cl_id=oi.cl_order_id INNER JOIN tb_menu mi ON oi.cl_menu_id=mi.cl_id 
            WHERE oi.cl_status="Completed"  AND  mi.cl_type="Drinks" AND  o.deleted_at IS NULL AND oi.deleted_at IS NULL AND o.cl_company_id=? GROUP BY oi.cl_id ORDER BY o.created_at DESC LIMIT 0, 50';
        }
        else
        {
            $sql = 'SELECT o.*, oi.cl_id AS order_item_id,oi.cl_status AS item_status, GROUP_CONCAT("<tr><td>",mi.cl_name, "</td><td>", oi.cl_quantity, "</td></tr>") AS items FROM tb_order o inner join tb_order_item oi ON o.cl_id=oi.cl_order_id INNER JOIN tb_menu mi ON oi.cl_menu_id=mi.cl_id 
            WHERE oi.cl_status="Completed"  AND  o.deleted_at IS NULL AND oi.deleted_at IS NULL AND o.cl_company_id=? GROUP BY oi.cl_id ORDER BY o.created_at DESC LIMIT 0, 50';
        }
        return $this->callQuery($sql, [$companyID]);
      //  $where['order_status'] = 'Pending';
        //    return $this->fetchAll('orders',$where);
    }
    
    public function getAdminUser($id)
    {
        return $this->fetchRow('tb_users', ['cl_type' => 'customer_admin', 'cl_id' => $id]);
    }
   	
   
}

$obj = new new_category();

$adminUserID = $user['cl_user_id'];
$adminUser = $obj->getAdminUser($adminUserID);
if(!$adminUser)
{
    die('Smething went wrong. Refresh page');
}

$completedOrders = $obj->GetAllCompletedorders($user['cl_user_id'], $adminUser['cl_chef_order']);
$orders = $obj->GetAllorders($user['cl_user_id'], $adminUser['cl_chef_order']);

 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
											 
                                                            foreach($orders as $u)
                                                            {

                                                           ?>

                                                           <div class=" col-12 col-md-6 col-lg-4 mt-3">
                                                               <div class="card">
                                                                   <div class="card-body">
                                                                       Order ID: <?php echo htmlspecialchars($u['cl_id']);?>
                                                                       <br/>
                                                                       Table No: <?php echo htmlspecialchars($u['cl_table_no']);?>
                                                                       <br/>
                                                                       Date : <?php echo date('d M Y',strtotime($u['created_at']));?>
                                                                       <br/>
                                                                       <div class="table-container">
                                                                           <b>Items and Quantity : </b>
                                                                           <table class="table table-striped">
                                                                               <tr>
                                                                                   <th>
                                                                                       Item
                                                                                   </th>
                                                                                   <th>
                                                                                       Quantity
                                                                                   </th>
                                                                               </tr>
                                                                               <?php echo  ($u['items']);?>
                                                                           </table>
                                                                       </div>


                                                                      <div class="col-12 mt-3 mb-3">
                                                                       Order Status: <?php echo htmlspecialchars($u['item_status']);?>
                                                                       </div>
                                                                       <?php
                                                                           if($u['item_status'] != 'Completed')
                                                                           {
                                                                           ?>
                                                                               <button  data-href="ajax/complete.php?id=<?php  echo htmlspecialchars($u['order_item_id']); ?>" class="btn btn-primary complete">COMPLETE</button>
                                                                           <?php

                                                                           }
                                                                           ?>
                                                                   </div>
                                                               </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=" col-12 col-md-6 col-lg-4 mt-3">
                                                                                            <div class="card">
											    <div class="card-body">
											        Order ID: <?php echo htmlspecialchars($u['cl_id']);?>
											        <br/>
											        Date : <?php echo date('d M Y',strtotime($u['created_at']));?>
											        <br/>
                                                                                                <div class="table-container">
                                                                                                    <b>Items and Quantity : </b>
                                                                                                    <table class="table table-striped">
                                                                                                        <tr>
                                                                                                            <th>
                                                                                                                Item
                                                                                                            </th>
                                                                                                            <th>
                                                                                                                Quantity
                                                                                                            </th>
                                                                                                        </tr>
                                                                                                        <?php echo  ($u['items']);?>
                                                                                                    </table>
                                                                                                </div>
											       
											        <div class="col-12 mt-3 mb-3">
											            Order Status: <?php echo htmlspecialchars($u['item_status']);?>
											        </div>
											        
											        <br/>
											        <?php
												    if($u['item_status'] != 'Completed')
												    {
												    ?>
												        <button id="" data-href="ajax/complete?id=<?php  echo htmlspecialchars($u['order_item_id']); ?>" class="btn btn-primary complete">COMPLETE</button>
												    <?php
												        
												    }
												    ?>
											    </div>
											</div>
                                                                                        </div>
											
											<?php
											 }
											 ?>
											 </div>
											 
                                        
									
                                    	
                                    </div>
                            	</div>
                                
                        	</div>                            
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
	</div>
</div>

<div id="absolute_sidebar_notify" style="">
    
</div>
    <script>
        
        var audioElement = document.createElement('audio');
        audioElement.setAttribute('src', '<?php echo DOMAIN ?>/assets/audio/ring.mpeg');
        function checkNewOrders()
        {
            $.get('ajax/check_bar_new_orders.php', function(res){
                $(res).each(function(i, value){
                   if(value.count == 0)
                    {
                        $('#absolute_sidebar_notify').html('');
                        return;
                    }
                    $('#absolute_sidebar_notify').html('<div class="notify-absolute"><span class="close_notify">x</span><div class="order_text_notify">You have ' + value.count + ' new orders</div><a href="<?php echo DOMAIN ?>/orders/bar_read.php" class="btn btn-primary" style="margin-top: 10px;">View</a></div>');
                 
                });
                if($(res).length >= 1)
                {
                    audioElement.play();
                }
                    
            }, 'json');
        }
        
        
        $(document).on('click', '.close_notify', function(){
            var parent = $(this).parents('.notify-absolute').first();
            $(parent).fadeOut();
            
        })
     $(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');
            
        });
        
        checkNewOrders();
        setInterval(function(){checkNewOrders()}, 5000);

    });
	
    
       </script>

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