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

<head>
    <meta charset="UTF-8">
    <title>Relatório Matricial – Indicadores de TI {{ $ano }}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'DejaVu Sans', sans-serif;
            font-size: 8px;
            color: #000;
            margin: 14px 18px;
        }

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

        .meta {
            font-size: 7.5px;
            color: #555;
            margin-bottom: 14px;
            line-height: 1.6;
        }

        .meta strong {
            color: #000;
        }

        .sec-title {
            font-weight: bold;
            font-size: 10px;
            border-left: 3px solid #1a1a2e;
            padding-left: 8px;
            margin: 16px 0 10px;
        }

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

        thead th {
            background: #1a1a2e;
            color: #fff;
            padding: 5px 4px;
            font-size: 6.5px;
            text-align: center;
            font-weight: 600;
            border: 0.5px solid #333;
        }

        thead th:first-child {
            text-align: left;
            width: 30%;
        }

        tbody td {
            padding: 4px;
            border: 0.5px solid #ccc;
            text-align: center;
            font-size: 7.5px;
        }

        tbody td:first-child {
            text-align: left;
            padding-left: 8px;
        }

        .total-geral td {
            background-color: #cfe2ff;
            font-weight: bold;
            font-size: 8.5px;
            border-top: 2px solid #000;
            border-bottom: 2px solid #000;
        }

        .zero {
            color: #bbb;
        }

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

<body>

    <h1>Relatório Matricial – Indicadores de TI</h1>
    <div class="meta">
        <strong>Ano de referência:</strong> {{ $ano }}<br>
        <strong>Total geral de chamados:</strong> {{ $totalGeral }}<br>
        <strong>Gerado em:</strong> {{ now()->format('d/m/Y \à\s H:i') }}<br>
        @if (!empty($filters['data_inicio']) || !empty($filters['data_fim']))
            <strong>Período:</strong>
            {{ !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') : '—' }}<br>
        @endif
        @if (!empty($filters['status']))
            <strong>Status:</strong>
            {{ __(\App\Models\IndicadoresTi\Chamado::STATUS[$filters['status']] ?? $filters['status']) }}<br>
        @endif
        @if (!empty($filters['prioridade']))
            <strong>Prioridade:</strong>
            {{ __(\App\Models\IndicadoresTi\Chamado::PRIORIDADE[$filters['prioridade']] ?? $filters['prioridade']) }}<br>
        @endif
    </div>

    @php
        use App\Services\Reports\SvgChartBuilder;

        $chart = new SvgChartBuilder();
        $mensal = $totalMensal['mensal'];

        $barData = [];
        foreach ($meses as $m => $nome) {
            $barData[$nome] = $mensal[$m] ?? 0;
        }
        $svgBar = $chart->barChart($barData, 800, 110, ['#4F46E5']);

    @endphp

    <div class="sec-title">Evolução Mensal de Chamados – {{ $ano }}</div>
    <div style="text-align:center;margin-bottom:20px;">{!! $svgBar !!}</div>

    <div class="sec-title">1. Total de Chamados por Mês – {{ $ano }}</div>
    <table>
        <thead>
            <tr>
                <th></th>
                @foreach ($meses as $nome)
                    <th>{{ mb_strtoupper(mb_substr($nome, 0, 3)) }}</th>
                @endforeach
                <th>TOTAL</th>
            </tr>
        </thead>
        <tbody>
            <tr class="total-geral">
                <td><strong>Total de Chamados</strong></td>
                @foreach (range(1, 12) as $m)
                    <td class="{{ ($totalMensal['mensal'][$m] ?? 0) == 0 ? 'zero' : '' }}">
                        <strong>{{ $totalMensal['mensal'][$m] ?? 0 }}</strong>
                    </td>
                @endforeach
                <td><strong>{{ $totalMensal['total'] }}</strong></td>
            </tr>
        </tbody>
    </table>

    <div class="sec-title">2. Indicadores por Setor – {{ $ano }}</div>
    <table>
        <thead>
            <tr>
                <th>Setor</th>
                @foreach ($meses as $nome)
                    <th>{{ mb_strtoupper(mb_substr($nome, 0, 3)) }}</th>
                @endforeach
                <th>TOTAL</th>
            </tr>
        </thead>
        <tbody>
            @php
                $totMeses = array_fill(1, 12, 0);
                $totGeral = 0;
            @endphp
            @foreach ($matrizSetores as $linha)
                @php
                    foreach (range(1, 12) as $m) {
                        $totMeses[$m] += $linha['mensal'][$m] ?? 0;
                    }
                    $totGeral += $linha['total'];
                @endphp
                <tr>
                    <td>{{ $linha['setor'] }}</td>
                    @foreach (range(1, 12) as $m)
                        <td class="{{ ($linha['mensal'][$m] ?? 0) == 0 ? 'zero' : '' }}">
                            {{ $linha['mensal'][$m] ?? 0 }}
                        </td>
                    @endforeach
                    <td><strong>{{ $linha['total'] }}</strong></td>
                </tr>
            @endforeach
            <tr class="total-geral">
                <td><strong>TOTAL GERAL</strong></td>
                @foreach (range(1, 12) as $m)
                    <td class="{{ $totMeses[$m] == 0 ? 'zero' : '' }}">
                        <strong>{{ $totMeses[$m] }}</strong>
                    </td>
                @endforeach
                <td><strong>{{ $totGeral }}</strong></td>
            </tr>
        </tbody>
    </table>

    <div class="footer">
        Sistema de Indicadores de TI &bull; Relatório Matricial {{ $ano }} &mdash; 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>
