@extends('layouts.app')
@section('title','Resultado por Categoria')
@section('content')
@include('components.seletor-contest')

 
<div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:.5rem;margin-bottom:1rem">
    <div>
        <h2 style="margin:0">🏆 Resultado por Categoria</h2>
        <p style="margin:.25rem 0 0;color:var(--muted);font-size:.82rem">
            Ranking final por categoria — baseado em <code>resultados_final</code>.
        </p>
    </div>
    <div class="stats-grid" style="max-width:380px;margin:0">
        <div class="stat-card stat-card-green">
            <div class="stat-number">{{ number_format($totalOps) }}</div>
            <div class="stat-label">Operadores</div>
        </div>
        <div class="stat-card">
            <div class="stat-number">{{ number_format($totalCategorias) }}</div>
            <div class="stat-label">Categorias</div>
        </div>
    </div>
</div>
 
{{-- Filtros --}}
<form method="GET" action="{{ route('relatorios.resultado') }}"
      style="display:flex;flex-wrap:wrap;gap:.5rem;align-items:center;margin-bottom:1.5rem">
    <input type="text" name="q" value="{{ $busca }}"
           placeholder="Buscar callsign, nome ou clube..."
           style="padding:.4rem .7rem;border:1px solid #ddd;border-radius:var(--radius);
                  font-size:.88rem;min-width:240px">
    <select name="categoria"
            style="padding:.4rem .6rem;border:1px solid #ddd;border-radius:var(--radius);font-size:.88rem;max-width:320px">
        <option value="">— Todas as categorias —</option>
        @foreach($categorias as $cat)
            <option value="{{ $cat }}" {{ $categoria===$cat ? 'selected':'' }}>{{ $cat }}</option>
        @endforeach
    </select>
    <button type="submit" class="btn btn-secondary" style="padding:.4rem .9rem">Filtrar</button>
    @if($busca || $categoria)
        <a href="{{ route('relatorios.resultado') }}"
           class="btn btn-secondary" style="padding:.4rem .9rem">✕ Limpar</a>
    @endif
</form>
 
@if(count($grupos) === 0)
    <p style="color:#aaa;text-align:center;padding:2rem">Nenhum resultado encontrado.</p>
@else
 
