<!DOCTYPE html>
<html lang="pt-BR">

<head>
    <meta charset="UTF-8">
    <title>Relatório Resumido – Indicadores de TI</title>
    <style>
        body {
            font-family: 'DejaVu Sans', sans-serif;
            font-size: 9px;
            color: #000;
            margin: 14px 18px;
        }

        h1 {
            font-size: 14px;
            margin-bottom: 2px;
            color: #1a1a2e;
        }

        .meta {
            font-size: 8px;
            color: #444;
            margin-bottom: 10px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 12px;
        }

        th {
            background: #1a1a2e;
            color: #fff;
            padding: 5px;
            font-size: 8px;
            text-align: left;
        }

        td {
            padding: 4px;
            border-bottom: 1px solid #ddd;
        }

        tfoot tr {
            background: #e9ecef;
            font-weight: 700;
        }

        tfoot td {
            padding: 4px;
        }

        .badge {
            padding: 1px 5px;
            border-radius: 6px;
            font-size: 7px;
            font-weight: bold;
        }

        .b-ok {
            background: #10B981;
            color: #fff;
        }

        .b-pen {
            background: #F59E0B;
            color: #fff;
        }

        .b-prog {
            background: #4F46E5;
            color: #fff;
        }

        .b-canc {
            background: #6c757d;
            color: #fff;
        }

        .text-right {
            text-align: right;
        }

        .text-center {
            text-align: center;
        }

        .text-muted {
            color: #6c757d;
        }

        .fw-bold {
            font-weight: 700;
        }

        .footer {
            margin-top: 18px;
            padding-top: 6px;
            border-top: 1px solid #ccc;
            font-size: 7px;
            color: #888;
        }
    </style>
</head>

