<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pump Pending Slip Report</title>
    <style>
        .table, .tcell, .thead {
            border: 1px solid black;
        }
        .table {
            width: 100%;
            border-collapse: collapse;
        }
    </style>
</head>
<body>

<table style="width: 100%; border-collapse: collapse; margin-bottom: 10px">
    <tr>
        <td style="text-align: center">
            <h1>Pump Pending Slip Report</h1>
        </td>
    </tr>
</table>

<table style="width: 100%; border-collapse: collapse; margin-bottom: 10px">
    <tr>
        <td style="text-align: left">
            <h4>
                @if($broker_id != 'all')
                    {{ $brokers->first()->name ?? 'N/A' }}
                @endif
            </h4>
        </td>
        <td style="text-align: right">
            <b>From:</b> {{ date("M j, Y", strtotime($starting_date)) }} &nbsp;
            <b>To:</b> {{ date("M j, Y", strtotime($ending_date)) }}
        </td>
    </tr>
</table>

<table class="table">
    <thead>
        <tr>
            <th class="thead">#</th>
            <th class="thead">Date</th>
            <th class="thead">Slip No.</th>
            <th class="thead">Trip ID</th>
            <th class="thead">Vehicle No</th>
            <th class="thead">Amount</th>
        </tr>
    </thead>
    <tbody>
        @php
            $iteration = 0;
            $grand_total = 0;
        @endphp

        @foreach($brokers as $broker)
            @php
                $advance_total = $broker->broker_advance->sum(function($advance) {
                    return $advance->short_amount ?: $advance->remainder_amount;
                });
                $reach_total = $broker->broker_reach->sum(function($reach) {
                    return $reach->short_amount ?: $reach->remainder_amount;
                });
                $broker_total = $advance_total + $reach_total;
                $grand_total += $broker_total;
            @endphp

            @if($broker_total > 0)
                <tr>
                    <td class="thead" colspan="6"><b><u>{{ $broker->name }}</u></b></td>
                </tr>

                <!-- Broker Advance Section -->
                @if($advance_total > 0)
                    <tr>
                        <td class="thead" colspan="6"><b>Advance</b></td>
                    </tr>
                    @foreach($broker->broker_advance as $advance)
                        @php
                            $amount = $advance->short_amount ?: $advance->remainder_amount;
                        @endphp
                        @if($amount > 0)
                            @php $iteration++; @endphp
                            <tr>
                                <td class="thead">{{ $iteration }}</td>
                                <td class="thead">{{ $advance->created_at->format('M j, Y') }}</td>
                                <td class="thead">{{ $advance->voucher_number ?? '-' }}</td>
                                <td class="thead">{{ $advance->trip_id ?? '-' }}</td>
                                <td class="thead">{{ $advance->vehicle->vehicle_no ?? '-' }}</td>

                                <td class="thead">{{ number_format($amount, 2) }}</td>
                            </tr>
                        @endif
                    @endforeach
                    <tr>
                        <td class="thead" colspan="6" style="text-align: right;"><b>Advance Sub Total:</b></td>
                        <td class="thead"><b>{{ number_format($advance_total, 2) }}</b></td>
                    </tr>
                @endif

                <!-- Broker Reach Section -->
                @if($reach_total > 0)
                    <tr>
                        <td class="thead" colspan="6"><b>Reach</b></td>
                    </tr>
                    @foreach($broker->broker_reach as $reach)
                        @php
                            $amount = $reach->short_amount ?: $reach->remainder_amount;
                        @endphp
                        @if($amount > 0)
                            @php $iteration++; @endphp
                            <tr>
                                <td class="thead">{{ $iteration }}</td>
                                <td class="thead">{{ $reach->created_at->format('M j, Y') }}</td>
                                <td class="thead">{{ $reach->voucher_number ?? '-' }}</td>
                                <td class="thead">{{ $reach->trip_id ?? '-' }}</td>
                                <td class="thead">{{ $reach->vehicle->vehicle_no ?? '-' }}</td>

                                <td class="thead">{{ number_format($amount, 2) }}</td>
                            </tr>
                        @endif
                    @endforeach
                    <tr>
                        <td class="thead" colspan="5" style="text-align: right;"><b>Reach Sub Total:</b></td>
                        <td class="thead"><b>{{ number_format($reach_total, 2) }}</b></td>
                    </tr>
                @endif
            @endif
        @endforeach
    </tbody>

    <!-- Grand Total -->
    <tr>
        <td class="thead" colspan="5" style="text-align: right;"><b>Grand Total:</b></td>
        <td class="thead"><b>{{ number_format($grand_total, 2) }}</b></td>
    </tr>
</table>

</body>
</html>
