@extends('layouts.app')
@section('title', 'Estados de Tickets')
@section('content')

<div class="mb-6">
    <h1 class="text-2xl font-bold text-gray-900">Estados de Tickets</h1>
    <p class="text-sm text-gray-500 mt-1">Define los estados del flujo de atención</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">
    {{-- Nuevo estado --}}
    <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">Nuevo estado</h2>
        <form method="POST" action="{{ route('ticket-statuses.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">Color</label>
                    <input type="color" name="color" value="#6b7280"
                           class="w-full h-9 rounded-lg border-gray-300 cursor-pointer">
                </div>
                <div class="flex items-center gap-2">
                    <input type="checkbox" name="is_final" value="1" id="is-final"
                           class="rounded border-gray-300 text-blue-600">
                    <label for="is-final" class="text-xs text-gray-700">Estado final (cierra el ticket)</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 estado
                </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">Estado</th>
                        <th class="px-4 py-3 text-center text-xs font-semibold text-gray-500 uppercase">Tipo</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($statuses as $status)
                    <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: {{ $status->color }}"></div>
                                <span class="font-medium text-gray-900">{{ $status->name }}</span>
                            </div>
                        </td>
                        <td class="px-4 py-3 text-center">
                            @if($status->is_initial)
                                <span class="inline-flex px-2 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-700">Inicial</span>
                            @elseif($status->is_final)
                                <span class="inline-flex px-2 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-700">Final</span>
                            @else
                                <span class="inline-flex px-2 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-600">Intermedio</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            <form method="POST" action="{{ route('ticket-statuses.update', $status) }}" class="inline">
                                @csrf @method('PATCH')
                                <input type="hidden" name="name" value="{{ $status->name }}">
                                <input type="hidden" name="color" value="{{ $status->color }}">
                                <input type="hidden" name="active" value="{{ $status->active ? '0' : '1' }}">
                                <button type="submit" class="text-xs text-blue-600 hover:underline">
                                    {{ $status->active ? 'Desactivar' : 'Activar' }}
                                </button>
                            </form>
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="3" class="px-4 py-8 text-center text-gray-400 text-sm">Sin estados</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
