@extends('layouts.admin.theme')

@section('title', env('APP_NAME') . ' :: Logs dos Usuários - Identificador de Usuarios')
@section('body_class', 'page-logs-usuarios')

@section('content')
    <div class="card shadow-sm">

        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
            <h5 class="my-2">Logs de Usuários - Identificador de Usuarios</h5>
        </div>

        <div class="card-body">
            <div class="row">
                <div class="my-4">
                    <button onclick="window.location.href='{{ route('admin.gerador-id.index') }}'"
                        class="btn btn-secondary float-start">
                        <i class="fa-solid fa-reply"></i> Voltar
                    </button>
                </div>
            </div>

            <div class="mb-4">
                <form method="GET" action="{{ route('admin.gerador-id.logs.logsUsuario') }}" class="mb-3">
                    <div class="input-group">
                        <input type="text" name="busca" value="{{ request('busca') }}" class="form-control"
                            placeholder="Buscar log ...">
                        <button class="btn btn-primary" type="submit">Buscar</button>
                        <a href="{{ route('admin.gerador-id.logs.logsUsuario') }}" class="btn btn-outline-secondary">Limpar</a>
                    </div>
                </form>
            </div>

            @if (session('success'))
                <div class="alert alert-success">{{ session('success') }}</div>
            @endif

            @if (session('error'))
                <div class="alert alert-danger">{{ session('error') }}</div>
            @endif

            <div class="table-responsive">
                <table class="table table-striped table-bordered align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th class="text-dark">Data</th>
                            <th class="text-dark">Nome Usuário</th>
                            <th class="text-dark">Email</th>
                            <th class="text-dark">Matrícula</th>
                            <th class="text-dark">IP</th>
                            <th class="text-dark">Contexto</th>
                            <th class="text-dark">ID Gerado</th>
                        </tr>
                    </thead>
                    <tbody class="table-light">
                        @forelse ($logs as $log)
                            <tr>
                                <td>{{ $log['data'] }}</td>
                                <td>
                                    @if ($log['usuario'])
                                        {{ $log['usuario']->firstName }} {{ $log['usuario']->lastName }}
                                    @else
                                        <span class="text-muted">Desconhecido</span>
                                    @endif
                                </td>
                                <td>{{ $log['usuario']->email ?? '—' }}</td>
                                <td>{{ $log['usuario']->matriculation ?? '—' }}</td>
                                <td>{{ $log['contexto']['ip'] ?? '—' }}</td>
                                <td>{{ $log['mensagem'] }}</td>
                                <td>{{ $log['contexto']['codigo'] ?? '—' }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="text-center">Nenhum log encontrado.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if (count($logs) > 0)
                <div class="d-flex justify-content-center mt-4">
                    {{ $logs->links() }}
                </div>
            @endif

        </div>
    </div>
@endsection
