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

    $hostelFees = $hostelFees->get();

$hostel = $contract->hostel;
?>

<div class="table-scrollable">
    <table class="table table-bordered table-hover">
        <input type="hidden" name="contract_id" value="<?php echo e($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>
            <?php if($contract->room->hostel->type_rent != \App\Models\Hostel::TYPE_RENT_EVERY): ?>
                <th>Chỉ số đầu</th>
                <th>Chỉ số cuối</th>
            <?php endif; ?>
            <th> Số lượng</th>
            <th> Thành tiền</th>
        </tr>
        </thead>
        <?php $dateAction = \Carbon\Carbon::createFromFormat('d/m/Y', '01/'.$month); ?>

        <?php if($hostelFees->count() > 0): ?>
            <tbody id="table-hostel-fees">

            <?php $sum = 0; $discount = 0; ?>
            <?php $__currentLoopData = $hostelFees; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $hostelFee): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                <?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();
                                }
                        ?>

                <?php if(!empty($item)): ?>
                    <?php $moneyDetail = \App\Models\MoneyDetail::query()
->where('money_info_id', $item->id)->where('hostel_fee_id', $hostelFee->id)
->first();  ?>
                <?php else: ?>
                    <?php $moneyDetail = null ?>
                <?php endif; ?>
                <tr class="table-hostel-fees-items" <?php if(!is_array($fees)): ?> style="opacity: 0.5" <?php endif; ?>
                <?php if(is_array($fees) and !in_array($hostelFee->id, $fees)): ?>

                style="opacity: 0.5"

                        <?php endif; ?>
                >
                    <td>

                        <label class="mt-checkbox">
                            <input type="checkbox" class="checkbox-contract-fee-all"
                                   name="fee-contracts[]" value="<?php echo e($hostelFee->id); ?>"
                                   
                                   
                                   

                                   <?php if(is_array($fees) and in_array($hostelFee->id, $fees)): ?> checked <?php endif; ?>
                                    
                            > <span></span>
                        </label>

                    </td>
                    <td> <?php echo e($hostelFee->name); ?> </td>
                    <td> <?php echo e($hostelFee->type_text); ?> </td>
                    <td>
                        <?php if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::WATER): ?>
                            Biến động
                        <?php 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[<?php echo e($hostelFee->id); ?>]"
                                   value="0"
                                   data-item-price="<?php echo e($hostelFee->fee); ?>">
                        <?php else: ?>
                            <?php echo e(number_format($hostelFee->fee, 0, '.', '.' )); ?>

                        <?php endif; ?>
                    </td>
                    <td><?php echo e($hostelFee->unit); ?></td>
                    <?php if($contract->room->hostel->type_rent != \App\Models\Hostel::TYPE_RENT_EVERY): ?>
                        <td>
                            <?php 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;
        }
        }
   }
   }
   }

                                       }
                                ?>
                                <?php 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[<?php echo e($hostelFee->id); ?>]"
                                           value="<?php echo e($startElectric); ?>"
                                           data-item-price="<?php echo e($hostelFee->fee); ?>">
                                <?php else: ?>
                                    <input type="number" class="form-control voucher-ew-start"
                                           name="ew-start[<?php echo e($hostelFee->id); ?>]"
                                           value="<?php echo e($startWater); ?>"
                                           data-item-price="<?php echo e($hostelFee->fee); ?>">
                                <?php endif; ?>

                            <?php else: ?>
                                0
                            <?php endif; ?>

                        </td>
                        <td>
                            <?php 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 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[<?php echo e($hostelFee->id); ?>]"
                                           value="<?php echo e($endElectric); ?>"
                                           data-item-price="<?php echo e($hostelFee->fee); ?>">
                                <?php else: ?>
                                    <input type="number" class="form-control voucher-ew-start"
                                           name="ew-end[<?php echo e($hostelFee->id); ?>]"
                                           value="<?php echo e($endWater); ?>"
                                           data-item-price="<?php echo e($hostelFee->fee); ?>">
                                <?php endif; ?>
                            <?php else: ?>
                                0
                            <?php endif; ?>
                        </td>
                    <?php endif; ?>
                    <td>
                        <?php 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 if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK): ?>
                                <?php echo e($endElectric - $startElectric); ?>


                            <?php else: ?>
                                <?php echo e($endWater - $startWater); ?>

                            <?php endif; ?>

                        <?php else: ?>

                            <?php if($moneyDetail): ?>
                                <input type="number" class="form-control voucher-fee-all"
                                       name="qty[<?php echo e($hostelFee->id); ?>]"
                                       value="<?php echo e($moneyDetail->qty); ?>"
                                       data-item-price="<?php echo e($hostelFee->fee); ?>">
                            <?php else: ?>
                                <?php $contractFee = \App\Models\ContractFee::where('contract_id', $contract->id)
                                                     ->where('fee_id', $hostelFee->id)->first(); ?>
                                <?php if($contractFee): ?>
                                    <input type="number" class="form-control voucher-fee-all"
                                           name="qty[<?php echo e($hostelFee->id); ?>]"
                                           value="<?php echo e($contractFee->qty); ?>" data-item-price="<?php echo e($hostelFee->fee); ?>">
                                <?php else: ?>
                                    <input type="number" class="form-control voucher-fee-all"
                                           name="qty[<?php echo e($hostelFee->id); ?>]"
                                           value="1" data-item-price="<?php echo e($hostelFee->fee); ?>">
                                <?php endif; ?>
                            <?php endif; ?>


                        <?php endif; ?>
                    </td>
                    <td>

                        <?php if(is_array($fees) and in_array($hostelFee->id, $fees)): ?>
                            <?php if($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::WATER): ?>

                                <?php if($hostelFee->type == \App\Models\HostelFee::ELECTRIC): ?>
                                    <?php
                                        $amount = \App\Components\Functions::calculateElectricAmount($endElectric - $startElectric, $room, \App\Models\HostelFee::ELECTRIC, $hostelFee->id);
                                            $sum += $amount;
                                    ?>

                                    
                                <?php else: ?>
                                    <?php
                                        $amount = \App\Components\Functions::calculateElectricAmount($endWater - $startWater, $room, \App\Models\HostelFee::WATER, $hostelFee->id);
                                            $sum += $amount;
                                    ?>
                                    
                                <?php endif; ?>
                            <?php elseif($hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK || $hostelFee->type == \App\Models\HostelFee::WATER_BY_CLOCK): ?>
                                <?php if($hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK): ?>
                                    <?php $amount = ($endElectric - $startElectric) * $hostelFee->fee; ?>
                                <?php else: ?>
                                    <?php $amount = ($endWater - $startWater) * $hostelFee->fee; ?>
                                <?php endif; ?>

                                <?php $sum += $amount ?>
                            <?php else: ?>

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

                            } else {

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

                                $sum += $amount


                                ?>

                            <?php endif; ?>
                        <?php endif; ?>

                        <?php if(isset($amount)): ?>
                            <?php echo e(number_format($amount, 0, '.', '.')); ?>

                        <?php else: ?>
                            0
                        <?php endif; ?>

                    </td>

                </tr>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            <?php 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=""><?php echo e($item->note); ?></textarea>
                        </div>
                    </div>
                </td>
            </tr>
            <?php endif; ?>


            <?php if($item): ?>
                <tr>
                    <td colspan="8"><b>Tổng tiền</b></td>
                    <td><?php echo e(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="<?php echo e($item->discount); ?>"></td>

                </tr>
                <tr>
                    <td colspan="8"><b>Tổng cộng</b></td>
                    <td><?php echo e(number_format($item->amount, 0, '.', '.')); ?></td>

                </tr>
            <?php else: ?>
                <tr>
                    <td colspan="8"><b>Tổng tiền</b></td>
                    <td><?php echo e(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><?php echo e(number_format($sum, 0, '.', '.')); ?></td>

                </tr>
            <?php endif; ?>
            </tbody>
        <?php endif; ?>
    </table>

</div>
