<?php
	session_start();
	include("library/conn.php");
	
	$role_rs = array();
	$get_role_mode = mysqli_query($conn, "SELECT * FROM user_role_privileges WHERE userID = '". $_SESSION['userIdd'] ."' AND roleName = 'delivery_orders'") or die (mysqli_error($conn));
	if (mysqli_num_rows($get_role_mode) > 0) {
		$role_rs = mysqli_fetch_array($get_role_mode);
	} else {
		$role_rs = ['-1', '-1', '-1', '-1', '-1', '-1'];
	}
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<?php include("library/head.php");?>
		<title>Delivery Orders | <?php echo $_SESSION['systemName']; ?></title>
	</head>
	<body class="app sidebar-mini">

		<!-- Navbar-->
		<?php include("library/header.php");?>
		<!-- Sidebar menu-->
		<div class="app-sidebar__overlay" data-toggle="sidebar"></div>
		<?php include("library/sidebar.php");?>
		
		<main class="app-content">
			<div class="app-title">
				<div>
					<h4 class="text-primary"><i class="fa fa-motorcycle fa-lg"></i> DELIVERY ORDERS</h4>
				</div>
				<ul class="app-breadcrumb breadcrumb">
					<li class="breadcrumb-item"><i class="fa fa-home"></i></li>
					<li class="breadcrumb-item"><a href="delivery-orders">Delivery Orders</a></li>
				</ul>
			</div>

			<?php include("models/admin_config.php");?>
			<?php include("models/system_config.php");?>
      
			<div class="row">
				<div class="col-md-12">
					<div class="tile">
						<div class="tile-body">
						    <?php
						        if (($_SESSION['userTypeID'] == 'PS0001') || ($_SESSION['userTypeID'] == 'PS0002') || ($_SESSION['userTypeID'] == 'PS0003')) {
						            ?><form action="#" method="POST" id="searchDateBasedDeliveryOrderForm">
                    				    <div class="row mt-1 mb-5 no-gutters align-items-center bg-primary p-3 pb-4 rounded-lg">
                                            <div class="col-md-4">
                                                <h2 class="text-white pt-2">Search Delivery</h2>
                                            </div>
                                            <div class="col-md-2 pr-3">
                    							<label class="control-label text-white">Status</label><br>
                                                <select class="form-control" name="cmbOrderStatusSearchDateBasedDeliveryOrders" id="cmbOrderStatusSearchDateBasedDeliveryOrders" style="width: 100%;">
                                                    <option value="0">General (All)</option>
                                                    <option value="1">In Kitchen</option>
                                                    <option value="2">On the way</option>
                                                    <option value="3">Delivered</option>
                    							</select>
                    						</div>
                                            <div class="col-md-2 pr-3">
                								<label class="control-label text-white">From Date</label>
                								<input class="form-control" type="date" name="txtFromDateSearchDateBasedDeliveryOrders" id="txtFromDateSearchDateBasedDeliveryOrders" />
                							</div>
                                            <div class="col-md-2 pr-3">
                								<label class="control-label text-white">To Date</label>
                								<input class="form-control" type="date" name="txtToDateSearchDateBasedDeliveryOrders" id="txtToDateSearchDateBasedDeliveryOrders" />
                							</div>
                							<div class="col-md-2">
                							    <label class="control-label">&nbsp;</label>
                								<button type="submit" class="btn btn-warning col-md-12" name="btnSearchDateBasedDeliveryOrder" id="btnSearchDateBasedDeliveryOrder"><i class="fa fa-search" style="font-size: 18px; margin-left: 5px; margin-bottom: 3px;"></i> Search</button>
                							</div>
                						</div>
                				    </form><?php
						        }
						    ?>
							<div class="table-responsive dateBasedDeliveryOrdersSectionArea">
                                <table class="table table-bordered table-sm display nowrap" id="deliveryOrdersTable">
                                    <thead>
                                        <tr class="text-light bg-primary text-center" style="height: 45px;">
                                            <th class="align-middle">View</th>
                                            <th class="align-middle">Order ID</th>
                                            <th class="align-middle" width="25%">Customer Information</th>
                                            <th class="align-middle">Order Status</th>
                                            <th class="align-middle">Orderer</th>
                                            <th class="align-middle">Time</th>
                                            <th class="align-middle">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $total_in_kitchen = 0;
                                            $total_on_the_way = 0;
                                            $total_delivered = 0;
                                            $total_delivery_orders = 0;
                                            $start_date_time = (date("H") > 6) ? date("Y-m-d 06:00:00") : date('Y-m-d 06:00:00', strtotime("-1 days"));
                                            $getDeliveryOrdersList = mysqli_query($conn, "SELECT restaurant_orders.orderID, customers.customerName, customers.customerMobile, restaurant_orders.deliveryAddress, restaurant_orders.orderStatus, employees.employeePhoto, orderer_user.Username, restaurant_orders.registerDate, delivery_employee.employeeName, delivery_employee.employeeMobileNo 'DeliverymanPhone', delivery_registerer.Username
                                            FROM restaurant_orders
                                            JOIN customers ON restaurant_orders.customerID = customers.customerID
											JOIN system_users orderer_user ON restaurant_orders.registeredBy = orderer_user.userID
                                            JOIN employees ON orderer_user.employeeID = employees.employeeID
                                            LEFT JOIN employees delivery_employee ON restaurant_orders.deliveredBy = delivery_employee.employeeID
                                            LEFT JOIN delivery_orders ON restaurant_orders.orderID = delivery_orders.orderID
                                            LEFT JOIN system_users delivery_registerer ON delivery_orders.registeredBy = delivery_registerer.userID
											WHERE restaurant_orders.orderTypeID = 'OT0003' AND restaurant_orders.orderStatus != '4' AND restaurant_orders.registerDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d 23:59:59") ."'
                                            ORDER BY restaurant_orders.registerDate DESC") or die(mysqli_error($conn));
                                            while ($delivery_orders_rs = mysqli_fetch_array($getDeliveryOrdersList)) {?>
                                                <tr>
                                                    <td class="align-middle text-center">
                                                        <?php
															if ($delivery_orders_rs[4] == "1" || $delivery_orders_rs[4] == "3") {
														        ?><span class="bg-success btn-lg text-white pl-2 pt-1 pr-2 pb-1 rounded-lg font-weight-bold btnViewOrderInformation" style="cursor: pointer;" data-id="<?php echo $delivery_orders_rs[0]; ?>" btn-type="View" data-toggle="modal" data-target="#viewAndPrintOrderReceiptModal" title="View Order"><i class="fa fa-eye fa-md"></i></span><?php
															} else if ($delivery_orders_rs[4] == "2") {
															    ?>
															        <span class="bg-success btn-lg text-white pl-2 pt-1 pr-2 pb-1 rounded-lg font-weight-bold btnViewOrderInformation" style="cursor: pointer;" data-id="<?php echo $delivery_orders_rs[0]; ?>" btn-type="View" data-toggle="modal" data-target="#viewAndPrintOrderReceiptModal" title="View Order"><i class="fa fa-eye fa-md"></i></span>
    															    <span class="bg-primary btn-lg text-white pl-2 pt-1 pr-2 pb-1 rounded-lg font-weight-bold btnChangeDeliverymanModal" style="cursor: pointer;" data-id="<?php echo $delivery_orders_rs[0]; ?>" data-toggle="modal" data-target="#changeDeliverymanModal" title="Change Deliveryman"><i class="fa fa-refresh fa-md"></i></span>
															    <?php
															}
														?>
													</td>
                                                    <td class="align-middle text-center"><span class="text-primary font-weight-bold"><?php echo $delivery_orders_rs[0]; ?></span></td>
                                                    <td class="align-middle text-left" width="25%">
														<h6><?php echo $delivery_orders_rs[1]. " - ". $delivery_orders_rs[2]; ?></h6>
														<h6>Address: <span class="font-weight-normal"><?php echo $delivery_orders_rs[3]; ?></span></h6>
														<?php
														    if ($delivery_orders_rs[8] != "") {
														        ?><h6>Deliveryman: <br /><span class="font-weight-normal"><?php echo $delivery_orders_rs[8]. " <b>- ". $delivery_orders_rs[9] ."</b>"; ?></span></h6><?php
														    }
												        ?>
													</td>
                                                    <td class="align-middle text-center">
														<?php
															if ($delivery_orders_rs[4] == "1") {
																?><h6 class="bg-primary text-white text-center p-2 rounded-lg col-md-10"><i class='fa fa-spinner fa-pulse fa-lg'></i> &nbsp; In Kitchen</h6><?php
																$total_in_kitchen += 1;
															} else if ($delivery_orders_rs[4] == "2") {
																?><h6 class="bg-dark text-white text-center p-2 rounded-lg col-md-10"><i class='fa fa-motorcycle fa-lg'></i> &nbsp; On the way</h6><?php
																$total_on_the_way += 1;
															} else if ($delivery_orders_rs[4] == "3") {
																?><h6 class="bg-success text-white text-center p-2 rounded-lg col-md-10"><i class='fa fa-check fa-lg'></i> &nbsp; Delivered</h6><?php
																$total_delivered += 1;
															}
														?>
													</td>
                                                    <td class="align-middle text-left"><img class="img-fluid rounded-circle pr-2" src="<?php echo $delivery_orders_rs[5]; ?> " width="40px" height="40px"/><?php $waiter_name = explode(" ", $delivery_orders_rs[6]); echo $waiter_name[0]; ?></td>
                                                    <td class="align-middle text-center font-weight-bold"><?php echo date('h:i A', strtotime($delivery_orders_rs[7])); ?></td>
                                                    <td class="align-middle text-center">
                                                        <?php
															if ($delivery_orders_rs[4] == "1") {
                                                                ?><button class="btn btn-lg btn-primary col-md-9 pt-3 pb-3 text-left font-weight-bold btnDeliverNowWaitingDeliveries" id="<?php echo "Update". $delivery_orders_rs[0]; ?>" data-id="<?php echo $delivery_orders_rs[0]; ?>" data-toggle="modal" data-target="#registerDeliveryOrderModal"><i class="fa fa-send"></i>Deliver Now</button><?php
															} else if ($delivery_orders_rs[4] == "2") {
                                                                ?><button class="btn btn-lg btn-warning col-md-9 pt-3 pb-3 text-left font-weight-bold btnCompleteNowWaitingDeliveries" id="<?php echo "Complete". $delivery_orders_rs[0]; ?>" data-id="<?php echo $delivery_orders_rs[0]; ?>"><i class="fa fa-hourglass-1"></i>Complete Now</button><?php
															} else if ($delivery_orders_rs[4] == "3") {
															    ?>
                                                                    <button class="btn btn-lg btn-success col-md-9 pt-3 pb-3 text-left font-weight-bold">
                                                                        <span class="font-weight-normal" style="font-size: 12px; display: block;">Processed: <b><?php echo $delivery_orders_rs[10]; ?></b></span>
                                                                        <i class="fa fa-check fa-2x"></i>Delivered
                                                                    </button>
                                                                <?php
															}
														?>
                                                    </td>
                                                </tr><?php
                                                $total_delivery_orders += 1;
                                            }
                                        ?>
                                    </tbody>
                                    <tfoot>
                                        <tr>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td class="align-middle text-center pt-4 pb-3"><h4 class="text-primary">In Kitchen<br /><span class="text-dark"><?php echo $total_in_kitchen; ?></span></h4></td>
                                            <td class="align-middle text-center pt-4 pb-3"><h4 class="text-warning">On the way<br /><span class="text-dark"><?php echo $total_on_the_way; ?></span></h4></td>
                                            <td class="align-middle text-center pt-4 pb-3"><h4 class="text-success">Delivered<br /><span class="text-dark"><?php echo $total_delivered; ?></span></h4></td>
                                            <td class="align-middle text-center pt-4 pb-3"><h4 class="text-dark">Total<br /><span class="text-dark"><?php echo $total_delivery_orders; ?></span></h2></h4>
                                        </tr>
                                    </tfoot>
                                </table>

							</div>
						</div>
					</div>
				</div>
			</div>
		</main>
	
		<!-- Essential javascripts for application to work-->
		<?php include("library/footer.php");?>	    	
		<?php include("library/scripts.php"); ?>
		<script type="text/javascript"> 
		    $("#deliveryOrdersTable").DataTable({"pageLength": 100, "order": []})
		    $("#cmbOrderStatusSearchDateBasedDeliveryOrders").select2({ tags: false })
		    $("#cmbDepositAccountRegisterCustomerPayment0").select2({ tags: false, dropdownParent: $("#registerCustomerPaymentModal") })
		</script>
		
	</body>
</html>