@extends('layouts.app')
@section('title', 'Categorías de Activos')
@section('content')

<div class="flex items-center justify-between mb-6">
    <div>
        <h1 class="text-2xl font-bold text-gray-900">Categorías de Activos</h1>
        <p class="text-sm text-gray-500 mt-1">Gestión de tipos de activos tecnológicos</p>
    </div>
</div>

@if(session('success'))
<div class="mb-4 p-4 bg-green-50 border border-green-200 rounded-xl text-green-800 text-sm">
    {{ session('success') }}
</div>
@endif

<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
    {{-- Formulario nueva categoría --}}
    <div class="bg-white rounded-xl border border-gray-200 shadow-sm p-6">
        <h2 class="text-sm font-semibold text-gray-700 mb-4">Nueva categoría</h2>
        <form method="POST" action="{{ route('asset-categories.store') }}">
            @csrf
            <div class="space-y-4">
                <div>
                    <label class="block text-xs font-medium text-gray-700 mb-1">Nombre *</label>
                    <input type="text" name="name" required
                           class="w-full rounded-lg border-gray-300 text-sm focus:ring-blue-500 focus:border-blue-500">
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-700 mb-1">Icono</label>
                    <input type="text" name="icon" placeholder="ej: computer"
                           class="w-full rounded-lg border-gray-300 text-sm focus:ring-blue-500 focus:border-blue-500">
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-700 mb-1">Color</label>
                    <input type="color" name="color" value="#3b82f6"
                           class="w-full h-9 rounded-lg border-gray-300 cursor-pointer">
                </div>
                <div class="flex items-center gap-2">
                    <input type="checkbox" name="active" value="1" checked id="active-new"
                           class="rounded border-gray-300 text-blue-600">
                    <label for="active-new" class="text-xs text-gray-700">Activa</label>
                </div>
                <button type="submit"
                        class="w-full py-2 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 transition">
                    Crear categoría
                </button>
            </div>
        </form>
    </div>

    {{-- Lista de categorías --}}
    <div class="lg:col-span-2">
        <div class="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 border-b border-gray-200">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Categoría</th>
                        <th class="px-4 py-3 text-center text-xs font-semibold text-gray-500 uppercase">Activos</th>
                        <th class="px-4 py-3 text-center text-xs font-semibold text-gray-500 uppercase">Estado</th>
                        <th class="px-4 py-3 text-center text-xs font-semibold text-gray-500 uppercase">Acción</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($categories as $cat)
                    <tr>
                        <td class="px-4 py-3">
                            <div class="flex items-center gap-2">
                                <div class="w-3 h-3 rounded-full" style="background-color: {{ $cat->color ?? '#3b82f6' }}"></div>
                                <span class="font-medium text-gray-900">{{ $cat->name }}</span>
                            </div>
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600">{{ $cat->assets_count }}</td>
                        <td class="px-4 py-3 text-center">
                            <span class="inline-flex px-2 py-0.5 rounded-full text-xs font-medium
                                {{ $cat->active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-500' }}">
                                {{ $cat->active ? 'Activa' : 'Inactiva' }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center">
                            <form method="POST" action="{{ route('asset-categories.update', $cat) }}" class="inline">
                                @csrf @method('PATCH')
                                <input type="hidden" name="name" value="{{ $cat->name }}">
                                <input type="hidden" name="active" value="{{ $cat->active ? '0' : '1' }}">
                                <button type="submit" class="text-xs text-blue-600 hover:underline">
                                    {{ $cat->active ? 'Desactivar' : 'Activar' }}
                                </button>
                            </form>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="4" class="px-4 py-8 text-center text-gray-400 text-sm">
                            No hay categorías creadas aún
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
