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

    $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); ?>">
        <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 if($hostelFees->count() > 0): ?>
            <tbody id="table-hostel-fees">

            <?php $sum = 0; $amount = 0;?>
            <?php $__currentLoopData = $hostelFees; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $hostelFee): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <?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[]"
                                   <?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"
                                   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>
                    <?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 if(isset($ewStart[$hostelFee->id])): ?>
                                    <input type="number" class="form-control voucher-ew-start"
                                           name="ew-start[<?php echo e($hostelFee->id); ?>]"
                                           value="<?php echo e($ewStart[$hostelFee->id]); ?>"
                                           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="0"
                                           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-ew-end"
                                           name="ew-end[<?php echo e($hostelFee->id); ?>]"
                                           value="<?php echo e($ewEnd[$hostelFee->id]); ?>"
                                           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="0"
                                           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(isset($ewEnd[$hostelFee->id]) && isset($ewStart[$hostelFee->id])): ?>
                            <?php echo e($ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id]); ?>

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

                        <?php else: ?>

                            <?php if(isset($qtys[$hostelFee->id]))
                                {
                                    $qty = $qtys[$hostelFee->id];
                                } 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($hostelFee->type == \App\Models\HostelFee::ELECTRIC || $hostelFee->type == \App\Models\HostelFee::WATER): ?>

                            <?php if($hostelFee->type == \App\Models\HostelFee::ELECTRIC): ?>
                                <?php
                                if(isset($ewEnd[$hostelFee->id])) {
                                    $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(intval($amount), 0, '.', '.')); ?>

                            <?php else: ?>
                                <?php
                                    if(isset($ewEnd[$hostelFee->id])) {
                                        $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(intval($amount), 0, '.', '.')); ?>

                            <?php endif; ?>

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

                            <?php
                                if(isset($ewEnd[$hostelFee->id]) && isset($ewStart[$hostelFee->id])) {
                                $qty = $ewEnd[$hostelFee->id] - $ewStart[$hostelFee->id];
                        $sum += $hostelFee->fee * $qty;
                            } else {
                            $qty = 0;
                            }

                            ?>

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



                        <?php elseif($hostelFee->type == \App\Models\HostelFee::DYNAMIC || $hostelFee->type == \App\Models\HostelFee::WATER_DYNAMIC || $hostelFee->type == \App\Models\HostelFee::ELECTRIC_DYNAMIC): ?>

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


                            $sum += \App\Components\Functions::filterInputNumber($feeDynamics[$hostelFee->id]) * $qty;
                            ?>



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

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

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

                            $sum += $hostelFee->fee * $qty;
                            ?>

                            <?php echo e(number_format($hostelFee->fee * $qty, 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($sum2, 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($sum2 - $discount, 0, '.', '.')); ?></td>

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

</div>

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