<?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('manager');
$authRes = checkAuth($auth, $user);
if(!$authRes)
{
    header('Location: ../index.php');
    exit;
}

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

class Orders extends database
{
        
        public function GetAllorders()
        {
            $sql = 'SELECT o.*, GROUP_CONCAT("<div >",mi.cl_name, " ", "<br/> Quantity: " , oi.cl_quantity, "<br/> Price:", oi.cl_price, "</div>","<br/>") AS items, SUM(oi.cl_price) AS total_price 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 o.deleted_at IS NULL AND oi.deleted_at IS NULL GROUP BY o.cl_id ORDER BY o.created_at DESC LIMIT 0, 150';
            return $this->callQuery($sql);
        }


}

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

$obj = new Orders();
$orders = $obj->GetAllorders();
include '../header.php';
?>
<div class="ordes-content mt-3">
    <div class="container midle-order">
        <div class="row">
        	<?php include 'menu.php';?>
            <div class="col-md-12">
                <div class="order-wraper">
                	<div class="col-md-12">
                    	<h1>Orders</h1>
			 </div>
                    <div class="clearfix"></div>
					<table class="table table-responsive" style="width:100%"> 
											<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 cluster-bulk-id="<?php echo htmlspecialchars($u['cl_id']) ?>">
												<td><?php echo htmlspecialchars($u['cl_id']);?></td>
                                                                                                <td><?php echo htmlspecialchars($u['cl_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 cluster-bulk-id="<?php echo htmlspecialchars($u['cl_id']) ?>" style="border-bottom: 2px solid black">
                                                                                                    <td colspan="2">Status : <?php echo htmlspecialchars($u['cl_status']);?></td>
    												<td colspan="2">
    												    <a class="btn btn-primary" href="edit.php?id=<?php  echo htmlspecialchars($u['cl_id']); ?>">Edit</a>
    												    <button data-href="ajax/delete_ajax.php?id=<?php  echo htmlspecialchars($u['cl_id']); ?>" class="btn btn-danger btn-remove" cluster-id="<?php  echo htmlspecialchars($u['cl_id']); ?>">REMOVE</button>
    												    <?php
    												    if($u['cl_status'] == 'Pending')
    												    {
    												    ?>
    												        <button id="" data-href="ajax/complete.php?id=<?php  echo htmlspecialchars($u['cl_id']); ?>" class="btn btn-success complete">TO COMPLETE</button>
    												    <?php
    												        
    												    }
    												    if($u['cl_status'] == 'Completed')
    												    {
    												    ?>
    												     <button id="" data-href="ajax/pending.php?id=<?php  echo htmlspecialchars($u['cl_id']); ?>" class="btn btn-warning pending">TO PENDING</button>
    												     <?php
    												    }
    												    
    												    
    												    ?>
                                                                                                     <div class="error-input"></div>
    												</td>
                                                                                        </tr>
											<?php
											
											 }
											?>
											
                                </table>
                                    	
                    
                </div>
                
            </div>
        </div>
	</div>
</div>

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

    
</script>


<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 id = $(this).attr('cluster-id');
                var href = $(this).attr('data-href');
                $.post(href, function(res){
                    alert("Deleted Successfully");
                      $("[cluster-bulk-id='" + id + "']").remove();
                   
                },'json').fail(function(xhr){
                   alert(xhr.responseJSON);
                },'json');
            }
       
      
    });
    </script>

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