<?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 = 'cancelled_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>Cancelled 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-danger"><i class="fa fa-times-rectangle fa-lg"></i> CANCELLED 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="cancelled-orders">Cancelled 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">
							<div class="table-responsive">
                                <table class="table table-bordered table-sm" id="cancelledOrdersTable">
                                    <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">Order Type</th>
                                            <th class="align-middle">Reason of Cancel</th>
                                            <th class="align-middle">Order Date</th>
                                            <th class="align-middle">Canceller</th>
                                            <th class="align-middle">Cancel Date</th>
                                            <?php if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[4] == '1') || ($role_rs[5] == '1')) {?><th class="align-middle"><center>Action</center></th><?php } ?>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $start_date_time = (date("H") > 6) ? date("Y-m-d 06:00:00") : date('Y-m-d 06:00:00', strtotime("-1 days"));
								 			$getCancelledOrdersList = mysqli_query($conn, "SELECT cancelled_orders.orderID, restaurant_orders.orderTypeID, order_types.orderTypeName, cancelled_orders.reasonForCancel, restaurant_orders.registerDate 'OrderDate', employees.employeePhoto, system_users.Username, cancelled_orders.cancelDate 'CancelDate'
                                            FROM cancelled_orders
                                            JOIN restaurant_orders ON cancelled_orders.orderID = restaurant_orders.orderID
                                            JOIN order_types ON restaurant_orders.orderTypeID = order_types.orderTypeID
                                            JOIN system_users ON cancelled_orders.cancelledBy = system_users.userID
                                            JOIN employees ON system_users.employeeID = employees.employeeID
                                            WHERE cancelled_orders.cancelDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d 23:59:59") ."'
                                            ORDER BY LENGTH(cancelled_orders.cancelDate),cancelled_orders.cancelDate DESC") or die(mysqli_error($conn));
                                            while ($cancelled_orders_rs = mysqli_fetch_array($getCancelledOrdersList)) {?>
                                                <tr>
                                                    <td class="align-middle text-center">
														<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 $cancelled_orders_rs[0]; ?>" btn-type="View" data-toggle="modal" data-target="#viewAndPrintOrderReceiptModal" title="View Order"><i class="fa fa-eye fa-md"></i></span>
													</td>
                                                    <td class="align-middle text-center"><span class="text-primary font-weight-bold"><?php echo $cancelled_orders_rs[0]; ?></span></td>
                                                    <td class="align-middle">
														<?php 
                                                            if ($cancelled_orders_rs[1] == "OT0001") {
                                                                ?><h6 class="text-success"><i class='fa fa-hotel'></i> <?php echo $cancelled_orders_rs[2]; ?></h6><?php
                                                            } else if ($cancelled_orders_rs[1] == "OT0002") {
                                                                ?><h6 class="text-primary"><i class='fa fa-shopping-basket'></i> <?php echo $cancelled_orders_rs[2]; ?></h6><?php
                                                            } else if ($cancelled_orders_rs[1] == "OT0003") {
                                                                ?><h6 class="text-danger"><i class='fa fa-motorcycle'></i> <?php echo $cancelled_orders_rs[2]; ?></h6><?php
                                                            }
                                                        ?>
													</td>
													<td class="align-middle"><?php echo $cancelled_orders_rs[3]; ?></td>
													<td class="align-middle text-center"><?php echo substr($cancelled_orders_rs[4], 0, 16); ?></td>
                                                    <td class="align-middle text-left"><img class="img-fluid rounded-circle pr-2" src="<?php echo $cancelled_orders_rs[5]; ?> " width="45px" height="45px"/><?php $waiter_name = explode(" ", $cancelled_orders_rs[6]); echo $waiter_name[0]; ?></td>
                                                    <td class="align-middle text-center"><?php echo substr($cancelled_orders_rs[7], 0, 16); ?></td>
                                                    <?php
                                                        if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[5] == '1')) {
                                                            ?><td class="align-middle text-center">
                                                                <button class="btn btn-danger col-md-10 pt-3 pb-3 font-weight-bold btnDeleteCancelledOrder" id="<?php echo "Delete". $cancelled_orders_rs[0]; ?>" data-id="<?php echo $cancelled_orders_rs[0]; ?>"><i class="fa fa-trash" style="font-size: 18px;"></i>Delete</button>
                                                            </td><?php
                                                        }
                                                    ?>
                                                    
                                                </tr><?php
                                            }
                                        ?>
                                    </tbody>
                                </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"> 
		    $("#cancelledOrdersTable").DataTable({"pageLength": 100, "order": []})
		</script>
		
	</body>
</html>