@php
    $months = $reportData['months'];
    $tree   = $reportData['tree'];
    $supplierSection = $reportData['supplierSection'];
    $customerSection = $reportData['customerSection'];
    $colCount = 12 + 3; // months + Average + Total + Account + Code

    // Format: negative → (1,234) | zero → '-' | positive → 1,234
    $fmt = fn($v) => $v == 0
        ? '-'
        : ($v < 0
            ? '(' . number_format(abs($v), 0) . ')'
            : number_format($v, 0));
@endphp

{{-- ══ Company Header ══ --}}
<div class="acc-header">
    <div class="co-name">M.D.S</div>
    <div class="co-sub">مابوار کھاتہ موازنہ &nbsp;&nbsp; Accounts Comparision Report</div>
    <div class="co-date">
        Start Date : {{ \Carbon\Carbon::parse($reportData['startDate'])->format('d/m/Y') }}
    </div>
</div>

<table class="acc-table">
    <thead>
        <tr>
            <th class="th-code">Account Code</th>
            <th class="th-account">Account</th>
            @foreach($months as $m)
                <th>{{ $m->format('M,y') }}</th>
            @endforeach
            <th>Average</th>
            <th>Total</th>
        </tr>
    </thead>
    <tbody>

        {{-- ════ SECTION 1: Account Hierarchy ════ --}}
        @foreach($tree as $ma)

            {{-- Main Account heading --}}
            <tr class="row-main">
                <td colspan="{{ $colCount + 2 }}" class="heading-cell">
                    {{ strtoupper($ma['name']) }}
                </td>
            </tr>

            @foreach($ma['children'] as $ca)
                @foreach($ca['children'] as $sca)
                    @foreach($sca['children'] as $tacc)
                        @if($tacc['total'] != 0)
                        <tr class="row-tacc">
                            <td class="td-code">{{ $tacc['code'] }}</td>
                            <td class="td-account">{{ $tacc['name'] }}</td>
                            @foreach($tacc['monthly'] as $val)
                                <td class="td-num {{ $val < 0 ? 'neg' : '' }}">{{ $fmt($val) }}</td>
                            @endforeach
                            <td class="td-num {{ $tacc['average'] < 0 ? 'neg' : '' }}">{{ $fmt($tacc['average']) }}</td>
                            <td class="td-num {{ $tacc['total']   < 0 ? 'neg' : '' }}">{{ $fmt($tacc['total']) }}</td>
                        </tr>
                        @endif
                    @endforeach
                @endforeach

                @if($ca['total'] != 0)
                {{-- Control Account subtotal --}}
                <tr class="row-control">
                    <td class="td-code"></td>
                    <td class="td-account">{{ $ca['name'] }}</td>
                    @foreach($ca['monthly'] as $val)
                        <td class="td-num {{ $val < 0 ? 'neg' : '' }}">{{ $fmt($val) }}</td>
                    @endforeach
                    <td class="td-num {{ $ca['average'] < 0 ? 'neg' : '' }}">{{ $fmt($ca['average']) }}</td>
                    <td class="td-num {{ $ca['total']   < 0 ? 'neg' : '' }}">{{ $fmt($ca['total']) }}</td>
                </tr>
                @endif
            @endforeach

            @if($ma['total'] != 0)
            {{-- Main Account total --}}
            <tr class="row-ma-total">
                <td class="td-code"></td>
                <td class="td-account">{{ $ma['name'] }}</td>
                @foreach($ma['monthly'] as $val)
                    <td class="td-num {{ $val < 0 ? 'neg' : '' }}">{{ $fmt($val) }}</td>
                @endforeach
                <td class="td-num {{ $ma['average'] < 0 ? 'neg' : '' }}">{{ $fmt($ma['average']) }}</td>
                <td class="td-num {{ $ma['total']   < 0 ? 'neg' : '' }}">{{ $fmt($ma['total']) }}</td>
            </tr>
            @endif

        @endforeach

        {{-- ════ SECTION 2: SUPPLIERS ════ --}}
        @if(!empty($supplierSection['children']))
            <tr class="row-main">
                <td colspan="{{ $colCount + 2 }}" class="heading-cell">SUPPLIERS — سپلائرز</td>
            </tr>
            @foreach($supplierSection['children'] as $row)
                <tr class="row-tacc">
                    <td class="td-code">{{ $row['code'] }}</td>
                    <td class="td-account">{{ $row['name'] }}</td>
                    @foreach($row['monthly'] as $val)
                        <td class="td-num {{ $val < 0 ? 'neg' : '' }}">{{ $fmt($val) }}</td>
                    @endforeach
                    <td class="td-num {{ $row['average'] < 0 ? 'neg' : '' }}">{{ $fmt($row['average']) }}</td>
                    <td class="td-num {{ $row['total']   < 0 ? 'neg' : '' }}">{{ $fmt($row['total']) }}</td>
                </tr>
            @endforeach
            {{-- Suppliers total --}}
            <tr class="row-control">
                <td class="td-code"></td>
                <td class="td-account" style="font-weight:700;">Suppliers Total</td>
                @foreach($supplierSection['monthly'] as $val)
                    <td class="td-num {{ $val < 0 ? 'neg' : '' }}">{{ $fmt($val) }}</td>
                @endforeach
                <td class="td-num {{ $supplierSection['average'] < 0 ? 'neg' : '' }}">{{ $fmt($supplierSection['average']) }}</td>
                <td class="td-num {{ $supplierSection['total']   < 0 ? 'neg' : '' }}">{{ $fmt($supplierSection['total']) }}</td>
            </tr>
        @endif

        {{-- ════ SECTION 3: CUSTOMERS ════ --}}
        @if(!empty($customerSection['children']))
            <tr class="row-main">
                <td colspan="{{ $colCount + 2 }}" class="heading-cell">CUSTOMERS — کسٹمرز</td>
            </tr>
            @foreach($customerSection['children'] as $row)
                <tr class="row-tacc">
                    <td class="td-code">{{ $row['code'] }}</td>
                    <td class="td-account">{{ $row['name'] }}</td>
                    @foreach($row['monthly'] as $val)
                        <td class="td-num {{ $val < 0 ? 'neg' : '' }}">{{ $fmt($val) }}</td>
                    @endforeach
                    <td class="td-num {{ $row['average'] < 0 ? 'neg' : '' }}">{{ $fmt($row['average']) }}</td>
                    <td class="td-num {{ $row['total']   < 0 ? 'neg' : '' }}">{{ $fmt($row['total']) }}</td>
                </tr>
            @endforeach
            {{-- Customers total --}}
            <tr class="row-control">
                <td class="td-code"></td>
                <td class="td-account" style="font-weight:700;">Customers Total</td>
                @foreach($customerSection['monthly'] as $val)
                    <td class="td-num {{ $val < 0 ? 'neg' : '' }}">{{ $fmt($val) }}</td>
                @endforeach
                <td class="td-num {{ $customerSection['average'] < 0 ? 'neg' : '' }}">{{ $fmt($customerSection['average']) }}</td>
                <td class="td-num {{ $customerSection['total']   < 0 ? 'neg' : '' }}">{{ $fmt($customerSection['total']) }}</td>
            </tr>
        @endif

    </tbody>
</table>
