<!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 Ranking
        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">
            <h3>{{  $business_name->name }}</h3>
        </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 Ranking 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($start_date)) }}</span><br>
            <span style="font-weight: bold">To Date:</span> <span
                style="text-decoration: underline">{{ date("M j, Y",strtotime($end_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">Days</th>
        <th class="thead">Per Day Profit</th>
        <th class="thead">Total Profit</th>
    </tr>
    </thead>
    <tbody>

    @forelse($trips_collection as $trip_key=> $trips)
        @php
            $total_days = 0;
              $total_profit = 0;
              $profit_collection = collect([]);
            $key = array_keys($trips);
            $value = array_values($trips);
        @endphp   @if($value[0]->count()>0)
            <tr>
                <th class="thead" colspan="{{ $vehicle_id=='all'? 5:4 }}">{{ $key[0]  }}</th>
            </tr>

            @foreach($value[0] as $trip)
                @php

                    $start = \Carbon\Carbon::parse(max($trip->starting_date, $value[1]));
                    $end = \Carbon\Carbon::parse(min($trip->ending_date, $value[2]));
                    $days = $start->diffInDays($end) + 1;
                    $trip_start_date = \Carbon\Carbon::parse($trip->starting_date);
                    $trip_end_date = \Carbon\Carbon::parse($trip->ending_date);
                    $total_trip_days = $trip_start_date->diffInDays($trip_end_date) + 1;
                    $total_freight = $trip->trip_details != null ? $trip->trip_details->sum('final_total_amount') : 0;
                    $total_expenses = $trip->trip_expenses != null ? $trip->trip_expenses->sum('total_amount') : 0;
                    $total_oil = $trip->trip_oils != null ? $trip->trip_oils->sum('total_amount') : 0;
                    $total_trip_profit = $total_freight - ($total_oil + $total_expenses);
                    $per_day_trip_profit = $total_trip_profit / $total_trip_days;
                    $trip_monthly_profit = $per_day_trip_profit * $days;
                    $total_days = $total_days + $days;
                    $total_profit = $total_profit + $trip_monthly_profit;
                    // add profit to monthly report
                $total_days = $total_days == 0 ? 1 : $total_days;
                $profit = $total_profit / $total_days;
                $profit_collection->push(['vehicle_no'=>$trip->vehicle->vehicle_no ,'profit'=>$trip_monthly_profit,'trip'=>$trip->id,'days'=>$days,
                'vehicle_id'=>$trip->vehicle->id,
                'number'=>$loop->iteration]);
                @endphp

            @endforeach
            @php

                $rankings =$profit_collection->groupBy('vehicle_no')->map(function ($value,$index) {
              $profit = $value->sum('profit');
              $days = $value->sum('days');
              $per_day = $profit/$days;
                return ['profit'=>$profit,
                'days'=>$days,
                'per_day_profit'=>$per_day,
                'vehicle_id'=>count($value->toArray())>0?$value->toArray()[0]['vehicle_id']:0
                ];
                });
            @endphp
            @foreach($rankings->sortByDesc('per_day_profit')->toArray() as $key=> $ranking)
                @php
                    if($vehicle_id!='all' && $ranking['vehicle_id']!=$vehicle?->id){continue;}
                @endphp
                <tr>
                    <td class="thead" colspan="">{{$loop->iteration}}</td>
                    @if($vehicle_id=='all')

                        <td class="thead" colspan="">{{ $key }}</td>
                    @endif
                    <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
        @endif

    @empty
        <tr>
            <th class="thead" colspan="{{ $vehicle_id=='all'? 5:4 }}"><u>No Record Found</u></th>

        </tr>

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

    </tr>

    </tbody>
</table>


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

</html>
