@extends('layouts.app')

@section('title', 'Editar Empresa')
@section('page-title', 'Editar Empresa')

@section('content')
<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header bg-white d-flex justify-content-between align-items-center">
                <h5 class="mb-0">Editar: {{ $company->name }}</h5>
                <a href="{{ route('companies.show', $company) }}" class="btn btn-sm btn-outline-secondary">
                    <i class="bi bi-arrow-left"></i> Volver
                </a>
            </div>
            <div class="card-body">
                <form action="{{ route('companies.update', $company) }}" method="POST">
                    @csrf
                    @method('PUT')
                    
                    <div class="mb-3">
                        <label for="name" class="form-label">
                            Nombre de la Empresa <span class="text-danger">*</span>
                        </label>
                        <input 
                            type="text" 
                            class="form-control @error('name') is-invalid @enderror" 
                            id="name" 
                            name="name" 
                            value="{{ old('name', $company->name) }}" 
                            required
                            placeholder="Ej: Empresa ABC S.A."
                        >
                        @error('name')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="contact_email" class="form-label">
                                Email de Contacto
                            </label>
                            <input 
                                type="email" 
                                class="form-control @error('contact_email') is-invalid @enderror" 
                                id="contact_email" 
                                name="contact_email" 
                                value="{{ old('contact_email', $company->contact_email) }}"
                                placeholder="contacto@empresa.com"
                            >
                            @error('contact_email')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="contact_phone" class="form-label">
                                Teléfono de Contacto
                            </label>
                            <input 
                                type="text" 
                                class="form-control @error('contact_phone') is-invalid @enderror" 
                                id="contact_phone" 
                                name="contact_phone" 
                                value="{{ old('contact_phone', $company->contact_phone) }}"
                                placeholder="+502 2345-6789"
                            >
                            @error('contact_phone')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>

                    <div class="mb-3">
                        <label for="address" class="form-label">
                            Dirección
                        </label>
                        <textarea 
                            class="form-control @error('address') is-invalid @enderror" 
                            id="address" 
                            name="address" 
                            rows="3"
                            placeholder="Dirección completa de la empresa"
                        >{{ old('address', $company->address) }}</textarea>
                        @error('address')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="mb-3">
                        <label for="is_active" class="form-label">
                            Estado <span class="text-danger">*</span>
                        </label>
                        <select 
                            class="form-select @error('is_active') is-invalid @enderror" 
                            id="is_active" 
                            name="is_active"
                            required
                        >
                            <option value="1" {{ old('is_active', $company->is_active ? '1' : '0') == '1' ? 'selected' : '' }}>Activa</option>
                            <option value="0" {{ old('is_active', $company->is_active ? '1' : '0') == '0' ? 'selected' : '' }}>Inactiva</option>
                        </select>
                        @error('is_active')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                        <div class="form-text">
                            Solo las empresas activas pueden crear proyectos y tickets
                        </div>
                    </div>

                    <hr class="my-4">

                    <div class="d-flex justify-content-between align-items-center">
                        <button 
                            type="button" 
                            class="btn btn-danger"
                            onclick="if(confirm('¿Estás seguro de eliminar esta empresa? Esta acción no se puede deshacer.')) { document.getElementById('delete-form').submit(); }"
                        >
                            <i class="bi bi-trash"></i> Eliminar Empresa
                        </button>

                        <div class="d-flex gap-2">
                            <a href="{{ route('companies.show', $company) }}" class="btn btn-secondary">
                                <i class="bi bi-x-circle"></i> Cancelar
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-check-circle"></i> Guardar Cambios
                            </button>
                        </div>
                    </div>
                </form>

                <!-- Formulario de eliminación -->
                <form id="delete-form" action="{{ route('companies.destroy', $company) }}" method="POST" class="d-none">
                    @csrf
                    @method('DELETE')
                </form>
            </div>
        </div>

        <!-- Advertencia sobre eliminación -->
        @if($company->projects->count() > 0 || $company->users->count() > 0 || $company->tickets->count() > 0)
        <div class="alert alert-warning mt-3">
            <i class="bi bi-exclamation-triangle me-2"></i>
            <strong>Advertencia:</strong> Esta empresa tiene:
            <ul class="mb-0 mt-2">
                @if($company->projects->count() > 0)
                <li>{{ $company->projects->count() }} proyecto(s) asociado(s)</li>
                @endif
                @if($company->users->count() > 0)
                <li>{{ $company->users->count() }} usuario(s) asociado(s)</li>
                @endif
                @if($company->tickets->count() > 0)
                <li>{{ $company->tickets->count() }} ticket(s) asociado(s)</li>
                @endif
            </ul>
            <p class="mt-2 mb-0">Se recomienda marcar como "Inactiva" en lugar de eliminar para preservar el historial.</p>
        </div>
        @endif
    </div>
</div>
@endsection

@push('styles')
<style>
    .form-label {
        font-weight: 500;
        color: #495057;
    }
    
    .card {
        box-shadow: 0 0.125rem 0.25rem rgba(0,0,0,0.075);
    }
</style>
@endpush
