<?php
session_start();
include_once('includes/config.php');
if (strlen($_SESSION["aid"]) == 0) {
	header('location:logout.php');
	exit;
} else {
	date_default_timezone_set('Asia/Kolkata'); // change according timezone
	$currentTime = date('d-m-Y h:i:s A', time());
	?>
	<!DOCTYPE html>
	<html lang="en">

	<head>
		<meta charset="utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
		<meta name="description" content="Pending Orders - Awesome Mall Admin" />
		<meta name="author" content="" />
		<title>Awesome Mall | Pending Orders</title>
		<link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" />
		<link href="css/styles.css" rel="stylesheet" />
		<link href="css/admin-professional.css" rel="stylesheet" />
		<script src="js/all.min.js" crossorigin="anonymous"></script>
	</head>

	<body class="sb-nav-fixed">
		<?php include_once('includes/header.php'); ?>
		<div id="layoutSidenav">
			<?php include_once('includes/sidebar.php'); ?>
			<div id="layoutSidenav_content">
				<main>
					<div class="container-fluid px-4">
						<!-- Page Header -->
						<div class="page-header mt-4">
							<div>
								<h1><i class="fas fa-clock me-2"></i> Pending Orders</h1>
								<p>Orders awaiting processing and delivery</p>
							</div>
						</div>

						<!-- Orders Table -->
						<div class="table-card">
							<div class="card-header">
								<h5 class="mb-0"><i class="fas fa-hourglass-half me-2"></i>Undelivered Orders</h5>
							</div>
							<div class="card-body">
								<table class="table table-hover" id="datatablesSimple">
									<thead>
										<tr>
											<th>#</th>
											<th>Customer Name</th>
											<th>Contact Info</th>
											<th>Product</th>
											<th>Quantity</th>
											<th>Amount</th>
											<th>Order Date</th>
											<th>Action</th>
										</tr>
									</thead>
									<tbody>
										<?php
										$status = 'Delivered';
										$query = mysqli_query($con, "SELECT user_details.name as username, users.email as useremail, user_details.contactno as usercontact, 
                                        products.productName as productname, products.shippingCharge as shippingcharge, 
                                        orders.quantity as quantity, orders.orderDate as orderdate, products.productPrice as productprice, orders.id as id  
                                        FROM orders 
                                        JOIN users ON orders.userId=users.id 
                                        LEFT JOIN user_details ON user_details.user_id=users.id 
                                        JOIN products ON products.id=orders.productId 
                                        WHERE orders.orderStatus!='$status' OR orders.orderStatus IS NULL
                                        ORDER BY orders.orderDate DESC");

										$cnt = 1;
										$totalPending = mysqli_num_rows($query);

										while ($row = mysqli_fetch_array($query)) {
											$totalAmount = ($row['productprice'] * $row['quantity']) + $row['shippingcharge'];
											?>
											<tr>
												<td><?php echo htmlentities($cnt); ?></td>
												<td><strong><?php echo htmlentities($row['username'] ? $row['username'] : 'N/A'); ?></strong>
												</td>
												<td>
													<small>
														<i
															class="fas fa-envelope me-1"></i><?php echo htmlentities($row['useremail']); ?><br>
														<i
															class="fas fa-phone me-1"></i><?php echo htmlentities($row['usercontact'] ? $row['usercontact'] : 'N/A'); ?>
													</small>
												</td>
												<td><?php echo htmlentities($row['productname']); ?></td>
												<td><span
														class="badge bg-secondary"><?php echo htmlentities($row['quantity']); ?></span>
												</td>
												<td><strong>$<?php echo number_format($totalAmount, 2); ?></strong></td>
												<td>
													<small>
														<i class="fas fa-calendar me-1"></i>
														<?php echo date('M d, Y', strtotime($row['orderdate'])); ?>
													</small>
												</td>
												<td>
													<a href="order-details.php?oid=<?php echo htmlentities($row['id']); ?>"
														class="btn btn-sm btn-outline-primary" title="View Order Details">
														<i class="fas fa-eye me-1"></i>View
													</a>
												</td>
											</tr>
											<?php
											$cnt = $cnt + 1;
										}
										?>
									</tbody>
								</table>

								<?php if ($totalPending == 0) { ?>
									<div class="alert alert-success text-center">
										<i class="fas fa-check-circle me-2"></i>
										Great! All orders have been delivered.
									</div>
								<?php } else { ?>
									<div class="mt-3">
										<strong>Total Pending Orders: </strong>
										<span class="badge bg-warning text-dark fs-6"><?php echo $totalPending; ?></span>
									</div>
								<?php } ?>
							</div>
						</div>
					</div>
				</main>
				<?php include_once('includes/footer.php'); ?>
			</div>
		</div>
		<script src="js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
		<script src="js/scripts.js"></script>
		<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
		<script src="js/datatables-simple-demo.js"></script>
	</body>

	</html>
<?php } ?>
