<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ $data['vehicle']->vehicle_no }} - Vehicle Ledger</title>
    <style>
        body {
            font-family: 'Noto Nastaliq Urdu', 'Jameel Noori Nastaleeq', 'Arial', sans-serif;
            direction: rtl;
        }
        .table, .tcell, .thead {
            border: 1px solid black;
            text-align: center;
        }
        .table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        .trip-header {
            margin: 20px 0 5px;
            font-weight: bold;
            font-size: 16px;
            text-align: left;
        }
    </style>
</head>

<body>
    <div>
        <table style="width: 100%; margin-bottom: 10px">
            <tr>
                <td style="width:33.33%; text-align: left">
                    <h4>{{ $business_name->name ?? 'Business Name' }}</h4>
                </td>
                <td style="width:33.33%; text-align: center"><b>Vehicle Advance Ledger</b></td>
                <td style="width:33.33%; text-align: right"></td>
            </tr>
        </table>

        <table style="width: 100%; margin-bottom: 10px">
            <tr>
                <td style="width:33.33%; text-align: left"><h4>{{ $data['vehicle']->vehicle_no }}</h4></td>
                <td style="width:33.33%; text-align: center"></td>
                <td style="width:33.33%; text-align: right">
                    <strong>From Date:</strong> {{ $data['form_date'] }}<br>
                    <strong>To Date:</strong> {{ $data['to_date'] }}
                </td>
            </tr>
        </table>
    </div>

    @php
        $total_debit = 0;
        $total_credit = 0;
        $balance = 0;
        $groupedByTrip = collect();

$groupedByTrip = collect();

foreach ($data['ledgers'] as $ledger) {
    if ($ledger->ref_type === 'reach') {
        // Safely extract trip ID from description (e.g., "ٹرپ 36")
        preg_match('/ٹرپ\s*(\d+)/u', $ledger->description, $matches);

        // Use matched trip ID or fallback to original trip_id
        $targetTripId = isset($matches[1]) ? (int)$matches[1] : $ledger->trip_id;

        // Group safely
        $groupedByTrip[$targetTripId] = $groupedByTrip->get($targetTripId, collect())->push($ledger);
    } else {
        $groupedByTrip[$ledger->trip_id] = $groupedByTrip->get($ledger->trip_id, collect())->push($ledger);
    }
}


    @endphp

    @forelse ($groupedByTrip as $trip_id => $tripLedgers)
        <div class="trip-header">Trip ID: {{ $trip_id }}</div>

        {{-- PAID SECTION --}}
        @php
            $paidLedgers = $tripLedgers->filter(function ($ledger) {
                return $ledger->bility_detail_type === 'Paid' || in_array($ledger->ref_type, ['advance', 'reach']);
            });
        @endphp
        @if ($paidLedgers->count())
            <h5 style="text-align: left; margin: 10px 0;">Paid</h5>
            <table class="table">
                <thead>
                    <tr>
                        <th class="thead">ID</th>
                        <th class="thead">Date</th>
                        <th class="thead">Description</th>
                        <th class="thead">Debit</th>
                        <th class="thead">Credit</th>
                        <th class="thead">Balance</th>
                    </tr>
                </thead>
                <tbody>
                    @php $balance = 0; @endphp
                    @foreach ($paidLedgers as $ledger)
                        @php
                            $balance += $ledger->debit - $ledger->credit;
                            $total_debit += $ledger->debit;
                            $total_credit += $ledger->credit;
                        @endphp
                        <tr>
                            <td class="tcell">{{ $ledger->id }}</td>
                            <td class="tcell">{{ date('F j, Y', strtotime($ledger->date)) }}</td>
                            <td class="tcell">{{ $ledger->description }}</td>
                            <td class="tcell">{{ $ledger->debit != 0 ? number_format($ledger->debit) : '' }}</td>
                            <td class="tcell">{{ $ledger->credit != 0 ? number_format($ledger->credit) : '' }}</td>
                            <td class="tcell">{{ number_format($balance) }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        @endif

        {{-- TO_PAID SECTION --}}
        @php
            $toPaidLedgers = $tripLedgers->filter(function ($ledger) {
                return $ledger->bility_detail_type === 'To_paid' || in_array($ledger->ref_type, ['opening_balance', 'trip_advance', 'trip_receiving']);
            });
        @endphp
        @if ($toPaidLedgers->count())
            <h5 style="text-align: left; margin: 10px 0;">To Paid</h5>
            <table class="table">
                <thead>
                    <tr>
                        <th class="thead">ID</th>
                        <th class="thead">Date</th>
                        <th class="thead">Description</th>
                        <th class="thead">Debit</th>
                        <th class="thead">Credit</th>
                        <th class="thead">Balance</th>
                    </tr>
                </thead>
                <tbody>
                    @php $balance = 0; @endphp
                    @foreach ($toPaidLedgers as $ledger)
                        @php
                            $balance += $ledger->debit - $ledger->credit;
                            $total_debit += $ledger->debit;
                            $total_credit += $ledger->credit;
                        @endphp
                        <tr>
                            <td class="tcell">{{ $ledger->id }}</td>
                            <td class="tcell">{{ date('F j, Y', strtotime($ledger->date)) }}</td>
                            <td class="tcell">{{ $ledger->description }}</td>
                            <td class="tcell">{{ $ledger->debit != 0 ? number_format($ledger->debit) : '' }}</td>
                            <td class="tcell">{{ $ledger->credit != 0 ? number_format($ledger->credit) : '' }}</td>
                            <td class="tcell">{{ number_format($balance) }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        @endif

    @empty
        <p style="text-align: center; font-weight: bold;">No records available for selected dates.</p>
    @endforelse

    {{-- Grand Totals --}}
    <table class="table">
        <tr>
            <th class="tcell" colspan="3" style="text-align: right;">Grand Total:</th>
            <td class="tcell">{{ number_format($total_debit) }}</td>
            <td class="tcell">{{ number_format($total_credit) }}</td>
            <td class="tcell">{{ number_format($total_debit - $total_credit) }}</td>
        </tr>
    </table>

</body>
</html>
