<div class="btn-group ml-1">
    @php
        $previous_advance  = $trips->calculate_previous_advance($trips->id);
        $trip_advance = $trips->trip_advances !=null?$trips->trip_advances->sum('amount'):0;
        $actual_freight = $trips->trip_details !=null?$trips->trip_details->sum('final_total_amount'):0;
        $trip_expenses = $trips->trip_expenses !=null?$trips->trip_expenses->sum('total_amount'):0;
        // $trip_payables = $trips->tirp_payables !=null?$trips->tirp_payables->sum('amount'):0;
        $trip_payables = $trips->tirp_payables != null ? $trips->tirp_payables->where('ref_type', 'advance')->sum('amount') : 0;
        $trip_reach_payables = $trips->tirp_payables != null ? $trips->tirp_payables->where('ref_type', 'reach')->sum('amount') : 0;
        $trip_receiving = $trips->tirp_receivings !=null?$trips->tirp_receivings->sum('amount'):0;
        // $trip_oils = $trips->cash_oil()!=null? $trips->cash_oil():0;
        $salaries =$trips->tripSalary!=null ? $trips->tripSalary->total_paid_amount:0;
        $adjustments =$trips->tripAdjustments!=null ? $trips->tripAdjustments->sum('amount'):0;
        $deposit = 0;
        $remaining_amount =$trips->calculate_trip_remaining_amount($trips->id);
    @endphp
    <button data-toggle="dropdown" class="btn btn-default btn-xs dropdown-toggle" aria-expanded="false"><i
            class="fa fa-bars"></i> <span class="caret"></span></button>
    <ul class="dropdown-menu" style="width: 300px;">
        <li>
            <span class="badge badge-info position-absolute r-5 mt-2" style="background-color: #1fa118">Total:
                {{ number_format($previous_advance, 0) }}</span>
            <a class="pointer" data-id="{{ $trips->id }}" type="button">
                <i class="fa fa-money"></i> سابقہ رقم</a>
        </li>
        <li class="divider"></li>

        @if (auth()->user()->hasPermissionTo('view trip details'))
        <li>
        <span class="badge badge-color4 position-absolute r-5 mt-2"
              style="position: absolute;background-color: #1fa118">Freight:
            {{ number_format($actual_freight, 0) }}</span>
            <a class="pointer show_trip_detail_modal" data-id="{{ $trips->id }}" target="">
                <i class="fa fa-money"></i> بلٹی کی تفصیلات</a>
        </li>

        <li class="divider"></li>
    @endif

        @if (auth()->user()->hasPermissionTo('view trip receiving'))
        <li>
        <span class="badge badge-color2 position-absolute r-5 mt-2" style="background-color: #139b07">Total:
            {{ number_format($trip_receiving,0) }}
        </span>
            <a data-id="{{ $trips->id }}" class="pointer show_received_account_modal" type="button">
                <i class="fa fa-money"></i>گاڑی سے وصول رقم</a>
        </li>
        <li class="divider"></li>
    @endif

        @if (auth()->user()->hasPermissionTo('view trip advance'))
            <li>
            <span class="badge badge-info position-absolute r-5 mt-2" style="background-color: #c00b0b">Total:
                {{ number_format($trip_advance, 0) }}</span>
                <a class="pointer add_trip_advance" data-id="{{ $trips->id }}" type="button">
                    <i class="fa fa-money"></i>گاڑی کو ادا رقم</a>
            </li>
            <li class="divider"></li>
        @endif
        @if (auth()->user()->hasPermissionTo('view trip payable'))
            <li>
                        <span class="badge  position-absolute r-5 mt-2" style="background-color: #c00b0b">Total:
                            {{ number_format($trip_payables,0) }}
                        </span>
                <a data-id="{{ $trips->id }}" data-vehicle-id="{{ $trips->vehicle_id }}"  class="pointer show_payable_account_modal" type="button">
                    <i class="fa fa-money"></i>  ایڈوانس بقایا</a>
            </li>
            <li class="divider"></li>

            <li>
                <span class="badge  position-absolute r-5 mt-2" style="background-color: #c00b0b">Total:
                    {{ number_format($trip_reach_payables,0) }}
                </span>
        <a data-reach-id="{{ $trips->id }}" data-vehicle-id="{{ $trips->vehicle_id }}" class="pointer show_reach_payable_account_modal" type="button">
            <i class="fa fa-money"></i>پہنچ بقایا </a>
    </li>
    <li class="divider"></li>
        @endif



        @if (auth()->user()->hasPermissionTo('view trip adjustments'))
            <li>
                
                <span class="badge badge-color1 position-absolute r-5"
                      style="background-color: #c00b0b">Total:{{number_format($adjustments,0)}}</span>

                {{-- <a class="pointer show_adjustments" data-id="{{ $trips->id }}" type="button">
                    <i class="fa fa-money"></i> ایڈجسٹمنٹس</a> --}}
            </li>
            <li class="divider"></li>
        {{-- @endif

        @if (auth()->user()->hasPermissionTo('view trip salary'))
            <li>
                <a class="pointer trip_salary" data-id="{{ $trips->id }}" type="button">
                <span class="badge badge-color1 position-absolute r-5"
                      style="background-color: #c00b0b">Total:{{ number_format($salaries,0) }}</span>

                    <i class="fa fa-money"></i>ٹرپ تنخواہ</a>
            </li>
            <li class="divider"></li> --}}
        {{-- @endif --}}

        {{--        @if ( $trips->trip_expenses->count() >= 1 && $trips->trip_oils->count() >= 1 && $trips->trip_details->count() >= 1)--}}
        {{--<li>
            <span class="badge badge-color1 position-absolute r-5"
                  style="background-color: #c00b0b">Total:{{ number_format($deposit ,0) }}</span>
            <a class="pointer trip_company_deposit" data-id="{{ $trips->id }}" type="button">
                <i class="fa fa-money"></i>کمپنی ڈپازٹ</a>
        </li>
        <li class="divider"></li>--}}
        {{-- @if (auth()->user()->hasPermissionTo('view trip oil'))
            <li>
            <span
                class="badge badge-color1 position-absolute r-5"
                style="background-color: #c00b0b">Total:{{ number_format($trip_oils ,0) }}</span>
                <a class="pointer " data-id="{{ $trips->id }}" type="button" onclick="show_oli_modal({{ $trips->id }})">
                    <i class="fa fa-money"></i>ٹرپ کیش آئل </a>
            </li>
            <li class="divider"></li> --}}
        @endif
        <li>
            <span
                class="badge badge-color1 position-absolute r-5"
                style="background-color: #0b9fc0">Total:{{ number_format(floor($remaining_amount) ,0) }}</span>
            <a class="pointer " data-id="{{ $trips->id }}" type="button">
                <i class="fa fa-money"></i>بقایہ رقم</a>
        </li>
        <li class="divider"></li>

        {{--        @endif--}}

    </ul>
