<?php 
include("side_nav.php");
include("top_nav.php"); 

function cust($user_id){
   global $con;
   $sql = mysqli_query($con, "SELECT * FROM account WHERE account_id = $user_id");
   $row = mysqli_fetch_array($sql);
   $output = ucfirst($row['first_name']) . " " . ucfirst($row['last_name']) ;
   return $output;
}

function countOrder($order_id){
   global $con;
   $sql = mysqli_query($con, "SELECT * FROM cart WHERE order_id = '$order_id' AND delivery_status = 0  ");
   $count = mysqli_num_rows($sql);
   return $count;
}

$sql = mysqli_query($con, "SELECT * FROM cart WHERE delivery_status = 0 GROUP BY order_id ");
?>

        <!-- page content -->
        <div class="right_col" role="main">
         
            <div class="clearfix"></div>

            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2><span id="title">Incoming Orders</span> <small></small></h2>
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                      <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                          <li><a href="#">Settings 1</a>
                          </li>
                          <li><a href="#">Settings 2</a>
                          </li>
                        </ul>
                      </li>
                      <li><a class="close-link"><i class="fa fa-close"></i></a>
                      </li>
                    </ul>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                    <p class="text-muted font-13 m-b-30">
                      Table showing the list of displayed items.
                    </p>
					
                    <table id="datatable-buttons" class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>Message</th>
                          <th>Order Id</th>
                          <th>Date</th>
                          <th>Action</th>
                        </tr>
                      </thead>

                      <tbody id="pendingOrders">
					  <?php while($row = mysqli_fetch_array($sql)): ?>
                        <tr>
                          <td><a ><?php echo "You have <b>" . countOrder($row['order_id']) . "</b> order(s) from <b>" . cust($row['user_id']) .  "</b>";  ?></a></td>
                          <td><?php echo $row['order_id']; ?></td>
                          <td><?php echo $row['date']; ?></td>
						  <td>
							<div class='btn-group'>
								<a href="pending-delivery?oid=<?php echo $row['order_id']; ?>" class='btn btn-primary pointer'>View Order</a>
							</div>
						  </td>
						</tr>
					  <?php endwhile; ?>
                      </tbody>
                    </table>
				  </div>
                </div>
              </div>
            </div>
        </div>
        <!-- /page content -->
		
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<?php include("footer.php"); ?>

<script>

var ajax_call = function() {
    //your jQuery ajax code
    $.ajax({
		url	:	"process.php",
		type:	"POST",
		data	:	{
			getPendingOrders:1
		},
		success	:	function(data){ 
			$('#pendingOrders').html(data);
            console.log(data);			
		}
	 })
};
var x = 5;
var interval = 1000 * 60 * x; // where X is your every X minutes

setInterval(ajax_call, interval);
</script>





