@php $amount = 0; $payAmount = 0;@endphp
@if(!empty($items))
@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)
       ->where(function ($q) use ($contract) {

                    $q->orwhere('user_id', $contract->renter_id);
                    $q->orWhere('contract_id', $contract->id);

                })
        ->where('remain', '>', 0)
        ->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);
        }
    }

    $moneyInfoItems = $moneyInfoItems->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">

                    @php $amount = 0;
                    @endphp
                    @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'];
               $payAmount+= $pay;
               $sum = 0;
                           foreach($details as $detail) {
                           if(!empty($detail->sum_amount))
                           {
                       $sum+= $detail->sum_amount;
                       } else {
                         $sum+= $detail->amount;
                       }
                       $amount += $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 && $moneyInfoItem->type == \App\Models\MoneyInfo::VOUCHER_CONTRACT)
                                            <h5>Hóa đơn khởi tạo hợp
                                                đồng {{ $moneyInfoItem->contract->code }}</h5> @endif

                                        @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">
                                    <div class="col-md-3">
                                        <a target="_blank"
                                           href="{{ url('admin/money/print', ['id' => $moneyInfoItem->id]) }}"> In
                                            phiếu</a>
                                    </div>
                                    <div class="col-md-3">
                                        <a href="#">Email thông báo</a>
                                    </div>
                                    <div class="col-md-3">
                                        <a href="#">Nhắn tin thông báo miễn phí</a>
                                    </div>
                                </div>


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

                                @if(isset($pay))
                                    <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
@endif

<div class="portlet light portlet-fit bordered">
    <div class="portlet-title">
        <div class="caption">
            <span class="caption-subject font-dark bold uppercase">Tổng hợp</span>
        </div>
    </div>
    <div class="portlet-body">
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    <label>Ngày thanh lý</label>

                    <input type="text" class="form-control datepicker" id="date-end-contract" name="date-end"
                           value="{{ \Carbon\Carbon::now()->format('d/m/Y') }}">

                </div>
                <div class="mt-checkbox-inline">
                    <label class="mt-checkbox">
                        <input type="checkbox" id="return-deposit" name="return_deposit" value="1" checked> Có trả lại
                        tiền cọc
                        <span></span>
                    </label>
                    <label class="mt-checkbox">
                        <input type="checkbox" id="return-money-info" name="return_money_info" value="1" checked> Có trả
                        lại tiền thừa
                        <span></span>
                    </label>
                </div>
                <input type="hidden" id="contract_deposit_end" value="{{ number_format($contract->deposit, 0, '.', '.') }}">

            </div>
        </div>

        <div class="table-scrollable" id="payment-end-contract">

            <table class="table table-bordered table-hover">
                <tbody>
                <tr>
                    <td> Tổng số tiền trong các hóa đơn</td>
                    <td> {{ number_format($amountSum, 0, '.', '.') }} </td>
                </tr>
                @php $extraPrice = 0;@endphp
                @if($amountReturned > 0)
                    <tr>
                        <td> @if(!empty($messageReturned)) {{ $messageReturned }} @endif </td>
                        <td> {{ number_format($amountReturned, 0, '.', '.') }} </td>
                    </tr>
                    @endif
                <tr>
                    <td> Số tiền cọc trả lại</td>
                    <td> <input class="form-control price-r" id="amount_return_deposit" type="text" name="amount_return_deposit" value="{{ number_format($contract->deposit, 0, '.', '.') }}"> </td>
                </tr>
                <tr>
                    <td> Đã thu</td>
                    <td> {{ number_format($amountPay, 0, '.', '.') }} </td>
                </tr>
                <tr>
                    <td> Còn lại phải thanh toán</td>
                    <td>  {{ number_format($amountReturnUser, 0, '.', '.') }} </td>
                </tr>
                </tbody>

            </table>

        </div>
        <button class="btn btn-success" id="cf-end-contract" data-contract="{{ $contract->id }}">Xác nhận đã thanh toán
            đủ và thanh lý
        </button>
        <button class="btn btn-danger" id="print-end-contract" data-contract="{{ $contract->id }}">In hóa đơn thanh lý
        </button>
    </div>
</div>
