<?php $title = 'AdminSB Pro - Audit Log'; ?>

<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">Audit Log</h1>
</div>

<div class="card shadow mb-4">
    <div class="card-header py-3">
        <form method="GET" class="form-inline">
            <div class="input-group">
                <input type="text" class="form-control" placeholder="Filter by action..." name="action" value="<?= isset($action) ? htmlspecialchars($action) : '' ?>">
                <div class="input-group-append">
                    <button class="btn btn-primary" type="submit"><i class="fas fa-search fa-sm"></i></button>
                </div>
            </div>
            <?php if (!empty($action)): ?>
                <a href="<?= $baseUrl ?>/audit" class="btn btn-secondary ml-2">Clear</a>
            <?php endif; ?>
        </form>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered" width="100%" cellspacing="0">
                <thead>
                    <tr>
                        <th>User</th>
                        <th>Action</th>
                        <th>Entity</th>
                        <th>Description</th>
                        <th>IP Address</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (isset($logs) && count($logs) > 0): ?>
                        <?php foreach ($logs as $log): ?>
                        <tr>
                            <td><?= htmlspecialchars(isset($log->user_name) ? $log->user_name : 'System') ?></td>
                            <td><span class="badge badge-info"><?= htmlspecialchars($log->action) ?></span></td>
                            <td><?= htmlspecialchars(isset($log->entity_type) ? $log->entity_type : '-') ?> <?= $log->entity_id ? '(' . $log->entity_id . ')' : '' ?></td>
                            <td><?= htmlspecialchars(isset($log->description) ? $log->description : '') ?></td>
                            <td><?= htmlspecialchars(isset($log->ip_address) ? $log->ip_address : '-') ?></td>
                            <td><?= date('M j, Y g:i A', strtotime($log->created_at)) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="6" class="text-center">No audit logs found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if (isset($lastPage) && $lastPage > 1): ?>
        <nav>
            <ul class="pagination justify-content-center">
                <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                    <a class="page-link" href="<?= $baseUrl ?>/audit?page=<?= $page - 1 ?><?= !empty($action) ? '&action=' . urlencode($action) : '' ?>">Previous</a>
                </li>
                <?php for ($i = 1; $i <= $lastPage; $i++): ?>
                    <li class="page-item <?= $i == $page ? 'active' : '' ?>">
                        <a class="page-link" href="<?= $baseUrl ?>/audit?page=<?= $i ?><?= !empty($action) ? '&action=' . urlencode($action) : '' ?>"><?= $i ?></a>
                    </li>
                <?php endfor; ?>
                <li class="page-item <?= $page >= $lastPage ? 'disabled' : '' ?>">
                    <a class="page-link" href="<?= $baseUrl ?>/audit?page=<?= $page + 1 ?><?= !empty($action) ? '&action=' . urlencode($action) : '' ?>">Next</a>
                </li>
            </ul>
        </nav>
        <?php endif; ?>
    </div>
</div>