{{-- ── Tabela completa por categoria ── --}}
@foreach($grupos as $cat => $rows)
<div style="margin-bottom:2.5rem">
 
    {{-- Cabeçalho da categoria --}}
    <div style="background:var(--blue);color:#fff;padding:.5rem 1rem;
                border-radius:var(--radius) var(--radius) 0 0;
                font-weight:bold;font-size:.88rem;display:flex;
                justify-content:space-between;align-items:center">
        <span>Categoria: {{ $cat }}</span>
        <span style="opacity:.85;font-weight:normal;font-size:.8rem">
            {{ count($rows) }} operador(es)
        </span>
    </div>
 
    <div style="overflow-x:auto">
    <table style="width:100%;border-collapse:collapse;font-size:.8rem">
        <thead>
            <tr style="background:#e8eef7">
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3;width:45px;text-align:center">Rank</th>
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3;white-space:nowrap">Callsign</th>
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3;text-align:right;white-space:nowrap">QSOs Val.</th>
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3;text-align:right">Pontos</th>
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3;text-align:right">M1</th>
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3;text-align:right">M2</th>
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3;text-align:right">M3</th>
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3;text-align:right;white-space:nowrap">Claimed Inf.</th>
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3;text-align:right;white-space:nowrap">Claimed Cal.</th>
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3;text-align:right;white-space:nowrap">Final Score</th>
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3;text-align:right;white-space:nowrap">% Redução</th>
                <th style="padding:.35rem .5rem;border:1px solid #d0d7e3">Nome</th>
            </tr>
        </thead>
        <tbody>
            @php $catTotal = 0; @endphp
            @foreach($rows as $r)
            @php $catTotal += $r->final_score; @endphp
            <tr style="background:{{ $r->rk == 1 ? '#fffde7' : '#fff' }}">
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8;text-align:center;
                    {{ $r->rk == 1 ? 'font-weight:bold;color:var(--blue)' : 'color:#888' }}">
                    {{ $r->rk == 1 ? '🥇' : ($r->rk == 2 ? '🥈' : ($r->rk == 3 ? '🥉' : $r->rk)) }}
                </td>
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8">
                    <strong>{{ $r->callsign }}</strong>
                    @if($r->club)
                        <span style="color:#aaa;font-size:.72rem"> — {{ $r->club }}</span>
                    @endif
                </td>
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8;text-align:right">{{ number_format($r->total_qsos_validos) }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8;text-align:right">{{ number_format($r->total_pontos) }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8;text-align:right">{{ number_format($r->total_multi_um) }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8;text-align:right">{{ number_format($r->total_multi_dois) }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8;text-align:right">{{ number_format($r->total_multi_tres) }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8;text-align:right;color:#888">{{ number_format($r->claimed_informado) }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8;text-align:right;color:#888">{{ number_format($r->claimed_calculado) }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8;text-align:right;font-weight:bold;color:var(--blue)">
                    {{ number_format($r->final_score) }}
                </td>
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8;text-align:right;
                    {{ $r->percentual_reducao > 20 ? 'color:#C62828' : ($r->percentual_reducao > 10 ? 'color:#E65100' : 'color:#555') }}">
                    {{ $r->percentual_reducao !== null ? number_format($r->percentual_reducao, 2).'%' : '—' }}
                </td>
                <td style="padding:.3rem .5rem;border:1px solid #e8e8e8;font-size:.75rem">{{ $r->nome }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr style="background:#f0f4fa;font-weight:bold">
                <td colspan="9" style="padding:.4rem .5rem;border:1px solid #d0d7e3;font-size:.82rem">
                    Total {{ $cat }}: {{ count($rows) }} operador(es)
                </td>
                <td style="padding:.4rem .5rem;border:1px solid #d0d7e3;text-align:right;color:var(--green)">
                    {{ number_format($catTotal) }}
                </td>
                <td colspan="2" style="border:1px solid #d0d7e3"></td>
            </tr>
        </tfoot>
    </table>
    </div>
</div>
@endforeach
 
{{-- ── Primeiros Colocados ── --}}
@if(count($primeiros) > 0 && !$busca && !$categoria)
<div style="margin-top:2rem">
    <h3 style="margin-bottom:.75rem">🏅 Primeiros Colocados por Categoria</h3>
    <div style="overflow-x:auto">
    <table style="width:100%;border-collapse:collapse;font-size:.82rem">
        <thead>
            <tr style="background:#e8eef7">
                <th style="padding:.4rem .6rem;border:1px solid #d0d7e3;width:40px;text-align:center">Seq.</th>
                <th style="padding:.4rem .6rem;border:1px solid #d0d7e3">Categoria</th>
                <th style="padding:.4rem .6rem;border:1px solid #d0d7e3">Callsign</th>
                <th style="padding:.4rem .6rem;border:1px solid #d0d7e3">Nome</th>
                <th style="padding:.4rem .6rem;border:1px solid #d0d7e3">E-mail</th>
                <th style="padding:.4rem .6rem;border:1px solid #d0d7e3;text-align:right">Pontos</th>
                <th style="padding:.4rem .6rem;border:1px solid #d0d7e3;text-align:right">M1</th>
                <th style="padding:.4rem .6rem;border:1px solid #d0d7e3;text-align:right">M2</th>
                <th style="padding:.4rem .6rem;border:1px solid #d0d7e3;text-align:right">M3</th>
                <th style="padding:.4rem .6rem;border:1px solid #d0d7e3;text-align:right;white-space:nowrap">Final Score</th>
            </tr>
        </thead>
        <tbody>
            @php $seq = 1; @endphp
            @foreach($primeiros as $cat => $lider)
            <tr style="background:{{ $seq % 2 === 0 ? '#f8f9fa' : '#fff' }}">
                <td style="padding:.35rem .6rem;border:1px solid #e8e8e8;text-align:center;color:#888">{{ $seq++ }}</td>
                <td style="padding:.35rem .6rem;border:1px solid #e8e8e8;font-size:.75rem">{{ $cat }}</td>
                <td style="padding:.35rem .6rem;border:1px solid #e8e8e8"><strong>{{ $lider->callsign }}</strong></td>
                <td style="padding:.35rem .6rem;border:1px solid #e8e8e8">{{ strtoupper($lider->nome ?? '') }}</td>
                <td style="padding:.35rem .6rem;border:1px solid #e8e8e8;font-size:.75rem;color:var(--blue)">
                    {{ strtolower($lider->email ?? '') }}
                </td>
                <td style="padding:.35rem .6rem;border:1px solid #e8e8e8;text-align:right">{{ number_format($lider->total_pontos) }}</td>
                <td style="padding:.35rem .6rem;border:1px solid #e8e8e8;text-align:right">{{ number_format($lider->total_multi_um) }}</td>
                <td style="padding:.35rem .6rem;border:1px solid #e8e8e8;text-align:right">{{ number_format($lider->total_multi_dois) }}</td>
                <td style="padding:.35rem .6rem;border:1px solid #e8e8e8;text-align:right">{{ number_format($lider->total_multi_tres) }}</td>
                <td style="padding:.35rem .6rem;border:1px solid #e8e8e8;text-align:right;font-weight:bold;color:var(--blue)">
                    {{ number_format($lider->final_score) }}
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
    </div>
</div>
@endif
 
@endif
@endsection