<!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;
        }

        .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;
        }

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

        /* Header row — solid borders top & bottom, no side borders on cells */
        .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;
            /* thin vertical separator between header cells */
            border-right: 1px solid #ccc;
            direction: rtl;
            line-height: 1.3;
        }
        .rpt-table thead th:last-child { border-right: none; }

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

        /* Numbers left-aligned (LTR), text right-aligned (RTL) */
        .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 ─── */
        .row-vehicle td {
            font-weight: 900;
            font-size: 11.5px;
            color: #c00;
            text-align: right;
            direction: rtl;
            padding-top: 10px;
            padding-bottom: 2px;
        }

        /* ── Item rows ─── */
        .row-item td {
            /* no background, no borders */
        }

        /* ── 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 ─── */
        .row-grand td {
            font-weight: 800;
            border-top:    2px solid #000 !important;
            border-bottom: 2px solid #000 !important;
            padding-top:    5px;
            padding-bottom: 5px;
        }

        .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; }
        }

        .row-box td{
    border:none;
    padding:0;
}

.row-box table{
    width:100%;
    border:1px solid #000;
    margin-top:4px;
    margin-bottom:10px;
}

.row-box table td{
    padding:4px 6px;
    border:none;
    font-size:10px;
    direction:rtl;
    text-align:right;
}
    </style>
