@php
    $fees = DB::table('money_details')->where('money_info_id', $item->id)->pluck('hostel_fee_id')->toArray();

    $hostelFees = \App\Models\HostelFee::where('hostel_id', $contract->room->hostel->id);

    if($contract->room->hostel->type_rent == \App\Models\Hostel::TYPE_RENT_EVERY)
    {
    //$hostelFees = $hostelFees->where('type', '<>', \App\Models\HostelFee::ELECTRIC)->where('type', '<>', \App\Models\HostelFee::WATER);
    }

    $hostelFees = $hostelFees->get();

$hostel = $contract->hostel;
@endphp
<div class="alert alert-warning">
    <strong>Chú ý!</strong> Bạn đã lập hóa đơn cho tháng {{ $item->date_action->month }}. Các thao tác dưới đây sẽ cập
    nhật lại hóa đơn
</div>
@if(!empty($contract->contract_note))
    <div class="row">
        <div class="col-md-12">
            <div class="alert alert-danger">
                <strong>Ghi chú hợp đồng: </strong> {{ $contract->contract_note }}</div>
        </div>
    </div>
@endif

<div class="table-scrollable">
    <input type="hidden" name="contract_id" value="{{ $contract->id }}">
    <input type="hidden" name="user_id" value="{{ $renterId }}">
    <table class="table table-bordered table-hover">
        <thead>
        <tr>
            <th><label class="mt-checkbox">
                    <input type="checkbox" class="checkbox-all"> <span></span>
                </label>
            </th>
            <th> Tên</th>
            <th> Loại phí</th>
            <th> Đơn giá</th>
            <th> Đơn vị</th>

            <th>Chỉ số đầu</th>
            <th>Chỉ số cuối</th>

            <th> Số lượng</th>
            <th> Thành tiền</th>
        </tr>
        </thead>

        @if($hostelFees->count() > 0)
            <tbody id="table-hostel-fees">
            @php $sum = 0; $pay = 0; @endphp
            @foreach($hostelFees as $hostelFee)

                @php $moneyDetail = \App\Models\MoneyDetail::where('money_info_id', $item->id)->where('hostel_fee_id', $hostelFee->id)->first(); @endphp

                <tr class="table-hostel-fees-items" @if(!is_array($fees)) style="opacity: 0.5"
                    @endif @if(is_array($fees) and !in_array($hostelFee->id, $fees)) style="opacity: 0.5" @endif>
                    <td>

                        <label class="mt-checkbox">
                            <input type="checkbox" class="checkbox-contract-fee"
                                   name="fee-contracts[]"
                                   @if(is_array($fees) and in_array($hostelFee->id, $fees)) checked
                                   @endif value="{{ $hostelFee->id }}"> <span></span>
                        </label>

                    </td>
                    <td> {{ $hostelFee->name }} </td>
                    <td> {{ $hostelFee->type_text }} </td>
                    <td>
                        @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::WATER)
                            Biến động
                        @else

                            @if($moneyDetail)
                            <input type="text" class="form-control fee-dynamic-e"
                                   name="fee-dynamics[{{ $hostelFee->id }}]"
                                   value="{{ number_format($moneyDetail->value, 0, '.', '.') }}"
                                   data-item-price="{{ $moneyDetail->value }}">
                            @else
                                <input type="text" class="form-control fee-dynamic-e"
                                       name="fee-dynamics[{{ $hostelFee->id }}]"
                                       value="{{ number_format($hostelFee->fee, 0, '.', '.') }}"
                                       data-item-price="{{ $hostelFee->fee }}">
                                @endif
                        @endif
                    </td>
                    <td>{{ $hostelFee->unit }}</td>

                    @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::WATER
              || $hostelFee->type == \App\Models\HostelFee::WATER_BY_CLOCK || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK
              )

                        @php
                            $startElectric = 0;
                                                           $startWater = 0;
                                                           $endElectric = 0;
                                                           $endWater = 0;
                                                              if ($moneyDetail) {

                                                           $info = json_decode($moneyDetail->qty, true);
                                                                   $startElectric = $info['start'];
                                                                  $startWater = $info['start'];

                                                                  $endElectric = $info['end'];
                                                                  $endWater = $info['end'];
                                                              }
                        @endphp

                    @endif
                    <td>
                        @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::WATER
                   || $hostelFee->type == \App\Models\HostelFee::WATER_BY_CLOCK || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK
                   )

                            @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK)
                                <input type="number" class="form-control voucher-fee"
                                       name="ew-start[{{ $hostelFee->id }}]"
                                       value="{{ $startElectric }}"
                                       data-item-price="{{ $hostelFee->fee }}">
                            @else
                                <input type="number" class="form-control voucher-fee"
                                       name="ew-start[{{ $hostelFee->id }}]"
                                       value="{{ $startWater }}"
                                       data-item-price="{{ $hostelFee->fee }}">
                            @endif

                        @else
                            0
                        @endif

                    </td>
                    <td>
                        @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::WATER
                    || $hostelFee->type == \App\Models\HostelFee::WATER_BY_CLOCK || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK
                    )
                            @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK)
                                <input type="number" class="form-control voucher-fee"
                                       name="ew-end[{{ $hostelFee->id }}]"
                                       value="{{ $endElectric }}"
                                       data-item-price="{{ $hostelFee->fee }}">
                            @else
                                <input type="number" class="form-control voucher-fee"
                                       name="ew-end[{{ $hostelFee->id }}]"
                                       value="{{ $endWater }}"
                                       data-item-price="{{ $hostelFee->fee }}">
                            @endif
                        @else
                            0
                        @endif
                    </td>

                    <td>
                        @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::WATER
                        || $hostelFee->type == \App\Models\HostelFee::WATER_BY_CLOCK || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK
                        )
                            @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK)
                                {{ $endElectric - $startElectric }}
                            @else
                                {{ $endWater - $startWater }}
                            @endif

                        @else
                            @if($moneyDetail)

                                <input type="number" class="form-control voucher-fee" name="qty[{{ $hostelFee->id }}]"
                                       value="{{ $moneyDetail->qty }}" data-item-price="{{ $moneyDetail->value }}">

                            @else

                                <input type="number" class="form-control voucher-fee" name="qty[{{ $hostelFee->id }}]"
                                       value="1" data-item-price="{{ $hostelFee->fee }}">

                            @endif

                        @endif
                    </td>
                    <td>
                        @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::WATER)
                            @if($moneyDetail)
                                @php $sum +=  $moneyDetail->amount @endphp
                                {{ number_format($moneyDetail->amount, 0, '.', '.') }}
                            @endif
                        @elseif($hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK || $hostelFee->type == \App\Models\HostelFee::WATER_BY_CLOCK)
                            @if($moneyDetail)
                                @php $sum +=  $moneyDetail->amount @endphp
                                {{ number_format($moneyDetail->amount, 0, '.', '.') }}
                            @endif
                        @else
                            @if($moneyDetail)
                                {{ number_format($moneyDetail->amount, 0, '.', '.') }}
                                @php $sum +=  $moneyDetail->amount @endphp
                            @else
                                {{ number_format($hostelFee->fee * 1, 0, '.', '.') }}
                                @php $sum +=  $hostelFee->fee * 1 @endphp
                            @endif

                        @endif
                    </td>

                </tr>
            @endforeach

            @if(isset($isNote))
                <tr>
                    <td colspan="9">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label>Ghi chú</label>
                                <textarea name="note"  type="text" class="form-control" placeholder="">{{ $item->note }}</textarea>
                            </div>
                        </div>
                    </td>
                </tr>
            @endif

            <tr>
                <td colspan="8"><b>Tổng tiền</b></td>
                <td>{{  number_format($item->amount, 0, '.', '.') }}</td>

            </tr>
            <tr>
                <td colspan="8"><b>Giảm giá</b></td>
                <td><input type="number" class="form-control discount" name="discount" value="{{ $item->discount }}">
                </td>

            </tr>
            <tr>
                <td colspan="8"><b>Tổng cộng</b></td>
                <td>{{  number_format($item->amount - $item->discount, 0, '.', '.') }}</td>

            </tr>
            </tbody>
        @endif
    </table>

</div>
<textarea class="form-control" placeholder="Nhập ghi chú" name="note" >{{ $item->note }}</textarea>