<?php
session_start();
include_once('includes/config.php');
if (strlen($_SESSION["aid"]) == 0) {
	header('location:logout.php');
	exit;
} else {
	// Delete User Handler
	if (isset($_GET['id']) && $_GET['del']) {
		$id = intval($_GET['id']);
		mysqli_query($con, "delete from user_details where user_id='$id'");
		mysqli_query($con, "delete from users where id='$id'");
		$_SESSION['msg'] = "User deleted successfully";
		header("location:manage-users.php");
		exit;
	}
	?>
	<!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="Manage Users - Awesome Mall Admin" />
		<meta name="author" content="" />
		<title>Awesome Mall | Manage Users</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-users me-2"></i> Manage Users</h1>
								<p>View and manage all registered users</p>
							</div>
						</div>

						<!-- Users Table -->
                        <?php if (isset($_SESSION['msg'])) { ?>
                            <div class="alert alert-success alert-dismissible fade show">
                                <?php echo $_SESSION['msg']; unset($_SESSION['msg']); ?>
                                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                            </div>
                        <?php } ?>
						<div class="table-card">
							<div class="card-header">
								<h5 class="mb-0"><i class="fas fa-list me-2"></i>Registered Users</h5>
							</div>
							<div class="card-body">
								<table class="table table-hover" id="datatablesSimple">
									<thead>
										<tr>
											<th>#</th>
											<th>Name</th>
											<th>Email</th>
											<th>Contact No</th>
											<th>Shipping Address</th>
											<th>Billing Address</th>
											<th>Reg. Date</th>
											<th>Action</th>
										</tr>
									</thead>
									<tbody>
										<?php
										$query = mysqli_query($con, "SELECT u.*, ud.name, ud.contactno, ud.shippingAddress, ud.shippingCity, ud.shippingState, ud.shippingPincode, ud.billingAddress, ud.billingCity, ud.billingState, ud.billingPincode 
                                        FROM users u 
                                        LEFT JOIN user_details ud ON u.id = ud.user_id
                                        WHERE u.role NOT IN ('admin', 'staff', 'sales', 'vendor')");
										$cnt = 1;
										while ($row = mysqli_fetch_array($query)) {
											$shippingAddr = trim($row['shippingAddress'] . ", " . $row['shippingCity'] . ", " . $row['shippingState'] . " - " . $row['shippingPincode']);
											$billingAddr = trim($row['billingAddress'] . ", " . $row['billingCity'] . ", " . $row['billingState'] . " - " . $row['billingPincode']);
											?>
											<tr>
												<td><?php echo htmlentities($cnt); ?></td>
												<td><strong><?php echo htmlentities($row['name'] ? $row['name'] : 'N/A'); ?></strong>
												</td>
												<td><?php echo htmlentities($row['email']); ?></td>
												<td><?php echo htmlentities($row['contactno'] ? $row['contactno'] : 'N/A'); ?>
												</td>
												<td><small><?php echo htmlentities($shippingAddr ? $shippingAddr : 'N/A'); ?></small>
												</td>
												<td><small><?php echo htmlentities($billingAddr ? $billingAddr : 'N/A'); ?></small>
												</td>
												<td><?php echo htmlentities($row['regDate']); ?></td>
												<td>
													<a href="edit-user.php?id=<?php echo $row['id']; ?>"
														class="btn btn-primary btn-sm me-1" title="Edit"><i
															class="fas fa-edit"></i></a>
													<a href="manage-users.php?id=<?php echo $row['id']; ?>&del=delete"
														onclick="return confirm('Are you sure you want to delete?')"
														class="btn btn-danger btn-sm" title="Delete"><i
															class="fas fa-trash"></i></a>
												</td>
											</tr>
											<?php $cnt = $cnt + 1;
										} ?>
									</tbody>
								</table>
							</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 } ?>
