<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="UTF-8">
    <title>Purchase Billing Vehicle Register</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            font-family: 'Arial', sans-serif;
            font-size: 10px;
            background: #fff;
            color: #000;
            direction: rtl;
        }

        .container {
            width: 98%;
            margin: 12px auto;
        }

        /* ─── Header ─────────────────────────────────────── */
        .rpt-header {
            text-align: center;
            margin-bottom: 8px;
            line-height: 1.6;
        }
        .rpt-header .co-name {
            font-size: 19px;
            font-weight: 700;
            letter-spacing: 1px;
        }
        .rpt-header .co-sub {
            font-size: 12.5px;
            font-weight: 600;
            direction: rtl;
            unicode-bidi: plaintext;
        }
        .rpt-header .co-info {
            font-size: 10px;
            color: #222;
            direction: ltr;
        }

        /* ─── Main Report Table ───────────────────────────── */
        .rpt-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 9.5px;
            white-space: nowrap;
            direction: rtl;
        }

        /* Header row — solid top & bottom border only */
        .rpt-table thead tr {
            border-top: 1.5px solid #000;
            border-bottom: 1.5px solid #000;
        }
        .rpt-table thead th {
            padding: 4px 6px;
            text-align: center;
            font-weight: 700;
            background: #fff;
            vertical-align: bottom;
            border: none;
            border-left: 1px solid #ccc;
            direction: rtl;
            line-height: 1.3;
        }
        .rpt-table thead th:first-child { border-left: none; }

        /* Body cells — NO borders */
        .rpt-table tbody td {
            padding: 3px 6px;
            vertical-align: middle;
            border: none;
        }

        .rpt-table td.td-num  { text-align: left;   direction: ltr; font-variant-numeric: tabular-nums; }
        .rpt-table td.td-rtxt { text-align: right;  direction: rtl; }
        .rpt-table td.td-ctxt { text-align: center; direction: ltr; }

        /* Vehicle heading */
        .row-vehicle td {
            font-weight: 900;
            font-size: 11.5px;
            color: #c00;
            text-align: right;
            direction: rtl;
            padding-top: 10px;
            padding-bottom: 2px;
        }

        /* Vehicle subtotal row */
        .row-vtotal td {
            font-weight: 700;
            color: #c00;
            border-top:    none !important;
            border-bottom: 1px dashed #888 !important;
            padding-top:    4px;
            padding-bottom: 5px;
        }

        /* Grand total row in main table */
        .row-grand td {
            font-weight: 800;
            border-top:    2px solid #000 !important;
            border-bottom: 2px solid #000 !important;
            padding-top:    5px;
            padding-bottom: 5px;
        }

        /* ─── Grand Totals Box (matching screenshot) ─────── */
        /*
            Full bordered rectangle, RTL layout.
            Columns (right→left as seen on screen):
            كُل | گاڑی | بوری | قیمت | کرایہ محصول | نقد | بختہ بکری
                        | وزن  |      | مزدوری      | کمیشن
                               |      | مارکیٹ فیس  | کل خرچہ
                               |      | ڈاک خرچہ    |
                               |      | خرچہ متفرقات|
        */
        .totals-box {
            width: 100%;
            border: 1.5px solid #000;
            border-collapse: collapse;
            margin-top: 18px;
            direction: rtl;
            font-size: 10px;
        }
        .totals-box td {
            padding: 4px 10px;
            vertical-align: top;
            border: none;
            white-space: nowrap;
        }
        .totals-box .lbl {
            font-weight: 700;
            direction: rtl;
        }
        .totals-box .val {
            direction: ltr;
            font-variant-numeric: tabular-nums;
            padding-left: 4px;
        }
        /* label+value pair inline */
        .tv {
            display: inline-flex;
            gap: 4px;
            align-items: baseline;
            direction: rtl;
        }
        .tv .lbl { font-weight: 700; }
        .tv .val { direction: ltr; }

        /* ─── Print ──────────────────────────────────────── */
        .no-print {
            display: inline-block;
            margin-top: 14px;
            padding: 7px 18px;
            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
        $fmtT = fn($v) => $v == 0
            ? ''
            : ($v < 0
                ? '(' . number_format(abs($v), 0) . ')'
                : number_format($v, 0));

        $fmtZ = fn($v) => $v == 0
            ? '0'
            : ($v < 0
                ? '(' . number_format(abs($v), 0) . ')'
                : number_format($v, 0));

        $vehicles   = $reportData['vehicles'];
        $grandTotal = $reportData['grandTotal'];

        $fromFmt  = \Carbon\Carbon::parse($dateFrom)->format('d/m/y');
        $toFmt    = \Carbon\Carbon::parse($dateTo)->format('d/m/y');
        $clientStr = isset($supplierName) && $supplierName
            ? strtoupper($supplierName)
            : 'ALL SUPPLIERS';

        // count of distinct vehicles for کُل گاڑی
        $totalVehicles = count($vehicles);
    @endphp

    {{-- ─── Header ─────────────────────────────────────── --}}
    <div class="rpt-header">
        <div class="co-name">M.D.S</div>
        <div class="co-sub">Purchase Billing Vehicle Register &nbsp; گاڑی بیچک رجسٹر</div>
        <div class="co-info">
            From : {{ $fromFmt }}, To : {{ $toFmt }},
            Client: {{ $clientStr }}{{ isset($vehicleNo) && $vehicleNo ? ', Vehicle: ' . $vehicleNo : '' }}
        </div>
    </div>

    {{-- ─── Main Table ─────────────────────────────────── --}}
    <table class="rpt-table">
        <thead>
            <tr>
                <th style="text-align:right">تاریخ</th>
                <th style="text-align:right">بلٹی نمبر</th>
                <th style="text-align:right">گاڑی نمبر</th>
                <th style="text-align:right">سپلار</th>
                <th style="text-align:right">مال</th>
                <th>بوری</th>
                <th>وزن</th>
                <th>قیمت</th>
                <th>کرایہ<br>محصول</th>
                <th>مزدوری<br>محصول</th>
                <th>مارکیٹ<br>فیس</th>
                <th>ڈاک خرچ<br>متفرقات</th>
                <th>نقد</th>
                <th>کمیشن</th>
                <th>کل خرچہ</th>
                <th style="border-left:none">بختہ بکری</th>
            </tr>
        </thead>
        <tbody>

        @foreach($vehicles as $vehicle)

            {{-- Vehicle heading --}}
            <tr class="row-vehicle">
                <td colspan="16" style="text-align:right; direction:rtl;">
                    گاڑی نمبر : {{ $vehicle['truck_no'] ?? $vehicle['pb_no'] }}
                </td>
            </tr>

            @foreach($vehicle['bilties'] as $bilty)
                @php $itemCount = max(count($bilty['items']), 1); @endphp

                @if(count($bilty['items']) > 0)
                    @foreach($bilty['items'] as $idx => $item)
                    <tr class="row-item">
                        @if($idx === 0)
                        <td class="td-rtxt" rowspan="{{ $itemCount }}">
                            {{ \Carbon\Carbon::parse($bilty['date'])->format('d/m/y') }}
                        </td>
                        <td class="td-ctxt" rowspan="{{ $itemCount }}">{{ $bilty['bilty_no'] }}</td>
                        <td class="td-ctxt" rowspan="{{ $itemCount }}">{{ $vehicle['truck_no'] ?? $vehicle['pb_no'] }}</td>
                        <td class="td-rtxt" rowspan="{{ $itemCount }}">{{ $bilty['supplier_name'] }}</td>
                        @endif

                        <td class="td-rtxt">{{ $item->child_cate_name ?? '' }}</td>
                        <td class="td-ctxt">{{ $item->bori   ? number_format($item->bori)   : '' }}</td>
                        <td class="td-ctxt">{{ $item->weight ? number_format($item->weight) : '' }}</td>
                        <td class="td-num">{{ $item->amount  ? number_format($item->amount, 0) : '' }}</td>

                        @if($idx === 0)
                        <td class="td-num" rowspan="{{ $itemCount }}">{{ $fmtT($bilty['detail']['rent_recieve_amount']) }}</td>
                        <td class="td-num" rowspan="{{ $itemCount }}">{{ $fmtT($bilty['detail']['labour_amount']) }}</td>
                        <td class="td-num" rowspan="{{ $itemCount }}">{{ $fmtT($bilty['detail']['market_fees_amount']) }}</td>
                        <td class="td-num" rowspan="{{ $itemCount }}">{{ $fmtT($bilty['detail']['post_expense_amount']) }}</td>
                        <td class="td-num" rowspan="{{ $itemCount }}">{{ $fmtT($bilty['detail']['cash_amount']) }}</td>
                        <td class="td-num" rowspan="{{ $itemCount }}">{{ $fmtT($bilty['detail']['commission_amount']) }}</td>
                        <td class="td-num" rowspan="{{ $itemCount }}">{{ $fmtT($bilty['detail']['total_expanse_amount']) }}</td>
                        <td class="td-num" rowspan="{{ $itemCount }}">{{ $fmtT($bilty['detail']['save_amount']) }}</td>
                        @endif
                    </tr>
                    @endforeach

                @else
                    <tr class="row-item">
                        <td class="td-rtxt">{{ \Carbon\Carbon::parse($bilty['date'])->format('d/m/y') }}</td>
                        <td class="td-ctxt">{{ $bilty['bilty_no'] }}</td>
                        <td class="td-ctxt">{{ $vehicle['truck_no'] ?? $vehicle['pb_no'] }}</td>
                        <td class="td-rtxt">{{ $bilty['supplier_name'] }}</td>
                        <td></td><td></td><td></td><td></td>
                        <td class="td-num">{{ $fmtT($bilty['detail']['rent_recieve_amount']) }}</td>
                        <td class="td-num">{{ $fmtT($bilty['detail']['labour_amount']) }}</td>
                        <td class="td-num">{{ $fmtT($bilty['detail']['market_fees_amount']) }}</td>
                        <td class="td-num">{{ $fmtT($bilty['detail']['post_expense_amount']) }}</td>
                        <td class="td-num">{{ $fmtT($bilty['detail']['cash_amount']) }}</td>
                        <td class="td-num">{{ $fmtT($bilty['detail']['commission_amount']) }}</td>
                        <td class="td-num">{{ $fmtT($bilty['detail']['total_expanse_amount']) }}</td>
                        <td class="td-num">{{ $fmtT($bilty['detail']['save_amount']) }}</td>
                    </tr>
                @endif
            @endforeach

            {{-- Vehicle subtotal --}}
            <tr class="row-vtotal">
                <td></td><td></td><td></td><td></td><td></td>
                <td class="td-ctxt">{{ $vehicle['vehicleTotals']['total_bori']   > 0 ? number_format($vehicle['vehicleTotals']['total_bori'])   : '' }}</td>
                <td class="td-ctxt">{{ $vehicle['vehicleTotals']['total_weight'] > 0 ? number_format($vehicle['vehicleTotals']['total_weight']) : '' }}</td>
                <td class="td-num">{{ $fmtT($vehicle['vehicleTotals']['total_amount']) }}</td>
                <td class="td-num">{{ $fmtT($vehicle['vehicleTotals']['rent_recieve_amount']) }}</td>
                <td class="td-num">{{ $fmtT($vehicle['vehicleTotals']['labour_amount']) }}</td>
                <td class="td-num">{{ $fmtT($vehicle['vehicleTotals']['market_fees_amount']) }}</td>
                <td class="td-num">{{ $fmtT($vehicle['vehicleTotals']['post_expense_amount']) }}</td>
                <td class="td-num">{{ $fmtT($vehicle['vehicleTotals']['cash_amount']) }}</td>
                <td class="td-num">{{ $fmtT($vehicle['vehicleTotals']['commission_amount']) }}</td>
                <td class="td-num">{{ $fmtT($vehicle['vehicleTotals']['total_expanse_amount']) }}</td>
                <td class="td-num">{{ $fmtT($vehicle['vehicleTotals']['save_amount']) }}</td>
            </tr>

        @endforeach

        </tbody>
    </table>

    {{-- ═══════════════════════════════════════════════════════════
         GRAND TOTALS BOX — matches screenshot exactly
         Layout (RTL, 7 visual columns right→left):
         Col1: کُل label
         Col2: گاڑی count
         Col3: بوری / وزن
         Col4: قیمت (total_amount)
         Col5: کرایہ / مزدوری / مارکیٹ / ڈاک / خرچہ متفرقات
         Col6: نقد / کمیشن / کل خرچہ
         Col7: بختہ بکری
    ═══════════════════════════════════════════════════════════ --}}
    <table class="totals-box">
        <tr>
            {{-- Col 1: کُل label --}}
            <td rowspan="5" style="font-weight:900; font-size:12px; vertical-align:middle; text-align:right; border-left:1px solid #ccc;">
                کُل
            </td>

            {{-- Col 2: گاڑی count --}}
            <td style="vertical-align:top; border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl">گاڑی</span>
                    <span class="val">{{ $totalVehicles }}</span>
                </span>
            </td>

            {{-- Col 3: بوری --}}
            <td style="vertical-align:top; border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl">بوری</span>
                    <span class="val">{{ number_format($grandTotal['total_bori']) }}</span>
                </span>
            </td>

            {{-- Col 4: قیمت --}}
            <td style="vertical-align:top; border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl">قیمت</span>
                    <span class="val">{{ number_format($grandTotal['total_amount']) }}</span>
                </span>
            </td>

            {{-- Col 5: کرایہ محصول --}}
            <td style="vertical-align:top; border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl">کرایہ محصول</span>
                    <span class="val">{{ number_format($grandTotal['rent_recieve_amount']) }}</span>
                </span>
            </td>

            {{-- Col 6: نقد --}}
            <td style="vertical-align:top; border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl">نقد</span>
                    <span class="val">{{ $fmtZ($grandTotal['cash_amount']) }}</span>
                </span>
            </td>

            {{-- Col 7: بختہ بکری --}}
            <td style="vertical-align:top;">
                <span class="tv">
                    <span class="lbl">بختہ بکری</span>
                    <span class="val">{{ number_format($grandTotal['save_amount']) }}</span>
                </span>
            </td>
        </tr>

        <tr>
            {{-- Col 2: empty --}}
            <td style="border-left:1px solid #ccc;"></td>

            {{-- Col 3: وزن --}}
            <td style="border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl">وزن</span>
                    <span class="val">{{ number_format($grandTotal['total_weight']) }}</span>
                </span>
            </td>

            {{-- Col 4: empty --}}
            <td style="border-left:1px solid #ccc;"></td>

            {{-- Col 5: مزدوری --}}
            <td style="border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl">مزدوری</span>
                    <span class="val">{{ number_format($grandTotal['labour_amount']) }}</span>
                </span>
            </td>

            {{-- Col 6: کمیشن --}}
            <td style="border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl">کمیشن</span>
                    <span class="val">{{ number_format($grandTotal['commission_amount']) }}</span>
                </span>
            </td>

            {{-- Col 7: empty --}}
            <td></td>
        </tr>

        <tr>
            {{-- Col 2: empty --}}
            <td style="border-left:1px solid #ccc;"></td>
            {{-- Col 3: empty --}}
            <td style="border-left:1px solid #ccc;"></td>
            {{-- Col 4: empty --}}
            <td style="border-left:1px solid #ccc;"></td>

            {{-- Col 5: مارکیٹ فیس --}}
            <td style="border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl">مارکیٹ فیس</span>
                    <span class="val">{{ number_format($grandTotal['market_fees_amount']) }}</span>
                </span>
            </td>

            {{-- Col 6: کل خرچہ --}}
            <td style="border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl" style="font-size:11px; font-weight:900;">کل خرچہ</span>
                    <span class="val" style="font-weight:900;">{{ number_format($grandTotal['total_expanse_amount']) }}</span>
                </span>
            </td>

            {{-- Col 7: empty --}}
            <td></td>
        </tr>

        <tr>
            <td style="border-left:1px solid #ccc;"></td>
            <td style="border-left:1px solid #ccc;"></td>
            <td style="border-left:1px solid #ccc;"></td>

            {{-- Col 5: ڈاک خرچہ --}}
            <td style="border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl">ڈاک خرچہ</span>
                    <span class="val">{{ number_format($grandTotal['post_expense_amount']) }}</span>
                </span>
            </td>

            <td style="border-left:1px solid #ccc;"></td>
            <td></td>
        </tr>

        <tr>
            <td style="border-left:1px solid #ccc;"></td>
            <td style="border-left:1px solid #ccc;"></td>
            <td style="border-left:1px solid #ccc;"></td>

            {{-- Col 5: خرچہ متفرقات --}}
            <td style="border-left:1px solid #ccc;">
                <span class="tv">
                    <span class="lbl">خرچہ متفرقات</span>
                    <span class="val">{{ number_format($grandTotal['miscellaneous_expense_amount']) }}</span>
                </span>
            </td>

            <td style="border-left:1px solid #ccc;"></td>
            <td></td>
        </tr>
    </table>

    <button class="no-print" onclick="window.print()">Print / پرنٹ</button>

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