@php
    $hostelFees = null;
    if($contract){
        $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;
        $room = $contract->room;
        }


@endphp

<div class="table-scrollable">
    <table class="table table-bordered table-hover">
        <input type="hidden" name="contract_id" value="{{ $contract->id }}">
        <input type="hidden" name="user_id" value="{{ $contract->renter_id }}">
        <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">

            @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) 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
                        @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="{{ number_format(\App\Components\Functions::filterInputNumber($feeDynamics[$hostelFee->id]), 0, '.', '.') }}"
                                   data-item-price="{{ $hostelFee->fee }}">
                        @else
                            {{ number_format($hostelFee->fee, 0, '.', '.' ) }}
                        @endif
                    </td>
                    <td>{{ $hostelFee->unit }}</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(isset($ewStart[$hostelFee->id]))
                                    <input type="number" class="form-control voucher-fee"
                                           name="ew-start[{{ $hostelFee->id }}]"
                                           value="{{ $ewStart[$hostelFee->id] }}"
                                           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(isset($ewEnd[$hostelFee->id]))
                                    <input type="number" class="form-control voucher-fee"
                                           name="ew-end[{{ $hostelFee->id }}]"
                                           value="{{ $ewEnd[$hostelFee->id] }}"
                                           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
                        )
                            {{ $ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id] }}

                        @else

                                @php if(isset($qtys[$hostelFee->id]))
                                {
                                    $qty = $qtys[$hostelFee->id];
                                } else {
                                    $qty = 1;
                                } @endphp

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


                        @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)
                            @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC)
                                @php
                                    $amount = \App\Components\Functions::calculateElectricAmount($ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id], $room, \App\Models\HostelFee::ELECTRIC, $hostelFee->id);
                                        $sum += $amount;
                                @endphp

                                {{ number_format($amount, 0, '.', '.') }}
                            @else
                                @php
                                    $amount = \App\Components\Functions::calculateElectricAmount($ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id], $room, \App\Models\HostelFee::WATER, $hostelFee->id);
                                        $sum += $amount;
                                @endphp
                                {{ number_format($amount, 0, '.', '.') }}
                            @endif
                        @elseif($hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK || $hostelFee->type == \App\Models\HostelFee::WATER_BY_CLOCK)

                        @php $qty = $ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id];
                        $sum += $hostelFee->fee * $qty;

                        @endphp
                            {{ number_format($hostelFee->fee * $qty, 0, '.', '.') }}

                        @else
                                @php
                                    if(isset($qtys[$hostelFee->id]))
                            {
                                $qty = $qtys[$hostelFee->id];
                            } else {
                                $qty = 1;
                            }
                                @endphp
                        @if(isset($feeDynamics[$hostelFee->id]))
                                {{ number_format(\App\Components\Functions::filterInputNumber($feeDynamics[$hostelFee->id]) * $qty, 0, '.', '.') }}
                            @else
                                {{ number_format($hostelFee->fee * $qty, 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="{{ $discount }}"></td>

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

            </tr>
            </tbody>
        @endif
    </table>
</div>
<textarea class="form-control" placeholder="Nhập ghi chú" name="note" >@if(isset($note)) {{ $note }} @endif</textarea>