<!DOCTYPE html>
<html lang="ur" dir="rtl">
<head>
    <meta charset="UTF-8">
    <title>Aging Payables — Print</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: Arial, sans-serif; font-size: 12px; background: #fff; color: #000; }
        .container { width: 96%; margin: 18px auto; }

        /* Header */
        .rpt-header { text-align: center; margin-bottom: 10px; }
        .rpt-header .co-name  { font-size: 22px; font-weight: 700; letter-spacing: 1px; }
        .rpt-header .co-sub   { font-size: 14px; font-weight: 600; margin-top: 2px; }
        .rpt-header .co-range { font-size: 12px; margin-top: 4px; color: #222; }

        /* Table */
        table { width: 100%; border-collapse: collapse; margin-top: 10px; direction: rtl; }

        thead th {
            border: 1px solid #000;
            padding: 8px 10px;
            text-align: center;
            font-size: 13px;
            font-weight: 700;
            background: #fff;
            white-space: nowrap;
        }

        /* Solid side borders, dashed bottom — exact image match */
        tbody td {
            border-left:   1px solid #000;
            border-right:  1px solid #000;
            border-top:    none;
            border-bottom: 1px dashed #555;
            padding: 7px 10px;
            vertical-align: middle;
            font-size: 12px;
        }

        td.col-party  { text-align: right;  font-size: 13px; font-weight: 600; }
        td.col-num    { text-align: center; white-space: nowrap; width: 115px; }
        td.col-haliah { text-align: center; font-weight: 700;   white-space: nowrap; width: 125px; }

        tr.total-row td {
            border-top:    2px solid #000 !important;
            border-bottom: 2px solid #000 !important;
            background: #f0f0f0;
            font-weight: 700;
            text-align: center;
            font-size: 12px;
            padding: 8px 10px;
        }

        .no-print {
            margin-top: 20px;
            display: inline-block;
            padding: 9px 22px;
            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; }
            tfoot { display: table-footer-group; }
            tr    { page-break-inside: avoid; }
        }
    </style>
</head>
<body>
<div class="container">

    {{-- Header --}}
    <div class="rpt-header">
        <div class="co-name">M.D.S</div>
        <div class="co-sub">ادائیگی ادھاری &nbsp;&nbsp;&nbsp; Aging Payables</div>
        <div class="co-range">
            مورخہ:
            {{ $fromDate ? \Carbon\Carbon::parse($fromDate)->format('d/m/Y') : '—' }}
            تا
            {{ $toDate   ? \Carbon\Carbon::parse($toDate)->format('d/m/Y')   : '—' }}
        </div>
    </div>

    @if(empty($reportData['rows']))
        <p style="text-align:center;padding:30px;color:#888;">کوئی اندراج نہیں ملا۔</p>
    @else

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

        <table>
            <thead>
                <tr>
                    <th>پارٹی</th>
                    <th>سابقہ بیلنس</th>
                    <th>خرید وغیرہ</th>
                    <th>ادائیگی</th>
                    <th>حالیہ بیلنس</th>
                </tr>
            </thead>
            <tbody>

                @foreach($reportData['rows'] as $row)
                    <tr>
                        {{-- پارٹی --}}
                        <td class="col-party">{{ $row['supplier_name'] }}</td>

                        {{-- سابقہ بیلنس = from_date سے پہلے Debit − Credit --}}
                        <td class="col-num">{{ $fmt($row['sabiqah']) }}</td>

                        {{-- خرید وغیرہ = period میں Debit vouchers --}}
                        <td class="col-num">
                            {{ $row['kharid'] == 0 ? '-' : number_format($row['kharid'], 0) }}
                        </td>

                        {{-- ادائیگی = period میں Credit vouchers --}}
                        <td class="col-num">
                            {{ $row['adaegi'] == 0 ? '0' : number_format($row['adaegi'], 0) }}
                        </td>

                        {{-- حالیہ بیلنس = سابقہ + خرید − ادائیگی --}}
                        <td class="col-haliah">{{ $fmt($row['haliah'], '0') }}</td>
                    </tr>
                @endforeach

                {{-- کُل مجموعہ --}}
                <tr class="total-row">
                    <td>کُل مجموعہ</td>
                    <td>{{ $fmt($reportData['grandSabiqah'], '0') }}</td>
                    <td>{{ number_format($reportData['grandKharid'], 0) }}</td>
                    <td>{{ number_format($reportData['grandAdaegi'], 0) }}</td>
                    <td>{{ $fmt($reportData['grandHaliah'], '0') }}</td>
                </tr>

            </tbody>
        </table>
    @endif

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

</div>
</body>
</html>