<?php
/**
 * @var object $logs
 * @var array $users
 * @var array $distinctActions
 * @var array $distinctTables
 * @var string $userId
 * @var string $action
 * @var string $tableName
 * @var string $fromDate
 * @var string $toDate
 * @var object|null $log
 * @var array|null $oldValues
 * @var array|null $newValues
 */

$isDetail = isset($log) && $log !== null;
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-history me-2 text-gold"></i><?= $isDetail ? 'Audit Log Detail' : 'Audit Logs' ?></h5>
</div>

<?php if ($isDetail): ?>
<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-info-circle card-icon"></i>
            <h6 class="card-title">Log #<?= $log->id ?> - <?= e($log->action) ?> on <?= e($log->table_name) ?></h6>
        </div>
        <div>
            <a href="<?= url('audit/logs') ?>" class="btn btn-sm btn-outline-gold">
                <i class="fas fa-arrow-left me-1"></i>Back to Logs
            </a>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-4">
            <div class="col-md-3">
                <small class="text-muted d-block">User</small>
                <strong><?= e($log->first_name . ' ' . $log->last_name) ?></strong>
            </div>
            <div class="col-md-3">
                <small class="text-muted d-block">Action</small>
                <span class="badge bg-info"><?= e($log->action) ?></span>
            </div>
            <div class="col-md-3">
                <small class="text-muted d-block">Table</small>
                <code><?= e($log->table_name) ?></code>
            </div>
            <div class="col-md-3">
                <small class="text-muted d-block">Record ID</small>
                <code>#<?= (int)$log->record_id ?></code>
            </div>
        </div>
        <div class="row mb-3">
            <div class="col-md-6">
                <small class="text-muted d-block">IP Address</small>
                <code><?= e($log->ip_address) ?></code>
            </div>
            <div class="col-md-6">
                <small class="text-muted d-block">Timestamp</small>
                <strong><?= formatDateTime($log->created_at) ?></strong>
            </div>
        </div>

        <div class="row g-3 mt-3">
            <?php if ($oldValues): ?>
            <div class="col-md-6">
                <div class="glass-card p-3">
                    <h6 class="text-danger mb-3"><i class="fas fa-minus-circle me-1"></i>Old Values</h6>
                    <div class="table-responsive">
                        <table class="table table-sm table-dark mb-0">
                            <tbody>
                                <?php foreach ($oldValues as $key => $value): ?>
                                    <tr>
                                        <td class="text-gold" style="width:120px"><?= e($key) ?></td>
                                        <td><?= e(is_string($value) ? $value : json_encode($value)) ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <?php endif; ?>
            <?php if ($newValues): ?>
            <div class="col-md-6">
                <div class="glass-card p-3">
                    <h6 class="text-success mb-3"><i class="fas fa-plus-circle me-1"></i>New Values</h6>
                    <div class="table-responsive">
                        <table class="table table-sm table-dark mb-0">
                            <tbody>
                                <?php foreach ($newValues as $key => $value): ?>
                                    <tr>
                                        <td class="text-gold" style="width:120px"><?= e($key) ?></td>
                                        <td><?= e(is_string($value) ? $value : json_encode($value)) ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php else: ?>

<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">Audit Trail</h6>
        </div>
        <div class="card-header-right">
            <a href="<?= url('audit/activity') ?>" class="btn btn-sm btn-outline-gold">
                <i class="fas fa-activity me-1"></i>Activity Logs
            </a>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" action="<?= url('audit/logs') ?>" class="row mb-3 g-2">
            <div class="col-md-2">
                <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-2">
                <select name="action" class="form-select form-select-sm bg-transparent">
                    <option value="">All Actions</option>
                    <?php foreach ($distinctActions as $a): ?>
                        <option value="<?= $a->action ?>" <?= $action === $a->action ? 'selected' : '' ?>>
                            <?= e(ucfirst($a->action)) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <select name="table_name" class="form-select form-select-sm bg-transparent">
                    <option value="">All Tables</option>
                    <?php foreach ($distinctTables as $t): ?>
                        <option value="<?= $t->table_name ?>" <?= $tableName === $t->table_name ? 'selected' : '' ?>>
                            <?= e($t->table_name) ?>
                        </option>
                    <?php endforeach; ?>
                </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>Table</th>
                        <th>Record ID</th>
                        <th>IP Address</th>
                        <th width="80" class="text-center">View</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($logs->data)): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                <i class="fas fa-history fa-2x mb-2"></i>
                                <p class="mb-0">No audit 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><?= e($log->first_name . ' ' . $log->last_name) ?></td>
                                <td>
                                    <span class="badge bg-<?= statusBadge($log->action === 'delete' ? 'cancelled' : ($log->action === 'create' ? 'active' : 'info')) ?>">
                                        <?= e(ucfirst($log->action)) ?>
                                    </span>
                                </td>
                                <td><code><?= e($log->table_name) ?></code></td>
                                <td><code>#<?= (int)$log->record_id ?></code></td>
                                <td><small class="text-muted"><?= e($log->ip_address) ?></small></td>
                                <td class="text-center">
                                    <a href="<?= url('audit/logs/' . $log->id) ?>" class="btn btn-sm btn-outline-info" title="View Details">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                </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/logs')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>
<?php endif; ?>
