<?php
session_start();
include_once('includes/config.php');
if (strlen($_SESSION["aid"]) == 0) {
	header('location:logout.php');
	exit;
} else {
	?>
	<!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="User Login Logs - Awesome Mall Admin" />
		<meta name="author" content="" />
		<title>Awesome Mall | User Login Log</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-history me-2"></i> User Login Log</h1>
								<p>Track all user login and logout activities</p>
							</div>
						</div>

						<!-- Login Logs Table -->
						<div class="table-card">
							<div class="card-header">
								<h5 class="mb-0"><i class="fas fa-sign-in-alt me-2"></i>Login Activity</h5>
							</div>
							<div class="card-body">
								<table class="table table-hover" id="datatablesSimple">
									<thead>
										<tr>
											<th>#</th>
											<th>User Email</th>
											<th>User IP</th>
											<th>Login Time</th>
											<th>Logout Time</th>
											<th>Status</th>
										</tr>
									</thead>
									<tbody>
										<?php
										$query = mysqli_query($con, "select * from userlog ORDER BY id DESC");
										$cnt = 1;
										while ($row = mysqli_fetch_array($query)) {
											$st = $row['status'];
											?>
											<tr>
												<td><?php echo htmlentities($cnt); ?></td>
												<td><strong><?php echo htmlentities($row['userEmail']); ?></strong></td>
												<td>
													<code><?php echo htmlentities($row['userip']); ?></code>
												</td>
												<td>
													<i class="fas fa-clock me-1"></i>
													<?php echo htmlentities($row['loginTime']); ?>
												</td>
												<td>
													<?php
													if ($row['logout']) {
														echo '<i class="fas fa-clock me-1"></i>' . htmlentities($row['logout']);
													} else {
														echo '<span class="text-muted">Still logged in</span>';
													}
													?>
												</td>
												<td>
													<?php
													if ($st == 1) {
														echo '<span class="status-badge delivered">Successful</span>';
													} else {
														echo '<span class="status-badge cancelled">Failed</span>';
													}
													?>
												</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 } ?>
