<?php
/**
 * @var object $logs
 * @var array $users
 * @var string $userId
 * @var string $action
 * @var string $fromDate
 * @var string $toDate
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-activity me-2 text-gold"></i>Activity Logs</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">User Activity</h6>
        </div>
        <div class="card-header-right">
            <a href="<?= url('audit/logs') ?>" class="btn btn-sm btn-outline-gold">
                <i class="fas fa-history me-1"></i>Audit Logs
            </a>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" action="<?= url('audit/activity') ?>" class="row mb-3 g-2">
            <div class="col-md-3">
                <select name="user_id" class="form-select form-select-sm bg-transparent">
                    <option value="">All Users</option>
                    <?php foreach ($users as $u): ?>
                        <option value="<?= $u->id ?>" <?= $userId == $u->id ? 'selected' : '' ?>>
                            <?= e($u->first_name . ' ' . $u->last_name) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <select name="action" class="form-select form-select-sm bg-transparent">
                    <option value="">All Actions</option>
                    <option value="login" <?= $action === 'login' ? 'selected' : '' ?>>Login</option>
                    <option value="logout" <?= $action === 'logout' ? 'selected' : '' ?>>Logout</option>
                    <option value="create" <?= $action === 'create' ? 'selected' : '' ?>>Create</option>
                    <option value="update" <?= $action === 'update' ? 'selected' : '' ?>>Update</option>
                    <option value="delete" <?= $action === 'delete' ? 'selected' : '' ?>>Delete</option>
                </select>
            </div>
            <div class="col-md-2">
                <input type="date" name="from_date" class="form-control form-control-sm bg-transparent"
                       value="<?= e($fromDate) ?>">
            </div>
            <div class="col-md-2">
                <input type="date" name="to_date" class="form-control form-control-sm bg-transparent"
                       value="<?= e($toDate) ?>">
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-sm btn-gold w-100">
                    <i class="fas fa-filter me-1"></i>Filter
                </button>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Date</th>
                        <th>User</th>
                        <th>Action</th>
                        <th>Description</th>
                        <th>IP Address</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($logs->data)): ?>
                        <tr>
                            <td colspan="6" class="text-center text-muted py-4">
                                <i class="fas fa-activity fa-2x mb-2"></i>
                                <p class="mb-0">No activity logs found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($logs->currentPage - 1) * $logs->perPage + 1; ?>
                        <?php foreach ($logs->data as $log): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><?= formatDateTime($log->created_at) ?></td>
                                <td>
                                    <strong><?= e($log->first_name . ' ' . $log->last_name) ?></strong>
                                </td>
                                <td>
                                    <span class="badge bg-<?= statusBadge($log->action) ?>">
                                        <?= e(ucfirst($log->action)) ?>
                                    </span>
                                </td>
                                <td><?= e($log->description) ?></td>
                                <td><small class="text-muted"><?= e($log->ip_address) ?></small></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($logs->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($logs->currentPage - 1) * $logs->perPage) + 1 ?>
                    to <?= min($logs->currentPage * $logs->perPage, $logs->total) ?>
                    of <?= $logs->total ?> entries
                </div>
                <?= paginateLinks($logs, url('audit/activity')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>
