<table>
    <td colspan="20" style="font-weight: bold; background-color: gray; color: black;height: 68px; font-size: 16px;">
        Annual Profit Sheet
        {{ $vehicle->vehicle_no }} From {{ date('F Y', strtotime($start_date)) }}
        to {{ date('F Y', strtotime($end_date)) }}
    </td>
</table>
<table>
    <td colspan="10"
        style="text-align: center; height: 32px; background-color: #66b5dd;color: white; font-weight: bold; font-size: 13px; text-decoration: underline">
        Income
    </td>
    <td colspan="10"
        style="text-align: center; height: 32px; background-color: #01a8fe;color: white;font-weight: bold; font-size: 13px; text-decoration: underline">
        Expense
    </td>
</table>
<style>
    .table_th {
        width: 50px;
        text-align: center;
        font-weight: bold;

    }
</style>
<table class="table">
    <thead>
    <tr>
        <th class="table_th" style="width: 70px;text-align: center;font-weight: bold;font-size: 14px;">Year</th>
        <th class="table_th" style="width: 100px;text-align: center;font-weight: bold;font-size: 14px;">Month</th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Starting
            Date
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Ending Date
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Up Trip</th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Down Trip
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Trip Days
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Up Freight
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Down Freight
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Total
            Freight
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Fuel Qty
        </th>
        <th class="table_th" style="width: 140px;text-align: center;font-weight: bold;font-size: 14px;">Meter
            Reading
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Average</th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Fuel Amount
        </th>
        <th class="table_th" style="width: 135px;text-align: center;font-weight: bold;font-size: 14px;">Road Expense
        </th>
        <th class="table_th" style="width: 167px;text-align: center;font-weight: bold;font-size: 14px;">
            Market Repairing
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Auto Oil
            Bill
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Salary</th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Total
            Expense
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Net Profit
        </th>
    </tr>
    </thead>
    <tbody>
    @php
        $total_up_trips  =0;
        $total_down_trips = 0;
        $total_trip_days = 0;
        $total_up_freight = 0;
        $total_down_freight = 0;
        $total_trip_diesel =0;
$total_trip_reading = 0;
$total_diesel_amount =0;
$total_trip_expenses=0;
$total_market_repairings=0;
$total_salary=0;
$grand_total_expense=0;
$total_profit = 0;
$total_auto_oil =0;
    @endphp
    @forelse($trips as $key=> $trip)
        @php
            $total_up_trips  = $total_up_trips + $trip->up_trips();
            $total_down_trips = $total_down_trips +  $trip->down_trips();
            $total_up_freight = $total_up_freight + $trip->up_actual_freight();
            $total_down_freight = $total_down_freight +$trip->down_actual_freight();
            $total_trip_diesel = $total_trip_diesel + $trip->diesel();
        @endphp
        <tr>
            <td style="text-align: center; font-size: 13px;">{{ date('Y', strtotime($trip->ending_date)) }}</td>
            <td style="text-align: center; font-size: 13px;">{{ date('M', strtotime($trip->ending_date)) }}</td>
            <td style="text-align: center; font-size: 13px;">{{ date('F j, Y', strtotime($trip->starting_date)) }}
            </td>
            <td style="text-align: center; font-size: 13px;">
                {{ $trip->ending_date == null ? 'UNKNOWN' : date('F j, Y', strtotime($trip->ending_date)) }}</td>
            <td style="text-align: center; font-size: 13px;">{{ $trip->up_trips() }}</td>
            <td style="text-align: center; font-size: 13px;">{{ $trip->down_trips() }}</td>
            <td style="text-align: center; font-size: 13px;">
                @php
                    $datetime1 = new DateTime($trip->starting_date);
                    $datetime2 = new DateTime($trip->ending_date);
                    if ($datetime1 == $datetime2) {
                        $days = 1;
                    } else {
                        $interval = $datetime1->diff($datetime2);
                        $days = $interval->format('%a'); //now do whatever you like with $days

                    }
               $days= $days == 0 ? 1 : $days;
                    $total_trip_days = $total_trip_days + $days;
                @endphp
                {{ $days }} Days
            </td>
            <td style="text-align: center; font-size: 13px;">{{ number_format($trip->up_actual_freight(), 0) }}
            </td>
            <td style="text-align: center; font-size: 13px;">
                {{ $trip->down_trips() == 0 ? 0: number_format($trip->down_actual_freight(), 0) }}
            </td>
            <td style="text-align: center; font-size: 13px;">
                @php
                    $total_freight = $trip->up_actual_freight() + $trip->down_actual_freight();
                @endphp
                {{ number_format($total_freight, 0) }}
            </td>
            <td style="text-align: center; font-size: 13px;">{{ number_format($trip->diesel(), 0) }}</td>
            @php
                $trip_reading = $trip->end_reading - $trip->start_reading;
                $total_trip_reading  = $total_trip_reading + $trip_reading;
                if ($trip->diesel() == 0) {
                    $average = 0;
                } else {
                    $average = $trip_reading / $trip->diesel();
                }
                $diesel_amount = $trip->diesel_amount();
                $total_diesel_amount = $total_diesel_amount+ $diesel_amount;
                $trip_expenses = $trip->trip_expenses->sum('total_amount');
                $total_trip_expenses = $total_trip_expenses+ $trip_expenses;
                $market_repairings = $vehicle->specific_maintenances($start_date, $end_date);
                $total_market_repairings = $total_market_repairings+ $market_repairings;
                $auto_oil = $trip->oil_amount();
                $total_auto_oil = $total_auto_oil+ $auto_oil;
                $salary = 00;
                $total_salary = $total_salary+ $salary;
                $total_expense = $trip_expenses + $market_repairings + $diesel_amount +  $trip->oil_amount() + $salary;
              $grand_total_expense = $grand_total_expense+ $total_expense;
                $profit = $total_freight - $total_expense;


            @endphp
            <td style="text-align: center; font-size: 13px;">{{ number_format($trip_reading, 0) }}</td>
            <td style="text-align: center; font-size: 13px;">{{$average== 0 ? 0: number_format($average, 0) }}</td>
            <td style="text-align: center; font-size: 13px;">{{ $diesel_amount== 0 ? 0: number_format($diesel_amount, 0) }}</td>
            <td style="text-align: center; font-size: 13px;">{{$trip_expenses== 0 ? 0: number_format($trip_expenses, 0) }}</td>
            <td style="text-align: center; font-size: 13px;">{{ $market_repairings== 0 ? 0: number_format($market_repairings, 0) }}</td>
            <td style="text-align: center; font-size: 13px;">{{ $trip->oil_amount()==0 ? 0: number_format( $trip->oil_amount(), 0) }}</td>
            <td style="text-align: center; font-size: 13px;">{{ $salary == 0 ? 0 : number_format($salary, 0) }}</td>
            <td style="text-align: center; font-size: 13px;">{{$total_expense== 0 ? 0: number_format($total_expense, 0) }}</td>
            <td style="text-align: center; font-size: 13px;">{{$profit== 0 ? 0: number_format($profit, 0) }}</td>

        </tr>
    @empty
        <tr>
            <td colspan="20" style="font-size: 13px; font-weight: bold;"> No Data Available</td>
        </tr>
    @endforelse
    </tbody>
