<?php
	session_start();
	date_default_timezone_set('Africa/Mogadishu');
	include("library/conn.php");
    // This is getting the start date of every thing during the day
    $start_date_time = (date("H") > 6) ? date("Y-m-d 06:00:00") : date('Y-m-d 06:00:00', strtotime("-1 days"));
?>
<!DOCTYPE html>
<html lang="en">
	<head>
        <?php include("library/head.php");?>
        <title>My Orders | <?php echo $_SESSION['systemName']; ?></title>
        <style>
			.modal { overflow: auto !important; }
		</style>
	</head>
	<body class="app sidebar-mini pace-done sidenav-toggled">
	    
	    <?php include("models/admin_config.php");?>
		<?php include("models/system_config.php");?>

		<!-- Navbar-->
		<?php include("library/waiter-header.php");?>
		<!-- Sidebar menu-->
		<?php
			date_default_timezone_set('Africa/Mogadishu');
			if ((isset($_SESSION['userName']) == true) && ($_SESSION['userName']) != "") {
				if ((time() - $_SESSION['lastLoginTimeStamp']) < 900) $_SESSION["lastLoginTimeStamp"] = time();
				else echo "<script>location ='logout'</script>";
			} else echo "<script>location ='logout'</script>";
		?>

		<main class="app-content" style="margin-left: 0px;">
			<div class="row">
				<div class="col-md-12">
					<div class="tile">
						<div class="tile-body">
							<div class="row">
								<div class="form-group col-md-12">
									<h4 class="pb-2">
										<span><i class="fa fa-list-ul"></i> My Orders <sup class="font-weight-normal text-success"><?php echo date("l, F j, Y"); ?></sup></span>
									</h4>
									<hr />
								</div>
							</div>
							<div class="table-responsive">
                                <table class="table table-bordered table-sm display nowrap" id="waiterOrdersTable">
                                    <thead>
                                        <tr class="text-light bg-primary text-center" style="height: 45px;">
                                            <th class="align-middle">Edit</th>
                                            <th class="align-middle">Order ID</th>
                                            <th class="align-middle">Customer Name</th>
                                            <th class="align-middle">Order Type</th>
                                            <th class="align-middle">Orderer</th>
                                            <th class="align-middle">Time</th>
                                            <th class="align-middle">Balance</th>
                                            <th class="align-middle">Order Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $getMyOrdersList = mysqli_query($conn, "SELECT restaurant_orders.orderID, customers.customerName, order_types.orderTypeName, employees.employeePhoto, system_users.Username, restaurant_orders.registerDate, ROUND(IFNULL(restaurant_orders.totalAmount, 0), 2) 'OrderBalance', ROUND(IFNULL(restaurant_orders.totalAmount, 0) - IFNULL(SUM(customer_payments.paidAmount), 0), 2) 'UnpaidAmount'
                                            FROM restaurant_orders
                                            LEFT JOIN customers ON restaurant_orders.customerID = customers.customerID
											JOIN order_types ON restaurant_orders.orderTypeID = order_types.orderTypeID
											JOIN system_users ON restaurant_orders.registeredBy = system_users.userID
											JOIN employees ON system_users.employeeID = employees.employeeID
                                            LEFT JOIN customer_payments ON restaurant_orders.orderID = customer_payments.orderID
											WHERE restaurant_orders.orderStatus != '4' AND restaurant_orders.registeredBy = '". $_SESSION['userIdd'] ."' AND restaurant_orders.registerDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d H:i:s") ."'
                                            GROUP BY restaurant_orders.orderID
                                            ORDER BY restaurant_orders.registerDate DESC") or die(mysqli_error($conn));
        //                                     echo "SELECT restaurant_orders.orderID, customers.customerName, order_types.orderTypeName, employees.employeePhoto, system_users.Username, restaurant_orders.registerDate, ROUND(IFNULL(restaurant_orders.totalAmount, 0), 2) 'OrderBalance', ROUND(IFNULL(restaurant_orders.totalAmount, 0) - IFNULL(SUM(customer_payments.paidAmount), 0), 2) 'UnpaidAmount'
        //                                     FROM restaurant_orders
        //                                     JOIN customers ON restaurant_orders.customerID = customers.customerID
								// 			JOIN order_types ON restaurant_orders.orderTypeID = order_types.orderTypeID
								// 			JOIN system_users ON restaurant_orders.registeredBy = system_users.userID
								// 			JOIN employees ON system_users.employeeID = employees.employeeID
        //                                     LEFT JOIN customer_payments ON restaurant_orders.orderID = customer_payments.orderID
								// 			WHERE restaurant_orders.orderStatus != '4' AND restaurant_orders.registeredBy = '". $_SESSION['userIdd'] ."' AND restaurant_orders.registerDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d H:i:s") ."'
        //                                     GROUP BY restaurant_orders.orderID
        //                                     ORDER BY restaurant_orders.registerDate DESC";
                                            $count_waiter_orders = 0;
                                            $total_balance = 0;
                                            $due_balance = 0;
                                            while ($my_orders_rs = mysqli_fetch_array($getMyOrdersList)) {?>
                                                <tr>
                                                     <td class="align-middle text-center" width="20%">
                                                        <?php
                                                            if ($my_orders_rs[7] > 0) { ?>
        														<span class="bg-primary text-white p-2 rounded-lg font-weight-bold btnEditOrderInformationWaiterPOS" style="cursor: pointer;" data-id="<?php echo $my_orders_rs[0]; ?>" data-toggle="modal" data-target="#editRestaurantOrderWaiterPOSModal" title="Edit Order"><i class="fa fa-pencil fa-lg"></i> Edit</span>
            												    <span class="bg-success text-white p-2 rounded-lg font-weight-bold btnPrintOrderInformation" style="cursor: pointer;" data-id="<?php echo $my_orders_rs[0]; ?>" btn-type="View" data-toggle="modal" data-target="#viewAndPrintOrderReceiptModal" title="View Order"><i class="fa fa-eye fa-lg"></i> View</span>        													
            												<?php } else if ($my_orders_rs[7] <= 0) { ?>
            												    <span class="bg-success text-white p-2 rounded-lg font-weight-bold btnPrintOrderInformation" style="cursor: pointer;" data-id="<?php echo $my_orders_rs[0]; ?>" btn-type="View" data-toggle="modal" data-target="#viewAndPrintOrderReceiptModal" title="View Order"><i class="fa fa-eye fa-lg"></i> View Receipt</span>
                                                            <?php }
                                                        ?>
													</td>
                                                    <td class="align-middle text-center"><span class="text-primary font-weight-bold"><?php echo $my_orders_rs[0]; ?></span></td>
                                                    <td class="align-middle text-left"><?php echo $my_orders_rs[1]; ?></td>
                                                    <td class="align-middle text-left">
														<?php 
                                                            if ($my_orders_rs[2] == "Seat In") {
                                                                ?><h6 class="text-success"><i class='fa fa-hotel'></i> <?php echo $my_orders_rs[2]; ?></h6><?php
                                                            } else if ($my_orders_rs[2] == "Take Away") {
                                                                ?><h6 class="text-primary"><i class='fa fa-shopping-basket'></i> <?php echo $my_orders_rs[2]; ?></h6><?php
                                                            } else if ($my_orders_rs[2] == "Delivery") {
                                                                ?><h6 class="text-danger"><i class='fa fa-motorcycle'></i> <?php echo $my_orders_rs[2]; ?></h6><?php
                                                            }
                                                        ?>
                                                    </td>
                                                    <td class="align-middle text-left"><img class="img-fluid rounded-circle pr-2" src="<?php echo $my_orders_rs[3]; ?> " width="50px" height="50px"/><?php $waiter_name = explode(" ", $my_orders_rs[4]); echo $waiter_name[0]; ?></td>
													<td class="align-middle text-center font-weight-bold"><?php echo date("h:i A", strtotime($my_orders_rs[5])); ?></td>
                                                    <td class="align-middle text-center"><h6 class="text-success"><?php echo "$". number_format($my_orders_rs[6], 2); ?></h6></td>
													<td class="align-middle text-center">
                                                        <?php 
                                                            if ($my_orders_rs[7] == 0) {
                                                                ?><span class="btn btn-success rounded-pill font-weight-bold col-md-8 p-3"><i class="fa fa-check"></i> Full Paid</span><?php
                                                            } else if (($my_orders_rs[7] > 0) && ($my_orders_rs[7] < $my_orders_rs[6])) {
                                                                ?><span class="btn btn-warning rounded-pill font-weight-bold col-md-8 p-3"><i class="fa fa-hourglass"></i> Due: <?php echo "$". number_format($my_orders_rs[7], 2); ?></span><?php
                                                            } else if (($my_orders_rs[7] == $my_orders_rs[6])) {
                                                                ?><span class="btn btn-danger rounded-pill font-weight-bold col-md-8 p-3"><i class="fa fa-hourglass-o"></i> Not Paid</span><?php
                                                            }
                                                        ?>
                                                    </td>
												</tr><?php
												$total_balance += $my_orders_rs[6];
												$due_balance += $my_orders_rs[7];
												$count_waiter_orders++;
                                            }
                                        ?>
                                    </tbody>
                                    <tfoot>
                                        <tr height="50px">
                                            <th class="align-middle text-center" colspan="3"></th>
                                            <th class="align-middle text-right"><h5 style="display: inline;">Total Orders</h5></th>
                                            <th class="align-middle text-center"><h5 class="text-primary" style="display: inline;"><?php echo $count_waiter_orders; ?></h5></th>
                                            <th class="align-middle text-right"><h5 style="display: inline;">Total Balance</h5></th>
                                            <th class="align-middle text-center"><h5 class="text-success" style="display: inline;"><?php echo "$". number_format($total_balance, 2); ?></h5></th>
                                            <th class="align-middle text-center"><h5 class="text-danger" style="display: inline;"><?php echo "$". number_format($due_balance, 2); ?></h5></th>

                                        </tr>
                                    </tfoot>
                                </table>

							</div>
						</div>
					</div>
				</div>
			</div>
			
		</main>

		<!-- Essential javascripts for application to work-->
		<?php include("library/scripts.php");?>
		
		<script type="text/javascript">
			$(document).ready(function () {
				// Active the POS Button
				$(".btnWaiterOrdersWaiterDashboard").removeClass("btn-outline-warning")
				$(".btnWaiterOrdersWaiterDashboard").addClass("btn-warning")
			})
		    $("#waiterOrdersTable").DataTable({"pageLength": 100, "order": []})
		</script>

	</body>
</html>