<?php
// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once('../includes/admin-security.php');
include_once('includes/config.php');

// Access Check (assuming manage_settings or a new manage_logs permission)
if (!has_permission('manage_settings')) {
    echo "<script>alert('Access Denied. Redirecting to Dashboard...'); window.location.href='dashboard.php';</script>";
    exit();
}

// Clear logs if requested (only for admin)
if (isset($_POST['clear_logs']) && $_SESSION['role'] == 'admin') {
    mysqli_query($con, "DELETE FROM admin_activity_log");
    log_activity("Cleared Activity Logs", "All historical logs were deleted.");
    $_SESSION['msg'] = "Activity logs cleared successfully!";
}

?>

<!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" />
    <title>Admin Activity Log - Awesome Mall</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="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/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">
                    <div class="page-header mt-4">
                        <div>
                            <h1><i class="fas fa-history me-2"></i> Activity Log</h1>
                            <p>Track administrator actions and system changes</p>
                        </div>
                        <?php if ($_SESSION['role'] == 'admin'): ?>
                            <div class="actions">
                                <form method="post"
                                    onsubmit="return confirm('Are you sure you want to clear ALL activity logs? This cannot be undone.');">
                                    <button type="submit" name="clear_logs" class="btn btn-danger btn-sm">
                                        <i class="fas fa-trash-alt me-1"></i> Clear All Logs
                                    </button>
                                </form>
                            </div>
                        <?php endif; ?>
                    </div>

                    <?php if (isset($_SESSION['msg'])) { ?>
                        <div class="alert alert-success mt-3">
                            <i class="fas fa-check-circle me-2"></i>
                            <?php echo htmlentities($_SESSION['msg']);
                            unset($_SESSION['msg']); ?>
                        </div>
                    <?php } ?>

                    <div class="card mb-4 shadow-sm border-0">
                        <div class="card-header bg-white py-3">
                            <h5 class="mb-0"><i class="fas fa-list me-2"></i>Recent Activities</h5>
                        </div>
                        <div class="card-body">
                            <table id="datatablesSimple" class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Portal</th>
                                        <th>User</th>
                                        <th>Action</th>
                                        <th>Details</th>
                                        <th>IP Address</th>
                                        <th>Date & Time</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $query = mysqli_query($con, "SELECT a.*, u.username 
                                                                 FROM admin_activity_log a 
                                                                 LEFT JOIN users u ON a.admin_id = u.id 
                                                                 ORDER BY a.created_at DESC");
                                    while ($row = mysqli_fetch_array($query)) {
                                        ?>
                                        <tr>
                                            <td>
                                                <?php echo $row['id']; ?>
                                            </td>
                                            <td>
                                                <?php if ($row['portal'] == 'admin'): ?>
                                                    <span class="badge bg-dark text-white"><i
                                                            class="fas fa-user-shield me-1"></i> ADMIN</span>
                                                <?php else: ?>
                                                    <span class="badge bg-info text-dark"><i class="fas fa-store me-1"></i>
                                                        VENDOR</span>
                                                <?php endif; ?>
                                            </td>
                                            <td>
                                                <strong>
                                                    <?php echo htmlentities($row['username'] ?: 'System/Unrecognized'); ?>
                                                </strong>
                                                <br><small class="text-muted">ID:
                                                    <?php echo $row['admin_id']; ?>
                                                </small>
                                            </td>
                                            <td>
                                                <?php
                                                $badgeClass = 'bg-secondary';
                                                $action = $row['action'];
                                                if (strpos($action, 'Added') !== false)
                                                    $badgeClass = 'bg-success';
                                                if (strpos($action, 'Updated') !== false)
                                                    $badgeClass = 'bg-primary';
                                                if (strpos($action, 'Deleted') !== false)
                                                    $badgeClass = 'bg-danger';
                                                if (strpos($action, 'Login') !== false)
                                                    $badgeClass = 'bg-info';
                                                ?>
                                                <span class="badge <?php echo $badgeClass; ?>">
                                                    <?php echo htmlentities($row['action']); ?>
                                                </span>
                                            </td>
                                            <td><small>
                                                    <?php echo htmlentities($row['details']); ?>
                                                </small></td>
                                            <td><code><?php echo htmlentities($row['ip_address']); ?></code></td>
                                            <td>
                                                <?php echo date('M d, Y h:i A', strtotime($row['created_at'])); ?>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </main>
            <?php include_once('includes/footer.php'); ?>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/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>