</div>
<div class="btn-group ml-1">
    @if (auth()->user()->hasPermissionTo('edit trip'))
        <a type="button" data-id="{{ $trips->id }}" class="btn btn-link btn-xs edit_trip  ">اپڈیٹ</a>
    @endif
    @if (auth()->user()->hasPermissionTo('delete trip'))
        <a type="button" data-id="{{ $trips->id }}" class="btn btn-link btn-xs delete_trip text-danger ">ڈلیٹ</a>
    @endif

</div>
{{-- <div class="btn-group ml-1">
    <button data-toggle="dropdown" class="btn btn-default btn-xs dropdown-toggle" aria-expanded="false"><i
            class="fa fa-bus"></i> <span class="caret"></span></button>
    <ul class="dropdown-menu">
        <li class="dropdown-header">Name: Due: </li>
        <li>
            <a data-target="#brokerPaymentDetailsModel" class="pointer" type="button" data-toggle="modal"
                onclick="">
                <i class="fa fa-eye"></i> View Details</a>
        </li>
        <li><a onclick="" data-target="#addBrokerPaymentModel" class="pointer" type="button" data-toggle="modal">
                <i class="fa fa-money"></i> Add Payment</a></li>
    </ul>
</div> --}}




{{-- Not remove this code --}}


{{-- <script>

$(document).ready(function() {
    function updateRemainingAmount(tripId) {
        $.ajax({
            url: '/trips',
            method: 'GET',
            data: { tripId: tripId },
            success: function(response) {
                $('#remaining-amount').text(response.remainingAmount);
            },
            error: function(xhr, status, error) {
                console.error(error);
            }
        });
    }
    var tripId = {{ $trips->id }};
    updateRemainingAmount(tripId);


    setInterval(function() {
        updateRemainingAmount(tripId);
    }, 10000);
});

</script> --}}
