<form role="form" id="form-voucher-all">
    <?php echo 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="<?php echo e($contract->room_id); ?>"
                           value="<?php echo e(\Carbon\Carbon::now()->format('m/Y')); ?>">
                </div>
            </div>
        </div>
        <?php 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> <?php echo e($contract->contract_note); ?></div>
                </div>
            </div>
            <?php 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 <?php echo e($room->name); ?>, <?php echo e($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;
                        ?>

                        <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
                                                        $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;
            }
            }
            }

       }
                                                            }
       }



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


                                                 } ?>

                                                    <input type="number" class="form-control voucher-fee-all"
                                                           name="qty[<?php echo e($hostelFee->id); ?>]"
                                                           value="<?php echo e($qty); ?>" data-item-price="<?php echo e($hostelFee->fee); ?>">


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


                                    <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="<?php echo e($discount); ?>"></td>

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

                                    </tr>
                                    </tbody>
                                <?php 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>