@extends('admin.layout')

@section('title', (isset($sponsor) ? 'Editar' : 'Nuevo') . ' Sponsor - Admin OnAPB')

@section('content')
<div class="page-header">
    <h2><i class="bi bi-star-fill text-warning"></i> {{ isset($sponsor) ? 'Editar' : 'Nuevo' }} Sponsor</h2>
    <a href="{{ route('admin.sponsors.index') }}" class="btn-admin-outline">
        <i class="bi bi-arrow-left"></i> Volver
    </a>
</div>

<div class="admin-card">
    <div class="card-body">
        <form action="{{ isset($sponsor) ? route('admin.sponsors.update', $sponsor->id_sponsor) : route('admin.sponsors.store') }}" method="POST" enctype="multipart/form-data">
            @csrf
            @if(isset($sponsor))
                @method('PUT')
            @endif

            <div class="row">
                <div class="col-md-8">
                    <div class="mb-3">
                        <label class="form-label">Nombre del Sponsor *</label>
                        <input type="text" name="nombre" class="form-control" value="{{ old('nombre', $sponsor->nombre ?? '') }}" required maxLength="100">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">URL del sitio (opcional)</label>
                        <input type="url" name="url" class="form-control" value="{{ old('url', $sponsor->url ?? '') }}" placeholder="https://example.com">
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Orden de aparición</label>
                                <input type="number" name="orden" class="form-control" value="{{ old('orden', $sponsor->orden ?? 0) }}">
                                <small class="text-muted">Menor número aparece primero.</small>
                            </div>
                        </div>
                        <div class="col-md-6 pt-4">
                            <div class="form-check form-switch mt-2">
                                <input class="form-check-input" type="checkbox" name="activo" id="activo" value="1" {{ old('activo', $sponsor->activo ?? true) ? 'checked' : '' }}>
                                <label class="form-check-label" for="activo">Sponsor Activo</label>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Logo / Imagen *</label>
                        <input type="file" name="imagen" class="form-control" {{ isset($sponsor) ? '' : 'required' }} accept="image/*">
                        <div class="mt-3 text-center border p-3 rounded bg-light" style="min-height: 150px; display: flex; align-items: center; justify-content: center;">
                            @if(isset($sponsor) && $sponsor->imagen)
                                <img src="{{ asset($sponsor->imagen) }}" id="preview" style="max-width: 100%; max-height: 120px; object-fit: contain;">
                            @else
                                <div id="no-preview" class="text-muted">
                                    <i class="bi bi-image fs-1 d-block"></i>
                                    Vista previa
                                </div>
                                <img src="#" id="preview" style="max-width: 100%; max-height: 120px; object-fit: contain; display: none;">
                            @endif
                        </div>
                        <small class="text-muted d-block mt-2 text-center">Formato recomendado: PNG transparente o fondo blanco.</small>
                    </div>
                </div>
            </div>

            <hr>
            <div class="d-flex justify-content-end gap-2">
                <button type="submit" class="btn btn-primary px-4">
                    <i class="bi bi-save"></i> {{ isset($sponsor) ? 'Actualizar' : 'Guardar' }} Sponsor
                </button>
            </div>
        </form>
    </div>
</div>

@section('scripts')
<script>
    document.querySelector('input[name="imagen"]').addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(event) {
                const preview = document.getElementById('preview');
                const noPreview = document.getElementById('no-preview');
                preview.src = event.target.result;
                preview.style.display = 'block';
                if (noPreview) noPreview.style.display = 'none';
            };
            reader.readAsDataURL(file);
        }
    });
</script>
@endsection
@endsection
