@extends('layouts.app')

@section('title', 'Fractionals')

@section('content')
<div class="max-w-7xl mx-auto py-10 sm:px-6 lg:px-8">

    {{-- Título y botón --}}
    <div class="flex justify-between items-center mb-6">
        <h2 class="text-2xl font-semibold text-gray-800">Fractionals</h2>
        <a href="{{ route('fractionals.create') }}"
           class="inline-flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white text-sm font-semibold rounded-lg shadow-md hover:bg-indigo-700 transition duration-150">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
            </svg>
            Nuevo Fractional
        </a>
    </div>

    {{-- Mensaje de éxito --}}
    @if (session('success'))
        <div class="mb-4 p-4 bg-green-100 text-green-800 rounded">
            {{ session('success') }}
        </div>
    @endif

    {{-- Tabla --}}
    <div class="overflow-x-auto bg-white rounded shadow">
        <table class="min-w-full divide-y divide-gray-200 text-sm">
            <thead class="bg-gray-50">
                <tr>
                    <th class="px-4 py-2 text-left font-medium text-gray-500 uppercase">Imagen</th>
                    <th class="px-4 py-2 text-left font-medium text-gray-500 uppercase">Nombre</th>
                    <th class="px-4 py-2 text-left font-medium text-gray-500 uppercase">Sección</th>
                    <th class="px-4 py-2 text-left font-medium text-gray-500 uppercase">Proyecto</th>
                    <th class="px-4 py-2 text-left font-medium text-gray-500 uppercase">Estado</th>
                    <th class="px-4 py-2 text-left font-medium text-gray-500 uppercase">Acciones</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200">
                @forelse ($fractionals as $fractional)
                    <tr>
                        {{-- Imagen --}}
                        <td class="px-4 py-2">
                            @if($fractional->image)
                                <img src="{{ asset('storage/' . ltrim($fractional->image->path, '/')) }}"
                                     alt="{{ $fractional->image->alt ?? $fractional->name }}"
                                     class="w-14 h-14 object-cover rounded border">
                            @else
                                <span class="text-xs text-gray-400">Sin imagen</span>
                            @endif
                        </td>

                        {{-- Nombre --}}
                        <td class="px-4 py-2">{{ $fractional->name }}</td>

                        {{-- Sección --}}
                        <td class="px-4 py-2">
                            {{ optional($fractional->section)->name ?? '—' }}
                        </td>

                        {{-- Proyecto (vía section->project) --}}
                        <td class="px-4 py-2">
                            {{ optional(optional($fractional->section)->project)->name ?? '—' }}
                        </td>

                        {{-- Estado con badge (igual que Properties) --}}
                        <td class="px-4 py-2">
                            @php
                                $statusLabels = [
                                    'available' => ['text' => 'Disponible', 'color' => 'bg-green-100 text-green-800'],
                                    'reserved'  => ['text' => 'Reservado',  'color' => 'bg-yellow-100 text-yellow-800'],
                                    'sold'      => ['text' => 'Vendido',    'color' => 'bg-red-100 text-red-800'],
                                ];
                                $st = $statusLabels[$fractional->status] ?? ['text' => ucfirst($fractional->status), 'color' => 'bg-gray-100 text-gray-800'];
                            @endphp
                            <span class="px-2 py-1 rounded font-bold text-xs {{ $st['color'] }}">
                                {{ $st['text'] }}
                            </span>
                        </td>

                        {{-- Acciones --}}
                        <td class="px-4 py-2 flex gap-2 flex-nowrap items-center">
                            {{-- Clonar --}}
                            <form action="{{ route('fractionals.clone', $fractional) }}" method="POST"
                                  onsubmit="return confirm('¿Clonar este fractional?')">
                                @csrf
                                <button type="submit"
                                        class="inline-flex items-center gap-1 px-3 py-1 bg-yellow-500 text-white rounded hover:bg-yellow-600 text-sm font-medium transition">
                                    <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none"
                                         viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="M8 16h8M8 12h8m-6-8h6a2 2 0 012 2v12a2 2 0 01-2 2H6a2 2 0 01-2-2V8l4-4z" />
                                    </svg>
                                    Clonar
                                </button>
                            </form>

                            {{-- Ver --}}
                            <a href="{{ route('fractionals.show', $fractional) }}"
                               class="inline-flex items-center gap-1 px-3 py-1 bg-gray-200 text-gray-700 rounded hover:bg-gray-300 text-sm font-medium transition">
                                <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none"
                                     viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                                    <path stroke-linecap="round" stroke-linejoin="round"
                                          d="M2.458 12C3.732 7.943 7.523 5 12 5c4.477 0 8.268 2.943 9.542 7-1.274 4.057-5.065 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                                </svg>
                                Ver
                            </a>

                            {{-- Editar --}}
                            <a href="{{ route('fractionals.edit', $fractional) }}"
                               class="inline-flex items-center gap-1 px-3 py-1 bg-blue-600 text-white rounded hover:bg-blue-700 text-sm font-medium transition">
                                <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none"
                                     viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
                                    <path stroke-linecap="round" stroke-linejoin="round"
                                          d="M16.862 4.487a2.1 2.1 0 113.02 2.92L6.578 20.71l-3.712.793.793-3.713L16.862 4.487z" />
                                </svg>
                                Editar
                            </a>

                            {{-- Eliminar --}}
                            <form action="{{ route('fractionals.destroy', $fractional) }}" method="POST"
                                  onsubmit="return confirm('¿Seguro de eliminar este fractional?')">
                                @csrf
                                @method('DELETE')
                                <button type="submit"
                                        class="inline-flex items-center gap-1 px-3 py-1 bg-red-600 text-white rounded hover:bg-red-700 text-sm font-medium transition">
                                    <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none"
                                         viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
                                        <path stroke-linecap="round" stroke-linejoin="round"
                                              d="M6 18L18 6M6 6l12 12" />
                                    </svg>
                                    Eliminar
                                </button>
                            </form>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="6" class="px-4 py-6 text-center text-gray-500">
                            No hay fractionals registrados.
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>
@endsection
