@extends('admin.layout')

@section('title', ($club ? 'Editar' : 'Nuevo') . ' Club - Admin OnAPB')

@section('content')
<div class="page-header">
    <h2><i class="bi bi-shield-fill"></i> {{ $club ? 'Editar Club' : 'Nuevo Club' }}</h2>
    <a href="{{ session('admin_role') == 1 ? route('admin.clubes.index') : route('admin.dashboard') }}" class="btn-admin-outline">
        <i class="bi bi-arrow-left"></i> Volver
    </a>
</div>

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

            <div class="row">
                <!-- Columna Formulario -->
                <div class="col-lg-7">
                    <div class="mb-4">
                        <label class="form-label">Nombre del Club *</label>
                        <input type="text" name="nombre" class="form-control" value="{{ old('nombre', $club->nombre ?? '') }}" required {{ session('admin_role') == 2 ? 'readonly' : '' }}>
                        @error('nombre')
                            <div class="text-danger small mt-1">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="mb-4">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="es_seleccion" id="es_seleccion" value="1"
                                {{ old('es_seleccion', $club->es_seleccion ?? false) ? 'checked' : '' }}
                                {{ session('admin_role') == 2 ? 'disabled' : '' }}>
                            <label class="form-check-label fw-bold" for="es_seleccion">
                                <i class="bi bi-star-fill text-warning me-1"></i> Es Selección
                            </label>
                            <div class="text-muted small mt-1">Los jugadores pueden pertenecer a su club habitual y además ser convocados a esta selección.</div>
                        </div>
                    </div>

                    <div class="card bg-light border-0 mb-4">
                        <div class="card-body">
                            <h5 class="mb-3 border-bottom pb-2">Identidad del Club</h5>
                            <div class="row align-items-center">
                                <div class="col-md-8">
                                    <label class="form-label"><i class="bi bi-shield-shaded"></i> Escudo / Logo del Club</label>
                                    <input type="file" name="logo_club" class="form-control" accept="image/*">
                                    <small class="text-muted d-block mt-1">Se mostrará en la cartelera de partidos y perfiles.</small>
                                    @error('logo_club')
                                        <div class="text-danger small mt-1">{{ $message }}</div>
                                    @enderror
                                </div>
                                <div class="col-md-4 text-center">
                                    <div class="p-2 bg-white rounded border d-inline-block shadow-sm">
                                        @if($club && $club->imagen)
                                            <img src="{{ asset($club->imagen) }}" alt="Logo Actual" style="height: 60px; object-fit: contain;">
                                        @else
                                            <i class="bi bi-shield-fill text-muted" style="font-size: 3rem;"></i>
                                        @endif
                                    </div>
                                    <div class="small text-muted mt-1">Logo Actual</div>
                                </div>
                            </div>
                        </div>
                    </div>

                    @if($club)
                    <div class="card bg-light border-0 mb-4">
                        <div class="card-body">
                            <h5 class="mb-3 border-bottom pb-2">Personalización de QR</h5>
                            
                            <div class="mb-4">
                                <label class="form-label"><i class="bi bi-image"></i> Imagen de Fondo (Plantilla QR)</label>
                                <input type="file" name="qr_background" id="qr_background_input" class="form-control" accept="image/*">
                                <small class="text-muted d-block mt-1">Recomendado formato vertical (ej. 1080x1920px).</small>
                                @error('qr_background')
                                    <div class="text-danger small mt-1">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <label class="form-label"><i class="bi bi-palette"></i> Color Texto QR</label>
                                <input type="color" name="qr_color_texto" id="qr_color_input" class="form-control form-control-color w-100" value="{{ old('qr_color_texto', $club->qr_color_texto ?? '#000000') }}" title="Elegir color">
                                <small class="text-muted d-block mt-1">Este color se aplicará al texto sobre la plantilla.</small>
                                @error('qr_color_texto')
                                    <div class="text-danger small mt-1">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>
                    @endif

                    <button type="submit" class="btn-admin w-100 py-3">
                        <i class="bi bi-check-lg"></i> {{ $club ? 'Guardar Cambios' : 'Crear Club' }}
                    </button>
                </div>

                <!-- Columna Preview -->
                <div class="col-lg-5 text-center">
                    <div class="sticky-top" style="top: 20px;">
                        <h6 class="text-muted mb-3 text-uppercase fw-bold" style="font-size: 0.8rem; letter-spacing: 1px;">Vista Previa TICKET</h6>
                        
                        <div id="qr_preview_card" class="mx-auto shadow-lg" 
                             style="width: 320px; height: 500px; border-radius: 25px; overflow: hidden; position: relative; transition: all 0.3s ease;
                                    {{ ($club && $club->qr_background) ? "background: url('".asset($club->qr_background)."') no-repeat center center; background-size: cover;" : "background: #fff; border: 1px solid #ddd;" }}">
                            
                            <div id="preview_overlay" style="position: absolute; inset: 0; background: rgba(255,255,255,0.1); z-index: 1; {{ ($club && $club->qr_background) ? '' : 'display:none;' }}"></div>

                            <div id="preview_content" class="d-flex flex-column align-items-center justify-content-between p-4 h-100 text-center" 
                                 style="position: relative; z-index: 5; color: {{ $club->qr_color_texto ?? '#333' }};">
                                
                                <div class="mt-2 w-100 text-center">
                                    <img src="{{ asset('logo.png') }}" alt="OnAPB" style="height: 40px; filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));">
                                    <h6 class="mt-2 fw-bold" style="font-family: 'Bebas Neue', cursive; letter-spacing: 1px; font-size: 1.2rem;">ENTRADA DIGITAL</h6>
                                </div>

                                <div class="qr-box p-2 bg-white shadow" style="border-radius: 15px;">
                                    <img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=PREVIEW" alt="QR" style="width: 150px; opacity: 0.8;">
                                </div>

                                <div class="w-100 mb-2">
                                    <h6 class="fw-bold mb-0 text-uppercase" style="font-size: 0.9rem;">JUAN PÉREZ</h6>
                                    <p class="small mb-0 opacity-75" style="font-size: 0.7rem;">PARTIDO: LOCAL VS VISITANTE</p>
                                    <p class="small mb-0 opacity-75" style="font-size: 0.7rem; font-weight: bold;">CATEGORÍA: U19 / PRIMERA</p>
                                    <p class="small mb-0 opacity-75" style="font-size: 0.7rem;">FECHA: 00/00/0000</p>
                                </div>
                            </div>
                        </div>
                        <p class="text-muted small mt-3 px-4">Esta es una simulación de cómo verá el jugador su ticket en el celular.</p>
                    </div>
                </div>
            </div>
        </form>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const bgInput = document.getElementById('qr_background_input');
    const colorInput = document.getElementById('qr_color_input');
    const previewCard = document.getElementById('qr_preview_card');
    const previewContent = document.getElementById('preview_content');
    const previewOverlay = document.getElementById('preview_overlay');

    if (bgInput) {
        bgInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(event) {
                    previewCard.style.backgroundImage = `url('${event.target.result}')`;
                    previewCard.style.backgroundSize = 'cover';
                    previewCard.style.backgroundPosition = 'center';
                    previewCard.style.border = 'none';
                    previewOverlay.style.display = 'block';
                }
                reader.readAsDataURL(file);
            }
        });
    }

    if (colorInput) {
        colorInput.addEventListener('input', function(e) {
            previewContent.style.color = e.target.value;
        });
    }
});
</script>
    </div>
</div>
@endsection
