{{-- Panel de una entrada (clonable vía #rt-ticket-template). --}}
@php
    /** @var string|int $index */
    /** @var array<string, mixed> $ticket */
    $pfx = "tickets.{$index}";
    $tid = $ticket['id'] ?? null;
    $ticketCollapseId = 'rt-ticket-collapse-'.$index;
    $hasTicketErrors = false;
    if (is_numeric($index)) {
        $errPrefix = 'tickets.'.(int) $index.'.';
        foreach ($errors->keys() as $errKey) {
            if (str_starts_with((string) $errKey, $errPrefix)) {
                $hasTicketErrors = true;
                break;
            }
        }
    }
    $ticketPanelOpen = (is_numeric($index) && (int) $index === 0) || $hasTicketErrors;
@endphp
<div class="card shadow-sm mb-4 rt-ticket-card" data-rt-ticket-id="{{ $tid ?? '' }}" data-ticket-index="{{ $index }}">
    <div class="card-header d-flex justify-content-between align-items-center py-2 flex-wrap">
        <div class="d-flex align-items-center flex-grow-1 min-w-0 mb-1 mb-sm-0">
            <button type="button" class="btn btn-link btn-sm text-dark p-0 mr-2 rt-ticket-collapse-btn flex-shrink-0" data-toggle="collapse" data-target="#{{ $ticketCollapseId }}" aria-expanded="{{ $ticketPanelOpen ? 'true' : 'false' }}" aria-controls="{{ $ticketCollapseId }}" title="{{ __('Desplegar o plegar detalle de la entrada') }}">
                <i class="bi bi-chevron-down small rt-ticket-chevron @if ($ticketPanelOpen) rt-chevron-open @endif" aria-hidden="true"></i>
            </button>
            <strong class="text-dark rt-ticket-card-label text-truncate">@if (is_numeric($index)){{ __('Entrada :num', ['num' => (int) $index + 1]) }}@else{{ __('Nueva entrada') }}@endif</strong>
        </div>
        <button type="button" class="btn btn-sm rt-btn-cherry-outline rt-remove-ticket flex-shrink-0" title="{{ __('Quitar esta entrada') }}">
            <i class="bi bi-x-lg" aria-hidden="true"></i> {{ __('Quitar') }}
        </button>
    </div>
    <div id="{{ $ticketCollapseId }}" class="collapse @if ($ticketPanelOpen) show @endif">
    <div class="card-body border-top">
        @if($tid)
            <input type="hidden" name="tickets[{{ $index }}][id]" value="{{ $tid }}">
        @endif

        <div class="rt-form-subsection mb-3">
            <h6 class="text-uppercase text-muted small font-weight-bold mb-2">{{ __('Nombre visible') }} <span class="text-danger">*</span></h6>
            <div class="form-group">
                <label class="font-weight-bold" for="ticket-name-{{ $index }}">{{ __('Nombre de la entrada') }}</label>
                <input id="ticket-name-{{ $index }}" class="form-control" type="text" name="tickets[{{ $index }}][name]"
                       value="{{ old($pfx.'.name', $ticket['name'] ?? '') }}" required maxlength="255"
                       placeholder="{{ __('Ej.: 5K, 10K, 21K — o General, VIP, Tribuna') }}">
            </div>

            <div class="form-group mb-0">
                <label class="font-weight-bold" for="ticket-desc-{{ $index }}">{{ __('Descripción (opcional)') }}</label>
                <input id="ticket-desc-{{ $index }}" class="form-control" type="text" name="tickets[{{ $index }}][description]"
                       value="{{ old($pfx.'.description', $ticket['description'] ?? '') }}" maxlength="500"
                       placeholder="{{ __('Aclaraciones que verá el comprador antes de pagar') }}">
            </div>
        </div>

        <div class="form-row">
            <div class="form-group col-md-4">
                <label class="font-weight-bold" for="ticket-price-{{ $index }}">{{ __('Precio') }} <span class="text-danger">*</span></label>
                <input id="ticket-price-{{ $index }}" class="form-control rt-ticket-price-input" type="number" step="0.01" min="0" inputmode="decimal"
                       name="tickets[{{ $index }}][price]"
                       value="{{ old($pfx.'.price', $ticket['price'] ?? '0') }}" required>
                <div class="small text-muted mt-1 rt-price-preview">
                    {{ __('Final:') }} <span class="rt-final-price-val">--</span>
                </div>
            </div>
            <div class="form-group col-md-4">
                <label class="font-weight-bold" for="ticket-currency-{{ $index }}">{{ __('Moneda') }}</label>
                <input id="ticket-currency-{{ $index }}" class="form-control text-uppercase" type="text" name="tickets[{{ $index }}][currency]"
                       value="{{ old($pfx.'.currency', $ticket['currency'] ?? 'USD') }}" maxlength="10" placeholder="USD"
                       aria-describedby="ticket-currency-hint-{{ $index }}">
                <small id="ticket-currency-hint-{{ $index }}" class="form-text text-muted">{{ __('Código ISO de tres letras (EUR, USD, MXN…).') }}</small>
            </div>
            <div class="form-group col-md-4 d-flex align-items-end">
                <div class="custom-control custom-checkbox mb-2">
                    <input type="checkbox" class="custom-control-input rt-has-sale-price" id="ticket-sale-{{ $index }}"
                           name="tickets[{{ $index }}][has_sale_price]" value="1"
                           @checked(old($pfx.'.has_sale_price', $ticket['has_sale_price'] ?? false))>
                    <label class="custom-control-label" for="ticket-sale-{{ $index }}">{{ __('Añadir precio de oferta') }}</label>
                </div>
            </div>
        </div>
        <div class="form-group rt-sale-price-row d-none">
            <label class="font-weight-bold" for="ticket-saleprice-{{ $index }}">{{ __('Precio de oferta') }}</label>
            <input id="ticket-saleprice-{{ $index }}" class="form-control rt-ticket-price-input" type="number" step="0.01" min="0" inputmode="decimal"
                   name="tickets[{{ $index }}][sale_price]"
                   value="{{ old($pfx.'.sale_price', $ticket['sale_price'] ?? '') }}">
            <div class="small text-muted mt-1 rt-price-preview">
                {{ __('Final:') }} <span class="rt-final-price-val">--</span>
            </div>
        </div>

        <div class="border rounded p-3 mb-3 bg-light">
            <div class="d-flex align-items-center mb-2">
                <span class="font-weight-bold mr-2">{{ __('Tipo de entrada') }}</span>
                <span class="badge badge-secondary">{{ __('Estándar') }}</span>
                <span class="text-muted small ml-2" title="{{ __('Una entrada estándar es específica de este evento.') }}">
                    <i class="bi bi-info-circle" aria-hidden="true"></i>
                </span>
            </div>
            <input type="hidden" name="tickets[{{ $index }}][ticket_kind]" value="{{ old($pfx.'.ticket_kind', $ticket['ticket_kind'] ?? 'standard') }}">
        </div>

        <div class="form-group">
            <label class="font-weight-bold" for="capacity-strategy-{{ $index }}">{{ __('Capacidad y ventas') }}</label>
            <small class="form-text text-muted d-block mb-1">{{ __('Define si hay límite de boletos y cómo se reparte entre tipos de entrada.') }}</small>
            <select id="capacity-strategy-{{ $index }}" class="form-control rt-capacity-strategy rt-ts-search" name="tickets[{{ $index }}][capacity_strategy]">
                @foreach (\App\Enums\TicketCapacityStrategy::cases() as $case)
                    <option value="{{ $case->value }}" @selected(old($pfx.'.capacity_strategy', $ticket['capacity_strategy'] ?? 'per_ticket') === $case->value)>
                        @if($case === \App\Enums\TicketCapacityStrategy::Unlimited)
                            {{ __('Ilimitada') }}
                        @elseif($case === \App\Enums\TicketCapacityStrategy::PerTicket)
                            {{ __('Cupo propio de esta entrada') }}
                        @else
                            {{ __('Compartir cupo con otras entradas') }}
                        @endif
                    </option>
                @endforeach
            </select>
        </div>

        <div class="form-group rt-row-shared d-none">
            <label class="font-weight-bold" for="shared-pool-{{ $index }}">{{ __('Cupo compartido total') }}</label>
            <input id="shared-pool-{{ $index }}" class="form-control" type="number" min="1" name="tickets[{{ $index }}][shared_pool_size]"
                   value="{{ old($pfx.'.shared_pool_size', $ticket['shared_pool_size'] ?? '') }}">
            <small class="form-text text-muted">{{ __('Total de entradas a repartir entre los tipos que usen este modo (misma configuración en cada tipo).') }}</small>
        </div>

        <div class="form-group rt-row-capacity">
            <label class="font-weight-bold" for="capacity-{{ $index }}">
                <span class="rt-label-capacity-own">{{ __('Limitar ventas de esta entrada a') }}</span>
                <span class="rt-label-capacity-shared d-none">{{ __('Tope de ventas de esta entrada (dentro del cupo compartido)') }}</span>
            </label>
            <input id="capacity-{{ $index }}" class="form-control" type="number" min="0" name="tickets[{{ $index }}][capacity]"
                   value="{{ old($pfx.'.capacity', $ticket['capacity'] ?? 0) }}">
        </div>

        <div class="border rounded p-3 mb-3">
            <div class="font-weight-bold mb-2">{{ __('Periodo de venta') }}</div>
            <small class="text-muted d-block mb-2">{{ __('Si no indicas inicio, la venta queda disponible de inmediato al publicar el evento.') }}</small>
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="sale-start-{{ $index }}">{{ __('Inicio de venta') }}</label>
                    <input id="sale-start-{{ $index }}" class="form-control" type="datetime-local" name="tickets[{{ $index }}][sale_start]"
                           value="{{ old($pfx.'.sale_start', $ticket['sale_start'] ?? '') }}">
                </div>
                <div class="form-group col-md-6">
                    <label for="sale-end-{{ $index }}">{{ __('Fin de venta') }}</label>
                    <input id="sale-end-{{ $index }}" class="form-control" type="datetime-local" name="tickets[{{ $index }}][sale_end]"
                           value="{{ old($pfx.'.sale_end', $ticket['sale_end'] ?? '') }}">
                </div>
            </div>
        </div>

        <div class="mb-3">
            <button class="btn btn-sm btn-link text-decoration-none p-0 rt-toggle-collapse" type="button" data-toggle="collapse" data-target="#adv-{{ $index }}" aria-expanded="false">
                <i class="bi bi-chevron-down" aria-hidden="true"></i> {{ __('Opciones avanzadas') }}
            </button>
            <div class="collapse mt-2" id="adv-{{ $index }}">
                <div class="form-group mb-0">
                    <label class="font-weight-bold" for="sku-{{ $index }}">{{ __('SKU') }}</label>
                    <input id="sku-{{ $index }}" class="form-control" type="text" name="tickets[{{ $index }}][sku]" maxlength="100"
                           value="{{ old($pfx.'.sku', $ticket['sku'] ?? '') }}">
                    <small class="form-text text-muted">{{ __('Código interno opcional para inventario o integraciones.') }}</small>
                </div>
            </div>
        </div>

        <div class="mb-3">
            <button class="btn btn-sm btn-link text-decoration-none p-0 rt-toggle-collapse" type="button" data-toggle="collapse" data-target="#iac-{{ $index }}" aria-expanded="false">
                <i class="bi bi-chevron-down" aria-hidden="true"></i> {{ __('Recolección de asistentes') }}
            </button>
            <div class="collapse mt-2" id="iac-{{ $index }}">
                <p class="small text-muted">{{ __('Define si quieres nombre y correo por cada entrada vendida.') }}</p>
                <div class="custom-control custom-radio">
                    <input class="custom-control-input" type="radio" name="tickets[{{ $index }}][iac]" id="iac-none-{{ $index }}" value="none"
                           @checked(old($pfx.'.iac', $ticket['iac'] ?? 'required') === 'none')>
                    <label class="custom-control-label" for="iac-none-{{ $index }}">{{ __('Sin recolección individual') }}</label>
                </div>
                <div class="custom-control custom-radio">
                    <input class="custom-control-input" type="radio" name="tickets[{{ $index }}][iac]" id="iac-opt-{{ $index }}" value="optional"
                           @checked(old($pfx.'.iac', $ticket['iac'] ?? 'required') === 'optional')>
                    <label class="custom-control-label" for="iac-opt-{{ $index }}">{{ __('Permitir recolección individual') }}</label>
                </div>
                <div class="custom-control custom-radio">
                    <input class="custom-control-input" type="radio" name="tickets[{{ $index }}][iac]" id="iac-req-{{ $index }}" value="required"
                           @checked(old($pfx.'.iac', $ticket['iac'] ?? 'required') === 'required')>
                    <label class="custom-control-label" for="iac-req-{{ $index }}">{{ __('Exigir recolección individual') }}</label>
                </div>
            </div>
        </div>

        <div class="border-top pt-3 rt-attendee-fields-section">
            <div class="d-flex justify-content-between align-items-center mb-2 flex-wrap">
                <span class="font-weight-bold text-dark">{{ __('Información del asistente') }}</span>
                <button type="button" class="btn btn-sm rt-btn-cherry rt-add-custom-field">{{ __('+ Añadir campo') }}</button>
            </div>
            <p class="small text-muted mb-0">{{ __('Añade preguntas extra (talla, club, alergias…). Se guardan al enviar el formulario junto con la entrada.') }}</p>

            <div class="rt-custom-fields-list mt-2">
                @php $cfs = old($pfx.'.custom_fields', $ticket['custom_fields'] ?? []); @endphp
                @foreach (is_array($cfs) ? $cfs : [] as $cfIndex => $cf)
                    @include('dashboard.events.partials.custom-field-row', ['ticketIndex' => $index, 'cfIndex' => $cfIndex, 'cf' => is_array($cf) ? $cf : []])
                @endforeach
            </div>
        </div>

        <div class="mt-2">
            <input type="hidden" name="tickets[{{ $index }}][is_active]" value="0">
            <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" id="active-{{ $index }}" name="tickets[{{ $index }}][is_active]" value="1"
                       @checked((bool) old($pfx.'.is_active', $ticket['is_active'] ?? true))>
                <label class="custom-control-label" for="active-{{ $index }}">{{ __('Entrada activa a la venta') }}</label>
            </div>
        </div>
    </div>
    </div>
</div>
