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

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{{ __('Relatório dinâmico — Projetos e Programas') }}</title>
    <link rel="stylesheet" href="{{ asset('pmar/css/projetos-programas-relatorio-print.css') }}">
    <script src="{{ asset('pmar/js/projetos-programas-relatorio-print.js') }}"></script>
</head>

<body>
    <div class="report-header">
        <div class="report-title">{{ __('Relatório dinâmico — Projetos e Programas') }}</div>
        <div class="report-subtitle">{{ __('Impressão do relatório personalizado (colunas e agrupamentos selecionados).') }}</div>
    </div>

    @php
        $allowedColumns  = $allowedColumns  ?? [];
        $selectedColumns = $selectedColumns ?? [];
        $reportRows      = $reportRows      ?? [];

        // Denominação sempre aparece como 1ª coluna; excluir da lista de colunas extras.
        $printColsData = [];
        foreach ($selectedColumns as $c) {
            if ($c !== 'denominacao' && isset($allowedColumns[$c])) {
                $printColsData[] = $c;
            }
        }

        // Total de colunas: 1 (nome) + extras + 3 (qtd, previsto, disponível)
        $colCount = 1 + count($printColsData) + 3;
    @endphp

    <table class="report-table" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <th class="col-name">{{ __('Projeto / Grupo') }}</th>
                @foreach($printColsData as $colKey)
                    <th>{{ $allowedColumns[$colKey] }}</th>
                @endforeach
                <th class="col-num">{{ __('Qtd.') }}</th>
                <th class="col-money">{{ __('Total previsto') }}</th>
                <th class="col-money">{{ __('Total disponível') }}</th>
            </tr>
        </thead>
        <tbody>
            @forelse($reportRows as $row)
                @php
                    $type  = (string) ($row['type'] ?? 'data');
                    $depth = (int) ($row['depth'] ?? 0);
                    $pad   = 10 * $depth;
                @endphp

                @if($type === 'group_header')
                    <tr class="row-group">
                        <td colspan="{{ $colCount }}" class="cell-group">
                            <strong>{{ (string) ($row['label'] ?? '') }}</strong>
                        </td>
                    </tr>
                    @continue
                @endif

                @if($type === 'subtotal')
                    <tr class="row-subtotal">
                        <td colspan="{{ 1 + count($printColsData) }}" class="cell-label text-end">
                            <strong>{{ __('Subtotal') }}</strong>
                        </td>
                        <td class="text-center"><strong>{{ $row['totals']['count'] ?? '' }}</strong></td>
                        <td class="text-end"><strong>R$ {{ number_format((float)($row['totals']['orcamento_previsto'] ?? 0), 2, ',', '.') }}</strong></td>
                        <td class="text-end"><strong>R$ {{ number_format((float)($row['totals']['orcamento_disponivel'] ?? 0), 2, ',', '.') }}</strong></td>
                    </tr>
                    @continue
                @endif

                @if($type === 'grand_total')
                    <tr class="row-total">
                        <td colspan="{{ 1 + count($printColsData) }}" class="cell-label text-end">
                            {{ __('Total geral') }}
                        </td>
                        <td class="text-center">{{ $row['totals']['count'] ?? '' }}</td>
                        <td class="text-end">R$ {{ number_format((float)($row['totals']['orcamento_previsto'] ?? 0), 2, ',', '.') }}</td>
                        <td class="text-end">R$ {{ number_format((float)($row['totals']['orcamento_disponivel'] ?? 0), 2, ',', '.') }}</td>
                    </tr>
                    @continue
                @endif

                {{-- Linha de dado --}}
                <tr>
                    <td class="cell-name" style="padding-left: {{ $pad }}px;">
                        {{ (string) ($row['label'] ?? '') }}
                    </td>
                    @foreach($printColsData as $colKey)
                        <td class="text-center">
                            @if($colKey === 'prazo')
                                {!! nl2br(e($row['cols'][$colKey] ?? '')) !!}
                            @else
                                {{ $row['cols'][$colKey] ?? '' }}
                            @endif
                        </td>
                    @endforeach
                    <td class="text-center">1</td>
                    <td class="text-end">R$ {{ number_format((float)($row['totals']['orcamento_previsto'] ?? 0), 2, ',', '.') }}</td>
                    <td class="text-end">R$ {{ number_format((float)($row['totals']['orcamento_disponivel'] ?? 0), 2, ',', '.') }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="{{ $colCount }}" class="text-center">—</td>
                </tr>
            @endforelse
        </tbody>
    </table>
</body>

</html>
