<x-app-layout>
    <x-ui.page-header title="Journal d'audit" description="Historique des actions réalisées sur la plateforme" />

    <x-alert />

    @php
        $actionLabels = [
            'login' => 'Connexion',
            'logout' => 'Déconnexion',
            'login.failed' => 'Échec de connexion',
            'form.created' => 'Formulaire créé',
            'form.updated' => 'Formulaire modifié',
            'form.deleted' => 'Formulaire supprimé',
            'form.locked' => 'Formulaire verrouillé',
            'form.unlocked' => 'Formulaire déverrouillé',
            'filiale.created' => 'Filiale créée',
            'filiale.updated' => 'Filiale modifiée',
            'filiale.deleted' => 'Filiale supprimée',
            'assignment.created' => 'Formulaire assigné',
            'assignment.due_date.updated' => 'Échéance modifiée',
            'admin.user.created' => 'Compte admin créé',
            'admin.user.updated' => 'Compte admin modifié',
            'admin.user.deleted' => 'Compte admin supprimé',
            'filiale.user.created' => 'Compte filiale créé',
            'filiale.user.updated' => 'Compte filiale modifié',
            'filiale.user.deleted' => 'Compte filiale supprimé',
            'submission.submitted' => 'Soumission',
            'submission.updated' => 'Soumission mise à jour',
            'submission.draft' => 'Brouillon enregistré',
        ];
    @endphp

    <form method="GET" class="card p-4 mb-6 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-3 items-end">
        <div>
            <label class="block text-xs font-medium text-slate-600 mb-1">Utilisateur</label>
            <select name="user_id" class="input-modern text-sm">
                <option value="">Tous</option>
                @foreach($users as $user)
                    <option value="{{ $user->id }}" @selected(request('user_id') == $user->id)>{{ $user->name }}</option>
                @endforeach
            </select>
        </div>
        <div>
            <label class="block text-xs font-medium text-slate-600 mb-1">Action</label>
            <select name="action" class="input-modern text-sm">
                <option value="">Toutes</option>
                @foreach($actions as $action)
                    <option value="{{ $action }}" @selected(request('action') === $action)>{{ $actionLabels[$action] ?? $action }}</option>
                @endforeach
            </select>
        </div>
        <div>
            <label class="block text-xs font-medium text-slate-600 mb-1">Du</label>
            <input type="date" name="from" value="{{ request('from') }}" class="input-modern text-sm">
        </div>
        <div>
            <label class="block text-xs font-medium text-slate-600 mb-1">Au</label>
            <input type="date" name="to" value="{{ request('to') }}" class="input-modern text-sm">
        </div>
        <div class="flex gap-2">
            <button type="submit" class="btn-primary btn-sm">Filtrer</button>
            <a href="{{ route('admin.activity.index') }}" class="btn-secondary btn-sm">Réinitialiser</a>
        </div>
    </form>

    <div class="card overflow-hidden">
        <div class="overflow-x-auto">
            <table class="min-w-full table-modern">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Utilisateur</th>
                        <th>Action</th>
                        <th>Détail</th>
                        <th>IP</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($logs as $log)
                        <tr>
                            <td class="whitespace-nowrap text-slate-600">{{ $log->created_at?->format('d/m/Y H:i') }}</td>
                            <td class="font-medium text-slate-800">{{ $log->user?->name ?? '—' }}</td>
                            <td><span class="badge-info">{{ $actionLabels[$log->action] ?? $log->action }}</span></td>
                            <td class="text-slate-600 max-w-xs truncate" title="{{ $log->description }}">{{ $log->description ?? '—' }}</td>
                            <td class="text-slate-400 text-sm whitespace-nowrap">{{ $log->ip_address ?? '—' }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="text-center py-12 text-slate-400">Aucune activité enregistrée.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($logs->hasPages())
            <div class="px-6 py-4 border-t border-slate-100">{{ $logs->links() }}</div>
        @endif
    </div>
</x-app-layout>
