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

    $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 }}">
        <thead>
        <tr>
            <th><label class="mt-checkbox">
                    <input type="checkbox" class="checkbox-all"> <span></span>
                </label>
            </th>
            <th style="width: 20%"> Tên</th>
            <th style="width: 20%"> Loại phí</th>
            <th style="width: 20%"> Đơn giá</th>
            <th> Đơn vị</th>
            @if($contract->room->hostel->type_rent != \App\Models\Hostel::TYPE_RENT_EVERY)
                <th>Chỉ số đầu</th>
                <th>Chỉ số cuối</th>
            @endif
            <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; $amount = 0;@endphp
            @foreach($hostelFees as $hostelFee)
                @if(!empty($item))
                    @php $moneyDetail = \App\Models\MoneyDetail::query()->where('money_info_id', $item->id)->where('hostel_fee_id', $hostelFee->id)->first();  @endphp
                @else
                    @php $moneyDetail = null @endphp
                @endif

                <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-all"
                                   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"
                                   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>
                    @if($contract->room->hostel->type_rent != \App\Models\Hostel::TYPE_RENT_EVERY)
                        <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-ew-start"
                                           name="ew-start[{{ $hostelFee->id }}]"
                                           value="{{ $ewStart[$hostelFee->id] }}"
                                           data-item-price="{{ $hostelFee->fee }}">
                                @else
                                    <input type="number" class="form-control voucher-ew-start"
                                           name="ew-start[{{ $hostelFee->id }}]"
                                           value="0"
                                           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-ew-end"
                                           name="ew-end[{{ $hostelFee->id }}]"
                                           value="{{ $ewEnd[$hostelFee->id] }}"
                                           data-item-price="{{ $hostelFee->fee }}">
                                @else
                                    <input type="number" class="form-control voucher-ew-end"
                                           name="ew-end[{{ $hostelFee->id }}]"
                                           value="0"
                                           data-item-price="{{ $hostelFee->fee }}">
                                @endif
                            @else
                                0
                            @endif
                        </td>
                    @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(isset($ewEnd[$hostelFee->id]) && isset($ewStart[$hostelFee->id]))
                            {{ $ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id] }}
                                @else
                                0
                                @endif

                        @else

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

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


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

                            @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC)
                                @php
                                if(isset($ewEnd[$hostelFee->id])) {
                                    $amount = \App\Components\Functions::calculateElectricAmount($ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id], $room, \App\Models\HostelFee::ELECTRIC, $hostelFee->id);
                                        $sum += $amount;
                                }

                                @endphp



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

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

                            @php
                                if(isset($ewEnd[$hostelFee->id]) && isset($ewStart[$hostelFee->id])) {
                                $qty = $ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id];
                        $sum += $hostelFee->fee * $qty;
                            } else {
                            $qty = 0;
                            }

                            @endphp

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


                        @elseif($hostelFee->type == \App\Models\HostelFee::DYNAMIC || $hostelFee->type == \App\Models\HostelFee::WATER_DYNAMIC || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_DYNAMIC)

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


                            $sum += \App\Components\Functions::filterInputNumber($feeDynamics[$hostelFee->id]) * $qty;
                            @endphp



                            @if(isset($feeDynamics[$hostelFee->id]))
                                {{ number_format(\App\Components\Functions::filterInputNumber($feeDynamics[$hostelFee->id]) * $qty, 0, '.', '.') }}
                            @else
                                0
                            @endif

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

                            $sum += $hostelFee->fee * $qty;
                            @endphp

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

                        @endif

                    </td>

                </tr>
            @endforeach


            <tr>
                <td colspan="8"><b>Tổng tiền</b></td>

                <td>{{  number_format($sum2, 0, '.', '.') }}</td>

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

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

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

</div>

<script>
    $(document).ready(function () {
        $('.fee-dynamic').zeninput();
    })
</script>