@extends('layouts.app')

@section('title', 'Propiedades')

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

    <div class="flex justify-between items-center mb-6">
        <h2 class="text-2xl font-semibold text-gray-800">Propiedades</h2>
        <a href="{{ route('properties.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>
            Nueva Propiedad
        </a>
    </div>

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

    <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">Código</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">Fractional</th>
        <th class="px-4 py-2 text-left font-medium text-gray-500 uppercase">Tipo de Propiedad</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">
    @foreach ($properties as $property)
        <tr>
            <td class="px-4 py-2">{{ $property->code }}</td>
            <td class="px-4 py-2">{{ $property->name }}</td>
            <td class="px-4 py-2">
                {{ optional($property->fractional)->name ?? '—' }}
            </td>
            <td class="px-4 py-2">
                {{ optional($property->propertyType)->name ?? '—' }}
            </td>

            <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' => 'Vendida',   'color' => 'bg-red-100 text-red-800'],
                    ];
                    $status = $statusLabels[$property->status] ?? ['text' => ucfirst($property->status), 'color' => 'bg-gray-100 text-gray-800'];
                @endphp

                <span class="px-2 py-1 rounded font-bold text-xs {{ $status['color'] }}">
                    {{ $status['text'] }}
                </span>
            </td>


            <td class="px-4 py-2 flex gap-2">

            {{-- Clonar --}}
<form action="{{ route('properties.clone', $property) }}" method="POST"
    onsubmit="return confirm('¿Clonar esta propiedad?')">
    @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>


                {{-- Botón Editar --}}
                <a href="{{ route('properties.edit', $property) }}"
                    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>

                {{-- Botón Eliminar --}}
                <form action="{{ route('properties.destroy', $property) }}" method="POST"
                    onsubmit="return confirm('¿Eliminar esta propiedad?')">
                    @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>
    @endforeach
</tbody>

            </tbody>
        </table>
    </div>

</div>
@endsection
