@extends('companies.layout')

@section('company-content')


    <!-- Header -->
    <div class="mb-8">
        <div class="flex items-center gap-4 mb-4">
            <flux:icon.building-office class="size-10" variant="solid"/>
            <h1 class="text-3xl font-bold text-gray-800">
                {{ (isset($company) && $company->id) ? 'Editar Empresa' : 'Nueva Empresa' }}
            </h1>
        </div>
        <p class="text-gray-600 text-sm">
            @if(isset($user) && $user->id)
                Modifique los campos necesarios para actualizar la información del usuario.
            @else
                Complete todos los campos obligatorios para registrar un nuevo usuario en el sistema.
            @endisset
        </p>
    </div>
    
    <form action="{{ isset($company) ? route('companies.update', $company) : route('companies.store') }}" 
          method="POST" 
          enctype="multipart/form-data"
          class="p-6">
        @csrf
        @if(isset($company))
            @method('PUT')
        @endif

        <!-- Separador -->
        <div class="relative">
            <div class="absolute inset-0 flex items-center">
                <div class="w-full border-t border-gray-300"></div>
            </div>
            <div class="relative flex justify-center">
                <span class="px-4 bg-white text-sm text-gray-500">Datos Personales</span>
            </div>
        </div>

        <!-- Datos Personales -->
        <div class="bg-white py-6">
            <table class="w-full">
                <tbody class="space-y-4">
                    <!-- Nombre -->
                    <tr>
                        <td class="py-2 pr-4">
                            <label class="block text-sm font-bold text-gray-700">
                                Nombre
                            </label>
                        </td>
                        <td class="py-2">
                            <input type="text" name="name" id="name" 
                                value="{{ old('name', $company->name ?? '') }}"
                                required
                                class="w-[500px] border-b-1 border-gray-300 focus:border-blue-500 focus:outline-none" 
                                autofocus>
                            @error('name')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </td>
                    </tr>

                    <!-- Nombre comercial -->
                    <tr>
                        <td class="py-2 pr-4">
                            <label class="block text-sm text-gray-700">
                                Nombre Comercial
                            </label>
                        </td>
                        <td class="py-2">
                            <input type="text" name="commercial_name" id="commercial_name" 
                                value="{{ old('commercial_name', $company->commercial_name ?? '') }}"
                                required
                                class="w-[500px] border-b-1 border-gray-300 focus:border-blue-500 focus:outline-none">
                            @error('commercial_name')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </td>
                    </tr>

                    <!-- CIF -->
                    <tr>
                        <td class="py-2 pr-4">
                            <label class="block text-sm text-gray-700">
                                CIF
                            </label>
                        </td>
                        <td class="py-2">
                            <input type="text" name="cif" id="cif" 
                                value="{{ old('cif', $company->cif ?? '') }}"
                                class="w-[250px] border-b-1 border-gray-300 focus:border-blue-500 focus:outline-none">
                            @error('cif')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </td>
                    </tr>

                    <!-- Estado -->
                    <tr>
                        <td class="py-2 pr-4">
                            <label class="block text-sm text-gray-700">
                                Estado
                            </label>
                        </td>
                        <td class="py-2">
                            <select name="status" id="status" 
                                required
                                class="w-[100px] border-b-1 border-gray-300 focus:border-blue-500 focus:outline-none">
                                <option value="active" {{ (old('status', $company->status ?? '') == 'active' ? 'selected' : '') }}>
                                    Activo
                                </option>
                                <option value="closed" {{ (old('status', $company->status ?? '') == 'closed' ? 'selected' : '') }}>
                                    Cerrado
                                </option>
                            </select>
                            @error('status')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

        <!-- Separador -->
        <div class="relative">
            <div class="absolute inset-0 flex items-center">
                <div class="w-full border-t border-gray-300"></div>
            </div>
            <div class="relative flex justify-center">
                <span class="px-4 bg-white text-sm text-gray-500">Configuración de acceso</span>
            </div>
        </div>

        <!-- Datos Personales -->
        <div class="bg-white py-6">
            <table class="w-full">
                <tbody class="space-y-4">
                    <!-- Dirección -->
                    <tr>
                        <td class="py-2 pr-4">
                            <label class="block text-sm font-bold text-gray-700">
                                Dirección
                            </label>
                        </td>
                        <td class="py-2">
                            <input type="text" name="address" id="address" 
                                value="{{ old('address', $company->address ?? '') }}"
                                class="w-full border-b-1 border-gray-300 focus:border-blue-500 focus:outline-none">
                            @error('address')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </td>
                    </tr>

                    <!-- Código postal -->
                    <tr>
                        <td class="py-2 pr-4">
                            <label class="block text-sm text-gray-700">
                                Código Postal
                            </label>
                        </td>
                        <td class="py-2">
                            <input type="text" name="postal_code" id="postal_code" 
                                value="{{ old('postal_code', $company->postal_code ?? '') }}"
                                class="w-[250px] border-b-1 border-gray-300 focus:border-blue-500 focus:outline-none">
                            @error('postal_code')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </td>
                    </tr>

                    <!-- Ciudad -->
                    <tr>
                        <td class="py-2 pr-4">
                            <label class="block text-sm text-gray-700">
                                Ciudad
                            </label>
                        </td>
                        <td class="py-2">
                            <input type="text" name="city" id="city" 
                                value="{{ old('city', $company->city ?? '') }}"
                                class="w-[250px] border-b-1 border-gray-300 focus:border-blue-500 focus:outline-none">
                            @error('city')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </td>
                    </tr>

                    <!-- Estado -->
                    <tr>
                        <td class="py-2 pr-4">
                            <label class="block text-sm text-gray-700">
                                Pais
                            </label>
                        </td>
                        <td class="py-2">
                            <input type="text" name="country" id="country" 
                                value="{{ old('country', $company->country ?? '') }}"
                                class="w-[250px] border-b-1 border-gray-300 focus:border-blue-500 focus:outline-none">
                            @error('country')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </td>
                    </tr>

                    <!-- Teléfono -->
                    <tr>
                        <td class="py-2 pr-4">
                            <label class="block text-sm text-gray-700">
                                Teléfono
                            </label>
                        </td>
                        <td class="py-2">
                            <input type="text" name="phone" id="phone" 
                                value="{{ old('phone', $company->phone ?? '') }}"
                                class="w-[250px] border-b-1 border-gray-300 focus:border-blue-500 focus:outline-none">
                            @error('phone')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </td>
                    </tr>

                    <!-- email -->
                    <tr>
                        <td class="py-2 pr-4">
                            <label class="block text-sm text-gray-700">
                                email
                            </label>
                        </td>
                        <td class="py-2">
                            <input type="email" name="email" id="email" 
                                value="{{ old('email', $company->email ?? '') }}"
                                class="w-[250px] border-b-1 border-gray-300 focus:border-blue-500 focus:outline-none">
                            @error('email')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </td>
                    </tr>

                    <!-- logo -->
                    <tr>
                        <td class="py-2 pr-4">
                            <label class="block text-sm text-gray-700">
                                Logo
                            </label>
                        </td>
                        <td class="py-2">
                            <input type="file" name="logo" id="logo" 
                                class="mt-1 block w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-md file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100">
                            @error('logo')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                            
                            @if(isset($company) && $company->logo)
                                <div class="mt-2 flex items-center">
                                    <img src="{{ Storage::disk('public')->url($company->logo) }}" 
                                        alt="Logo actual" 
                                        class="h-16 w-16 rounded object-contain border">
                                    <div class="ml-4">
                                        <label class="flex items-center">
                                            <input type="checkbox" name="remove_logo" class="rounded">
                                            <span class="ml-2 text-sm text-gray-600">Eliminar logo</span>
                                        </label>
                                    </div>
                                </div>
                            @endif
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        
        <div class="flex justify-end space-x-3 pt-6 border-t">
            <a href="{{ route('companies.index') }}" class="btn btn-secondary">
                Cancelar
            </a>
            <button type="submit" class="btn btn-primary">
                {{ isset($company) ? 'Actualizar Empresa' : 'Crear Empresa' }}
            </button>
        </div>
        
    </form>
@endsection