<!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_id=='all')? "All Vehicles": "Vehicle" }} Profit Report| {{  $business_name->value }}</title>
    <style>
        .table,
        .tcell,
        .thead {
            border: 1px solid black;
            text-align: center;
        }

        .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($vehicle_id!='all')
                    {{ $vehicle->vehicle_no }}
                @endif
            </h4>
        </td>
        <td style="width:33.33%; text-align: center">
            <h4>  {{ ($vehicle_id=='all')? "All Vehicles": "Vehicle" }} Profit 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($from_date)) }}</span><br>
            <span style="font-weight: bold">To Date:</span> <span
                style="text-decoration: underline">{{ date("M j, Y",strtotime($to_date)) }}</span>
        </td>
    </tr>
</table>
<table class="table">
    <thead>
    <tr>
        <th class="thead">#</th>
        @if($vehicle_id=='all')
            <th class="thead">Vehicle</th>
        @endif
        <th class="thead">Trip</th>
        <th class="thead">Days</th>
        <th class="thead">Per Day Profit</th>
        <th class="thead">Total Profit</th>
    </tr>
    </thead>
    <tbody>
    @php
        $total_days = 0;
          $total_profit = 0;
          $profit = 0;
          $profit_collection = collect([]);
    @endphp
    @forelse($trips as $trip)
        <tr>
            <td class="thead">{{ $loop->iteration }}</td>
            @if($vehicle_id=='all')
                <td class="thead">{{ $trip['vehicle']->vehicle_no }}</td>
            @endif
            <td class="thead">{{ $trip['trip_id'] }}</td>
            <td class="thead">{{ number_format($trip['total_days'],0) }}</td>
            <td class="thead">{{ number_format($trip['per_day_profit'],2) }}</td>
            <td class="thead">{{ number_format($trip['total_profit'],2) }}</td>
        </tr>
    @empty
        <tr>
            <th class="thead" colspan="{{ $vehicle_id=='all'? 6:5 }}"><u>No Record Found</u></th>

        </tr>
    @endforelse
    <tr>
        <th class="thead" colspan="{{ $vehicle_id=='all'? 6:5 }}" style="margin-top: 9px"></th>

    </tr>
    <tr>
        <th class="thead" colspan="{{ $vehicle_id=='all'? 3:2 }}"><u>Grand Total:</u></th>
        <td class="thead">{{ number_format($trips->sum('total_days'),0) }}</td>
        <td class="thead">{{ number_format($trips->sum('total_profit')/$trips->sum('total_days'),0) }}</td>
        <td class="thead">{{ number_format($trips->sum('total_profit'),2) }}</td>
    </tr>
    </tbody>
</table>
@php
    $rankings =$trips->groupBy('vehicle_no')->map(function ($value,$index){
      $days=$value->sum('total_days');
                                   $profit=$value->sum('total_profit');
                                     return ['profit'=>$profit,'days'=>$days,'per_day_profit'=>$profit/$days];
     });

@endphp
@if($vehicle_id=='all')

    <table class="table">
        <thead>
        <tr>
            <th class="thead" colspan="5">Overall Rankings</th>
        </tr>
        <tr>
            <th class="thead" colspan="">#</th>
            <th class="thead" colspan="">Vehicle</th>
            <th class="thead" colspan="">Days</th>
            <th class="thead" colspan="">Per Day Profit</th>
            <th class="thead" colspan="">Profit</th>
        </tr>
        </thead>
        @foreach($rankings->sortByDesc('per_day_profit')->toArray() as $key=> $ranking)
            <tr>
                <td class="thead" colspan="">{{$loop->iteration}}</td>
                <td class="thead" colspan="">{{ $key }}</td>
                <td class="thead" colspan="">{{ $ranking['days'] }}</td>
                <td class="thead" colspan="">{{ number_format($ranking['per_day_profit'],2) }}</td>
                <td class="thead" colspan="">{{ number_format($ranking['profit'],2) }}</td>
            </tr>
        @endforeach
    </table>
@endif

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

</html>
