<?php
require_once 'config/db.php';
require_once 'includes/auth.php';
require_once 'includes/functions.php';
requireLogin();
requireAdmin();

$page    = max(1, intval($_GET['page'] ?? 1));
$limit   = 30;
$offset  = ($page-1)*$limit;
$logs    = getActivityLog($conn, $limit, $offset);
$total   = getCount($conn, "SELECT COUNT(*) FROM activity_log");
$pages   = ceil($total/$limit);

$action_colors = [
    'add_loan'        => 'bg-success',
    'approve_loan'    => 'bg-primary',
    'record_payment'  => 'bg-info',
    'edit_borrower'   => 'bg-warning text-dark',
    'delete_borrower' => 'bg-danger',
    'edit_user'       => 'bg-secondary',
    'password_reset'  => 'bg-dark',
    'add_borrower'    => 'bg-success',
];

include 'includes/header.php';
include 'includes/sidebar.php';
?>
<div class="row mb-4">
    <div class="col"><h3><i class="fas fa-history"></i> Activity Log</h3></div>
</div>
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">All Activity</h5>
        <small class="text-muted"><?php echo number_format($total); ?> records</small>
    </div>
    <div class="card-body p-0">
        <?php if (!empty($logs)): ?>
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr><th>Time</th><th>User</th><th>Action</th><th>Description</th><th>IP</th></tr>
                </thead>
                <tbody>
                    <?php foreach ($logs as $log):
                        $color = $action_colors[$log['action']] ?? 'bg-secondary';
                    ?>
                    <tr>
                        <td class="text-muted" style="white-space:nowrap;font-size:12px;">
                            <?php echo date('M d, Y', strtotime($log['created_at'])); ?><br>
                            <small><?php echo date('h:i A', strtotime($log['created_at'])); ?></small>
                        </td>
                        <td><?php echo htmlspecialchars($log['full_name'] ?? 'System'); ?><br>
                            <small class="text-muted"><?php echo htmlspecialchars($log['username'] ?? ''); ?></small>
                        </td>
                        <td><span class="badge <?php echo $color; ?>"><?php echo str_replace('_',' ', strtoupper($log['action'])); ?></span></td>
                        <td><?php echo htmlspecialchars($log['description'] ?? ''); ?></td>
                        <td><code style="font-size:11px;"><?php echo htmlspecialchars($log['ip_address'] ?? ''); ?></code></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php if ($pages > 1): ?>
        <div class="d-flex justify-content-between align-items-center p-3 border-top">
            <small class="text-muted">Page <?php echo $page; ?> of <?php echo $pages; ?></small>
            <nav><ul class="pagination pagination-sm mb-0">
                <?php for ($p=1; $p<=$pages; $p++): ?>
                    <li class="page-item <?php echo $p===$page?'active':''; ?>">
                        <a class="page-link" href="?page=<?php echo $p; ?>"><?php echo $p; ?></a>
                    </li>
                <?php endfor; ?>
            </ul></nav>
        </div>
        <?php endif; ?>
        <?php else: ?>
            <div class="p-5 text-center text-muted"><i class="fas fa-history fa-3x mb-3 opacity-25"></i><h5>No activity yet</h5></div>
        <?php endif; ?>
    </div>
</div>
<?php include 'includes/footer.php'; ?>
