<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>

<form role="form" id="form-voucher-all">
    {!! csrf_field() !!}
    <div class="form-body">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label>Tháng</label>
                    <input type="text" name="month" class="form-control monthpicker" id="month-voucher-all" data-room="{{ $contract->room_id }}"
                           value="{{ \Carbon\Carbon::createFromFormat('d/m/Y', '01/'.$month)->format('m/Y') }}">
                </div>
            </div>
        </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="row">
            <div class="col-md-12">
                <!-- BEGIN Portlet PORTLET-->
                <div class="portlet box blue-hoki">
                    <div class="portlet-title">
                        <div class="caption">
                            Phòng {{ $room->name }}, {{ $room->hostel->name }}
                        </div>
                    </div>
                    <div class="portlet-body" id="voucher-body-all">

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

                            $hostelFees = $hostelFees->get();

                        $hostel = $contract->hostel;
                        @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> Tên</th>
                                    <th> Loại phí</th>
                                    <th> Đơ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>
                                @php $dateAction = \Carbon\Carbon::createFromFormat('d/m/Y', '01/'.$month); @endphp
                                @if($hostelFees->count() > 0)
                                    <tbody id="table-hostel-fees">

                                    @php $sum = 0; $discount = 0; @endphp
                                    @foreach($hostelFees as $hostelFee)
                                        @php
                                            $latestMoneyDetail=null;
                                                $latestMoneyInfo= \App\Models\MoneyInfo::query()->where( 'room_id', $contract->room->id )
                                                             ->where( 'type', \App\Models\MoneyInfo::VOUCHER_SERVICE )
                                                             ->validate( $contract->id )
                                                             ->where( 'date_action', '<', $dateAction->copy()->startOfMonth()->startOfDay() )
                                                             ->orderBy( 'date_action', 'desc' )
                                                             ->first();

                            if($latestMoneyInfo) {
                                                        $latestMoneyDetail = \App\Models\MoneyDetail::query()
                                                ->where('money_info_id', '=', $latestMoneyInfo->id)
                                                ->where('hostel_fee_id', $hostelFee->id)
                                                ->first();
                                                        }
                                        @endphp

                                        @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-all"
                                                           name="fee-contracts[]" value="{{ $hostelFee->id }}"
                                                           {{--@if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::WATER)--}}
                                                           {{--checked--}}
                                                           {{--@else--}}

                                                           @if(is_array($fees) and in_array($hostelFee->id, $fees)) checked @endif
                                                            {{--@endif--}}
                                                    > <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="@if($moneyDetail) {{ number_format($moneyDetail->value, 0, '.', '.') }} @else 0 @endif"
                                                           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
                             )

                                                        @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'];
                                                               } else if($latestMoneyDetail) {
                                            $info = json_decode($latestMoneyDetail->qty, true);
                                            $startElectric = $info['end'];
                                           $startWater = $info['end'];

                                           $endElectric = $info['end'];
                                           $endWater =$info['end'];
                                       }
                                                        @endphp
                                                        @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK)
                                                            <input type="number" class="form-control voucher-ew-start"
                                                                   name="ew-start[{{ $hostelFee->id }}]"
                                                                   value="{{ $startElectric }}"
                                                                   data-item-price="{{ $hostelFee->fee }}">
                                                        @else
                                                            <input type="number" class="form-control voucher-ew-start"
                                                                   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-ew-start"
                                                                   name="ew-end[{{ $hostelFee->id }}]"
                                                                   value="{{ $endElectric }}"
                                                                   data-item-price="{{ $hostelFee->fee }}">
                                                        @else
                                                            <input type="number" class="form-control voucher-ew-start"
                                                                   name="ew-end[{{ $hostelFee->id }}]"
                                                                   value="{{ $endWater }}"
                                                                   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($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-all"
                                                               name="qty[{{ $hostelFee->id }}]"
                                                               value="{{ $moneyDetail->qty }}"
                                                               data-item-price="{{ $hostelFee->fee }}">
                                                    @else
                                                        <input type="number" class="form-control voucher-fee-all"
                                                               name="qty[{{ $hostelFee->id }}]"
                                                               value="1" data-item-price="{{ $hostelFee->fee }}">
                                                    @endif


                                                @endif
                                            </td>
                                            <td>


                                                @php
                                                    if($moneyDetail)
                                            {
                                                $amount = $moneyDetail->amount;
                                                $sum += $amount;
                                            } else {
                                                $amount = $hostelFee->fee * 1;
                                            }

                                                @endphp

                                                {{ number_format($amount, 0, '.', '.') }}


                                            </td>

                                        </tr>
                                    @endforeach


                                    <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-all" 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>

                    </div>
                </div>
                <!-- END Portlet PORTLET-->
            </div>
        </div>
    </div>

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

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