@extends('layouts.admin')

@section('title', 'System Audit Trail – Admin CRM')
@section('page_title', 'System Audit Logs')

@section('content')
<div class="space-y-6 max-w-7xl">
    
    <div>
        <h2 class="text-lg font-bold text-white font-heading">Security & Regulatory Audit Trail</h2>
        <p class="text-xs text-slate-400">Immutable ledger tracking staff bid approvals, import status updates, payment reconciliations, and tariff changes.</p>
    </div>

    <div class="bg-slate-900 border border-white/10 rounded-2xl overflow-hidden shadow-xl">
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs">
                <thead class="bg-slate-950 text-slate-400 uppercase tracking-wider text-[10px] border-b border-white/10">
                    <tr>
                        <th class="p-4">Timestamp</th>
                        <th class="p-4">Staff User</th>
                        <th class="p-4">Action Recorded</th>
                        <th class="p-4">Entity Type & ID</th>
                        <th class="p-4">IP Address</th>
                        <th class="p-4">Context Payload</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-white/5 text-slate-300">
                    @forelse($logs as $log)
                        <tr class="hover:bg-white/5 transition">
                            <td class="p-4 font-mono text-[11px] text-slate-400">
                                {{ $log->created_at->format('Y-m-d H:i:s') }}
                            </td>
                            <td class="p-4 font-bold text-white">
                                {{ $log->user->name ?? 'System Event' }}
                            </td>
                            <td class="p-4">
                                <span class="px-2 py-0.5 rounded text-[10px] font-bold uppercase bg-white/10 text-red-400">
                                    {{ $log->action }}
                                </span>
                            </td>
                            <td class="p-4 font-mono text-[11px] text-slate-300">
                                {{ class_basename($log->model_type ?? 'N/A') }} #{{ $log->model_id ?? '-' }}
                            </td>
                            <td class="p-4 font-mono text-[11px] text-slate-500">
                                {{ $log->ip_address ?? '127.0.0.1' }}
                            </td>
                            <td class="p-4 text-[10px] font-mono text-slate-400 max-w-xs truncate">
                                @if($log->new_values)
                                    {{ json_encode($log->new_values) }}
                                @else
                                    -
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="p-8 text-center text-slate-500">
                                No audit records logged yet.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="p-4 border-t border-white/10">
            {{ $logs->links() }}
        </div>
    </div>

</div>
@endsection
