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

function prd($product_id){
   global $con;
   $sql = mysqli_query($con, "SELECT * FROM epos_products WHERE product_id = '$product_id' ");
   $row = mysqli_fetch_array($sql);
   $output = $row['name'];
   return $output;
}

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 ph($user_id){
   global $con;
   $sql = mysqli_query($con, "SELECT * FROM account WHERE account_id = $user_id");
   $row = mysqli_fetch_array($sql);
   $output = $row['phone'];
   return $output;
}

function trans($val){
   if($val == 0){
	  return "Pending Delivery";
   }else{
	   return "Delivered";
   }
}

$sql = mysqli_query($con, "SELECT * FROM cart ORDER BY cart_id DESC");

?>

        <!-- 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>All Orders <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 all ordered item, both delivered and pending delivery.
                    </p>
					
					<table id="datatable-buttons" class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>Product Name</th>
                          <th>Quantity</th>
                          <th>Order Id</th>
                          <th>Customer Name</th>
                          <th>Phone</th>
                          <th>Status</th>
                          <th>Date</th>
                        </tr>
                      </thead>

                      <tbody>
					  <?php while($row = mysqli_fetch_array($sql)): ?>
                        <tr>
                          <td><a href="../../product?pid=<?php echo $row['product_id'] ?>"><?php echo prd($row['product_id']); ?></a></td>
                          <td id="upd" cid="<?php echo $row['cart_id'];?>" pid="<?php echo $row['product_id'];?>" contenteditable><?php echo $row['quantity']; ?></td>
                          <td><?php echo $row['order_id']; ?></td>
                          <td><a href="user-info?uid=<?php echo $row['user_id']; ?>"><?php echo cust($row['user_id']); ?></a></td>
						  <td><?php echo ph($row['user_id']); ?></td>
						  <td><?php echo trans($row['delivery_status']); ?></td>
						  <td><?php echo $row['date']; ?></td>
						</tr>
					  <?php endwhile; ?>
                      </tbody>
                    </table>
				  </div>
                </div>
              </div>

            
          </div>
        </div>
        <!-- /page content -->

        
<?php include("footer.php"); ?>
<script>
	$("body").delegate("#upd","blur",function(event){
		var qty = $(this).html();
		var cid = $(this).attr('cid');
		var pid = $(this).attr('pid');
		
		$.ajax({
			url	:	"process.php",
			type:	"POST",
			data	:	{
				           update_cart:1, 
						   cart_id:cid, 
						   product_id:pid, 
						   quantity:qty						   
						},
			success	:	function(){ 
			             window.location.href = "carts"; 
			}
         })
		 
	})
	
	$("body").delegate("#del","click",function(event){
		
		var cid = $(this).attr('cid');
		
		$.ajax({
			url	:	"process.php",
			type:	"POST",
			data	:	{del_cart:1, cart_id:cid},
			success	:	function(){ 
				window.location.href = "carts"; 
			}
         })
		 
	})
</script>