<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::now()->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 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>
                                @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

                                        @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[]" value="{{ $hostelFee->id }}"

                                                           @if(is_array($fees) and in_array($hostelFee->id, $fees)) checked @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="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
                             )

                                                        @php
                                                        $currentInfo = \App\Models\ElectricWater::query()
                                                                ->whereBetween('date_action', [\Carbon\Carbon::now()->startOfMonth()->startOfDay(), \Carbon\Carbon::now()->endOfMonth()->endOfDay()])
                                                                ->where('hostel_id', $contract->hostel->id)
                                                                ->where('room_id', $contract->room->id)
                                                                ->where('contract_id', $contract->id)
                                                                ->latest('id')
                                                                ->first();
if($currentInfo)
    {


         $startElectric = $currentInfo->start_electric;
           $startWater = $currentInfo->start_water;

           $endElectric = $currentInfo->end_electric;
           $endWater = $currentInfo->end_water;
    } else if($latestMoneyDetail) {



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

                                               $endElectric = $info['end'];
                                               $endWater =$info['end'];
                                           } else {

                                                                $lastInfo = \App\Models\ElectricWater::query()
                                                                ->whereBetween('date_action', [\Carbon\Carbon::now()->startOfMonth()->startOfDay(), \Carbon\Carbon::now()->endOfMonth()->endOfDay()])
                                                                ->where('hostel_id', $contract->hostel->id)
                                                                ->where('room_id', $contract->room->id)
                                                                ->where('contract_id', $contract->id)
                                                                ->latest('id')
                                                                ->first();



    $startElectric = 0;
    $startWater = 0;
    $endElectric = 0;
    $endWater = 0;
       if ($lastInfo) {

            $startElectric = $lastInfo->start_electric;
           $startWater = $lastInfo->start_water;

           $endElectric = $lastInfo->end_electric;
           $endWater = $lastInfo->end_water;
       } else {

        $startTime = \Carbon\Carbon::now()->subMonth()->startOfMonth()->startOfDay()->toDateTimeString();
        $endTime = \Carbon\Carbon::now()->subMonth()->endOfMonth()->endOfDay()->toDateTimeString();

                                                                     $previousInfo  = \App\Models\ElectricWater::query()
                                                                    ->where('room_id', $contract->room->id)
                                                                    ->where('contract_id', $contract->id)
                                                                  ->whereBetween('date_action', [$startTime, $endTime])
                                                                  ->latest('id')->first();

                                                                  if(!empty($previousInfo))
                                                                  {
                                                                   $startElectric = $previousInfo->end_electric;
           $startWater = $previousInfo->end_water;
            $endElectric = $previousInfo->end_electric;
           $endWater = $previousInfo->end_water;
                                                                  } else {

                                                                      $feeContract = \App\Models\ContractFee::query()
                                                                      ->where('contract_id', $contract->id)
                                                                      ->where('fee_id', $hostelFee->id)
                                                                      ->first();
       if($feeContract) {
            $fee = $feeContract->fee_id;
            $feeItem = \App\Models\HostelFee::find($fee);
            if($feeItem) {
            if($feeItem->type == \App\Models\HostelFee::ELECTRIC || $feeItem->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK)
            {
                $startElectric = $feeContract->qty;
                $endElectric = $feeContract->qty;
            } else if ($feeItem->type == \App\Models\HostelFee::WATER || $feeItem->type == \App\Models\HostelFee::WATER_BY_CLOCK)
            {
                $startWater = $feeContract->qty;
                $endWater = $feeContract->qty;
            }
            }
            }

       }
                                                            }
       }



                                                        @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-end"
                                                                   name="ew-end[{{ $hostelFee->id }}]"
                                                                   value="{{ $endElectric }}"
                                                                   data-item-price="{{ $hostelFee->fee }}">
                                                        @else
                                                            <input type="number" class="form-control voucher-ew-end"
                                                                   name="ew-end[{{ $hostelFee->id }}]"
                                                                   value="{{ $endWater }}"
                                                                   data-item-price="{{ $hostelFee->fee }}">
                                                        @endif

                                                        {{--@if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::WATER)--}}
                                                        {{--<input type="number" class="form-control voucher-ew-end"--}}
                                                        {{--name="ew-end[{{ $hostelFee->id }}]"--}}
                                                        {{--value="0"--}}
                                                        {{--data-item-price="{{ $hostelFee->fee }}">--}}
                                                    @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

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

                                                     $contractFee = \App\Models\ContractFee::where('contract_id', $contract->id)
                                                     ->where('fee_id', $hostelFee->id)->first();

                                                     if($contractFee) {
                                                      $qty = $contractFee->qty;
                                                     } 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(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($endElectric - $startElectric, $room, \App\Models\HostelFee::ELECTRIC, $hostelFee->id);
                                                                    $sum += $amount;
                                                            @endphp

                                                            {{--{{ number_format($amount, 0, '.', '.') }}--}}
                                                        @else
                                                            @php
                                                                $amount = \App\Components\Functions::calculateElectricAmount($endWater - $startWater, $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)
                                                        @if($hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK)
                                                            @php $amount = ($endElectric - $startElectric) * $hostelFee->fee; @endphp
                                                        @else
                                                            @php $amount = ($endWater - $startWater) * $hostelFee->fee; @endphp
                                                        @endif

                                                        @php $sum += $amount @endphp
                                                    @else

                                                        @php
                                                            if($moneyDetail)
                                                    {
                                                        $amount = $moneyDetail->amount;

                                                    } else {

                                                    if($contractFee)
                                                    {
                                                    $amount = $hostelFee->fee * $contractFee->qty;
                                                    } else {
                                                     $amount = $hostelFee->fee * 1;
                                                    }
                                                    }

                                                        $sum += $amount


                                                        @endphp

                                                    @endif
                                                @endif

                                                @if(isset($amount))
                                                    {{ number_format($amount, 0, '.', '.') }}
                                                @else
                                                    0
                                                @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-all" 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>

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

            </div>
        </div>
    </div>


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

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