</head>
<body>
<div class="container">

    @php
        /*
         * fmt(): formats numbers exactly as in the screenshot
         *  0       → empty (or '-' for totals)
         *  positive → number_format no decimals
         *  negative → (number)
         */
        $fmt = fn($v, bool $dashZero = false) => $v == 0
            ? ($dashZero ? '' : '')
            : ($v < 0
                ? '(' . number_format(abs($v), 0) . ')'
                : number_format($v, 0));

        $fmtT = fn($v) => $v == 0          // for totals: show nothing if zero
            ? ''
            : ($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 = $supplierName
            ? strtoupper($supplierName)
            : 'ALL SUPPLIERS';
    @endphp

    {{-- ─── Report 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 }}{{ $vehicleNo ? ', Vehicle: ' . $vehicleNo : '' }}
        </div>
    </div>

    {{-- ─── Table ──────────────────────────────────────── --}}
    <table class="rpt-table">
        <thead>
            <tr>
                {{--
                    Columns exactly as screenshot (right→left visual order,
                    but table is LTR so leftmost column is تاریخ on far right visually):

                    Screenshot header (RTL reading order, left on page = last column):
                    بختہ بکری | کل خرچہ | کمیشن | نقد | ڈاک خرچ متفرقات | مارکیٹ فیس | مزدوری محصول | کرایہ محصول | قیمت | وزن | بوری | مال | سپلار | گاڑی نمبر | بلٹی نمبر | تاریخ

                    In LTR table: col 1 = تاریخ (rightmost visible), col 16 = بختہ بکری (leftmost visible)
                    We set dir=rtl on the whole outer div so that the table appears RTL.
                --}}
                <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-right: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['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">

                        {{-- Date — rowspan all items in this bilty --}}
                        @if($idx === 0)
                        <td class="td-rtxt" rowspan="{{ $itemCount }}">
                            {{ \Carbon\Carbon::parse($bilty['date'])->format('d/m/y') }}
                        </td>
                        @endif

                        {{-- Bilty No --}}
                        @if($idx === 0)
                        <td class="td-ctxt" rowspan="{{ $itemCount }}">
                            {{ $bilty['bilty_no'] }}
                        </td>
                        @endif

                        {{-- Vehicle No --}}
                        @if($idx === 0)
                        <td class="td-ctxt" rowspan="{{ $itemCount }}">
                            {{ $vehicle['pb_no'] }}
                        </td>
                        @endif

                        {{-- Supplier --}}
                        @if($idx === 0)
                        <td class="td-rtxt" rowspan="{{ $itemCount }}">
                            {{ $bilty['supplier_name'] }}
                        </td>
                        @endif

                        {{-- Mal (item name) --}}
                        <td class="td-rtxt">{{ $item->child_cate_name ?? '' }}</td>

                        {{-- Bori --}}
                        <td class="td-ctxt">{{ $item->bori ? number_format($item->bori) : '' }}</td>

                        {{-- Weight --}}
                        <td class="td-ctxt">{{ $item->weight ? number_format($item->weight) : '' }}</td>

                        {{-- Qeemat (amount per item row) --}}
                        <td class="td-num">{{ $item->amount ? number_format($item->amount, 0) : '' }}</td>

                        {{-- Expense columns — rowspan the whole bilty --}}
                        @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
                    {{-- No items — single blank row --}}
                    <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['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 row ── --}}
            <tr class="row-vtotal">
                {{--
                    In screenshot the subtotal row shows:
                    col1(تاریخ) empty | col2(بلٹی) empty | col3(گاڑی نمبر) empty |
                    col4(سپلار) empty | col5(مال) empty |
                    col6(بوری)=301 | col7(وزن)=32,978 | col8(قیمت)=1,484,010 |
                    col9(کرایہ)=513,500 | col10(مزدوری)=3,913 | col11(مارکیٹ)=400 |
                    col12(ڈاک)=200 | col13(نقد)=empty | col14(کمیشن)=74,201 |
                    col15(کل خرچہ)=592,214 | col16(بختہ بکری)=891,796
                --}}
                <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>
            <tr class="row-box">
    <td colspan="16">
        <table width="100%" style="border-collapse:collapse;">
            <tr>
                <td><strong>گل</strong></td>
                <td>{{ $vehicle['pb_no'] }}</td>

                <td><strong>گاڑی</strong></td>
                <td>
                    @if(count($vehicle['bilties']))
                        {{ $vehicle['pb_no'] }}
                    @endif
                </td>

                <td><strong>بوری</strong></td>
                <td>{{ number_format($vehicle['vehicleTotals']['total_bori']) }}</td>

                <td><strong>وزن</strong></td>
                <td>{{ number_format($vehicle['vehicleTotals']['total_weight']) }}</td>

                <td><strong>قیمت</strong></td>
                <td>{{ number_format($vehicle['vehicleTotals']['total_amount']) }}</td>

                <td><strong>بختہ بکری</strong></td>
                <td>{{ number_format($vehicle['vehicleTotals']['save_amount']) }}</td>
            </tr>

            <tr>
                <td><strong>کرایہ محصول</strong></td>
                <td>{{ number_format($vehicle['vehicleTotals']['rent_recieve_amount']) }}</td>

                <td><strong>مزدوری</strong></td>
                <td>{{ number_format($vehicle['vehicleTotals']['labour_amount']) }}</td>

                <td><strong>مارکیٹ فیس</strong></td>
                <td>{{ number_format($vehicle['vehicleTotals']['market_fees_amount']) }}</td>

                <td><strong>ڈاک خرچہ</strong></td>
                <td>{{ number_format($vehicle['vehicleTotals']['post_expense_amount']) }}</td>

                <td><strong>نقد</strong></td>
                <td>{{ number_format($vehicle['vehicleTotals']['cash_amount']) }}</td>

                <td><strong>کمیشن</strong></td>
                <td>{{ number_format($vehicle['vehicleTotals']['commission_amount']) }}</td>
            </tr>

            <tr>
                <td colspan="10"></td>
                <td><strong>کل خرچہ</strong></td>
                <td>{{ number_format($vehicle['vehicleTotals']['total_expanse_amount']) }}</td>
            </tr>
        </table>
    </td>
</tr>

        @endforeach

        {{-- ── Grand Total ── --}}
        @if(count($vehicles) > 1)
        <tr class="row-grand">
            <td colspan="5" style="text-align:right; direction:rtl; font-weight:800;">
                کل مجموعہ
            </td>
            <td class="td-ctxt">
                {{ $grandTotal['total_bori'] > 0 ? number_format($grandTotal['total_bori']) : '' }}
            </td>
            <td class="td-ctxt">
                {{ $grandTotal['total_weight'] > 0 ? number_format($grandTotal['total_weight']) : '' }}
            </td>
            <td class="td-num">{{ $fmtT($grandTotal['total_amount']) }}</td>
            <td class="td-num">{{ $fmtT($grandTotal['rent_recieve_amount']) }}</td>
            <td class="td-num">{{ $fmtT($grandTotal['labour_amount']) }}</td>
            <td class="td-num">{{ $fmtT($grandTotal['market_fees_amount']) }}</td>
            <td class="td-num">{{ $fmtT($grandTotal['post_expense_amount']) }}</td>
            <td class="td-num">{{ $fmtT($grandTotal['cash_amount']) }}</td>
            <td class="td-num">{{ $fmtT($grandTotal['commission_amount']) }}</td>
            <td class="td-num">{{ $fmtT($grandTotal['total_expanse_amount']) }}</td>
            <td class="td-num">{{ $fmtT($grandTotal['save_amount']) }}</td>
        </tr>
        @endif

        </tbody>
    </table>

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

</div>

{{--
    RTL trick: wrap the entire page in RTL so the table reads right-to-left
    visually (تاریخ on the far right, بختہ بکری on the far left) while the
    table itself remains LTR internally for correct column ordering.
--}}
<style>
    body { direction: rtl; }
    .rpt-table { direction: rtl; }
    .rpt-table td.td-num  { direction: ltr; text-align: right; }
    .rpt-table td.td-ctxt { direction: ltr; text-align: center; }
    .rpt-table td.td-rtxt { direction: rtl; text-align: right; }
    .rpt-table thead th   { direction: rtl; text-align: center; }
</style>

</body>
</html>
