<style>
    .table-responsive-scroll {
        max-height: 750px;
        overflow-x: auto;
        overflow-y: auto;
        position: relative;
    }
    
    .table-responsive-scroll table {
        width: max-content;
        border-collapse: collapse;
        min-width: 100%;
    }
    
    .table-responsive-scroll thead {
        position: sticky;
        top: 0;
        z-index: 3;
        background-color: #2e367e; /* Add background for thead */
    }
    
    .table-responsive-scroll thead th {
        position: sticky;
        top: 0;
        z-index: 4;
        background-color: #2e367e; /* Ensure th has background too */
        color: #fff;
        text-align: center;
        padding: 8px;
    }
</style>

@php
    $grand_on_road_price = 0;
    $grand_less_discount = 0;
    $grand_after_discount_price = 0;
    $grand_booking_amount = 0;
    $grand_dp_amount = 0;
    $grand_net_disbursement = 0;
    $grand_exchange_amount = 0;
    $grand_total_received = 0;
    $grand_receivable_amt = 0;
@endphp

<div class="table-responsive-scroll">
    <table class="table table-bordered table-striped mt-4">
        <thead>
            <tr>
                <th>ID</th>
                <th>Booking Date</th>
                <th>Branch</th>
                <th>Team Leader</th>
                <th>KEC Name</th>
                <th>Customer Name</th>
                <th>Model</th>
                <th>Varient</th>
                <th>Fuel Type</th>
                <th>Exterior Color</th>
                <th>Interior Color</th>
                <th>MFG. Year</th>
                <th>VIN No.</th>
                <th>On Road Price (A)</th>
                <th>Less Discount (B)</th>
                <th>After Disc On Road Price (A-B = C)</th>
                <th>Booking Amount</th>
                <th>DP Amount</th>
                <th>Fin. Disbusement Amt</th>
                <th>Exchange</th>
                <th>Total (D)</th>
                <th>Net Receivable Amt. (C - D)</th>
            </tr>
        </thead>
        <tbody>
            @forelse($data as $key => $val)
                @php
                    $onRoadPrice = $val->on_road_price ?? 0;
                    $less_discount = ($val->finance_discount ?? 0) + ($val->way_off ?? 0) + ($val->foc_accessories ?? 0) + ($val->offer_ib_amount ?? 0) + ($val->offer_kin_amount ?? 0);
                    $after_dis_road_price = $onRoadPrice - $less_discount;
        
                    $booking_amount = $val->booking_amount ?? 0;
                    $dp_amount = $val->dp_amount ?? 0;
                    $net_disbursement = $val->net_disbusment ?? 0;
                    $exchange_amount = $val->total_exchange_amount ?? 0;
        
                    $total = $booking_amount + $dp_amount + $net_disbursement + $exchange_amount;
                    $receivable_amt = $after_dis_road_price - $total;
        
                    // Grand Totals
                    $grand_on_road_price += $onRoadPrice;
                    $grand_less_discount += $less_discount;
                    $grand_after_discount_price += $after_dis_road_price;
                    $grand_booking_amount += $booking_amount;
                    $grand_dp_amount += $dp_amount;
                    $grand_net_disbursement += $net_disbursement;
                    $grand_exchange_amount += $exchange_amount;
                    $grand_total_received += $total;
                    $grand_receivable_amt += $receivable_amt;
                @endphp
                <tr>
                    <td>{{ $key+1 }}</td>
                    <td>{{ \Carbon\Carbon::parse($val->booking_date)->format('d-m-Y H:i:s') ?? '' }}</td>
                    <td>{{ $val->branch_name ?? '' }}</td>
                    <td>{{ $val->team_name ?? '' }}</td>
                    <td>{{ $val->sales_person_name ?? '' }}</td>
                    <td>{{ $val->customer_name ?? '' }}</td>
                    <td>{{ $val->product ?? '' }}</td>
                    <td>{{ $val->car_varient_name ?? '' }}</td>
                    <td>{{ $val->fuel_type_name ?? '' }}</td>
                    <td>{{ $val->exterior_color ?? '' }}</td>
                    <td>{{ $val->interior_color ?? '' }}</td>
                    <td>{{ $val->manufacturing_year ?? '' }}</td>
                    <td>{{ $val->vin_number ?? '' }}</td>
                    <td>{{ $onRoadPrice }}</td>
                    <td>{{ $less_discount }}</td>
                    <td>{{ $after_dis_road_price }}</td>
                    <td>{{ $booking_amount }}</td>
                    <td>{{ $dp_amount }}</td>
                    <td>{{ $net_disbursement }}</td>
                    <td>{{ $exchange_amount }}</td>
                    <td>{{ $total }}</td>
                    <td>{{ $receivable_amt }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="22" class="text-center">No booking found</td>
                </tr>
            @endforelse
        </tbody>
        <tfoot>
            <tr class="table-footer" style="background-color:#2e367e;">
                <td colspan="13" class="text-end">Total</td>
                <td>{{ $grand_on_road_price }}</td>
                <td>{{ $grand_less_discount }}</td>
                <td>{{ $grand_after_discount_price }}</td>
                <td>{{ $grand_booking_amount }}</td>
                <td>{{ $grand_dp_amount }}</td>
                <td>{{ $grand_net_disbursement }}</td>
                <td>{{ $grand_exchange_amount }}</td>
                <td>{{ $grand_total_received }}</td>
                <td>{{ $grand_receivable_amt }}</td>
            </tr>
        </tfoot>
    </table>
</div>

