<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Broker Freight Slip Report | {{  $business_name->value }}</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="width:33.33%; text-align: center">
            <h1>{{  $business_name->name }}</h1>
        </td>

    </tr>
</table>

<table style="width: 100%;border-collapse: collapse; margin-bottom: 10px">
    <tr>
        <td style="width:33.33%; text-align: left">
            <h4>
                @if($broker_id!='all')
                    {{ $broker->name }}
                @endif
            </h4>
        </td>
        <td style="width:33.33%; text-align: center">
            <h4> Broker Freight Slip Report</h4>
        </td>
        <td style="width:33.33%; text-align: right">
            <span style="font-weight: bold">From Date:</span> <span
                style="text-decoration: underline">{{ date("M j, Y",strtotime($starting_date)) }}</span><br>
            <span style="font-weight: bold">To Date:</span> <span
                style="text-decoration: underline">{{ date("M j, Y",strtotime($ending_date)) }}</span>
        </td>
    </tr>
</table>
<h1> All Report</h1>

 {{-- working --}}
 <table class="table">
    <thead>
        <tr>
            <th class="thead">Vehicle Name</th>
            <th class="thead">Debit</th>
            <th class="thead">Credit</th>
            <th class="thead">Balance</th>
        </tr>
    </thead>
    <tbody>
        @php
            $totalDebit = 0; // Initialize total debit
            $totalCredit = 0; // Initialize total credit
        @endphp

        @foreach($vehicles as $vehicle)
            @php
                $balance = 0;

                if ($vehicle->brokerTransactions->count() == 0) {
                    continue;
                }

                foreach ($vehicle->brokerTransactions->sortBy('date') as $transaction) {
                    $balance = ($balance + $transaction->debit) - $transaction->credit;
                }
                $debit = $balance < 0 ? abs($balance) : 0;
                $credit = $balance > 0 ? $balance : 0;

                $totalDebit += $debit; // Accumulate total debit
                $totalCredit += $credit; // Accumulate total credit
            @endphp
            <tr>
                <td class="thead">{{ $vehicle->vehicle_no }}</td>
                <td class="thead">{{ $debit > 0 ? number_format($debit, 2) : '0' }}</td>
                <td class="thead">{{ $credit > 0 ? number_format($credit, 2) : '0' }}</td>
                {{-- <td class="thead"><b>{{ number_format($balance, 2) }}</b></td> --}}
            </tr>
        @endforeach

        <!-- Add totals row -->
        <tr>
            <td class="thead"><b>Total</b></td>
            <td class="thead"><b>{{ number_format($totalDebit, 2) }}</b></td>
            <td class="thead"><b>{{ number_format($totalCredit, 2) }}</b></td>
            <td class="thead"><b>{{ $totalCredit - $totalDebit}}</b></td>
        </tr>
    </tbody>
</table>



<table class="table">
    <thead>
    <tr>
        <th class="thead">#</th>
        <th class="thead">Date</th>
        {{--        <th class="thead">Slip No.</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
            $iteration =0;
            $balance = 0;
            $total_credit  =0;
            $total_debit =0;
    @endphp
    @foreach($vehicles as $vehicle)
        @php
            $sub_total_debit =0;
            $sub_total_credit =0;
            $balance = 0;
            if($vehicle->brokerTransactions->count()==0){
            continue;
        }

        @endphp
        <tr>
            <td class="thead" colspan="4"><b><u>{{ $vehicle->vehicle_no }}</u></b></td>
        </tr>
     @foreach($vehicle->brokerTransactions->sortBy('date') as $oil)
            @php
                $iteration += 1;
                $sub_total_debit += $oil->debit;
                $sub_total_credit += $oil->credit;
                $formatted_date = date("M j, Y", strtotime($oil->date));

             @endphp

              @php
                $balance = ($balance + $oil->debit) - $oil->credit;
                $total_debit += $oil->debit;
                $total_credit += $oil->credit;
              @endphp
            <tr>
                <td class="thead">{{ $iteration }}</td>
                <td class="thead">{{ $formatted_date }}</td>
                {{-- <td class="thead">{{ $oil->slip_number }}</td> --}}
                <td class="thead">{{ $oil->description }}</td>
                <td class="thead">{{ number_format($oil->debit, 0) }}</td>
                <td class="thead">{{ number_format($oil->credit, 0) }}</td>
                <td class="thead">
                  {{ $balance }}
                </td>

            </tr>
        @endforeach

        <tr>
            <td class="thead" colspan="3" style="text-align: right;"><u><b>Sub Total:</b></u></td>
            <td class="thead" colspan=""><u>{{ number_format($sub_total_debit,0) }}</u></td>
            <td class="thead" colspan=""><u>{{ number_format($sub_total_credit,0) }}</u></td>
            <td class="thead " colspan=""><u><b>{{ $balance }}</b></u></td>

        </tr>

    @endforeach
    </tbody>
    <tr>
        <td class="thead" colspan="3" style="text-align: right;"><u><b>Grand Total:</b></u></td>
        <td class="thead" colspan=""><u>{{ number_format($total_debit,0) }}</u></td>
        <td class="thead" colspan=""><u>{{ number_format($total_credit,0) }}</u></td>
    </tr>
    @php
        $balance= $total_credit-$total_debit;
    @endphp
    <tr>
        <td class="thead" colspan="3" style="text-align: right;"><u><b>Balance:</b></u></td>
        <td class="thead" colspan="2"><u>{{ number_format(abs($balance),0) }}
            {{ $balance<0?"Dr": "Cr" }}
            </u></td>
    </tr>
</table>

{{-- <div>
<div style="text-align: right;">
    Total: {{ $data['total_amount'] }}
</div>
</div> --}}
</body>

</html>
