<!DOCTYPE html>
<html lang="ur" dir="ltr">
<head>
    <meta charset="UTF-8">
    <title>Purchase Register — خرید رجسٹر</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;
            line-height: 1.7;
            margin-bottom: 6px;
        }
        .rpt-header .co-name {
            font-size: 22px;
            font-weight: 700;
            letter-spacing: 1px;
        }
        .rpt-header .co-sub {
            font-size: 14px;
            font-weight: 600;
        }
        .rpt-header .co-range {
            font-size: 12px;
            color: #111;
        }

        /* Single solid line under header */
        .header-rule {
            border: none;
            border-top: 1.5px solid #000;
            margin: 8px 0 0;
        }

        /* ══════════════════════════════
           MAIN TABLE
           No vertical lines anywhere —
           only thead bottom underline
        ══════════════════════════════ */
        table.main-table {
            width: 100%;
            border-collapse: collapse;
            direction: rtl;
            font-size: 12px;
            margin-top: 2px;
        }

        /* thead: bottom border only */
        table.main-table thead th {
            border: none;
            border-bottom: 1px solid #000;
            padding: 6px 10px 5px;
            text-align: center;
            font-weight: 700;
            font-size: 12px;
            background: #fff;
            white-space: nowrap;
        }
        table.main-table thead th.th-date { text-align: right; }
        table.main-table thead th.th-mal  { text-align: right; padding-right: 16px; }

        /* tbody: no borders by default */
        table.main-table tbody td {
            border: none;
            padding: 4px 10px;
            vertical-align: middle;
            font-size: 12px;
            white-space: nowrap;
        }

        /* ── Purchaser group header (red, right-aligned) ── */
        table.main-table tr.purchaser-row td {
            color: #cc0000;
            font-weight: 700;
            font-size: 13px;
            padding: 12px 10px 3px;
            text-align: right;
        }

        /* ── Column alignment ── */
        table.main-table td.td-date { text-align: right; }
        table.main-table td.td-gari { text-align: center; }
        table.main-table td.td-mal  { text-align: right; padding-right: 16px; }
        table.main-table td.td-bori { text-align: center; }
        table.main-table td.td-wazn { text-align: center; }
        table.main-table td.td-rate { text-align: center; }
        table.main-table td.td-qmt  { text-align: center; }

        /* ── Order subtotal row: solid top border across all cols ── */
        table.main-table tr.order-sub-row td {
            border-top: 1px solid #000;
            padding-top: 3px;
            padding-bottom: 5px;
        }

        /* ── Dot-dash separator row (long dashed line) ── */
        table.main-table tr.dash-row td {
            padding: 4px 0 2px;
            border: none;
        }
        .dash-line {
            height: 2px;
            background: repeating-linear-gradient(
                to right,
                #444 0px,   #444 9px,
                transparent 9px,  transparent 11px,
                #444 11px,  #444 12px,
                transparent 12px, transparent 15px
            );
            width: 100%;
        }

        /* ── کُل row (purchaser total) ── */
        table.main-table tr.kul-row td {
            padding: 3px 10px 8px;
            font-weight: 600;
        }
        table.main-table tr.kul-row td.kul-lbl {
            color: #cc0000;
            font-weight: 700;
            font-size: 13px;
            text-align: right;
        }

        /* ══════════════════════════════
           SUMMARY BOX
           Matches screenshot exactly:
           bordered box, title row,
           then 3 columns:
             [گاڑی N] | [label] | [value]
        ══════════════════════════════ */
        .summary-outer {
            margin-top: 20px;
            border: 1.5px solid #000;
            direction: rtl;
            width: 100%;
        }
        .sum-title {
            border-bottom: 1.5px solid #000;
            padding: 5px 14px;
            font-weight: 700;
            font-size: 13px;
            text-align: right;
        }
        .sum-body {
            display: flex;
            width: 100%;
            direction: rtl;
        }

        /* Right section: گاڑی count */
        .sum-gari {
            flex: 0 0 auto;
            min-width: 130px;
            border-left: 1px solid #000;
            display: flex;
            align-items: center;
            justify-content: flex-start;
            gap: 10px;
            padding: 10px 14px;
            font-size: 12.5px;
            font-weight: 600;
            direction: rtl;
        }
        .sum-gari .g-lbl { font-weight: 700; }

        /* Middle: Urdu labels */
        .sum-labels {
            flex: 1;
            border-left: 1px solid #000;
        }
        .sum-labels div {
            padding: 5px 14px;
            font-weight: 700;
            font-size: 12.5px;
            text-align: right;
            border-bottom: 1px solid #ddd;
        }
        .sum-labels div:last-child { border-bottom: none; }

        /* Left: numeric values */
        .sum-vals {
            flex: 1;
        }
        .sum-vals div {
            padding: 5px 14px;
            font-size: 12.5px;
            font-weight: 600;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        .sum-vals div:last-child { border-bottom: none; }

        /* Print button */
        .no-print {
            margin-top: 16px;
            display: inline-block;
            padding: 7px 22px;
            background: #007bff;
            color: #fff;
            border: none;
            cursor: pointer;
            font-size: 12px;
            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">

    {{-- ══ Header ══ --}}
    <div class="rpt-header">
        <div class="co-name">M.D.S</div>
        <div class="co-sub">Purchase Register &nbsp; خرید رجسٹر</div>
        <div class="co-range">
            From : {{ $fromDate ? \Carbon\Carbon::parse($fromDate)->format('d/m/y') : '—' }},
            &nbsp;To : {{ $toDate ? \Carbon\Carbon::parse($toDate)->format('d/m/y') : '—' }}
            @if($selectedPurchaser)
                , &nbsp;Client: {{ $selectedPurchaser->purchaser_name }}
            @endif
        </div>
    </div>
    <hr class="header-rule">

    @if(empty($reportData['purchasers']))
        <p style="text-align:center;padding:40px;color:#888;">No records found.</p>
    @else

    @php
        $n  = fn($v) => $v  ? number_format($v, 0) : '';
        $n0 = fn($v) => number_format($v, 0);
    @endphp

    <table class="main-table">
        <thead>
            <tr>
                <th class="th-date">تاریخ</th>
                <th>گاڑی نمبر</th>
                <th class="th-mal">مال</th>
                <th>بوری</th>
                <th>وزن</th>
                <th>ریٹ</th>
                <th>قیمت</th>
            </tr>
        </thead>
        <tbody>

        @foreach($reportData['purchasers'] as $purchaserName => $purchaserData)

            {{-- Purchaser name (red, Urdu) --}}
            <tr class="purchaser-row">
                <td colspan="7">{{ $purchaserData['urdu_name'] ?: $purchaserName }}</td>
            </tr>

            @foreach($purchaserData['orders'] as $order)

                {{-- Product rows --}}
                @foreach($order['details'] as $i => $detail)
                <tr>
                    <td class="td-date">{{ $i === 0 ? $order['order_date'] : '' }}</td>
                    <td class="td-gari">{{ $i === 0 ? ($order['po_number'] ?? '') : '' }}</td>
                    <td class="td-mal">{{ $detail['name'] }}</td>
                    <td class="td-bori">{{ $detail['bori'] ?: '' }}</td>
                    <td class="td-wazn">{{ $n($detail['weight']) }}</td>
                    <td class="td-rate">{{ $n($detail['rate']) }}</td>
                    <td class="td-qmt">{{ $n($detail['amount']) }}</td>
                </tr>
                @endforeach

                {{-- مزدوري row --}}
                @if(!empty($order['labour_charges']) && $order['labour_charges'] > 0)
                <tr>
                    <td class="td-date">{{ $order['order_date'] }}</td>
                    <td class="td-gari"></td>
                    <td class="td-mal">مزدوري</td>
                    <td class="td-bori"></td>
                    <td class="td-wazn"></td>
                    <td class="td-rate"></td>
                    <td class="td-qmt">{{ $n($order['labour_charges']) }}</td>
                </tr>
                @endif

                {{-- Order subtotal: solid line above, bori / wazn / qmt ── --}}
                <tr class="order-sub-row">
                    <td class="td-date"></td>
                    <td class="td-gari"></td>
                    <td class="td-mal"></td>
                    <td class="td-bori">{{ $order['total_bori'] ?: '' }}</td>
                    <td class="td-wazn">{{ $n0($order['total_weight']) }}</td>
                    <td class="td-rate"></td>
                    <td class="td-qmt">{{ $n0($order['grand_total']) }}</td>
                </tr>

            @endforeach

            {{-- Long dot-dash separator --}}
            <tr class="dash-row">
                <td colspan="7"><div class="dash-line"></div></td>
            </tr>

            {{-- کُل row (purchaser grand) --}}
            <tr class="kul-row">
                <td class="td-date kul-lbl">کُل</td>
                <td class="td-gari"></td>
                <td class="td-mal"></td>
                <td class="td-bori">{{ $purchaserData['subBori'] ?: '' }}</td>
                <td class="td-wazn">{{ $n0($purchaserData['subWeight']) }}</td>
                <td class="td-rate"></td>
                <td class="td-qmt">{{ $n0($purchaserData['subAmount']) }}</td>
            </tr>

        @endforeach

        </tbody>
    </table>

    {{-- ══ Summary Box ══ --}}
    <div class="summary-outer">
        <div class="sum-title">کُل</div>
        <div class="sum-body">

            {{-- Right col: گاڑی --}}
            <div class="sum-gari">
                <span class="g-lbl">گاڑی</span>
                <span>{{ $reportData['grandOrders'] }}</span>
            </div>

            {{-- Middle col: Urdu labels --}}
            <div class="sum-labels">
                <div>بوري</div>
                <div>وزن</div>
                <div>قیمت</div>
            </div>

            {{-- Left col: values --}}
            <div class="sum-vals">
                <div>{{ number_format($reportData['grandBori'],   0) }}</div>
                <div>{{ number_format($reportData['grandWeight'], 0) }}</div>
                <div>{{ number_format($reportData['grandAmount'], 0) }}</div>
            </div>

        </div>
    </div>

    @endif

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

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