<body>

    <h1>Relatório Resumido – Indicadores de TI</h1>
    <div class="meta">
        Gerado em {{ now()->format('d/m/Y H:i') }} | Total: {{ $total }} chamados
        @if (!empty($filters['data_inicio']) || !empty($filters['data_fim']))
            | Período:
            {{ !empty($filters['data_inicio']) ? \Carbon\Carbon::parse($filters['data_inicio'])->format('d/m/Y') : '—' }}
            a {{ !empty($filters['data_fim']) ? \Carbon\Carbon::parse($filters['data_fim'])->format('d/m/Y') : '—' }}
        @endif
    </div>

    <table style="margin-bottom:20px;">
        <tr>
            @include('admin.indicadoresTi.reports.partials._kpi_card', [
                'value' => $total,
                'label' => 'Total',
                'bg' => '#f4f4f4',
                'color' => '#111',
                'width' => '25%',
            ])
            @include('admin.indicadoresTi.reports.partials._kpi_card', [
                'value' => $porStatus['atendido'] ?? 0,
                'label' => 'Atendidos',
                'bg' => '#d1fae5',
                'color' => '#065f46',
                'width' => '25%',
            ])
            @include('admin.indicadoresTi.reports.partials._kpi_card', [
                'value' => $porStatus['pendente'] ?? 0,
                'label' => 'Pendentes',
                'bg' => '#fef3c7',
                'color' => '#92400e',
                'width' => '25%',
            ])
            @include('admin.indicadoresTi.reports.partials._kpi_card', [
                'value' => $porStatus['em_andamento'] ?? 0,
                'label' => 'Em Andamento',
                'bg' => '#dbeafe',
                'color' => '#1e40af',
                'width' => '25%',
            ])
        </tr>
    </table>

    @php
        use App\Services\Reports\SvgChartBuilder;
        use App\Models\IndicadoresTi\Chamado;

        $chart = new SvgChartBuilder();

        $statusColors = ['#4F46E5', '#F59E0B', '#10B981', '#6c757d'];
        $statusData = [];
        foreach (Chamado::STATUS as $v => $l) {
            if (($porStatus[$v] ?? 0) > 0) {
                $statusData[__($l)] = $porStatus[$v];
            }
        }

        $taxaAtend = $total > 0 ? round((($porStatus['atendido'] ?? 0) / $total) * 100, 1) : 0;

        $setorData = $porSetor
            ->sortByDesc('total')
            ->take(7)
            ->mapWithKeys(fn($r) => [$r['nome'] => $r['total']])
            ->toArray();

        $svgDonut = $chart->donutChart($statusData, 110, $statusColors);
        $svgGauge = $chart->gauge($porStatus['atendido'] ?? 0, max($total, 1), 100, '#10B981');
    @endphp

    <table style="margin-bottom:20px;">
        <tr>
            <td
                style="width:50%;vertical-align:top;padding-right:12px;border-right:1px solid #e5e7eb;text-align:center;">
                <div
                    style="font-weight:bold;font-size:11px;border-left:3px solid #1a1a2e;padding-left:5px;margin-bottom:10px;">
                    Status dos Chamados</div>
                <table style="border-collapse:collapse;width:auto;margin:0 auto;">
                    <tr>
                        <td style="text-align:center;padding:0;border:none;">{!! $svgDonut !!}</td>
                        <td style="vertical-align:middle;padding-left:8px;border:none;">
                            @php $ci = 0; @endphp
                            @foreach (Chamado::STATUS as $v => $l)
                                @php $q = $porStatus[$v] ?? 0; @endphp
                                @if ($q > 0)
                                    @php $p = $total > 0 ? round($q / $total * 100, 1) : 0; @endphp
                                    <div style="margin-bottom:3px;font-size:10px;white-space:nowrap;">
                                        <span
                                            style="display:inline-block;width:8px;height:8px;background:{{ $statusColors[$ci] }};border-radius:2px;margin-right:3px;"></span>
                                        {{ __($l) }}: <strong>{{ $q }}</strong> ({{ $p }}%)
                                    </div>
                                    @php $ci++; @endphp
                                @endif
                            @endforeach
                        </td>
                    </tr>
                </table>
            </td>

            <td style="width:50%;vertical-align:top;text-align:center;padding-left:12px;">
                <div
                    style="font-weight:bold;font-size:11px;border-left:3px solid #1a1a2e;padding-left:5px;margin-bottom:10px;">
                    Taxa de Atendimento</div>
                {!! $svgGauge !!}
                <div style="font-size:8.5px;color:#6b7280;margin-top:2px;">{{ $porStatus['atendido'] ?? 0 }} de
                    {{ $total }}</div>
            </td>
        </tr>
    </table>

    <table style="margin-bottom:25px;">
        <tr>
            <td style="text-align:center;vertical-align:top;">
                <div
                    style="font-weight:bold;font-size:11px;border-left:3px solid #1a1a2e;padding-left:5px;margin-bottom:6px;">
                    Volume por Setor</div>
                @php $svgSetores = $chart->horizontalBars($setorData, 500, max(count($setorData) * 16, 60)); @endphp
                {!! $svgSetores !!}
            </td>
        </tr>
    </table>

    <div style="font-weight:bold;font-size:11px;border-left:3px solid #1a1a2e;padding-left:5px;margin:10px 0 5px;">
        Distribuição por Status</div>
    <table>
        <thead>
            <tr>
                <th>Status</th>
                <th class="text-right">Qtd</th>
                <th class="text-right">%</th>
            </tr>
        </thead>
        <tbody>
            @foreach (Chamado::STATUS as $valor => $label)
                @php $qtd = $porStatus[$valor] ?? 0; @endphp
                @if ($qtd > 0)
                    @php
                        $cls = match ($valor) {
                            'atendido' => 'b-ok',
                            'pendente' => 'b-pen',
                            'em_andamento' => 'b-prog',
                            default => 'b-canc',
                        };
                        $pct = $total > 0 ? round(($qtd / $total) * 100, 1) : 0;
                    @endphp
                    <tr>
                        <td><span class="badge {{ $cls }}">{{ __($label) }}</span></td>
                        <td class="text-right fw-bold">{{ $qtd }}</td>
                        <td class="text-right text-muted">{{ $pct }}%</td>
                    </tr>
                @endif
            @endforeach
        </tbody>
        <tfoot>
            <tr>
                <td>Total</td>
                <td class="text-right">{{ $total }}</td>
                <td class="text-right">100%</td>
            </tr>
        </tfoot>
    </table>

    @if ($porSetor->count())
        <div style="font-weight:bold;font-size:11px;border-left:3px solid #1a1a2e;padding-left:5px;margin:10px 0 5px;">
            Chamados por Setor</div>
        <table>
            <thead>
                <tr>
                    <th>Setor</th>
                    <th class="text-right">Total</th>
                    <th class="text-right">Atend.</th>
                    <th class="text-right">Pend.</th>
                    <th class="text-right">%</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($porSetor->sortByDesc('total') as $row)
                    @php $pct = $total > 0 ? round($row['total'] / $total * 100, 1) : 0; @endphp
                    <tr>
                        <td>{{ $row['nome'] }}</td>
                        <td class="text-right fw-bold">{{ $row['total'] }}</td>
                        <td class="text-right">{{ $row['atendidos'] }}</td>
                        <td class="text-right">{{ $row['pendentes'] }}</td>
                        <td class="text-right text-muted">{{ $pct }}%</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif

    @if ($porSubassunto->count())
        <div style="font-weight:bold;font-size:11px;border-left:3px solid #1a1a2e;padding-left:5px;margin:10px 0 5px;">
            Por Assunto / Subassunto</div>
        <table>
            <thead>
                <tr>
                    <th>Assunto &rsaquo; Subassunto</th>
                    <th class="text-right">Total</th>
                    <th class="text-right">Atendidos</th>
                    <th class="text-right">%</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($porSubassunto->sortByDesc('total')->take(15) as $row)
                    @php $pct = $total > 0 ? round($row['total'] / $total * 100, 1) : 0; @endphp
                    <tr>
                        <td>{{ $row['chave'] }}</td>
                        <td class="text-right fw-bold">{{ $row['total'] }}</td>
                        <td class="text-right">{{ $row['atendidos'] }}</td>
                        <td class="text-right">{{ $pct }}%</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif

    <div class="footer">Sistema de Indicadores de TI – Confidencial</div>

    <script type="text/php">
    if (isset($pdf)) {
        $font = $fontMetrics->get_font("DejaVu Sans", "normal");
        $w    = $pdf->get_width();
        $h    = $pdf->get_height();
        $pdf->page_text($w / 2 - 15, $h - 16, "Página {PAGE_NUM} / {PAGE_COUNT}", $font, 6, [0.5, 0.5, 0.5]);
    }
</script>

</body>

</html>
