@extends('layouts.dashboard')

@section('content')
<div class="container mx-auto px-4 py-6">
    @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">
        <div class="p-4 border-b border-gray-200 flex justify-between items-center">
            <h2 class="text-2xl font-bold">Gestión de Capacidades</h2>
            <div class="flex items-center gap-4">
                <a href="{{ route('capacidades.excel') }}" class="text-green-600 hover:text-green-800 text-2xl">
                    <i class="fas fa-file-excel"></i>
                </a>
                <a href="{{ route('capacidades.pdf') }}" class="text-red-600 hover:text-red-800 text-2xl">
                    <i class="fas fa-file-pdf"></i>
                </a>
                <a href="{{ route('capacidades.create') }}" class="text-blue-600 hover:text-blue-800 text-2xl">
                    <i class="fas fa-plus"></i>
                </a>
            </div>
        </div>

        <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">ID</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Capacidad</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($capacidades as $capacidad)
                    <tr class="hover:bg-gray-50">
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $capacidad->id }}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $capacidad->capacidad }}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm">
                            <div class="flex gap-2">
                                <a href="{{ route('capacidades.edit', $capacidad->id) }}"
                                   class="text-blue-600 hover:text-blue-900">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <form action="{{ route('capacidades.destroy', $capacidad->id) }}"
                                      method="POST"
                                      class="inline"
                                      onsubmit="return false;">
                                    @csrf
                                    @method('DELETE')

                                    <button type="button"
                                            onclick="confirmarEliminacion(this)"
                                            class="text-red-600 hover:text-red-900">
                                        <i class="fas fa-trash"></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); // Tiempo para remover el elemento después de la transición
    }
}, 3000); // Tiempo en milisegundos antes de comenzar a desaparecer

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').onsubmit = null;
            button.closest('form').submit();
        }
    });
}
</script>
@endsection