@php
    $fees = DB::table('contract_fees')->where('contract_id', $contract->id)->pluck('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();

   // dd($hostelFees);

$hostel = $contract->hostel;
@endphp
@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 style="width: 15%"> Đơn giá</th>
            <th> Đơn vị</th>

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

            <th style="width: 15%"> 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)

                <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)) @if(in_array($hostelFee->id, $fees)) checked @endif @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
                        @elseif($hostelFee->type == \App\Models\HostelFee::DYNAMIC || $hostelFee->type == \App\Models\HostelFee::WATER_DYNAMIC || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_DYNAMIC)

                            <input type="text" class="form-control fee-dynamic-e"
                                   name="fee-dynamics[{{ $hostelFee->id }}]"
                                   value="0"
                                   data-item-price="0">
                        @else
                            {{ number_format($hostelFee->fee, 0, '.', '.' ) }}
                        @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
                            $info = \App\Components\Functions::getStartEndElectricWater($hostelFee, $contract, $contract->room, $month);
                    $startElectric = $info['start_electric'];
                    $startWater = $info['start_water'];
                    $endElectric = $info['end_electric'];
                    $endWater = $info['end_water'];
                        @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(in_array($hostelFee->id, $fees))
                                @php $cFee = DB::table('contract_fees')->where('contract_id', $contract->id)->where('fee_id', $hostelFee->id)->first() @endphp
                                @if($cFee)
                                    <input type="number" class="form-control voucher-fee" name="qty[{{ $hostelFee->id }}]" value="{{ $cFee->qty }}" data-item-price="{{ $hostelFee->fee }}">
                                @else
                                    <input type="number" class="form-control voucher-fee" name="qty[{{ $hostelFee->id }}]" value="1" data-item-price="{{ $hostelFee->fee }}">
                                @endif

                            @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(is_array($fees) and in_array($hostelFee->id, $fees))
                        @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)
                                {{  number_format($hostelFee->fee * ($endElectric - $startElectric), 0, '.', '.') }}
                                @php $sum +=  $hostelFee->fee * ($endElectric - $startElectric) @endphp
                            @else
                                {{  number_format($hostelFee->fee * ($endWater - $startWater), 0, '.', '.') }}
                                @php $sum +=  $hostelFee->fee * ($endWater - $startWater)@endphp
                            @endif


                        @else
                            @if(in_array($hostelFee->id, $fees))
                                @php $cFee = DB::table('contract_fees')->where('contract_id', $contract->id)->where('fee_id', $hostelFee->id)->first() @endphp
                                @if($cFee)
                                    {{ number_format($hostelFee->fee * $cFee->qty, 0, '.', '.') }}
                                    @php $sum +=  $hostelFee->fee * $cFee->qty @endphp
                                @else
                                    {{ number_format($hostelFee->fee * 1, 0, '.', '.') }}
                                    @php $sum +=  $hostelFee->fee * 1 @endphp
                                @endif

                            @else

                                {{ number_format($hostelFee->fee * 1, 0, '.', '.') }}

                            @endif

                        @endif
                            @endif
                    </td>

                </tr>
            @endforeach


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

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

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

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

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