<?php
session_start();
include('include/config.php');

if(strlen($_SESSION['alogin'])==0) {	
    header("Location: index.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin | Users Log</title>

<!-- ✅ Bootstrap 5 & Icons -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">

<!-- ✅ Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600&display=swap" rel="stylesheet">

<!-- ✅ DataTables Bootstrap 5 CSS -->
<link href="https://cdn.datatables.net/1.13.5/css/dataTables.bootstrap5.min.css" rel="stylesheet">

<style>
body {
    font-family: 'Open Sans', sans-serif;
    background-color: #f7f8fa;
    color: #333;
    margin: 0;
}

/* Navbar */
.navbar {
    background: linear-gradient(90deg, #ff7b00, #ff9500);
    color: #fff;
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}
.navbar-brand { font-weight: 600; color: #fff !important; }

/* Sidebar */
.wrapper { display: flex; min-height: 100vh; }
.sidebar {
    width: 250px;
    background: linear-gradient(180deg, #ff7b00, #ff9500);
    color: #fff;
    padding-top: 25px;
}
.sidebar a {
    display: block;
    color: #fff;
    padding: 12px 20px;
    text-decoration: none;
    transition: all 0.3s ease;
}
.sidebar a:hover, .sidebar a.active {
    background: rgba(255,255,255,0.2);
    border-left: 4px solid #fff;
    padding-left: 24px;
}

/* Content */
.content {
    flex: 1;
    padding: 30px;
}
.card {
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.08);
    margin-bottom: 30px;
}
.card-header {
    background: linear-gradient(90deg, #ff7b00, #ff9500);
    color: #fff;
    font-weight: 600;
    font-size: 1.2rem;
    border-radius: 12px 12px 0 0;
}

/* Table */
.table th {
    background-color: #fff8f1;
    color: #333;
}
.table td {
    background-color: #fff;
}
.table-hover tbody tr:hover {
    background-color: #ffe3cc;
}

/* Status badges */
.badge-success { background-color: #28a745; }
.badge-danger { background-color: #dc3545; }

/* Footer */
footer {
    background: linear-gradient(90deg, #ff7b00, #ff9500);
    color: #fff;
    text-align: center;
    padding: 12px;
}

/* Responsive */
@media(max-width:991px){
    .wrapper { flex-direction: column; }
    .sidebar { width: 100%; }
}
</style>
</head>
<body>

<!-- ✅ Header -->
<?php include('include/header.php'); ?>

<div class="wrapper">
    <!-- ✅ Sidebar -->
    <div class="sidebar">
        <?php include('include/sidebar.php'); ?>
    </div>

    <!-- ✅ Main Content -->
    <div class="content">
        <div class="card">
            <div class="card-header">
                <i class="bi bi-person-lines-fill me-2"></i>Users Log
            </div>
            <div class="card-body table-responsive">
                <table id="usersLogTable" class="table table-striped table-bordered table-hover align-middle">
                    <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($bd, "SELECT * FROM userlog");
$cnt = 1;
while($row = mysqli_fetch_array($query)) { ?>
                        <tr>
                            <td><?php echo htmlentities($cnt); ?></td>
                            <td><?php echo htmlentities($row['username']); ?></td>
                            <td><?php echo htmlentities($row['userip']); ?></td>
                            <td><?php echo htmlentities($row['loginTime']); ?></td>
                            <td><?php echo htmlentities($row['logout']); ?></td>
                            <td>
<?php 
$status = $row['status'];
if($status == 1) {
    echo '<span class="badge bg-success">Successful</span>';
} else {
    echo '<span class="badge bg-danger">Failed</span>';
}
?>
                            </td>
                        </tr>
<?php $cnt++; } ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- ✅ Footer -->
<?php include('include/footer.php'); ?>

<!-- ✅ JS -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<!-- ✅ DataTables JS -->
<script src="https://cdn.datatables.net/1.13.5/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.5/js/dataTables.bootstrap5.min.js"></script>

<script>
$(document).ready(function() {
    $('#usersLogTable').DataTable({
        "pagingType": "simple_numbers", // page numbers + arrows
        "lengthChange": false,
        "info": false,
        "order": [[ 3, "desc" ]] // order by login time descending
    });
});
</script>

</body>
</html>
