@extends('layouts.app')

@section('title', __('messages.audit_logs'))

@section('content')
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="fw-bold mb-0">System Audit Trail Logs</h5>
        <span class="text-muted fs-xs">Immutable Security Log Tracking User Actions, System Modifications, and Login Activity</span>
    </div>
</div>

<div class="card-erp p-3 mb-3">
    <form method="GET" action="{{ route('audit-logs.index') }}" class="row g-2">
        <div class="col-md-4">
            <input type="text" name="search" class="form-control form-control-sm" placeholder="Search by Action, User, or Record ID..." value="{{ request('search') }}">
        </div>
        <div class="col-md-3">
            <select name="module" class="form-select form-select-sm">
                <option value="">All Modules...</option>
                @foreach($modules as $mod)
                    <option value="{{ $mod }}" {{ request('module') === $mod ? 'selected' : '' }}>{{ $mod }}</option>
                @endforeach
            </select>
        </div>
        <div class="col-md-2">
            <button type="submit" class="btn btn-sm btn-primary w-100"><i class="bi bi-filter me-1"></i> Filter</button>
        </div>
        <div class="col-md-2">
            <a href="{{ route('audit-logs.index') }}" class="btn btn-sm btn-outline-secondary w-100">Reset</a>
        </div>
    </form>
</div>

<div class="card-erp">
    <div class="table-responsive">
        <table class="table table-dense table-hover mb-0">
            <thead>
                <tr>
                    <th>Timestamp</th>
                    <th>User</th>
                    <th>Action</th>
                    <th>Module</th>
                    <th>Record ID</th>
                    <th>IP Address</th>
                    <th>User Agent</th>
                    <th class="text-end">Changes</th>
                </tr>
            </thead>
            <tbody>
                @forelse($logs as $log)
                    <tr>
                        <td class="font-monospace fs-xs">{{ $log->created_at->format('Y-m-d H:i:s') }}</td>
                        <td class="fw-bold text-dark">{{ $log->user_name ?? ($log->user->name ?? 'System') }}</td>
                        <td><span class="badge bg-secondary font-monospace">{{ $log->action }}</span></td>
                        <td><span class="badge bg-light text-dark border">{{ $log->module }}</span></td>
                        <td>{{ $log->record_id ?? '-' }}</td>
                        <td class="font-monospace fs-xs">{{ $log->ip_address ?? '-' }}</td>
                        <td class="text-truncate fs-xs text-muted" style="max-width: 150px;" title="{{ $log->user_agent }}">
                            {{ $log->user_agent ?? '-' }}
                        </td>
                        <td class="text-end">
                            @if($log->old_values || $log->new_values)
                                <button class="btn btn-dense btn-outline-info" data-bs-toggle="modal" data-bs-target="#logModal{{ $log->id }}">
                                    <i class="bi bi-eye"></i> View JSON
                                </button>
                            @else
                                <span class="text-muted fs-xs">No Diff</span>
                            @endif
                        </td>
                    </tr>

                    @if($log->old_values || $log->new_values)
                        <div class="modal fade" id="logModal{{ $log->id }}" tabindex="-1">
                            <div class="modal-dialog modal-lg">
                                <div class="modal-content">
                                    <div class="modal-header py-2">
                                        <h6 class="modal-title fw-bold">Audit Log #{{ $log->id }} - {{ $log->action }}</h6>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                    </div>
                                    <div class="modal-body fs-xs">
                                        <div class="row g-2">
                                            <div class="col-6">
                                                <h6 class="fw-bold text-danger">Old Values</h6>
                                                <pre class="bg-light p-2 border rounded" style="max-height: 250px;">{{ json_encode($log->old_values, JSON_PRETTY_PRINT) }}</pre>
                                            </div>
                                            <div class="col-6">
                                                <h6 class="fw-bold text-success">New Values</h6>
                                                <pre class="bg-light p-2 border rounded" style="max-height: 250px;">{{ json_encode($log->new_values, JSON_PRETTY_PRINT) }}</pre>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endif
                @empty
                    <tr><td colspan="8" class="text-center py-4 text-muted">No audit logs recorded yet.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
    <div class="p-2 border-top">
        {{ $logs->links() }}
    </div>
</div>
@endsection
