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

<div class="mb-6">
    <h1 class="text-2xl font-bold text-gray-900">Categorías de Tickets</h1>
    <p class="text-sm text-gray-500 mt-1">Organiza los tipos de solicitudes</p>
</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">
    {{-- Nueva categoría --}}
    <div class="bg-white rounded-xl border border-gray-200 shadow-sm p-5">
        <h2 class="text-sm font-semibold text-gray-700 mb-4">Nueva categoría</h2>
        <form method="POST" action="{{ route('ticket-categories.store') }}">
            @csrf
            <div class="space-y-3">
                <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">Categoría padre</label>
                    <select name="parent_id" class="w-full rounded-lg border-gray-300 text-sm focus:ring-blue-500 focus:border-blue-500">
                        <option value="">— Ninguna (categoría raíz) —</option>
                        @foreach($categories->whereNull('parent_id') as $cat)
                        <option value="{{ $cat->id }}">{{ $cat->name }}</option>
                        @endforeach
                    </select>
                </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 --}}
    <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-left text-xs font-semibold text-gray-500 uppercase">Padre</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 font-medium text-gray-900">
                            {{ $cat->parent_id ? '└ ' : '' }}{{ $cat->name }}
                        </td>
                        <td class="px-4 py-3 text-gray-500 text-xs">
                            {{ $cat->parent?->name ?? '—' }}
                        </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('ticket-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">Sin categorías</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
