@extends('layouts.master')

@push('css')
<style>
    /* ── Layout ─────────────────────────────────────────── */
    .pbvr-wrap { overflow-x: auto; }

    .pbvr-header { text-align: center; margin-bottom: 12px; }
    .pbvr-header .co-name { font-size: 22px; font-weight: 700; letter-spacing: 1px; }
    .pbvr-header .co-sub  { font-size: 14px; font-weight: 600; }
    .pbvr-header .co-date { font-size: 12px; color: #555; margin-top: 2px; }

    /* ── Table ──────────────────────────────────────────── */
    .pbvr-table {
        width: 100%; border-collapse: collapse;
        font-size: 11.5px; white-space: nowrap;
    }
    .pbvr-table thead th {
        border: 1px solid #888; padding: 7px 8px;
        text-align: center; background: #f5f5f5;
        font-weight: 700;
    }
    .pbvr-table thead th.th-r { text-align: right; }

    .pbvr-table tbody td {
        border: 1px solid #ddd;
        padding: 5px 8px; vertical-align: middle;
        font-size: 11px;
    }
    .pbvr-table td.td-r   { text-align: right; }
    .pbvr-table td.td-c   { text-align: center; }
    .pbvr-table td.td-num { text-align: right; font-variant-numeric: tabular-nums; }

    /* Vehicle heading row */
    .pbvr-table tr.row-vehicle td {
        background: #fff3cd; font-weight: 800;
        font-size: 13px; color: #856404;
        border-top: 2px solid #aaa !important;
        border-bottom: 1px solid #aaa !important;
        text-align: right;
        direction: rtl;
    }

    /* Vehicle subtotal */
    .pbvr-table tr.row-vtotal td {
        background: #e8f4e8; font-weight: 700;
        border-top: 1px solid #aaa !important;
        border-bottom: 2px solid #888 !important;
    }

    /* Grand total */
    .pbvr-table tr.row-grand td {
        background: #cce5ff; font-weight: 800;
        font-size: 12px; color: #004085;
        border-top: 2px solid #004085 !important;
        border-bottom: 2px solid #004085 !important;
    }

    /* item rows */
    .pbvr-table tr.row-item td { background: #fff; }
    .pbvr-table tr.row-item:nth-child(even) td { background: #fafafa; }

    .neg { color: #c00; }
</style>
@endpush

@section('content')
<div class="card">
    <div class="card-header">
        <h3>Purchase Billing Vehicle Register — گاڑی بیچک رجسٹر</h3>
    </div>
    <div class="card-body">

        {{-- ── Filter Form ── --}}
        <form class="row g-2 align-items-end" method="GET"
              action="{{ route('purchase-billing-vehicle-register.index') }}">

            <div class="col-md-2">
                <label class="form-label">Date From</label>
                <input type="date" name="date_from" class="form-control form-control-sm"
                       value="{{ $dateFrom }}">
            </div>

            <div class="col-md-2">
                <label class="form-label">Date To</label>
                <input type="date" name="date_to" class="form-control form-control-sm"
                       value="{{ $dateTo }}">
            </div>

            <div class="col-md-3">
                <label class="form-label">Supplier — سپلائر</label>
                <select name="supplier_id" class="form-select form-select-sm">
                    <option value="">-- All Suppliers --</option>
                    @foreach($suppliers as $sup)
                        <option value="{{ $sup->id_supplier }}"
                            {{ $supplierId == $sup->id_supplier ? 'selected' : '' }}>
                            {{ $sup->supplier_name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="col-md-2">
                <label class="form-label">Vehicle No — گاڑی نمبر</label>
                <input type="text" name="vehicle_no" class="form-control form-control-sm"
                       value="{{ $vehicleNo }}" placeholder="e.g. 791">
            </div>

            <div class="col-md-3 d-flex gap-2">
                <button type="submit" class="btn btn-primary btn-sm">
                    <i class="fa fa-search"></i> دیکھیں
                </button>
                @if($reportData)
                    <a href="{{ route('purchase-billing-vehicle-register.print', request()->query()) }}"
                       target="_blank" class="btn btn-warning btn-sm">
                        <i class="fa fa-print"></i> پرنٹ
                    </a>
                @endif
                <a href="{{ route('purchase-billing-vehicle-register.index') }}"
                   class="btn btn-secondary btn-sm">
                    <i class="fa fa-refresh"></i> Reset
                </a>
            </div>
        </form>

        <hr />

        @if($reportData)

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

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

            @if(empty($vehicles))
                <div class="alert alert-info">No records found for selected filters.</div>
            @else
                <div class="pbvr-wrap">
                    <table class="pbvr-table">
                        <thead>
                            <tr>
                                <th>تاریخ<br>Date</th>
                                <th>بلٹی نمبر<br>Bilty No</th>
                                <th class="th-r">سپلار<br>Supplier</th>
                                <th class="th-r">مال<br>Item</th>
                                <th class="th-r">مارکیٹ<br>Marka</th>
                                <th>بوری<br>Bori</th>
                                <th>وزن<br>Weight</th>
                                <th>قیمت<br>Rate</th>
                                <th>کرایہ<br>Rent</th>
                                <th>مزدوری محصول<br>Labour</th>
                                <th>مارکیٹ فیس<br>Mkt Fees</th>
                                <th>ڈاک خرچ<br>Post Exp</th>
                                <th>نقد<br>Cash</th>
                                <th>کمیشن<br>Commission</th>
                                <th>کل خرچہ<br>Total Exp</th>
                                <th>بختہ بکری<br>Save Amt</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($vehicles as $vehicle)
                                {{-- Vehicle Heading --}}
                                <tr class="row-vehicle">
                                    <td colspan="16">
                                        گاڑی نمبر : {{ $vehicle['pb_no'] }}
                                    </td>
                                </tr>

                                @foreach($vehicle['bilties'] as $bilty)
                                    {{-- Item rows --}}
                                    @foreach($bilty['items'] as $idx => $item)
                                        <tr class="row-item">
                                            @if($idx === 0)
                                                <td class="td-c" rowspan="{{ count($bilty['items']) }}">
                                                    {{ \Carbon\Carbon::parse($bilty['date'])->format('d/m/y') }}
                                                </td>
                                                <td class="td-c" rowspan="{{ count($bilty['items']) }}">
                                                    {{ $bilty['bilty_no'] }}
                                                </td>
                                                <td class="td-r" rowspan="{{ count($bilty['items']) }}">
                                                    {{ $bilty['supplier_name'] }}
                                                </td>
                                            @endif
                                            <td class="td-r">{{ $item->item ?? '-' }}</td>
                                            <td class="td-r">{{ $item->marka ?? '-' }}</td>
                                            <td class="td-c">{{ number_format($item->bori ?? 0) }}</td>
                                            <td class="td-c">{{ number_format($item->weight ?? 0) }}</td>
                                            <td class="td-num">{{ $fmt($item->rate ?? 0) }}</td>
                                            {{-- Show expense columns only on first item row --}}
                                            @if($idx === 0)
                                                <td class="td-num" rowspan="{{ count($bilty['items']) }}">
                                                    {{ $fmt($bilty['detail']['rent_recieve_amount']) }}
                                                </td>
                                                <td class="td-num" rowspan="{{ count($bilty['items']) }}">
                                                    {{ $fmt($bilty['detail']['labour_amount']) }}
                                                </td>
                                                <td class="td-num" rowspan="{{ count($bilty['items']) }}">
                                                    {{ $fmt($bilty['detail']['market_fees_amount']) }}
                                                </td>
                                                <td class="td-num" rowspan="{{ count($bilty['items']) }}">
                                                    {{ $fmt($bilty['detail']['post_expense_amount']) }}
                                                </td>
                                                <td class="td-num" rowspan="{{ count($bilty['items']) }}">
                                                    {{ $fmt($bilty['detail']['cash_amount']) }}
                                                </td>
                                                <td class="td-num" rowspan="{{ count($bilty['items']) }}">
                                                    {{ $fmt($bilty['detail']['commission_amount']) }}
                                                </td>
                                                <td class="td-num" rowspan="{{ count($bilty['items']) }}">
                                                    {{ $fmt($bilty['detail']['total_expanse_amount']) }}
                                                </td>
                                                <td class="td-num" rowspan="{{ count($bilty['items']) }}">
                                                    {{ $fmt($bilty['detail']['save_amount']) }}
                                                </td>
                                            @endif
                                        </tr>
                                    @endforeach

                                    {{-- If no items --}}
                                    @if(empty($bilty['items']))
                                        <tr class="row-item">
                                            <td class="td-c">{{ \Carbon\Carbon::parse($bilty['date'])->format('d/m/y') }}</td>
                                            <td class="td-c">{{ $bilty['bilty_no'] }}</td>
                                            <td class="td-r">{{ $bilty['supplier_name'] }}</td>
                                            <td colspan="5">—</td>
                                            <td class="td-num">{{ $fmt($bilty['detail']['rent_recieve_amount']) }}</td>
                                            <td class="td-num">{{ $fmt($bilty['detail']['labour_amount']) }}</td>
                                            <td class="td-num">{{ $fmt($bilty['detail']['market_fees_amount']) }}</td>
                                            <td class="td-num">{{ $fmt($bilty['detail']['post_expense_amount']) }}</td>
                                            <td class="td-num">{{ $fmt($bilty['detail']['cash_amount']) }}</td>
                                            <td class="td-num">{{ $fmt($bilty['detail']['commission_amount']) }}</td>
                                            <td class="td-num">{{ $fmt($bilty['detail']['total_expanse_amount']) }}</td>
                                            <td class="td-num">{{ $fmt($bilty['detail']['save_amount']) }}</td>
                                        </tr>
                                    @endif
                                @endforeach

                                {{-- Vehicle Total --}}
                                <tr class="row-vtotal">
                                    <td colspan="5" class="td-r fw-bold">
                                        گاڑی نمبر {{ $vehicle['pb_no'] }} — کل
                                    </td>
                                    <td class="td-c">{{ number_format($vehicle['vehicleTotals']['total_bori']) }}</td>
                                    <td class="td-c">{{ number_format($vehicle['vehicleTotals']['total_weight']) }}</td>
                                    <td></td>
                                    <td class="td-num">{{ $fmt($vehicle['vehicleTotals']['rent_recieve_amount']) }}</td>
                                    <td class="td-num">{{ $fmt($vehicle['vehicleTotals']['labour_amount']) }}</td>
                                    <td class="td-num">{{ $fmt($vehicle['vehicleTotals']['market_fees_amount']) }}</td>
                                    <td class="td-num">{{ $fmt($vehicle['vehicleTotals']['post_expense_amount']) }}</td>
                                    <td class="td-num">{{ $fmt($vehicle['vehicleTotals']['cash_amount']) }}</td>
                                    <td class="td-num">{{ $fmt($vehicle['vehicleTotals']['commission_amount']) }}</td>
                                    <td class="td-num">{{ $fmt($vehicle['vehicleTotals']['total_expanse_amount']) }}</td>
                                    <td class="td-num">{{ $fmt($vehicle['vehicleTotals']['save_amount']) }}</td>
                                </tr>
                            @endforeach

                            {{-- Grand Total --}}
                            <tr class="row-grand">
                                <td colspan="5" class="td-r">کل مجموعہ — Grand Total</td>
                                <td class="td-c">{{ number_format($grandTotal['total_bori']) }}</td>
                                <td class="td-c">{{ number_format($grandTotal['total_weight']) }}</td>
                                <td></td>
                                <td class="td-num">{{ $fmt($grandTotal['rent_recieve_amount']) }}</td>
                                <td class="td-num">{{ $fmt($grandTotal['labour_amount']) }}</td>
                                <td class="td-num">{{ $fmt($grandTotal['market_fees_amount']) }}</td>
                                <td class="td-num">{{ $fmt($grandTotal['post_expense_amount']) }}</td>
                                <td class="td-num">{{ $fmt($grandTotal['cash_amount']) }}</td>
                                <td class="td-num">{{ $fmt($grandTotal['commission_amount']) }}</td>
                                <td class="td-num">{{ $fmt($grandTotal['total_expanse_amount']) }}</td>
                                <td class="td-num">{{ $fmt($grandTotal['save_amount']) }}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            @endif
        @endif

    </div>
</div>
@endsection
