<?php
require_once 'config.php';
checkLogin();

// Pagination settings
$limit = 50;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// Filter options
$filter_admin = isset($_GET['admin']) ? sanitizeInput($_GET['admin']) : '';
$filter_action = isset($_GET['action']) ? sanitizeInput($_GET['action']) : '';
$filter_date = isset($_GET['date']) ? sanitizeInput($_GET['date']) : '';

// Build query with filters
$query = "SELECT * FROM admin_activity_logs WHERE 1=1";
$params = [];

if (!empty($filter_admin)) {
    $query .= " AND admin_username LIKE ?";
    $params[] = "%$filter_admin%";
}

if (!empty($filter_action)) {
    $query .= " AND action LIKE ?";
    $params[] = "%$filter_action%";
}

if (!empty($filter_date)) {
    $query .= " AND DATE(created_at) = ?";
    $params[] = $filter_date;
}

// Get total count for pagination
$countQuery = "SELECT COUNT(*) FROM ($query) as total";
$countStmt = $db->prepare($countQuery);
$countStmt->execute($params);
$total_records = $countStmt->fetchColumn();
$total_pages = ceil($total_records / $limit);

// Add sorting and pagination to main query
$query .= " ORDER BY created_at DESC LIMIT $limit OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->execute($params);
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Get unique admin usernames for filter dropdown
$adminUsers = $db->query("SELECT DISTINCT admin_username FROM admin_activity_logs ORDER BY admin_username")->fetchAll(PDO::FETCH_COLUMN);

// Get unique actions for filter dropdown
$actions = $db->query("SELECT DISTINCT action FROM admin_activity_logs ORDER BY action")->fetchAll(PDO::FETCH_COLUMN);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Admin Activity Log - PAPUAPOOLS</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
    <style>
        .log-table {
            font-size: 0.9rem;
        }
        .log-table th {
            background-color: #f8f9fa;
        }
        .action-cell {
            max-width: 200px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .details-cell {
            max-width: 300px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .filters {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
<?php include 'navbar.php'; ?>
<div class="container mt-4">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-info text-white">
                    <h4><i class="fas fa-history"></i> Admin Activity Log</h4>
                </div>
                <div class="card-body">
                    <!-- Filters -->
                    <div class="filters">
                        <form method="GET" class="form-inline">
                            <div class="form-group mr-2">
                                <label for="admin" class="mr-2">Admin:</label>
                                <select class="form-control" id="admin" name="admin">
                                    <option value="">All Admins</option>
                                    <?php foreach ($adminUsers as $username): ?>
                                        <option value="<?= htmlspecialchars($username) ?>" <?= $filter_admin == $username ? 'selected' : '' ?>>
                                            <?= htmlspecialchars($username) ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="form-group mr-2">
                                <label for="action" class="mr-2">Action:</label>
                                <select class="form-control" id="action" name="action">
                                    <option value="">All Actions</option>
                                    <?php foreach ($actions as $action): ?>
                                        <option value="<?= htmlspecialchars($action) ?>" <?= $filter_action == $action ? 'selected' : '' ?>>
                                            <?= htmlspecialchars($action) ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="form-group mr-2">
                                <label for="date" class="mr-2">Date:</label>
                                <input type="date" class="form-control" id="date" name="date" value="<?= htmlspecialchars($filter_date) ?>">
                            </div>
                            <button type="submit" class="btn btn-primary mr-2">Filter</button>
                            <a href="admin_logs.php" class="btn btn-secondary">Reset</a>
                        </form>
                    </div>

                    <!-- Log Table -->
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped log-table">
                            <thead>
                                <tr>
                                    <th>Date & Time</th>
                                    <th>Admin</th>
                                    <th>Action</th>
                                    <th>Details</th>
                                    <th>IP Address</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (empty($logs)): ?>
                                    <tr>
                                        <td colspan="5" class="text-center">No activity logs found</td>
                                    </tr>
                                <?php else: ?>
                                    <?php foreach ($logs as $log): ?>
                                        <tr>
                                            <td><?= htmlspecialchars($log['created_at']) ?></td>
                                            <td><?= htmlspecialchars($log['admin_username']) ?></td>
                                            <td class="action-cell"><?= htmlspecialchars($log['action']) ?></td>
                                            <td class="details-cell" title="<?= htmlspecialchars($log['details']) ?>">
                                                <?= htmlspecialchars($log['details']) ?>
                                            </td>
                                            <td><?= htmlspecialchars($log['ip_address']) ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    <?php if ($total_pages > 1): ?>
                    <nav aria-label="Page navigation">
                        <ul class="pagination justify-content-center">
                            <?php if ($page > 1): ?>
                                <li class="page-item">
                                    <a class="page-link" href="?page=<?= $page-1 ?>&admin=<?= urlencode($filter_admin) ?>&action=<?= urlencode($filter_action) ?>&date=<?= urlencode($filter_date) ?>">Previous</a>
                                </li>
                            <?php endif; ?>
                            
                            <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                                <li class="page-item <?= $i == $page ? 'active' : '' ?>">
                                    <a class="page-link" href="?page=<?= $i ?>&admin=<?= urlencode($filter_admin) ?>&action=<?= urlencode($filter_action) ?>&date=<?= urlencode($filter_date) ?>"><?= $i ?></a>
                                </li>
                            <?php endfor; ?>
                            
                            <?php if ($page < $total_pages): ?>
                                <li class="page-item">
                                    <a class="page-link" href="?page=<?= $page+1 ?>&admin=<?= urlencode($filter_admin) ?>&action=<?= urlencode($filter_action) ?>&date=<?= urlencode($filter_date) ?>">Next</a>
                                </li>
                            <?php endif; ?>
                        </ul>
                    </nav>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>