@extends('layouts.app')
@section('title','Exchange Inválidos')
@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">🔄 Exchange Inválidos</h2>
        <p style="margin:.25rem 0 0;color:var(--muted);font-size:.82rem">
            QSOs cujo <code>exch_sent</code> ou <code>exch_rcvd</code> não existe na tabela
            <code>exchange</code>. Agrupado por <code>call_sent</code>.
        </p>
    </div>
    <div class="stats-grid" style="max-width:380px;margin:0">
        <div class="stat-card stat-card-red">
            <div class="stat-number">{{ number_format($totalRegistros) }}</div>
            <div class="stat-label">Registros com Exchange Inválido</div>
        </div>
        <div class="stat-card">
            <div class="stat-number">{{ number_format($totalOperadores) }}</div>
            <div class="stat-label">Operadores</div>
        </div>
    </div>
</div>

{{-- Legenda --}}
<div style="margin-bottom:1rem;font-size:.82rem;display:flex;gap:1.25rem;flex-wrap:wrap">
    <span style="display:inline-flex;align-items:center;gap:.35rem">
        <strong style="color:#C62828">vermelho</strong> = valor inválido (ausente na tabela exchange)
    </span>
    <span style="display:inline-flex;align-items:center;gap:.35rem">
        Flags: valid_qso / valid_exchange / confirmado
    </span>
</div>

{{-- Busca --}}
<form method="GET" action="{{ route('relatorios.exchange-invalidos') }}"
      style="display:flex;gap:.5rem;align-items:center;margin-bottom:1.25rem">
    <input type="text" name="q" value="{{ $busca }}"
           placeholder="Buscar callsign ou exchange..."
           style="padding:.4rem .7rem;border:1px solid #ddd;border-radius:var(--radius);
                  font-size:.88rem;min-width:240px">
    <button type="submit" class="btn btn-secondary" style="padding:.4rem .9rem">Buscar</button>
    @if($busca)
        <a href="{{ route('relatorios.exchange-invalidos') }}"
           class="btn btn-secondary" style="padding:.4rem .9rem">✕ Limpar</a>
    @endif
</form>

@forelse($grupos as $callSent => $regs)
<div style="margin-bottom:1.75rem">
    <div style="background:#555;color:#fff;padding:.45rem .75rem;font-weight:bold;font-size:.88rem;
                border-radius:var(--radius) var(--radius) 0 0;
                display:flex;justify-content:space-between">
        <span>Call Sent: {{ $callSent }}</span>
        <span style="font-weight:normal;opacity:.85;font-size:.8rem">{{ count($regs) }} registro(s)</span>
    </div>
    <table style="width:100%;border-collapse:collapse;font-size:.8rem">
        <thead>
            <tr style="background:#f0f0f0">
                <th style="padding:.33rem .5rem;border:1px solid #ccc">ID</th>
                <th style="padding:.33rem .5rem;border:1px solid #ccc">Call Sent</th>
                <th style="padding:.33rem .5rem;border:1px solid #ccc">Call Rcvd</th>
                <th style="padding:.33rem .5rem;border:1px solid #ccc">Exch Sent</th>
                <th style="padding:.33rem .5rem;border:1px solid #ccc">Exch Rcvd</th>
                <th style="padding:.33rem .5rem;border:1px solid #ccc;white-space:nowrap">Data/Hora</th>
                <th style="padding:.33rem .5rem;border:1px solid #ccc">Banda</th>
                <th style="padding:.33rem .5rem;border:1px solid #ccc;text-align:center" title="valid_qso">V.QSO</th>
                <th style="padding:.33rem .5rem;border:1px solid #ccc;text-align:center" title="valid_exchange">V.Exch</th>
                <th style="padding:.33rem .5rem;border:1px solid #ccc;text-align:center" title="confirmado">Conf.</th>
                <th style="padding:.33rem .5rem;border:1px solid #ccc">Motivo</th>
            </tr>
        </thead>
        <tbody>
            @foreach($regs as $r)
            <tr style="background:#fff">
                <td style="padding:.28rem .5rem;border:1px solid #e8e8e8;color:#aaa">{{ $r->id }}</td>
                <td style="padding:.28rem .5rem;border:1px solid #e8e8e8"><strong>{{ $r->call_sent }}</strong></td>
                <td style="padding:.28rem .5rem;border:1px solid #e8e8e8">{{ $r->call_rcvd }}</td>
                <td style="padding:.28rem .5rem;border:1px solid #e8e8e8;
                    {{ $r->sent_invalido ? 'color:#C62828;font-weight:bold' : '' }}">
                    {{ $r->exch_sent }}
                    @if($r->sent_invalido) <span title="Não existe na tabela exchange">⚠️</span> @endif
                </td>
                <td style="padding:.28rem .5rem;border:1px solid #e8e8e8;
                    {{ $r->rcvd_invalido ? 'color:#C62828;font-weight:bold' : '' }}">
                    {{ $r->exch_rcvd }}
                    @if($r->rcvd_invalido) <span title="Não existe na tabela exchange">⚠️</span> @endif
                </td>
                <td style="padding:.28rem .5rem;border:1px solid #e8e8e8;white-space:nowrap">{{ $r->date_time }}</td>
                <td style="padding:.28rem .5rem;border:1px solid #e8e8e8">{{ $r->band }}</td>
                <td style="padding:.28rem .5rem;border:1px solid #e8e8e8;text-align:center;
                    {{ $r->valid_qso == 0 ? 'color:#C62828;font-weight:bold' : 'color:#2E7D32' }}">
                    {{ $r->valid_qso ?? '—' }}
                </td>
                <td style="padding:.28rem .5rem;border:1px solid #e8e8e8;text-align:center;
                    {{ $r->valid_exchange == 0 ? 'color:#C62828;font-weight:bold' : 'color:#2E7D32' }}">
                    {{ $r->valid_exchange ?? '—' }}
                </td>
                <td style="padding:.28rem .5rem;border:1px solid #e8e8e8;text-align:center;
                    {{ $r->confirmado == 0 ? 'color:#C62828' : 'color:#2E7D32;font-weight:bold' }}">
                    {{ $r->confirmado ?? '—' }}
                </td>
                <td style="padding:.28rem .5rem;border:1px solid #e8e8e8;font-size:.73rem;color:#C62828">
                    {{ $r->motivo ?? '' }}
                </td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr style="background:#f5f5f5;font-weight:bold">
                <td colspan="11" style="padding:.35rem .5rem;border:1px solid #ddd;font-size:.8rem">
                    Total para {{ $callSent }}: {{ count($regs) }} registro(s)
                </td>
            </tr>
        </tfoot>
    </table>
</div>
@empty
<p style="color:#aaa;text-align:center;padding:2rem">
    ✅ Nenhum QSO com exchange inválido encontrado.
</p>
@endforelse

@if($totalRegistros > 0)
<div style="background:#f0f0f0;border-radius:var(--radius);padding:.75rem 1rem;
            font-weight:bold;font-size:.9rem;margin-top:.5rem">
    Total de registros sem Exch Sent ou Exch Rcvd encontrado: {{ number_format($totalRegistros) }}
</div>
@endif

@endsection
