@extends('public.resultados.layout')
@section('title', 'Meu LOG — ' . ($callsign ?: 'Consulta'))
@section('content')

<div class="card">
    <form method="GET" action="{{ route('public.resultados.callsign') }}" class="search-bar">
        <input type="text" name="callsign" value="{{ $callsign }}"
               placeholder="Digite seu callsign (ex: PY6MV)"
               style="text-transform:uppercase;max-width:220px"
               oninput="this.value=this.value.toUpperCase()"
               autofocus>
        <button type="submit">🔍 Consultar</button>
        @if($callsign)
            <a href="{{ route('public.resultados.callsign') }}"
               style="padding:.45rem .8rem;color:#666;text-decoration:none;font-size:.88rem">✕ Limpar</a>
        @endif
    </form>

    @if($callsign && $qsos->isEmpty())
        <p style="color:#888;padding:1.5rem 0">
            Nenhum QSO encontrado para <strong>{{ $callsign }}</strong> neste contest.
        </p>
    @endif
</div>

@if($callsign && !$qsos->isEmpty())

    {{-- Resumo do operador --}}
    @if($resumo)
    <div class="stats">
        <div class="stat">
            <div class="stat-num">{{ $resumo->rk }}º</div>
            <div class="stat-lbl">Posição — {{ $resumo->categoria }}</div>
        </div>
        <div class="stat green">
            <div class="stat-num">{{ number_format($resumo->total_qsos_validos) }}</div>
            <div class="stat-lbl">QSOs Válidos</div>
        </div>
        <div class="stat gold">
            <div class="stat-num">{{ number_format($resumo->final_score) }}</div>
            <div class="stat-lbl">Final Score</div>
        </div>
        <div class="stat">
            <div class="stat-num">{{ number_format($resumo->total_pontos) }}</div>
            <div class="stat-lbl">Pontos</div>
        </div>
        <div class="stat">
            <div class="stat-num">{{ $resumo->total_multi_um + $resumo->total_multi_dois + $resumo->total_multi_tres }}</div>
            <div class="stat-lbl">Multiplicadores</div>
        </div>
        @if($resumo->percentual_reducao > 0)
        <div class="stat" style="background:#c62828">
            <div class="stat-num">{{ number_format($resumo->percentual_reducao, 1) }}%</div>
            <div class="stat-lbl">Redução</div>
        </div>
        @endif
    </div>
    @endif

    {{-- Tabela de QSOs --}}
    <div class="card">
        @php
            $validos   = $qsos->where('confirmado', 1)->count();
            $invalidos = $qsos->where('confirmado', 0)->count();
        @endphp
        <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:1rem;flex-wrap:wrap;gap:.5rem">
            <h3 style="color:#1B4F8C;font-size:1rem">
                QSOs de <strong>{{ $callsign }}</strong>
                — {{ $qsos->count() }} total
            </h3>
            <div style="font-size:.82rem">
                <span class="badge-val">✅ {{ $validos }} válidos</span>
                &nbsp;
                @if($invalidos > 0)
                    <span class="badge-inv">❌ {{ $invalidos }} inválidos</span>
                @endif
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <th>Call Rcvd</th>
                    <th>Banda</th>
                    <th>Modo</th>
                    <th>Freq (kHz)</th>
                    <th>Data/Hora UTC</th>
                    <th>Exch Sent</th>
                    <th>Exch Rcvd</th>
                    <th style="text-align:center">Status</th>
                    <th>Motivo</th>
                </tr>
            </thead>
            <tbody>
                @foreach($qsos as $q)
                    <tr>
                        <td class="call">{{ $q->call_rcvd }}</td>
                        <td>{{ $q->band }}</td>
                        <td>{{ $q->category_mode }}</td>
                        <td>{{ number_format($q->frequency, 1) }}</td>
                        <td style="font-size:.78rem;white-space:nowrap">
                            {{ \Carbon\Carbon::parse($q->date_time)->format('d/m/Y H:i') }}
                        </td>
                        <td>{{ $q->exch_sent }}</td>
                        <td>{{ $q->exch_rcvd }}</td>
                        <td style="text-align:center">
                            @if($q->confirmado)
                                <span class="badge-val">✅</span>
                            @else
                                <span class="badge-inv">❌</span>
                            @endif
                        </td>
                        <td style="font-size:.75rem;color:#c62828">{{ $q->motivo }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
@endif
@endsection
