<!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>Vehicle Profit/Loss Summary</title>
    <style>
        .table,
        .tcell,
        .thead {
            text-align: center;
            border: 1px solid black;
            text-align: center;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
        }

    </style>
</head>

<body>
<div>
    <table style="width: 100%;border-collapse: collapse; margin-bottom: 10px">
        <tr>
            <td style="width:33.33%; text-align: left">{{$business_name->name}}</td>
            <td style="width:33.33%; text-align: center"><b>Vehicle Profit/Loss Summary</b></td>
            <td style="width:33.33%; text-align: right"></td>
        </tr>
    </table>

    <table style="width: 100%;border-collapse: collapse; margin-bottom: 10px">
        <tr>
            <td style="width:33.33%; text-align: left">
                <h4>{{ $data['vehicle'] != '' ? $data['vehicle']->vehicle_no: '' }} Profit/Loss Summary</h4>
            </td>
            <td style="width:33.33%; text-align: center">

            </td>
            <td style="width:33.33%; text-align: right">
                <span style="font-weight: bold">From Date:</span> <span
                    style="text-decoration: underline">{{ $data['form_date'] }}</span><br>
                <span style="font-weight: bold">To Date:</span> <span
                    style="text-decoration: underline">{{ $data['to_date'] }}</span>
            </td>
        </tr>
    </table>
</div>

<br>

<table class="table">
    <thead>
    <tr>
        <th class="thead">#</th>
        <th class="thead">Vehicle No</th>
        <th class="thead">Start Date</th>
        <th class="thead">End Date</th>
        <th class="thead">Actual Freight</th>
        <th class="thead">Diesel Exp.</th>
        <th class="thead">Trip Exp.</th>
        <th class="thead">Total Exp.</th>
        <th class="thead">Profit/Loss</th>
        {{-- <th class="thead">Status</th> --}}
    </tr>
    </thead>
    <tbody>
    @php
        $balance = 0;
            $total_debit = 0;
            $total_credit = 0;
            $total_actual_freight= 0;
            $total_deisel_exp= 0;
            $total_trip_exp= 0;
            $grand_total_exp= 0;
            $total_profit= 0;

    @endphp
    @forelse ($data['ledgers'] as $key =>$ledger)
        <tr>
            <td class="thead">{{ number_format($loop->iteration,0) }}</td>
            <td class="thead">{{ $ledger->vehicle->vehicle_no }}</td>
            <td class="thead">{{ date('F j, Y', strtotime($ledger->starting_date)) }}</td>
            <td class="thead">{{$ledger->ending_date != ''? date('F j, Y', strtotime($ledger->ending_date)): "UNKNOWN" }}</td>
            <td class="thead">
                @php
                    $actual_freight = $ledger->trip_details->sum('actual_freight');
                    $total_actual_freight = $total_actual_freight + $actual_freight;
                @endphp
                {{ number_format($actual_freight,0) }}</td>
            <td class="thead">
                @php
                    $diesel_amount = $ledger->diesel_amount();
                    $total_deisel_exp = $total_deisel_exp+ $diesel_amount;
                @endphp
                {{ number_format($diesel_amount,0) }}

            </td>
            <td class="thead">
                @php
                    $trip_expenses = $ledger->trip_expenses->sum('total_amount');
                    $total_trip_exp = $total_trip_exp+ $trip_expenses;
                    $total_expenses = $trip_expenses + $diesel_amount;
                    $grand_total_exp =$grand_total_exp+ $total_expenses ;
                @endphp
                {{ number_format($trip_expenses,0) }}
            </td>
            <td class="thead">{{ number_format($total_expenses, 0) }}</td>
            <td class="thead">
                @php
                    $profit = $actual_freight - $total_expenses;
                    $total_profit = $total_profit + $profit;
                @endphp
                {{ number_format($profit,0) }}
            </td>
            {{-- <th class="thead">Status</th> --}}
        </tr>
    @empty
        <tr>
            <td class="tcell" style="text-align: center" colspan="9"> No Data Available</td>
        </tr>
    @endforelse
    <tr>
        <th class="tcell" style="text-align: center" colspan="4"> Total:</th>
        <td class="tcell" style="text-align: center"> {{number_format($total_actual_freight, 0)}}</td>
        <td class="tcell" style="text-align: center"> {{number_format($total_deisel_exp, 0)}}</td>
        <td class="tcell" style="text-align: center"> {{number_format($total_trip_exp, 0)}}</td>
        <td class="tcell" style="text-align: center"> {{number_format($grand_total_exp, 0)}}</td>
        <td class="tcell" style="text-align: center"> {{number_format($total_profit, 0)}}</td>
    </tr>
    </tbody>
</table>

</body>

</html>
