@php
    $hostelFees = \App\Models\HostelFee::query()->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($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="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




                                    $startElectric = 0;
                                    $startWater = 0;
                                    $endElectric = 0;
                                    $endWater = 0;
                                    $info = null;
                                      $startTime = \Carbon\Carbon::createFromFormat('d/m/Y', '01/'.$month)->startOfMonth()->startOfDay();
                                                                $endTime = \Carbon\Carbon::createFromFormat('d/m/Y', '01/'.$month)->endOfMonth()->endOfDay();
                                                                $currentInfo = \App\Models\ElectricWater::query()
                                                                ->whereBetween('date_action', [$startTime, $endTime])
                                                                ->where('hostel_id', $contract->hostel->id)
                                                                  ->where('contract_id', $contract->id)
                                                                ->where('room_id', $contract->room->id)
                                                                ->latest('id')
                                                                ->first();
if($currentInfo)
    {


        $info = [];
         $startElectric = $currentInfo->start_electric;
           $startWater = $currentInfo->start_water;

           $endElectric = $currentInfo->end_electric;
           $endWater = $currentInfo->end_water;
    } else 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'];
                                       } else {


                                                                $lastInfo = \App\Models\ElectricWater::query()
                                                                ->where('contract_id', $contract->id)
                                                                ->where('room_id', $contract->room->id)
                                                              ->whereBetween('date_action', [$startTime, $endTime])
                                                              ->latest('id')->first();
                                                }
if(!is_array($info)) {
   if (!empty($lastInfo)) {
        $startElectric = $lastInfo->start_electric;
       $startWater = $lastInfo->start_water;
        $endElectric = $lastInfo->end_electric;
       $endWater = $lastInfo->end_water;

   }else {

    $startTime = \Carbon\Carbon::createFromFormat('d/m/Y', '01/'.$month)->subMonth()->startOfMonth()->startOfDay()->toDateTimeString();
    $endTime = \Carbon\Carbon::createFromFormat('d/m/Y', '01/'.$month)->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 ?? 0;
       $startWater = $previousInfo->end_water ?? 0;
        $endElectric = $previousInfo->end_electric ?? 0;
       $endWater = $previousInfo->end_water ?? 0;
                                                              } else {

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

   if ($checkContract)
   {
        $fee = $checkContract->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 = $checkContract->qty;
            $endElectric = $checkContract->qty;
        } else
        {
            $startWater = $checkContract->qty;
            $endWater = $checkContract->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-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
                                @php $contractFee = \App\Models\ContractFee::where('contract_id', $contract->id)
                                                     ->where('fee_id', $hostelFee->id)->first(); @endphp
                                @if($contractFee)
                                    <input type="number" class="form-control voucher-fee-all"
                                           name="qty[{{ $hostelFee->id }}]"
                                           value="{{ $contractFee->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


                        @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
            @if(isset($isNote))
            <tr>
                <td colspan="9">
                    <div class="col-md-12">
                        <div class="form-group">
                            <label>Ghi chú</label>
                            <textarea name="note"  type="text" class="form-control" placeholder="">{{ $item->note }}</textarea>
                        </div>
                    </div>
                </td>
            </tr>
            @endif


            @if($item)
                <tr>
                    <td colspan="8"><b>Tổng tiền</b></td>
                    <td>{{  number_format($item->amount + $item->discount, 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, 0, '.', '.') }}</td>

                </tr>
            @else
                <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="0"></td>

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

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

</div>
