<div class="row">
    <div class="col-md-12" id="payment-data" style="margin-bottom: 15px">
        <p>Tổng
            số: {{ \App\Models\MoneyInfo::query()
            ->when(!empty($contractId), function($q) use ($contractId) {
                $q->where('contract_id', $contractId);
            })
            ->where('room_id', $roomId)
            ->whereBetween('date_action', [$startTime, $endTime])->count() }}
            hóa đơn (Đã thanh
            toán: {{ \App\Models\MoneyInfo::query()
            ->when(!empty($contractId), function($q) use ($contractId) {
                $q->where('contract_id', $contractId);
            }) ->where('room_id', $roomId)->whereBetween('date_action', [$startTime, $endTime])->where('remain', 0)->count() }}
            hóa đơn. Chưa thanh toán
            xong: {{ \App\Models\MoneyInfo::query()
            ->when(!empty($contractId), function($q) use ($contractId) {
                $q->where('contract_id', $contractId);
            }) ->where('room_id', $roomId)->whereBetween('date_action', [$startTime, $endTime])->where('remain','>', 0)->count() }}
            hóa đơn)</p>
        @php
            $sum =     \App\Models\MoneyInfo::query()
            ->when(!empty($contractId), function($q) use ($contractId) {
                $q->where('contract_id', $contractId);
            }) ->where('room_id', $roomId)->whereBetween('date_action', [$startTime, $endTime])->sum('amount');
        $paid =     \App\Models\MoneyInfo::query()
            ->when(!empty($contractId), function($q) use ($contractId) {
                $q->where('contract_id', $contractId);
            }) ->where('room_id', $roomId)->whereBetween('date_action', [$startTime, $endTime])->sum('pay');
        $discount =     \App\Models\MoneyInfo::query()
            ->when(!empty($contractId), function($q) use ($contractId) {
                $q->where('contract_id', $contractId);
            }) ->where('room_id', $roomId)->whereBetween('date_action', [$startTime, $endTime])->sum('discount');
        $remain = $sum - $paid - $discount;
        @endphp

        <p>Tổng tiền: {{ number_format($sum, 0, '.', '.') }} (Đã thanh toán: {{ number_format($paid, 0, '.', '.') }}. Số
            tiền còn lại: {{ number_format($remain, 0, '.', '.') }})</p>
    </div>
