@extends('layouts.dashboard')

@section('content')
<div class="container mx-auto px-4 py-6">
    <!-- Mensajes de éxito y error -->
    @if(session('success'))
        <div id="success-message" class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
            <span class="block sm:inline">{{ session('success') }}</span>
        </div>
    @endif

    @if(session('error'))
        <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4" role="alert">
            <span class="block sm:inline">{{ session('error') }}</span>
        </div>
    @endif

    <div class="bg-white rounded-lg shadow-lg">
        <!-- Encabezado con título y botones de acción -->
        <div class="p-4 border-b border-gray-200 flex justify-between items-center">
            <h2 class="text-2xl font-bold">Gestión de Préstamos</h2>
            <div class="flex items-center space-x-6">

                <!-- Íconos de exportación -->
                <div class="flex space-x-4">
                    <!-- Exportar a Excel -->
                    <a href="{{ route('prestamos.excel') }}"
                       class="text-green-600 hover:text-green-700 transition-colors duration-200"
                       title="Exportar a Excel">
                        <i class="fas fa-file-excel text-xl"></i>
                    </a>

                    <!-- Exportar a PDF -->
                    <a href="{{ route('prestamos.pdf') }}"
                       class="text-red-600 hover:text-red-700 transition-colors duration-200"
                       title="Exportar a PDF">
                        <i class="fas fa-file-pdf text-xl"></i>
                    </a>
                    <!-- Agregar nuevo préstamo -->
                    <a href="{{ route('prestamos.create') }}"
                       class="text-blue-500 hover:text-blue-600 transition-colors duration-200"
                       title="Agregar nuevo préstamo">
                        <i class="fas fa-plus text-xl"></i>
                    </a>
                </div>
            </div>
        </div>

        <!-- Barra de búsqueda -->
        <div class="p-4 border-b border-gray-200 bg-gray-50">
            <form action="{{ route('prestamos.index') }}" method="GET" class="flex gap-2">
                <div class="relative w-full sm:w-64">
                    <input type="text"
                           name="busqueda"
                           placeholder="Buscar préstamo..."
                           value="{{ request('busqueda') }}"
                           class="w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                    <div class="absolute left-3 top-2.5 text-gray-400">
                        <i class="fas fa-search"></i>
                    </div>
                </div>
                <button type="submit" class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600">
                    Buscar
                </button>
                @if(request('busqueda'))
                    <a href="{{ route('prestamos.index') }}" class="px-4 py-2 bg-gray-500 text-white rounded-lg hover:bg-gray-600">
                        Limpiar
                    </a>
                @endif
            </form>
        </div>

        <!-- Tabla de préstamos -->
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Número</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Nombre Solicitante</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Destino</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Fecha y Hora Salida</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Fecha y Hora Llegada</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Motivo</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Domicilio</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Número de Personas</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Chofer</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Estado</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Acciones</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @foreach($prestamos as $index => $prestamo)
                    <tr class="hover:bg-gray-50">
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            <span class="font-medium">{{ $index + 1 }}</span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            <div class="flex items-center">
                                <i class="fas fa-user text-blue-500 mr-2"></i>
                                {{ $prestamo->nombre_solicitante }}
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            <div class="flex items-center">
                                <i class="fas fa-map-marker-alt text-red-500 mr-2"></i>
                                {{ $prestamo->destino }}
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            <div class="flex items-center">
                                <i class="fas fa-truck text-green-500 mr-2"></i>
                                {{ $prestamo->fecha_hora_salida }}
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            <div class="flex items-center">
                                <i class="fas fa-truck text-yellow-500 mr-2"></i>
                                {{ $prestamo->fecha_hora_llegada }}
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            <div class="flex items-center">
                                <i class="fas fa-info-circle text-purple-500 mr-2"></i>
                                {{ $prestamo->motivo }}
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            <div class="flex items-center">
                                <i class="fas fa-home text-indigo-500 mr-2"></i>
                                {{ $prestamo->domicilio }}
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            <div class="flex items-center">
                                <i class="fas fa-users text-blue-500 mr-2"></i>
                                {{ $prestamo->numero_personas }}
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            <div class="flex items-center">
                                <i class="fas fa-id-card text-gray-500 mr-2"></i>
                                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full {{ $prestamo->chofer ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }}">
                                    {{ $prestamo->chofer ? 'Sí' : 'No' }}
                                </span>
                            </div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm">
                            <span class="inline-flex items-center px-2 py-1 text-xs font-semibold text-green-800 bg-green-100 rounded-full">
                                <i class="fas fa-check-circle mr-1"></i> Activo
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                            <div class="flex space-x-2">
                                <a href="{{ route('prestamos.edit', $prestamo->id) }}" class="text-blue-600 hover:text-blue-900">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <form action="{{ route('prestamos.destroy', $prestamo->id) }}" method="POST" class="inline">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="text-red-600 hover:text-red-900" onclick="return confirm('¿Estás seguro de que deseas eliminar este préstamo?')">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </form>
                                <form action="{{ route('prestamos.aceptar', $prestamo->id) }}" method="POST" class="inline">
                                    @csrf
                                    <button type="submit" class="text-green-600 hover:text-green-900" onclick="return confirm('¿Estás seguro de que deseas aceptar este préstamo?')">
                                        <i class="fas fa-check"></i>
                                    </button>
                                </form>
                                <form action="{{ route('prestamos.rechazar', $prestamo->id) }}" method="POST" class="inline">
                                    @csrf
                                    <button type="submit" class="text-yellow-600 hover:text-yellow-900" onclick="return confirm('¿Estás seguro de que deseas rechazar este préstamo?')">
                                        <i class="fas fa-times"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
    // Desaparecer el mensaje después de 3 segundos
    setTimeout(function() {
        var message = document.getElementById('success-message');
        if (message) {
            message.style.transition = 'opacity 0.5s ease';
            message.style.opacity = '0';
            setTimeout(function() {
                message.remove();
            }, 500);
        }
    }, 3000);

    function confirmarEliminacion(button) {
        Swal.fire({
            title: '¿Estás seguro?',
            text: "Esta acción no se puede deshacer",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Sí, eliminar',
            cancelButtonText: 'Cancelar'
        }).then((result) => {
            if (result.isConfirmed) {
                button.closest('form').submit();
            }
        });
    }
</script>
@endsection