<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="UTF-8">
    <title>Accounts Comparison Report</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: Arial, sans-serif; font-size: 10.5px; background: #fff; }
        .container { width: 98%; margin: 14px auto; }

        .acc-header { text-align: center; margin-bottom: 10px; }
        .acc-header .co-name { font-size: 20px; font-weight: 700; letter-spacing: 1px; }
        .acc-header .co-sub  { font-size: 13px; font-weight: 600; margin-top: 2px; }
        .acc-header .co-date { font-size: 11px; color: #333; margin-top: 3px; }

        .acc-table {
            width: 100%; border-collapse: collapse;
            font-size: 10px; white-space: nowrap;
        }
        .acc-table thead th {
            border: 1px solid #000; padding: 6px 7px;
            text-align: center; font-weight: 700; background: #fff;
        }
        .acc-table thead th.th-account { text-align: right; min-width: 170px; }
        .acc-table thead th.th-code    { text-align: right; width: 95px; }

        .acc-table tbody td {
            border-left: 1px solid #000; border-right: 1px solid #000;
            border-top: none; border-bottom: 1px dashed #555;
            padding: 5px 7px; vertical-align: middle;
        }
        .acc-table td.td-account { text-align: right; }
        .acc-table td.td-code    { text-align: right; font-size: 9.5px; }
        .acc-table td.td-num     { text-align: center; }

        .acc-table tr.row-main td.heading-cell {
            background: #fff; font-weight: 900; font-size: 12px; color: #00c;
            border-bottom: none !important; border-top: 1px solid #000 !important;
            padding-top: 12px; text-align: right;
        }
        .acc-table tr.row-control td {
            background: #f0f0f0; font-weight: 700;
            border-top: 2px solid #000 !important;
            border-bottom: 1px solid #000 !important;
        }
        .acc-table tr.row-ma-total td {
            background: #e8eeff; font-weight: 800; color: #00c;
            border-top: 2px solid #000 !important;
            border-bottom: 2px solid #000 !important;
        }
        .acc-table tr.row-tacc td.td-account { padding-right: 18px; }

        .neg { color: #c00; }

        .no-print {
            margin-top: 18px; display: inline-block; padding: 8px 20px;
            background: #007bff; color: #fff; border: none;
            cursor: pointer; font-size: 13px; border-radius: 3px;
        }
        @media print {
            .no-print { display: none; }
            thead { display: table-header-group; }
            tr    { page-break-inside: avoid; }
        }
    </style>
</head>
<body>
<div class="container">

    @php
        $months          = $reportData['months'];
        $tree            = $reportData['tree'];
        $supplierSection = $reportData['supplierSection'];
        $customerSection = $reportData['customerSection'];
        $colCount        = 12 + 3;

        $fmt = fn($v) => $v == 0
            ? '-'
            : ($v < 0
                ? '(' . number_format(abs($v), 0) . ')'
                : number_format($v, 0));
    @endphp

    <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>

            {{-- ── Account Hierarchy ── --}}
            @foreach($tree as $ma)
                <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)
                    <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)
                <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

            {{-- ── 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
                <tr class="row-control">
                    <td class="td-code"></td>
                    <td class="td-account">Suppliers Total</td>
                    @foreach($supplierSection['monthly'] as $val)
                        <td class="td-num {{ $val < 0 ? 'neg' : '' }}">{{ $fmt($val) }}</td>
                    @endforeach
                    <td class="td-num">{{ $fmt($supplierSection['average']) }}</td>
                    <td class="td-num">{{ $fmt($supplierSection['total']) }}</td>
                </tr>
            @endif

            {{-- ── 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
                <tr class="row-control">
                    <td class="td-code"></td>
                    <td class="td-account">Customers Total</td>
                    @foreach($customerSection['monthly'] as $val)
                        <td class="td-num {{ $val < 0 ? 'neg' : '' }}">{{ $fmt($val) }}</td>
                    @endforeach
                    <td class="td-num">{{ $fmt($customerSection['average']) }}</td>
                    <td class="td-num">{{ $fmt($customerSection['total']) }}</td>
                </tr>
            @endif

        </tbody>
    </table>

    <button class="no-print" onclick="window.print()">Print Report</button>
</div>
</body>
</html>