</table>
<table class="table">
    <thead>
    <tr>
        <th class="table_th" colspan="4" rowspan="2"
            style="width: 70px;text-align: center;font-weight: bold;font-size: 14px;">Grand Totals:
        </th>

        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Up Trip</th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Down Trip
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Trip Days
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Up Freight
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Down Freight
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Total
            Freight
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Fuel Qty
        </th>
        <th class="table_th" style="width: 140px;text-align: center;font-weight: bold;font-size: 14px;">Meter
            Reading
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Average</th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Fuel Amount
        </th>
        <th class="table_th" style="width: 135px;text-align: center;font-weight: bold;font-size: 14px;">Road Expense
        </th>
        <th class="table_th" style="width: 167px;text-align: center;font-weight: bold;font-size: 14px;">
            Market Repairing
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Auto Oil
            Bill
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Salary</th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Total
            Expense
        </th>
        <th class="table_th" style="width: 125px;text-align: center;font-weight: bold;font-size: 14px;">Net Profit
        </th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td style="text-align: center; font-size: 13px;">{{ number_format($total_up_trips ,0)}}</td>
        <td style="text-align: center; font-size: 13px;">{{ number_format( $total_down_trips,0) }}</td>
        <td style="text-align: center; font-size: 13px;">

            {{ number_format($total_trip_days,0)}} Days
        </td>
        <td style="text-align: center; font-size: 13px;">{{ number_format($total_up_freight, 0) }}
        </td>
        <td style="text-align: center; font-size: 13px;">
            {{ $total_down_freight == 0 ? 0: number_format($total_down_freight, 0) }}
        </td>
        <td style="text-align: center; font-size: 13px;">
            @php
                $grand_total_freight = $total_up_freight + $total_down_freight;
                if($total_trip_diesel==0){
                    $total_average = 0;
                }else{
                    if($total_trip_reading ==0){
                        $total_average = 0;
                    }else{
                    $total_average = $total_trip_diesel/$total_trip_reading;

                    }
                }
                $total_profit = $grand_total_freight - $grand_total_expense;
            @endphp
            {{ number_format($grand_total_freight, 0) }}
        </td>
        <td style="text-align: center; font-size: 13px;">{{ number_format($total_trip_diesel, 0) }}</td>
        <td style="text-align: center; font-size: 13px;">{{ number_format($total_trip_reading, 0) }}</td>
        <td style="text-align: center; font-size: 13px;">{{$total_average== 0 ? 0: number_format($total_average, 0) }}</td>
        <td style="text-align: center; font-size: 13px;">{{ $total_diesel_amount== 0 ? 0: number_format($total_diesel_amount, 0) }}</td>
        <td style="text-align: center; font-size: 13px;">{{$total_trip_expenses== 0 ? 0: number_format($total_trip_expenses, 0) }}</td>
        <td style="text-align: center; font-size: 13px;">{{ $total_market_repairings== 0 ? 0: number_format($total_market_repairings, 0) }}</td>
        <td style="text-align: center; font-size: 13px;">{{ $total_auto_oil==0 ? 0: number_format($total_auto_oil, 0) }}</td>
        <td style="text-align: center; font-size: 13px;">{{ $total_salary == 0 ? 0 : number_format($total_salary, 0) }}</td>
        <td style="text-align: center; font-size: 13px;">{{ $grand_total_expense== 0 ? 0: number_format($grand_total_expense, 0) }}</td>
        <td style="text-align: center; font-size: 13px;">{{ $total_profit== 0 ? 0: number_format($total_profit, 0) }}</td>

    </tr>

    </tbody>
</table>
