@php
    /** @var array<string,mixed> $overview */
    $ov = $overview ?? [];
    $moneyFn = is_callable($money ?? null)
        ? $money
        : static function (float $n): string {
            return '$'.number_format($n, 2);
        };
@endphp

<ul class="nav nav-tabs rt-ev-att-nav rt-ev-sw-nav mb-0 border-bottom-0" role="tablist">
    <li class="nav-item">
        <a
            class="nav-link active font-weight-bold px-3 py-2"
            id="rt-ev-sw-tab-attendees"
            data-toggle="tab"
            href="#rt-ev-sw-pane-attendees"
            role="tab"
        >
            {{ __('Asistentes') }}
        </a>
    </li>
    <li class="nav-item">
        <a
            class="nav-link font-weight-bold px-3 py-2"
            id="rt-ev-sw-tab-orders"
            data-toggle="tab"
            href="#rt-ev-sw-pane-orders"
            role="tab"
        >
            {{ __('Pedidos') }}
        </a>
    </li>
</ul>

<div class="tab-content rt-ev-att-tab-content rt-ev-sw-tab-content border rounded-bottom bg-white mt-0">
    <div class="tab-pane fade show active p-3" id="rt-ev-sw-pane-attendees" role="tabpanel">
        @include('dashboard.events.partials.attendees-event-header', ['event' => $event, 'overview' => $ov])

        <div class="row">
            <div class="col-lg-6 mb-3">
                <div class="card border shadow-sm h-100">
                    <div class="card-header bg-white py-2 border-bottom">
                        <h6 class="m-0 font-weight-bold text-primary small">{{ __('Resumen de entradas') }}</h6>
                    </div>
                    <div class="card-body small">
                        @forelse (($ov['ticket_groups'] ?? []) as $group)
                            <div class="font-weight-bold text-muted text-uppercase mb-2" style="font-size: 0.65rem;">{{ $group['label'] }}</div>
                            <ul class="list-unstyled mb-3 rt-ev-att-ticket-lines">
                                @foreach ($group['tickets'] ?? [] as $t)
                                    <li class="mb-2 pb-2 border-bottom border-light">
                                        <div class="d-flex justify-content-between align-items-start flex-wrap">
                                            <span class="font-weight-bold text-dark">{{ $t['name'] }}</span>
                                            <span class="text-muted">{{ $t['sku_label'] }}</span>
                                        </div>
                                        <div class="text-muted mt-1">
                                            <span class="font-weight-bold text-body">{{ (int) $t['issued'] }}</span>
                                            {{ __('emitidas') }}
                                            @if (! empty($t['unlimited']))
                                                <span class="text-muted">({{ __('ilimitadas') }})</span>
                                            @elseif (! empty($t['shared_pool_note']))
                                                <span class="d-block small mt-1">{{ $t['shared_pool_note'] }}</span>
                                            @else
                                                <span>({{ (int) ($t['available'] ?? 0) }} {{ __('disponibles') }})</span>
                                            @endif
                                        </div>
                                    </li>
                                @endforeach
                            </ul>
                        @empty
                            <p class="text-muted small mb-0">{{ __('No hay tipos de entrada configurados.') }}</p>
                        @endforelse

                        @php
                            $totalIssued = 0;
                            $totalAvail = 0;
                            $hasLimited = false;
                            foreach ($ov['ticket_groups'] ?? [] as $g) {
                                foreach ($g['tickets'] ?? [] as $t) {
                                    $totalIssued += (int) ($t['issued'] ?? 0);
                                    if (empty($t['unlimited']) && empty($t['shared_pool_note'])) {
                                        $hasLimited = true;
                                        $totalAvail += (int) ($t['available'] ?? 0);
                                    }
                                }
                            }
                        @endphp
                        @if (($ov['ticket_groups'] ?? []) !== [])
                            <div class="pt-2 border-top small font-weight-bold text-dark">
                                {{ __('Total') }}:
                                {{ (int) $totalIssued }} {{ __('emitidas') }}
                                @if ($hasLimited)
                                    <span class="text-muted font-weight-normal">
                                        ({{ (int) $totalAvail }} {{ __('disponibles') }})
                                    </span>
                                @endif
                            </div>
                        @endif
                    </div>
                </div>
            </div>
            <div class="col-lg-6 mb-3">
                <div class="card border shadow-sm h-100">
                    <div class="card-header bg-white py-2 border-bottom">
                        <h6 class="m-0 font-weight-bold text-primary small">{{ __('Resumen de asistencia') }}</h6>
                    </div>
                    <div class="card-body small">
                        <dl class="row mb-0">
                            <dt class="col-7 text-muted">{{ __('Entradas (órdenes completadas)') }}</dt>
                            <dd class="col-5 text-right font-weight-bold mb-1">{{ (int) ($ov['attendance']['entrada_lines_total'] ?? 0) }}</dd>
                            <dt class="col-7 text-muted">{{ __('Check-in registrados') }}</dt>
                            <dd class="col-5 text-right font-weight-bold text-success mb-1">{{ (int) ($ov['attendance']['checked_in'] ?? 0) }}</dd>
                            <dt class="col-7 text-muted">{{ __('Pendientes de ingreso') }}</dt>
                            <dd class="col-5 text-right font-weight-bold text-warning mb-1">{{ (int) ($ov['attendance']['pending_checkin'] ?? 0) }}</dd>
                            <dt class="col-7 text-muted">{{ __('Órdenes pendientes de pago') }}</dt>
                            <dd class="col-5 text-right mb-1">{{ (int) ($ov['attendance']['orders_pending'] ?? 0) }}</dd>
                            <dt class="col-7 text-muted">{{ __('Órdenes completadas') }}</dt>
                            <dd class="col-5 text-right mb-1">{{ (int) ($ov['attendance']['orders_completed'] ?? 0) }}</dd>
                            <dt class="col-7 text-muted">{{ __('Órdenes reembolsadas') }}</dt>
                            <dd class="col-5 text-right mb-1">{{ (int) ($ov['attendance']['orders_refunded'] ?? 0) }}</dd>
                            <dt class="col-7 text-muted">{{ __('Asistieron') }}</dt>
                            <dd class="col-5 text-right font-weight-bold mb-0">
                                {{ (int) ($ov['attendance']['checked_in'] ?? 0) }}
                                ({{ (int) ($ov['attendance']['checkin_pct'] ?? 0) }}%)
                            </dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="tab-pane fade p-3" id="rt-ev-sw-pane-orders" role="tabpanel">
        <h6 class="font-weight-bold text-dark mb-2">{{ __('Evento · pedidos y cobros') }}</h6>

        @include('dashboard.events.partials.attendees-event-header', ['event' => $event, 'overview' => $ov])

        <div class="card border shadow-sm mb-3">
            <div class="card-header bg-white py-2 border-bottom">
                <h6 class="m-0 font-weight-bold text-primary small">{{ __('Ventas por entrada') }}</h6>
                <span class="text-muted d-block mt-1" style="font-size: 0.75rem;">{{ __('Precio base por tipo (sin incluir la comisión RegiTicket).') }}</span>
            </div>
            <div class="card-body small">
                @forelse (($ov['ticket_groups'] ?? []) as $group)
                    <div class="font-weight-bold text-muted text-uppercase mb-2" style="font-size: 0.65rem;">{{ $group['label'] }}</div>
                    <ul class="list-unstyled mb-3">
                        @foreach ($group['tickets'] ?? [] as $t)
                            @php
                                $c = $t['counts_by_status'] ?? [];
                                $pending = (int) ($c['pending'] ?? 0);
                                $completed = (int) ($c['completed'] ?? 0);
                                $refunded = (int) ($c['refunded'] ?? 0);
                            @endphp
                            <li class="mb-3 pb-2 border-bottom border-light">
                                <div class="d-flex flex-wrap justify-content-between align-items-baseline">
                                    <span class="font-weight-bold text-dark">{{ $t['name'] }} <span class="text-muted font-weight-normal">{{ $t['sku_label'] }}</span></span>
                                    <span class="font-weight-bold">{{ $moneyFn((float) ($t['base_price'] ?? 0)) }}</span>
                                </div>
                                <div class="text-muted mt-1">
                                    @if ($refunded > 0)
                                        <span>{{ $refunded }} {{ __('reembolsado') }}</span>
                                        @if ($pending > 0 || $completed > 0)
                                            <span class="mx-1">·</span>
                                        @endif
                                    @endif
                                    @if ($completed > 0)
                                        <span>{{ $completed }} {{ __('completado') }}</span>
                                        @if ($pending > 0)
                                            <span class="mx-1">·</span>
                                        @endif
                                    @endif
                                    @if ($pending > 0)
                                        <span>{{ $pending }} {{ __('procesando') }}</span>
                                    @endif
                                    @if ($pending === 0 && $completed === 0 && $refunded === 0)
                                        <span>{{ __('Sin movimientos') }}</span>
                                    @endif
                                </div>
                                @if (! empty($t['unlimited']))
                                    <div class="text-muted mt-1" style="font-size: 0.75rem;">{{ __('Disponibilidad: ilimitada') }}</div>
                                @elseif (! empty($t['shared_pool_note']))
                                    <div class="text-muted mt-1" style="font-size: 0.75rem;">{{ $t['shared_pool_note'] }}</div>
                                @elseif (isset($t['available']))
                                    <div class="text-muted mt-1" style="font-size: 0.75rem;">
                                        {{ __('Disponibles') }}: {{ (int) $t['available'] }}
                                        @if (! empty($t['capacity']))
                                            / {{ (int) $t['capacity'] }}
                                        @endif
                                    </div>
                                @endif
                            </li>
                        @endforeach
                    </ul>
                @empty
                    <p class="text-muted small mb-0">{{ __('No hay tipos de entrada.') }}</p>
                @endforelse
            </div>
        </div>

        @php
            $sales = $ov['sales'] ?? [];
            $pend = $sales['pending'] ?? [];
            $comp = $sales['completed'] ?? [];
            $ref = $sales['refunded'] ?? [];
        @endphp

        <div class="card border shadow-sm mb-0">
            <div class="card-header bg-white py-2 border-bottom">
                <h6 class="m-0 font-weight-bold text-primary small">{{ __('Totales de pedidos') }}</h6>
                <span class="text-muted d-block mt-1" style="font-size: 0.75rem;">{{ $ov['fee_explainer'] ?? '' }}</span>
            </div>
            <div class="card-body small">
                <dl class="row mb-0">
                    <dt class="col-7 text-muted">{{ __('Procesando (pendiente de pago)') }}</dt>
                    <dd class="col-5 text-right mb-2">
                        {{ $moneyFn((float) ($pend['total_amount'] ?? 0)) }}
                        <span class="text-muted">({{ (int) ($pend['orders'] ?? 0) }} {{ __('órdenes') }})</span>
                    </dd>
                    <dt class="col-7 text-muted">{{ __('Completado') }}</dt>
                    <dd class="col-5 text-right mb-2">
                        {{ $moneyFn((float) ($comp['total_amount'] ?? 0)) }}
                        <span class="text-muted">({{ (int) ($comp['orders'] ?? 0) }})</span>
                    </dd>
                    <dt class="col-7 text-muted pl-2">{{ __('· Comisión plataforma (en órdenes completadas)') }}</dt>
                    <dd class="col-5 text-right mb-2 text-muted">
                        {{ $moneyFn((float) ($comp['platform_fee'] ?? 0)) }}
                    </dd>
                    <dt class="col-7 text-muted pl-2">{{ __('· Neto organizador (precio base acordado)') }}</dt>
                    <dd class="col-5 text-right mb-2">
                        {{ $moneyFn((float) ($comp['organizer_net'] ?? 0)) }}
                    </dd>
                    <dt class="col-7 text-muted">{{ __('Reembolsado') }}</dt>
                    <dd class="col-5 text-right mb-2">
                        {{ $moneyFn((float) ($ref['total_amount'] ?? 0)) }}
                        <span class="text-muted">({{ (int) ($ref['orders'] ?? 0) }})</span>
                    </dd>
                    <dt class="col-7 text-muted border-top pt-2 mt-1">{{ __('Total cobrado / registrado (todas las órdenes)') }}</dt>
                    <dd class="col-5 text-right border-top pt-2 mt-1 font-weight-bold mb-0">
                        {{ $moneyFn((float) ($sales['grand_total_amount'] ?? 0)) }}
                        <span class="text-muted font-weight-normal">
                            ({{ (int) ($sales['grand_order_count'] ?? 0) }} {{ __('órdenes') }})
                        </span>
                    </dd>
                </dl>
                <p class="text-muted mb-0 mt-3 pt-2 border-top" style="font-size: 0.75rem;">
                    {{ __('El total pagado por el comprador en órdenes completadas incluye el precio base más la comisión (:formula por entrada).', ['formula' => $ov['fee_formula_short'] ?? '']) }}
                </p>
            </div>
        </div>
    </div>
</div>
