<!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>All Trips Report | {{ $business_name->value }}</title>
<style>
    @font-face {
        font-family: 'NoriNastaleeq';
        src: url('{{ asset("fonts/Nori_nastalekh.ttf") }}') format("truetype");
    }

    body {
        font-family: 'NoriNastaleeq', 'Noto Nastaliq Urdu', 'Arial', sans-serif;
        direction: rtl;
        text-align: right;
        padding: 10px;
        margin: 0;
    }

    .business-name {
        font-size: 70px;           
        font-weight: bold;
        text-align: right;
        margin: 0;
        padding-bottom: 0px;
        border-bottom: 2px solid rgb(67, 156, 40);
        color: rgb(46, 21, 21);
        /* line-height: 1.2; */
    }

    .report-header {
        display: flex;
        justify-content: space-between;
        align-items: baseline;
        margin-bottom: 10px;
        font-size: 14px;
        font-weight: bold;
    }

    .report-title {
        font-size: 16px;
        font-weight: bold;
    }

    .report-dates {
        font-size: 14px;
    }

    .table, .tcell, .thead {
        border: 1px solid black;
    }

    .table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 10px;
    }

    .highlight {
        color: green;
        font-weight: bold;
    }

    .hidden-column {
        display: none;
    }
</style>



{{-- Header Section --}}
<div>
    {{-- Business Name Right Side --}}
    <h1 class="business-name">{{ $business_name->name }}</h1>

    {{-- Report Meta Left Side --}}
    <div class="report-info">
        <p style="margin: 0;"><strong>All Trip Reports</strong></p>
        <p style="margin: 2px 0;">
            <strong>From:</strong> <u>{{ $fDate }}</u> &nbsp;&nbsp;
            <strong>To:</strong> <u>{{ $tDate }}</u>
        </p>
    </div>