</div>
@foreach($items as $item)

    @php

        $startTime = \Carbon\Carbon::createFromFormat('d/m/Y', '01/'.$item->date_action_group)->startOfMonth()->toDateString();
         $endTime = \Carbon\Carbon::createFromFormat('d/m/Y','01/'. $item->date_action_group)->endOfMonth()->toDateString();

         $moneyInfoItems = \App\Models\MoneyInfo::where('room_id', $roomId)
         ->whereBetween('date_action', [$startTime, $endTime]);

     if(!empty($type))
     {
         if($type == 'paid')
         {
         $moneyInfoItems = $moneyInfoItems->where('remain', 0);
         } else if ($type == 'not-paid')
         {
  $moneyInfoItems = $moneyInfoItems->where('remain', '>', 0);
         }
     }

     if(!empty($contractId))
     {
         $moneyInfoItems = $moneyInfoItems->where('contract_id', $contractId);
     }

     $moneyInfoItems = $moneyInfoItems->orderBy('id', 'desc')->get();
    @endphp


    @if($moneyInfoItems->count() > 0)

        <div class="portlet box yellow">
            <div class="portlet-title">
                <div class="caption">Tháng {{ $item->date_action_group }} </div>
                <div class="tools">
                    <a href="javascript:;" class="collapse" data-original-title="" title=""> </a>
                </div>
            </div>
            <div class="portlet-body" style="display: block;">
                <div class="row">
                    <div class="col-md-12">


                    @foreach($moneyInfoItems as $moneyInfoItem)
                        @php
                            $details = \App\Models\MoneyDetail::where('money_info_id', $moneyInfoItem->id)->get();
               $room = $moneyInfoItem->room;

               $dateAction = $item->date_action->format('m/Y');
               $pay = \App\Components\Functions::calculateAmountForOrder($moneyInfoItem->id)['pay'];
               $sum = 0;
                           foreach($details as $detail) {
                           if($detail->sum_amount != 0)
                           {
                             $sum+= $detail->sum_amount;
                           } else {
                       $sum+= $detail->amount;
                       }

                                       }
                        @endphp
                        <!-- BEGIN Portlet PORTLET-->

                            <div class="col-md-12">
                                <div class="portlet box @if($sum == ($pay + $moneyInfoItem->discount)) green-jungle @elseif ($pay == 0) red @else green-steel @endif">
                                    <div class="portlet-title">

                                        <div class="caption" style="float: left">{{ $moneyInfoItem->name }}</div>

                                        <div class="caption" style="float: right">
                                            @if($sum == ($pay +   $moneyInfoItem->discount)) Đã thanh toán @elseif ($pay == 0) Chưa thanh toán. Tổng số
                                            tiền: {{ number_format($sum -  $moneyInfoItem->discount, 0, '.', '.') }} @else Đã
                                            thanh toán một phần. Số tiền còn
                                            lại: {{ number_format($sum - $pay -  $moneyInfoItem->discount, 0, '.', '.') }} @endif</div>
                                    </div>
                                    <div class="portlet-body">

                                        @if($moneyInfoItem->contract)
                                            @if($moneyInfoItem->type != \App\Models\MoneyInfo::VOUCHER_CONTRACT)
                                                <h5>Hợp đồng: {{ $moneyInfoItem->contract->code }}</h5>

                                            @endif
                                            <h5>Người đại diện: {{ $moneyInfoItem->contract->name }}</h5>
                                        @endif
                                        @include('admin2.money.detail', [
                                    'item' => $moneyInfoItem,
                                    'details' => $details,
                                    'pay' => $pay, 'sum' => $sum])


                                    </div>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="row">
                                    @if(in_array($moneyInfoItem->type, [ App\Models\MoneyInfo::VOUCHER_CONTRACT, App\Models\MoneyInfo::VOUCHER_ROOM_PRICE]))
                                        @if(auth('backend')->user()->can('edit-money-info'))
                                    <div class="col-md-3">
                                        <a href="#" class="update-money-info"
                                           data-money-info="{{ $moneyInfoItem->id }}">Cập nhật hóa đơn</a>
                                    </div>
                                            @endif
                                    @endif
                                    <div class="col-md-3">
                                        <a data-money-info="{{ $moneyInfoItem->id }}"
                                           data-date="{{ $moneyInfoItem->date_action->format('m/Y') }}"
                                           class="print-money-info" target="_blank"
                                           href="{{ url('admin/money/print', ['id' => $moneyInfoItem->id]) }}"> In
                                            phiếu</a>
                                    </div>
                                    <div class="col-md-3">
                                        <a href="#" class="money-info-email" data-money-info="{{ $moneyInfoItem->id }}">Email
                                            thông báo</a>
                                    </div>
                                    <div class="col-md-3">
                                        <a href="#" class="money-info-message"
                                           data-money-info="{{ $moneyInfoItem->id }}">Nhắn tin thông báo miễn phí 1</a>
                                    </div>
                                </div>


                            </div>
                            <div class="col-md-12" style="margin-top: 15px">

                                @if(isset($pay))
                                    @if($pay == 0)
                                        @if(auth('backend')->user()->can('add-payment'))
                                            <button class="btn green-jungle btn-paid"
                                                    data-money-info="{{ $moneyInfoItem->id }}">Thanh toán
                                            </button>
                                        @endif
                                    @elseif ($pay < $sum)
                                        @if(auth('backend')->user()->can('add-payment'))
                                            <button class="btn green-jungle btn-paid"
                                                    data-money-info="{{ $moneyInfoItem->id }}">Thanh toán bổ sung
                                            </button>
                                        @endif
                                    @endif
                                    <a href="#detail-history" data-toggle="modal" data-room="{{ $room->id }}"
                                       data-money-info="{{ $moneyInfoItem->id }}"
                                       class="btn purple-plum btn-detail-history">Lịch
                                        sử thanh toán</a>
                                @endif
                            </div>
                            <div class="col-md-12 form-paid" id="form-paid-{{ $moneyInfoItem->id }}"
                                 style="margin-top: 15px; margin-bottom: 15px">

                            </div>
                    @endforeach
                    <!-- END GRID PORTLET-->
                    </div>
                </div>

            </div>
        </div>

    @endif
@endforeach