@extends('layouts.dashboard')

@section('content')
<h2>Editar Empresa</h2>

<form action="{{ route('superadmin.empresas.update', $empresa) }}" method="POST" enctype="multipart/form-data">
    @csrf
    @method('PUT')

    <div class="mb-3">
        <label class="form-label">Nombre</label>
        <input type="text" name="nombre" class="form-control" value="{{ $empresa->nombre }}" required>
    </div>

    <div class="mb-3">
        <label class="form-label">RUT</label>
        <input type="text" name="rut" class="form-control" value="{{ $empresa->rut }}" required>
    </div>

    <div class="mb-3">
        <label class="form-label">Teléfono</label>
        <input type="text" name="telefono" class="form-control" value="{{ $empresa->telefono }}">
    </div>

    <div class="mb-3">
        <label class="form-label">Email de contacto</label>
        <input type="email" name="email_contacto" class="form-control" value="{{ $empresa->email_contacto }}">
    </div>

    <div class="mb-3">
        <label class="form-label">Estado</label>
        <select name="estado" class="form-select">
            <option value="activo" {{ $empresa->estado === 'activo' ? 'selected' : '' }}>Activo</option>
            <option value="inactivo" {{ $empresa->estado === 'inactivo' ? 'selected' : '' }}>Inactivo</option>
        </select>
    </div>

    <div class="mb-3">
        <label class="form-label">Logo</label>
        <input type="file" name="logo" class="form-control" accept="image/*" onchange="previewLogo(this)">

        @if($empresa->logo)
            <img id="preview" src="{{ asset('storage/' . $empresa->logo) }}" class="mt-2" style="max-height: 100px;">
        @else
            <img id="preview" class="mt-2" style="max-height: 100px;">
        @endif
    </div>

    <button type="submit" class="btn btn-primary">Actualizar</button>
    <a href="{{ route('superadmin.empresas.index') }}" class="btn btn-secondary">Cancelar</a>
</form>

<script>
function previewLogo(input) {
    const preview = document.getElementById('preview');
    if (input.files && input.files[0]) {
        const reader = new FileReader();
        reader.onload = e => preview.src = e.target.result;
        reader.readAsDataURL(input.files[0]);
    }
}
</script>
@endsection