</div>
<br>
<table class="table">
    <thead>
    <tr>
        <th class="thead">Sr. No.</th>
        <th class="thead">Trip No.</th>
        <th class="thead">Vehicle Number</th>
        <th class="thead">Starting Date</th>
        <th class="thead">From</th>
        <th class="thead">To</th>
        <th class="thead">Broker Name</th>
        <th class="thead">Bill Freight</th>
        <th class="thead">Trip Advance</th>
        <th class="thead">Trip Reach</th>
        <th class="thead paid-section-balance">Paid Section Balance</th>
        <th class="thead balance">Balance</th>
        <th class="thead">Broker Advance</th>
        <th class="thead">Broker Reach</th>
        <th class="thead b-paid">B Paid</th>
        <th class="thead">Diff</th>
    </tr>
    </thead>
    <tbody>
    @php
        $totalTripAdvance = 0;
        $totalTripReach = 0;
        $totalBrokerAdvance = 0;
        $totalBrokerReach = 0;
        $totalPaidSectionBalance = 0;
        $totalBalance = 0;
        $totalBPaid = 0;
        $totalDiff = 0;
        $reachAmount = 0;
        $brokerReachAmount = 0;
        $brokerAdvanceAmount = 0;
    @endphp
    @forelse ($trips as $trip)
        @php
            // Initialize variables for each trip
            $tripAdvance = 0;
            $tripReach = 0;
            $brokerAdvance = 0;
            $brokerReach = 0;
            $reachAmount = 0;
            $brokerReachAmount = 0;
            $brokerAdvanceAmount = 0;
            $diff = 0;
            // Calculate trip advance
            if ($trip->trip_details->count() > 0 && $trip->trip_details[0]->status !== 'paid') {
                if ($trip->trip_details[0]->adv_short_amount == 0) {
                    $tripAdvance = $trip->trip_details[0]->advance_amount;
                } else {
                    $tripAdvance = $trip->trip_details[0]->adv_short_amount;
                }
            }
            // Calculate trip reach
            if ($trip->trip_details->count() > 0 && $trip->trip_details[0]->tripDetailReach) {
                $reach = $trip->trip_details[0]->tripDetailReach;
                if ($reach->status === 'paid') {
                    $tripReach = 0;
                    $reachAmount = 0;
                } elseif ($reach->short_amount == 0) {
                    $tripReach = $reach->remainder_amount;
                    $reachAmount = $reach->remainder_amount;
                } else {
                    $tripReach = $reach->short_amount;
                    $reachAmount = $reach->short_amount;
                }
            }
            // Calculate broker advance
            if ($trip->trip_details->count() > 0 && $trip->trip_details[0]->broker_advance) {
                $brokerAdv = $trip->trip_details[0]->broker_advance;
                if ($brokerAdv->status === 'paid') {
                    $brokerAdvance = 0;
                    $brokerAdvanceAmount = 0;
                } elseif ($brokerAdv->short_amount == 0) {
                    $brokerAdvance = $brokerAdv->remainder_amount;
                    $brokerAdvanceAmount = $brokerAdv->remainder_amount;
                } else {
                    $brokerAdvance = $brokerAdv->short_amount;
                    $brokerAdvanceAmount = $brokerAdv->short_amount;
                }
            }
            // Calculate broker reach
            if ($trip->trip_details->count() > 0 && $trip->trip_details[0]->broker_reach) {
                $brokerRch = $trip->trip_details[0]->broker_reach;
                if ($brokerRch->status === 'paid') {
                    $brokerReach = 0;
                    $brokerReachAmount = 0;
                } elseif ($brokerRch->short_amount == 0) {
                    $brokerReach = $brokerRch->remainder_amount;
                    $brokerReachAmount = $brokerRch->remainder_amount;
                } else {
                    $brokerReach = $brokerRch->short_amount;
                    $brokerReachAmount = $brokerRch->short_amount;
                }
            }
            // Calculate diff
            $bPaid = $trip->b_paid ?? 0;
            $diff = ($brokerReachAmount + $brokerAdvanceAmount) - $bPaid;
            $paidSectionBalance = \App\Models\VehicleAdvanceLedger::where('actual_trip_id', $trip->id)
                ->where('bility_detail_type', 'Paid')
                ->selectRaw('SUM(credit) - SUM(debit) as balance')
                ->value('balance') ?? 0;
            $balance = $reachAmount - $paidSectionBalance;
            $totalPaidSectionBalance += $paidSectionBalance;
            $totalTripAdvance += $tripAdvance;
            $totalTripReach += $tripReach;
            $totalBrokerAdvance += $brokerAdvance;
            $totalBrokerReach += $brokerReach;
            $totalBalance += ($reachAmount - $paidSectionBalance);
            $totalBPaid += $bPaid;
            $totalDiff += $diff;
        @endphp
        <tr style="text-align: center;">
            <td class="thead">{{ $loop->iteration }}</td>
            <td class="thead">{{ $trip->id }}</td>
            <td class="thead">{{ $trip->vehicle->vehicle_no }}</td>
            <td class="thead">{{ date('F j, Y', strtotime($trip->starting_date)) }}</td>
            <td class="thead">
                {{ $trip->trip_details->count() > 0 ? $trip->trip_details[0]->from_city->name : 'N/A' }}
            </td>
            <td class="thead">
                {{ $trip->trip_details->count() > 0 ? $trip->trip_details[0]->to_city->name : 'N/A' }}
            </td>
            <td class="thead">
                {{ $trip->trip_details->count() > 0 ? $trip->trip_details[0]->broker->name : 'N/A' }}
            </td>
            <td class="thead">
                {{ $trip->trip_details->count() > 0 ? number_format($trip->trip_details[0]->bill_freight, 0) : 'N/A' }}
            </td>
            <td class="thead"
                style="{{ $trip->trip_details->count() > 0 &&
                        ($trip->trip_details[0]->adv_short_amount > 0 || $trip->trip_details[0]->advance_amount > 0) ?
                        ($trip->trip_details[0]->status === 'pending' ? 'background-color: red;' :
                        ($trip->trip_details[0]->status === 'partial_paid' ? 'background-color: yellow;' :
                        ($trip->trip_details[0]->status === 'paid' ? 'background-color: green;' : ''))) : '' }}">
                {{ $trip->trip_details->count() > 0 ?
                    ($trip->trip_details[0]->status === 'paid' ? '0' :
                    number_format($trip->trip_details[0]->adv_short_amount == 0 ? $trip->trip_details[0]->advance_amount : $trip->trip_details[0]->adv_short_amount, 0))
                : 'N/A' }}
            </td>
            <td class="thead"
                style="{{ $trip->trip_details->count() > 0 && $trip->trip_details[0]->tripDetailReach ?
                    ($trip->trip_details[0]->tripDetailReach->status === 'pending' ? 'background-color: red;' :
                    ($trip->trip_details[0]->tripDetailReach->status === 'partial_paid' ? 'background-color: yellow;' :
                    ($trip->trip_details[0]->tripDetailReach->status === 'paid' ? 'background-color: green;' : ''))) : '' }}">
                {{ $trip->trip_details->count() > 0 && $trip->trip_details[0]->tripDetailReach ? $reachAmount : 'N/A' }}
            </td>
            <td class="thead paid-section-balance">{{ number_format($paidSectionBalance, 0) }}</td>
            <td class="thead balance">{{ number_format($balance, 0) }}</td>

            <td class="thead"
                style="{{ $trip->trip_details->count() > 0 && $trip->trip_details[0]->broker_advance ?
                    ($trip->trip_details[0]->broker_advance->status === 'pending' ? 'background-color: red;' :
                    ($trip->trip_details[0]->broker_advance->status === 'partial_paid' ? 'background-color: yellow;' :
                    ($trip->trip_details[0]->broker_advance->status === 'paid' ? 'background-color: green;' : ''))) : '' }}">
                @if($trip->trip_details->count() > 0 && $trip->trip_details[0]->broker_advance)
                    @if($trip->trip_details[0]->broker_advance->status === 'paid')
                        0
                    @elseif($trip->trip_details[0]->broker_advance->short_amount == 0)
                        {{ $trip->trip_details[0]->broker_advance->remainder_amount }}
                    @else
                        {{ $trip->trip_details[0]->broker_advance->short_amount }}
                    @endif
                @else
                    0
                @endif
            </td>
            <td class="thead"
                style="{{ $trip->trip_details->count() > 0 && $trip->trip_details[0]->broker_reach ?
                    ($trip->trip_details[0]->broker_reach->status === 'pending' ? 'background-color: red;' :
                    ($trip->trip_details[0]->broker_reach->status === 'partial_paid' ? 'background-color: yellow;' :
                    ($trip->trip_details[0]->broker_reach->status === 'paid' ? 'background-color: green;' : ''))) : '' }}">
                @if($trip->trip_details->count() > 0 && $trip->trip_details[0]->broker_reach)
                    @if($trip->trip_details[0]->broker_reach->status === 'paid')
                        0
                    @elseif($trip->trip_details[0]->broker_reach->short_amount == 0)
                        {{ $trip->trip_details[0]->broker_reach->remainder_amount }}
                    @else
                        {{ $trip->trip_details[0]->broker_reach->short_amount }}
                    @endif
                @else
                    N/A
                @endif
            </td>
            <td class="thead b-paid">{{ number_format($trip->b_paid, 0) }}</td>
            <td class="thead">{{ number_format($diff, 0) }}</td>
        </tr>
    @empty
        <tr>
            <td colspan="17" class="thead">No trips found</td>
        </tr>
    @endforelse
    <tr>
        <td class="thead" colspan="8" style="text-align: right; font-weight: bold;">Totals:</td>
        <td class="thead">{{ number_format($totalTripAdvance, 0) }}</td>
        <td class="thead">{{ number_format($totalTripReach, 0) }}</td>
        <td class="thead paid-section-balance">{{ number_format($totalPaidSectionBalance, 0) }}</td>
        <td class="thead balance">{{ number_format($totalBalance, 0) }}</td>
        <td class="thead">{{ number_format($totalBrokerAdvance, 0) }}</td>
        <td class="thead">{{ number_format($totalBrokerReach, 0) }}</td>
        <td class="thead b-paid">{{ number_format($totalBPaid, 0) }}</td>
        <td class="thead">{{ number_format($totalDiff, 0) }}</td>
    </tr>
    </tbody>
</table>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Check if the grand total for Paid Section Balance is 0
        const paidSectionBalanceTotal = parseFloat(document.querySelector('tr:last-child .paid-section-balance').textContent.replace(/,/g, ''));

        // If 0, hide the entire column
        if (paidSectionBalanceTotal === 0) {
            document.querySelectorAll('.paid-section-balance').forEach(cell => {
                cell.classList.add('hidden-column');
            });
        }

        // Always hide B Paid column
        document.querySelectorAll('.b-paid').forEach(cell => {
            cell.classList.add('hidden-column');
        });
    });
</script>
</body>
</html>
