@extends('admin.layout')

@section('title', 'Log Attività')
@section('page_title', 'Log Attività')

@section('breadcrumbs')
    <li class="breadcrumb-item active">Admin</li>
    <li class="breadcrumb-item active">Log Attività</li>
@endsection

@section('content')
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Attività di Sistema</h3>
            </div>
            <div class="card-body">
                <form method="GET" class="mb-3">
                    <div class="row">
                        <div class="col-md-2">
                            <select name="user_id" class="form-control">
                                <option value="">Tutti gli utenti</option>
                                @foreach($users as $u)
                                    <option value="{{ $u->id }}" {{ request('user_id') == $u->id ? 'selected' : '' }}>{{ $u->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select name="module" class="form-control">
                                <option value="">Tutti i moduli</option>
                                @foreach($modules as $module)
                                    <option value="{{ $module }}" {{ request('module') == $module ? 'selected' : '' }}>{{ $module }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select name="action" class="form-control">
                                <option value="">Tutte le azioni</option>
                                <option value="create" {{ request('action') == 'create' ? 'selected' : '' }}>Create</option>
                                <option value="update" {{ request('action') == 'update' ? 'selected' : '' }}>Update</option>
                                <option value="delete" {{ request('action') == 'delete' ? 'selected' : '' }}>Delete</option>
                                <option value="login" {{ request('action') == 'login' ? 'selected' : '' }}>Login</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <input type="date" name="from" class="form-control" value="{{ request('from') }}" placeholder="Da">
                        </div>
                        <div class="col-md-2">
                            <input type="date" name="to" class="form-control" value="{{ request('to') }}" placeholder="A">
                        </div>
                        <div class="col-md-2">
                            <button type="submit" class="btn btn-primary">Filtra</button>
                            <a href="/admin/activity-logs" class="btn btn-secondary">Reset</a>
                        </div>
                    </div>
                </form>

                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Data/Ora</th>
                            <th>Utente</th>
                            <th>Azione</th>
                            <th>Modulo</th>
                            <th>Descrizione</th>
                            <th>IP</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($logs as $log)
                        <tr>
                            <td><small>{{ $log->created_at->format('d/m/Y H:i:s') }}</small></td>
                            <td>{{ $log->user->name ?? 'Sistema' }}</td>
                            <td>
                                <span class="badge badge-{{ $log->action == 'delete' ? 'danger' : ($log->action == 'create' ? 'success' : ($log->action == 'login' ? 'info' : 'warning')) }}">
                                    {{ $log->action }}
                                </span>
                            </td>
                            <td>{{ $log->module }}</td>
                            <td>{{ $log->description }}</td>
                            <td><small>{{ $log->ip_address }}</small></td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="text-center">Nessun log presente</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>

                {{ $logs->links() }}
            </div>
        </div>
    </div>
</div>
@endsection