@extends('layouts.app')
@section('title','Relatório — Duplicados')
@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">
    <h2 style="margin:0">🔁 Relatório Detalhado de Duplicidade de QSOs</h2>
    <div class="stats-grid" style="max-width:380px;margin:0">
        <div class="stat-card stat-card-red">
            <div class="stat-number">{{ number_format($totalGrupos) }}</div>
            <div class="stat-label">Grupos de Duplicidade</div>
        </div>
        <div class="stat-card">
            <div class="stat-number">{{ number_format($totalRegistros) }}</div>
            <div class="stat-label">Registros Duplicados</div>
        </div>
    </div>
</div>
 
<p style="color:var(--muted);font-size:.85rem;margin-bottom:1rem">
    QSOs com <code>valid_qso=1 AND valid_tempqso=1 AND valid_trocaband!=0</code>
    agrupados por par call_sent/call_rcvd + banda + modo + dia.
    Grupos com mais de 1 registro indicam duplicidade.
</p>
 
{{-- Busca --}}
<form method="GET" action="{{ route('relatorios.duplicados') }}"
      style="display:flex;gap:.5rem;align-items:center;margin-bottom:1.5rem">
    <input type="text" name="q" value="{{ $busca }}"
           placeholder="Filtrar por callsign..."
           style="padding:.4rem .7rem;border:1px solid #ddd;border-radius:var(--radius);
                  font-size:.88rem;min-width:220px">
    <button type="submit" class="btn btn-secondary" style="padding:.4rem .9rem">Buscar</button>
    @if($busca)
        <a href="{{ route('relatorios.duplicados') }}"
           class="btn btn-secondary" style="padding:.4rem .9rem">✕ Limpar</a>
    @endif
</form>
 
@if($totalGrupos === 0)
    <div class="alert alert-success" style="max-width:500px">
        ✅ Nenhuma duplicidade encontrada{{ $busca ? ' para "'.e($busca).'"' : '' }}.
    </div>
@else
 
@php $grupo = 1; @endphp
@foreach($grupos as $g)
<div style="margin-bottom:2rem">
 
    {{-- Cabeçalho do grupo — igual ao legado --}}
    <div style="background:#ddd;padding:.45rem .75rem;font-weight:bold;font-size:.85rem;
                border:1px solid #bbb;border-bottom:none">
        Grupo {{ $grupo++ }}:
        <strong>{{ $g['call_sent'] }}</strong> ↔ <strong>{{ $g['call_rcvd'] }}</strong>
        &nbsp;|&nbsp; Band: {{ $g['band'] }}
        &nbsp;|&nbsp; Mode: {{ $g['category_mode'] }}
        &nbsp;|&nbsp; Data/Hora: {{ $g['data_dia'] }}
        &nbsp;|&nbsp; Duplicados: {{ $g['qtd'] }}
        @if($g['qtd_confirmados'] > 0)
            &nbsp;|&nbsp;
            <span style="color:#2E7D32">
                ✅ {{ $g['qtd_confirmados'] }} confirmado(s)
            </span>
        @else
            &nbsp;|&nbsp;
            <span style="color:#C62828">❌ Nenhum confirmado</span>
        @endif
    </div>
 
    <table style="width:100%;border-collapse:collapse;font-size:.82rem">
        <thead>
            <tr style="background:#f0f0f0">
                <th style="padding:.35rem .5rem;border:1px solid #ccc;text-align:left">ID</th>
                <th style="padding:.35rem .5rem;border:1px solid #ccc">Call Sent</th>
                <th style="padding:.35rem .5rem;border:1px solid #ccc">Call Rcvd</th>
                <th style="padding:.35rem .5rem;border:1px solid #ccc">Band</th>
                <th style="padding:.35rem .5rem;border:1px solid #ccc">Mode</th>
                <th style="padding:.35rem .5rem;border:1px solid #ccc;white-space:nowrap">Data/Hora</th>
                <th style="padding:.35rem .5rem;border:1px solid #ccc;text-align:center">Confirmado</th>
                <th style="padding:.35rem .5rem;border:1px solid #ccc">Motivo</th>
            </tr>
        </thead>
        <tbody>
            @foreach($g['detalhes'] as $d)
            @php
                $bg = $d->confirmado == 1 ? '#f0fff0' : ($d->motivo === 'Duplicado' ? '#fff8f0' : '#fff');
            @endphp
            <tr style="background:{{ $bg }}">
                <td style="padding:.3rem .5rem;border:1px solid #e0e0e0;color:#aaa">{{ $d->id }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e0e0e0"><strong>{{ $d->call_sent }}</strong></td>
                <td style="padding:.3rem .5rem;border:1px solid #e0e0e0">{{ $d->call_rcvd }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e0e0e0">{{ $d->band }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e0e0e0">{{ $d->category_mode }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e0e0e0;white-space:nowrap">{{ $d->date_time }}</td>
                <td style="padding:.3rem .5rem;border:1px solid #e0e0e0;text-align:center;
                    {{ $d->confirmado == 1 ? 'color:#2E7D32;font-weight:bold' : 'color:#C62828' }}">
                    {{ $d->confirmado ?? '—' }}
                </td>
                <td style="padding:.3rem .5rem;border:1px solid #e0e0e0;font-size:.75rem;
                    {{ $d->motivo === 'Duplicado' ? 'color:#E65100' : 'color:#555' }}">
                    {{ $d->motivo ?? '—' }}
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>
@endforeach
 
<div style="background:#f0f0f0;border-radius:var(--radius);padding:.75rem 1rem;
            font-weight:bold;font-size:.9rem;margin-top:.5rem">
    Total de grupos de duplicidade: {{ $totalGrupos }}
    &nbsp;|&nbsp;
    Total de registros duplicados: {{ $totalRegistros }}
</div>
 
@endif
@endsection