@extends('public.resultados.layout')
@section('title', 'Operadores e Clubes')
@section('content')
 
<div class="stats">
    <div class="stat">
        <div class="stat-num">{{ number_format($totalOps) }}</div>
        <div class="stat-lbl">Operadores</div>
    </div>
    <div class="stat green">
        <div class="stat-num">{{ number_format($totalClubes) }}</div>
        <div class="stat-lbl">Clubes</div>
    </div>
</div>
 
<div class="card">
    <form method="GET" action="{{ route('public.resultados.clubes') }}" class="search-bar">
        <input type="text" name="q" value="{{ $busca }}"
               placeholder="Buscar clube ou callsign...">
        <select name="grupo" onchange="this.form.submit()"
                style="padding:.45rem .8rem;border:1px solid #ddd;border-radius:5px;font-size:.88rem">
            <option value="">— Todos os grupos —</option>
            @foreach(['LABREs','BRAZIL','OUTROS'] as $g)
                <option value="{{ $g }}" {{ $grupo===$g ? 'selected':'' }}>{{ $g }}</option>
            @endforeach
        </select>
        <button type="submit">🔍 Filtrar</button>
        @if($busca || $grupo)
            <a href="{{ route('public.resultados.clubes') }}"
               style="padding:.45rem .8rem;color:#666;text-decoration:none;font-size:.88rem">✕ Limpar</a>
        @endif
    </form>
 
    @php
        $cores = [
            'LABREs' => ['bg'=>'#EBF5FB','titulo'=>'#1B4F8C','badge'=>'#AED6F1'],
            'BRAZIL' => ['bg'=>'#EAFAF1','titulo'=>'#1E8449','badge'=>'#A9DFBF'],
            'OUTROS' => ['bg'=>'#FEFCE8','titulo'=>'#7D6608','badge'=>'#F9E79F'],
        ];
    @endphp
 
    @forelse($dados as $groupName => $clubes)
        @php $cor = $cores[$groupName] ?? ['bg'=>'#f9f9f9','titulo'=>'#555','badge'=>'#ddd']; @endphp
 
        <div style="margin-bottom:2rem">
            {{-- Cabeçalho do grupo --}}
            <div style="background:{{ $cor['titulo'] }};color:#fff;padding:.55rem 1rem;
                        border-radius:6px 6px 0 0;font-weight:bold;font-size:.95rem;
                        display:flex;align-items:center;gap:.75rem">
                {{ $groupName }}
                <span style="background:rgba(255,255,255,.2);border-radius:20px;
                             padding:.1rem .6rem;font-size:.78rem">
                    {{ count($clubes) }} clube(s)
                </span>
            </div>
 
            @foreach($clubes as $clubNome => $info)
                @php
                    if (strtoupper($groupName)==='BRAZIL' &&
                        strtoupper(substr(trim($clubNome),0,5))==='LABRE') continue;
                @endphp
 
                <div style="border:1px solid #e0e0e0;border-top:none;margin-bottom:.4rem">
                    {{-- Cabeçalho do clube --}}
                    <div style="background:{{ $cor['bg'] }};padding:.45rem 1rem;
                                display:flex;justify-content:space-between;align-items:center">
                        <span style="font-weight:600;font-size:.88rem">{{ $clubNome }}</span>
                        <span style="background:{{ $cor['badge'] }};border-radius:4px;
                                     padding:.15rem .5rem;font-size:.8rem;font-weight:bold">
                            Total: {{ number_format($info['club_total']) }}
                        </span>
                    </div>
 
                    <table>
                        <thead>
                            <tr>
                                <th>Callsign</th>
                                <th style="text-align:right">Pontos</th>
                                <th style="text-align:right">M1 (DXCC)</th>
                                <th style="text-align:right">M2 (UF)</th>
                                <th style="text-align:right">M3 (HQ)</th>
                                <th style="text-align:right">Final Score</th>
                            </tr>
                        </thead>
                        <tbody>
                            @php $totP=$totM1=$totM2=$totM3=$totF=0; @endphp
                            @foreach($info['calls'] as $op)
                                @php
                                    $totP  += $op->total_pontos;
                                    $totM1 += $op->total_m1;
                                    $totM2 += $op->total_m2;
                                    $totM3 += $op->total_m3;
                                    $totF  += $op->final_score;
                                @endphp
                                <tr>
                                    <td class="call">
                                        <a href="{{ route('public.resultados.callsign', ['callsign' => $op->call_sent]) }}"
                                           style="color:#1B4F8C;text-decoration:none">
                                            {{ $op->call_sent }}
                                        </a>
                                    </td>
                                    <td style="text-align:right">{{ number_format($op->total_pontos) }}</td>
                                    <td style="text-align:right">{{ $op->total_m1 }}</td>
                                    <td style="text-align:right">{{ $op->total_m2 }}</td>
                                    <td style="text-align:right">{{ $op->total_m3 }}</td>
                                    <td class="score">{{ number_format($op->final_score) }}</td>
                                </tr>
                            @endforeach
                            {{-- Totais do clube --}}
                            <tr style="background:#f0f0f0;font-weight:bold;font-size:.8rem">
                                <td>TOTAL</td>
                                <td style="text-align:right">{{ number_format($totP) }}</td>
                                <td style="text-align:right">{{ $totM1 }}</td>
                                <td style="text-align:right">{{ $totM2 }}</td>
                                <td style="text-align:right">{{ $totM3 }}</td>
                                <td style="text-align:right;color:#1B4F8C">{{ number_format($totF) }}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            @endforeach
        </div>
    @empty
        <p style="color:#888;text-align:center;padding:2rem">Nenhum dado encontrado.</p>
    @endforelse
</div>
@endsection