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

        //if($contract->room->hostel->type_rent == \App\Models\Hostel::TYPE_RENT_EVERY)
        //{
        //$hostelFees = $hostelFees->where('type', '<>', \App\Models\HostelFee::ELECTRIC)->where('type', '<>', \App\Models\HostelFee::WATER);
        //}

        $hostelFees = $hostelFees->get();
        $hostel = $contract->hostel;
        $room = $contract->room;
        }


?>

<div class="table-scrollable">
    <table class="table table-bordered table-hover">
        <input type="hidden" name="contract_id" value="<?php echo e($contract->id); ?>">
        <input type="hidden" name="user_id" value="<?php echo e($contract->renter_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 style="width: 15%"> Đơn giá</th>
            <th> Đơn vị</th>

                <th>Chỉ số đầu</th>
                <th>Chỉ số cuối</th>

            <th style="width: 15%"> Số lượng</th>
            <th> Thành tiền</th>
        </tr>
        </thead>

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

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

                <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"
                                   name="fee-contracts[]" <?php if(is_array($fees) and in_array($hostelFee->id, $fees)): ?>  checked <?php endif; ?> value="<?php echo e($hostelFee->id); ?>"> <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-e"
                                   name="fee-dynamics[<?php echo e($hostelFee->id); ?>]"
                                   value="<?php echo e(number_format(\App\Components\Functions::filterInputNumber($feeDynamics[$hostelFee->id]), 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>

                        <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(isset($ewStart[$hostelFee->id])): ?>
                                    <input type="number" class="form-control voucher-fee"
                                           name="ew-start[<?php echo e($hostelFee->id); ?>]"
                                           value="<?php echo e($ewStart[$hostelFee->id]); ?>"
                                           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(isset($ewEnd[$hostelFee->id])): ?>
                                    <input type="number" class="form-control voucher-fee"
                                           name="ew-end[<?php echo e($hostelFee->id); ?>]"
                                           value="<?php echo e($ewEnd[$hostelFee->id]); ?>"
                                           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 echo e($ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id]); ?>


                        <?php else: ?>

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

                                <input type="number" class="form-control voucher-fee" 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($ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id], $room, \App\Models\HostelFee::ELECTRIC, $hostelFee->id);
                                        $sum += $amount;
                                ?>

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

                            <?php else: ?>
                                <?php
                                    $amount = \App\Components\Functions::calculateElectricAmount($ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id], $room, \App\Models\HostelFee::WATER, $hostelFee->id);
                                        $sum += $amount;
                                ?>
                                <?php echo e(number_format($amount, 0, '.', '.')); ?>

                            <?php endif; ?>
                        <?php elseif($hostelFee->type == \App\Models\HostelFee::ELECTRIC_BY_CLOCK || $hostelFee->type == \App\Models\HostelFee::WATER_BY_CLOCK): ?>

                        <?php $qty = $ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id];
                        $sum += $hostelFee->fee * $qty;

                        ?>
                            <?php echo e(number_format($hostelFee->fee * $qty, 0, '.', '.')); ?>


                        <?php else: ?>
                                <?php
                                    if(isset($qtys[$hostelFee->id]))
                            {
                                $qty = $qtys[$hostelFee->id];
                            } else {
                                $qty = 1;
                            }
                                ?>
                        <?php if(isset($feeDynamics[$hostelFee->id])): ?>
                                <?php echo e(number_format(\App\Components\Functions::filterInputNumber($feeDynamics[$hostelFee->id]) * $qty, 0, '.', '.')); ?>

                            <?php else: ?>
                                <?php echo e(number_format($hostelFee->fee * $qty, 0, '.', '.')); ?>

<?php endif; ?>
                        <?php endif; ?>
                            <?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" 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>
<textarea class="form-control" placeholder="Nhập ghi chú" name="note" ><?php if(isset($note)): ?> <?php echo e($note); ?> <?php endif; ?></textarea>