@extends('layouts.yellow')

@section('content')
<style>
    .badge-primary {
    color: #fff;
    background-color: #007bff;
}
.badge-secondary {
    color: #fff;
    background-color: #6c757d;
}

.badge-success {
    color: #fff;
    background-color: #28a745;
}

.badge-danger {
    color: #fff;
    background-color: #dc3545;
}

.badge-pill {
    padding-right: .6em;
    padding-left: .6em;
    border-radius: 10rem;
}   

.badge-warning {
    color: #212529;
    background-color: #ffc107;
}

.badge-info {
    color: #fff;
    background-color: #17a2b8;
}

.badge-light {
    color: #212529;
    background-color: #f8f9fa;
}

.badge-dark {
    color: #fff;
    background-color: #343a40;
}
</style>
<div class="container-fluid">
    <div class="card">
        <div class="card-header bg-primary text-white" style="color: white !important;">
            <h3>Auditoría del Sistema</h3>
        </div>
        <div class="card-body">
            <form action="{{ route('admin.audits.index') }}" method="GET" class="mb-4">
                <div class="row">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="user_id">Usuario</label>
                            <select class="form-control" id="user_id" name="user_id">
                                <option value="">Todos</option>
                                @foreach($users as $id => $name)
                                    <option value="{{ $id }}" {{ request('user_id') == $id ? 'selected' : '' }}>{{ $name }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="action">Acción</label>
                            <select class="form-control" id="action" name="action">
                                <option value="">Todas</option>
                                <option value="create" {{ request('action') == 'create' ? 'selected' : '' }}>Crear</option>
                                <option value="update" {{ request('action') == 'update' ? 'selected' : '' }}>Actualizar</option>
                                <option value="delete" {{ request('action') == 'delete' ? 'selected' : '' }}>Eliminar</option>
                                <option value="login" {{ request('action') == 'login' ? 'selected' : '' }}>Inicio de sesión</option>
                                <option value="logout" {{ request('action') == 'logout' ? 'selected' : '' }}>Cierre de sesión</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="model">Modelo</label>
                            <select class="form-control" id="model" name="model">
                                <option value="">Todos</option>
                                @foreach($models as $model)
                                    <option value="{{ $model }}" {{ request('model') == $model ? 'selected' : '' }}>
                                        {{ class_basename($model) }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="date_from">Desde</label>
                            <input type="date" class="form-control" id="date_from" name="date_from" value="{{ request('date_from') }}">
                        </div>
                    </div>
                </div>
                <div class="row mt-2">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="date_to">Hasta</label>
                            <input type="date" class="form-control" id="date_to" name="date_to" value="{{ request('date_to') }}">
                        </div>
                    </div>
                    <div class="col-md-3 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary">Filtrar</button>
                        <a href="{{ route('admin.audits.index') }}" class="btn btn-secondary ml-2">Limpiar</a>
                    </div>
                    <!--@if(Auth::user()->hasRole('root'))
                    <div class="col-md-6 text-right d-flex align-items-end justify-content-end">
                        <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#cleanupModal">
                            Limpiar registros antiguos
                        </button>
                    </div>
                    @endif-->
                </div>
            </form>

            @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-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Usuario</th>
                            <th>Acción</th>
                            <th>Modelo</th>
                            <th>ID Registro</th>
                            <th>Fecha</th>
                            <th>IP</th>
                            <th>Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($audits as $audit)
                        <tr>
                            <td>{{ $audit->id }}</td>
                            <td>{{ $audit->user ? $audit->user->name : 'Sistema' }}</td>
                            <td>
                                @switch($audit->action)
                                    @case('create')
                                        <span class="badge badge-pill badge-success">Crear</span>
                                        @break
                                    @case('update')
                                        <span class="badge badge-pill badge-warning">Actualizar</span>
                                        @break
                                    @case('delete')
                                        <span class="badge badge-pill badge-danger">Eliminar</span>
                                        @break
                                    @case('login')
                                        <span class="badge badge-pill badge-info">Inicio de sesión</span>
                                        @break
                                    @case('logout')
                                        <span class="badge badge-pill badge-secondary">Cierre de sesión</span>
                                        @break
                                    @default
                                        <span class="badge badge-pill badge-primary">{{ $audit->action }}</span>
                                @endswitch
                            </td>
                            <td>{{ class_basename($audit->model) }}</td>
                            <td>{{ $audit->model_id }}</td>
                            <td>{{ $audit->created_at->format('d/m/Y H:i:s') }}</td>
                            <td>{{ $audit->ip }}</td>
                            <td>
                                <a href="{{ route('admin.audits.show', $audit->id) }}" class="btn btn-sm btn-info">Ver detalles</a>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

            <div class="d-flex justify-content-center mt-4">
                {{ $audits->links() }}
            </div>
        </div>
    </div>
</div>

<!-- Modal Cleanup -->
<div class="modal fade" id="cleanupModal" tabindex="-1" role="dialog" aria-labelledby="cleanupModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="cleanupModalLabel">Limpiar registros antiguos</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="{{ route('admin.audits.cleanup') }}" method="POST">
                @csrf
                <div class="modal-body">
                    <p class="text-danger">Esta acción eliminará permanentemente los registros de auditoría antiguos.</p>
                    <div class="form-group">
                        <label for="months">Eliminar registros anteriores a</label>
                        <select class="form-control" id="months" name="months">
                            <option value="1">1 mes</option>
                            <option value="3">3 meses</option>
                            <option value="6" selected>6 meses</option>
                            <option value="12">1 año</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
                    <button type="submit" class="btn btn-danger">Confirmar eliminación</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection 