@extends('layouts.app')

@section('title', 'Nuevo Proyecto')

@section('content')
<div class="max-w-xl mx-auto bg-white p-6 rounded-lg shadow-md">
    <h2 class="text-xl font-bold mb-4 text-gray-800">Nuevo Proyecto</h2>

    @if ($errors->any())
        <div class="mb-4 p-3 bg-red-100 text-red-700 rounded">
            <ul class="list-disc list-inside">
                @foreach ($errors->all() as $error)
                    <li class="text-sm">{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

<form action="{{ route('projects.store') }}" method="POST" enctype="multipart/form-data" class="space-y-5">        @csrf

        {{-- Nombre --}}
        <div>
            <label for="name" class="block text-sm font-medium text-gray-700 mb-1">Nombre del proyecto</label>
            <div class="relative">
                <input type="text" name="name" id="name" required
                    class="w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-indigo-500 focus:border-indigo-500 text-sm"
                    value="{{ old('name') }}">
                <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none text-gray-400">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                    </svg>
                </div>
            </div>
        </div>

        {{-- Descripción --}}
        <div>
            <label for="description" class="block text-sm font-medium text-gray-700 mb-1">Descripción</label>
            <textarea name="description" id="description" rows="3"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-indigo-500 focus:border-indigo-500 text-sm"
                placeholder="Opcional">{{ old('description') }}</textarea>
        </div>

        {{-- Ubicación --}}
        <div>
            <label for="location" class="block text-sm font-medium text-gray-700 mb-1">Ubicación</label>
            <input type="text" name="location" id="location"
                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-indigo-500 focus:border-indigo-500 text-sm"
                value="{{ old('location') }}">
        </div>

        {{-- Imagen --}}
        <div class="mb-4">
            <label class="block font-medium text-sm text-gray-700 mb-1">Imagen del Proyecto</label>
            <div class="grid grid-cols-3 gap-4">
                @foreach ($images as $img)
                <label class="cursor-pointer block">
                    <input type="radio" name="image_id" value="{{ $img->id }}"
                        class="hidden peer"
                        {{ old('image_id', optional($project ?? null)->image_id) == $img->id ? 'checked' : '' }}>
                    <div class="border-2 rounded overflow-hidden peer-checked:border-blue-600">
                        <img src="{{ asset('storage/' . $img->path) }}" alt="{{ $img->alt }}" class="object-cover w-full h-28">
                        <p class="text-center text-xs p-1 truncate">{{ $img->name }}</p>
                    </div>
                </label>
                @endforeach
            </div>
        </div>


         {{-- Datos de la Empresa --}}
    <hr class="my-6 border-t">
    <h3 class="text-lg font-semibold text-gray-800 mb-4">Datos de la Empresa</h3>

    {{-- Nombre empresa --}}
    <div>
        <label for="company_name" class="block text-sm font-medium text-gray-700 mb-1">Nombre de la empresa</label>
        <input type="text" name="company_name" id="company_name"
            class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-indigo-500 focus:border-indigo-500 text-sm"
            value="{{ old('company_name') }}">
    </div>

    {{-- Teléfono empresa --}}
    <div>
        <label for="company_phone" class="block text-sm font-medium text-gray-700 mt-4 mb-1">Teléfono</label>
        <input type="text" name="company_phone" id="company_phone"
            class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-indigo-500 focus:border-indigo-500 text-sm"
            value="{{ old('company_phone') }}">
    </div>

    {{-- Email empresa --}}
    <div>
        <label for="company_email" class="block text-sm font-medium text-gray-700 mt-4 mb-1">Correo electrónico</label>
        <input type="email" name="company_email" id="company_email"
            class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-indigo-500 focus:border-indigo-500 text-sm"
            value="{{ old('company_email') }}">
    </div>

    {{-- Dirección empresa --}}
    <div>
        <label for="company_address" class="block text-sm font-medium text-gray-700 mt-4 mb-1">Dirección</label>
        <input type="text" name="company_address" id="company_address"
            class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-indigo-500 focus:border-indigo-500 text-sm"
            value="{{ old('company_address') }}">
    </div>

    {{-- Descripción empresa --}}
    <div>
        <label for="company_description" class="block text-sm font-medium text-gray-700 mt-4 mb-1">Descripción de la empresa</label>
        <textarea name="company_description" id="company_description" rows="3"
            class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-indigo-500 focus:border-indigo-500 text-sm"
            placeholder="Descripción breve">{{ old('company_description') }}</textarea>
    </div>

    {{-- Subir logotipo --}}
    <div class="mt-4">
        <label for="company_logo" class="block text-sm font-medium text-gray-700 mb-1">Logotipo</label>
        <input type="file" name="company_logo" id="company_logo"
            class="w-full text-sm border border-gray-300 rounded-lg py-2 px-3">
    </div>


        {{-- Botones --}}
        <div class="flex justify-end gap-3 pt-4">
            <a href="{{ route('projects.index') }}"
               class="px-4 py-2 bg-gray-200 text-gray-700 rounded hover:bg-gray-300 text-sm">Cancelar</a>
            <button type="submit"
               class="px-4 py-2 bg-indigo-600 text-white rounded hover:bg-indigo-700 text-sm font-semibold">
               Guardar Proyecto
            </button>
        </div>
    </form>
</div>
@endsection
