

<?php $__env->startSection('styles'); ?>

    <link href="/assets/global/plugins/bootstrap-wysihtml5/bootstrap-wysihtml5.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-datepicker/css/bootstrap-datepicker3.min.css" rel="stylesheet"
          type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css" rel="stylesheet"
          type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-editable/bootstrap-editable/css/bootstrap-editable.css"
          rel="stylesheet" type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-wysihtml5/bootstrap-wysihtml5.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/global/plugins/bootstrap-editable/inputs-ext/address/address.css" rel="stylesheet"
          type="text/css"/>


    <style>

        .pac-container {
            z-index: 10052 !important;
        }

        #map {
            width: auto !important;
            height: 500px !important;
        }

        #map_edit {
            width: auto !important;
            height: 500px !important;
        }

        tfoot {
            display: table-header-group;
        }

        tfoot input {
            font-weight: normal !important;
        }

        tfoot select {
            font-weight: normal !important;
        }

        .dataTables_filter {
            display: none;
        }

        #orders-table_length {
            display: none;
        }

        .ws-nr {
            width: 100% !important;
        }

        .warning > .sorting_1 {
            background-color: #F9E491 !important;
        }

        .warning > .sorting_2 {
            background-color: #F9E491 !important;
        }

        tr.warning td.sorting_1 {
            background-color: #F9E491 !important;
        }

        tr.warning td.sorting_2 {
            background-color: #F9E491 !important;
        }

    </style>

<?php $__env->stopSection(); ?>
<?php $__env->startSection('content'); ?>
    <?php if(session()->has('error')): ?>
        <div class="alert alert-danger"><?php echo e(session()->get('error')); ?></div>
    <?php endif; ?>
    <?php if(session()->has('success')): ?>
        <div class="alert alert-success"><?php echo e(session()->get('success')); ?></div>
    <?php endif; ?>
    <?php if(count($errors) > 0): ?>
        <div class="alert alert-danger">
            <ul>
                <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <li><?php echo e($error); ?></li>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </ul>
        </div>
    <?php endif; ?>
    <h1 class="page-title">
        Danh sách hóa đơn
    </h1>

    <div class="row"
         style="padding-bottom: 15px; margin-right: 0px; margin-left: 0px; padding-top: 15px; margin-bottom: 15px; border-bottom: 1px solid #e7ecf1; background-color: #EFF2F5">
        <div class="col-md-8">
            <div class="row">
                <div class="col-md-2">
                    <label class="control-label">Chọn nhà</label>
                </div>
                <div class="col-md-3">
                    <select class="form-control" id="hostels">
                        <option value="">Tất cả nhà</option>
                        <?php
                            $hostelArrs = \App\Models\Hostel::where('owner_id', auth('backend')->user()->id)->pluck('id')->toArray();
                            if(auth('backend')->user()->type == \App\User::STAFF)
	    {
		    $hostelArrs = \App\Components\Functions::getHostelArrStaff();
	    }
                                $hostels = \App\Models\Hostel::whereIn('id', $hostelArrs)->get();

                        ?>
                        <?php $__currentLoopData = $hostels; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $hostel): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option
                                    value="<?php echo e($hostel->id); ?>"><?php echo e($hostel->name); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <input class="form-control monthpicker" id="month" placeholder="Chọn tháng"
                           autocomplete="off">
                </div>
                <div class="col-md-3">
                    <select class="form-control" name="type" id="type">
                        <option value="">Tất cả hóa đơn</option>
                        <option value="1">Hoá đơn tiền nhà</option>
                        <option value="2">Hoá đơn tiền dịch vụ</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="row">
                <div class="col-md-12">
                    <a class="btn green-meadow pull-right" id="btn-export-voucher-excel"
                       href="<?php echo e(url('admin2/money/export-voucher-excel')); ?>"
                       download="">
                        <i class="fa fa-file-excel"></i> Xuất Excel</a>


                </div>
            </div>
        </div>
    </div>


    <div class="row">
        
        
        
        <div class="col-md-12">
            <table class="table table-striped table-bordered table-hover" id="orders-table">
                <thead>
                <tr style="background-color: #5376B9; color: white">
                    <th>STT</th>
                    <th>Mã hóa đơn</th>
                    <th>Mã HĐ</th>
                    <th>Khách thuê</th>
                    <th>Mã HĐ tham chiếu</th>

                    <th>Nội dung</th>
                    <th>Nhà</th>
                    <th>Phòng</th>
                    <th>Số tiền</th>
                    <th>Đã thanh toán</th>
                    <th>Còn lại</th>
                    <th>Thời gian</th>
                    <th style="width: 25%">Hành động</th>
                    
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th><input type="text" class="form-control"></th>
                    <th></th>
                    
                </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12" id="detail">
        </div>
    </div>


    <div class="modal fade bs-modal-lg" id="update-money-info-modal" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    <h4 class="modal-title">
                        Cập nhật thông tin hóa đơn
                    </h4>
                </div>
                <div class="modal-body" id="update-money-info-content">


                </div>
                <div class="modal-footer">
                    <button type="button" class="btn dark btn-outline" data-dismiss="modal" id="dismiss-modal">Đóng
                    </button>
                    <button type="button" class="btn red" id="btn-save-money-info-update">Lưu</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>

    <div class="modal fade bs-modal-lg" id="transaction-history" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content ">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    <h4 class="modal-title" id="voucher-title">Lịch sử thanh toán</h4>
                </div>
                <div class="modal-body" id="transaction-history-content">


                </div>
                <div class="modal-footer">
                    <button type="button" class="btn dark btn-outline" data-dismiss="modal" id="dismiss-modal">Đóng
                    </button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>

    <div class="modal fade bs-modal-lg" id="detail-money-info" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content ">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    <h4 class="modal-title" id="detail-money-info-title">Thông tin hóa đơn</h4>
                </div>
                <div class="modal-body" id="detail-money-info-content">


                </div>
                <div class="modal-footer">
                    <button type="button" class="btn dark btn-outline" data-dismiss="modal" id="dismiss-modal">Hủy
                    </button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>


<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
    <script src="/assets/global/plugins/dropzone/dropzone.min.js" type="text/javascript"></script>
    <script src="/assets/pages/scripts/form-dropzone.min.js" type="text/javascript"></script>
    <script src="/assets/global/plugins/jquery-validation/js/jquery.validate.min.js" type="text/javascript"></script>
    <script src="/assets/global/plugins/jquery-validation/js/additional-methods.min.js" type="text/javascript"></script>
    <script>
        $.validator.addMethod("greaterThan",
            function (value, element, param) {
                var $otherElement = $(param);
                return parseInt(value, 10) >= parseInt($otherElement.val(), 10);
            }
        );
    </script>


<?php $__env->stopPush(); ?>


<?php $__env->startPush('scripts'); ?>
    
    

    
    
    
    


    
    
    
    
    


    <script>
        $('.datepicker').datepicker({
            format: 'dd/mm/yyyy',
            autoclose: true
        });

        $(document).on('click', '.btn-paid-full', function (e) {
            e.preventDefault();
            var that = this;
            var money_info_id = $(this).attr('data-money-info');
            $.ajax({
                url: '<?php echo e(url('admin2/room/get-remain')); ?>',
                type: 'get',
                data: {
                    money_info_id: money_info_id
                },
                dataType: 'json',
                success: function (response) {
                    if (response.status == 2) {
                        swal('Thông báo', response.message, 'info');
                    }
                    if (response.status == 1) {
                        $('#form-paid-' + money_info_id).find('.price-r').val(response.data.remain);
                    }
//                $(that).parent().parent().find('.col-md-12').first().next().html(response.data);
                }
            })
        });
    </script>
    <script>


        $('.monthpicker').datepicker({
            format: 'mm/yyyy',
            autoclose: true,
            viewMode: "months",
            minViewMode: "months",
        }).on('changeDate', function () {
            getPostByAttr();
        });

        var table;

        $(document).on('click', '.detail-money', function () {
            var money_info_id = $(this).attr('data-id');
            $.ajax({
                url: '<?php echo e(url('admin2/money/detail-money-info')); ?>',
                type: 'get',
                data: {
                    money_info_id: money_info_id
                },
                dataType: 'json',
                beforeSend: function () {
                    $('#detail-money-info-content').addClass('ht-on-loading');
                },
                success: function (response) {
                    $('#detail-money-info-content').removeClass('ht-on-loading').html(response.data);
                    $('#detail-money-info-title').text(response.title);
                }
            });

        });

        $(document).on('click', '.delete-voucher', function () {
            var money_info_id = $(this).attr('data-money-info');
            bootbox.confirm({
                message: "Bạn có chắc chắn muốn xóa ?",
                buttons: {
                    confirm: {
                        label: 'Có',
                        className: 'btn-success'
                    },
                    cancel: {
                        label: 'Không',
                        className: 'btn-danger'
                    }
                },


                callback: function (result) {
                    if (result == true) {

                        $.ajax({
                            url: '<?php echo e(url('admin2/money/delete-voucher')); ?>',
                            type: 'post',
                            data: {
                                id: money_info_id
                            },
                            dataType: 'json',
                            success: function (response) {
                                if (response.status == 1) {
                                    swal('Thành công', 'Hóa đơn đã được xóa', 'success');
                                    table.ajax.reload();
                                } else {
                                    swal('Thông báo', response.message, 'info');
                                }
                            }
                        });

                    }
                }
            });


        });

        $(document).on('click', '.btn-transaction-history', function (e) {
            e.preventDefault();
            var roomId = $(this).attr('data-room');
            var moneyInfoId = $(this).attr('data-id');
            $.ajax({
                url: '/admin2/room/paid-history',
                data: {
                    room_id: roomId,
                    money_info_id: moneyInfoId
                },
                type: 'get',
                dataType: 'json',
                beforeSend: function () {
                    $('#transaction-history-content').addClass('ht-on-loading');
                },
                success: function (response) {
                    $('#transaction-history-content').removeClass('ht-on-loading').html(response.data);
                }
            })
        });


        $('#hostels, #type').on('change', function () {

            var hostel = $('#hostels').val();
            var month = $('#month').val();
            var type = $('#type').val();
            var params = jQuery.param(
                {
                    hostel_id: hostel,
                    month: month,
                    type: type

                }
            );
            $('#btn-export-voucher-excel').attr('href', '<?php echo e(url('admin2/money/export-voucher-excel')); ?>' + '?' + params);

            getPostByAttr();
        });


        function getPostByAttr() {
            table.ajax.reload();
        }

        $(function () {
            table = $('#orders-table').DataTable({
                "bDestroy": true,
                processing: true,
                searching: true,
                "aaSorting": [[7, 'desc'], [8, 'desc']],
                serverSide: true,
                ajax: {
                    url: '<?php echo url('admin2/money/voucher.data'); ?>',
                    data: function (d) {
                        d.month = $('#month').val();
                        d.hostel_id = $('#hostels').val();
                        d.type = $('#type').val();
                    }
                },
                columns: [
                    {data: 'DT_Row_Index', name: 'DT_Row_Index', orderable: false, searchable: false},
                    {data: 'name', name: 'name'},
                    {data: 'contract_code', name: 'contract_code'},
                    {data: 'contract_name', name: 'contract_name'},
                    {data: 'reference', name: 'reference'},
                    {data: 'money_info_name', name: 'money_info_name'},
                    {data: 'hostel_id', name: 'hostel_id'},
                    {data: 'room_id', name: 'room_id'},
                    {data: 'amount', name: 'amount'},
                    {data: 'pay', name: 'pay'},
                    {data: 'remain', name: 'remain'},
                    {data: 'date_action', name: 'date_action'},
                    {data: 'action', name: 'action'}
                ],
            });


            table.columns().every(function (index) {
                if (index !== 0 && index !== 11) {
                    var that = this;
                    $('input', this.footer()).on('keyup change', function () {
                        if (that.search() !== this.value) {
                            that
                                .search(this.value)
                                .draw();
                        }
                    });

                    $('select', this.footer()).on('change', function () {
                        if (that.search() !== this.value) {
                            that
                                .search(this.value)
                                .draw();
                        }
                    });
                }
            });

        });

        function getCoor(id, type) {
            $.ajax({
                url: '<?php echo e(url('get-location-coor')); ?>',
                type: 'get',
                data: {
                    'id': id,
                    'type': type
                },
                dataType: 'json',
                success: function (response) {
                    if (map) {
                        map.setCenter(
                            response.lat,
                            response.lng
                        );
                    }
                    cur_lat = response.lat;
                    cur_lng = response.lng;
                }
            });
        }

        $(document).on('change', '#province_id', function (e) {
            var id = $(this).val();
            $.ajax({
                url: '<?php echo e(url('get-sub-location')); ?>',
                type: 'get',
                data: {
                    'id': $(this).val(),
                    'type': 'province',
                },
                dataType: 'html',
                success: function (response) {
                    $('#district_id').html(response);
                    $('#ward_id').html('');
                    getCoor(id, 'province');


                }
            });
        });

        $(document).on('change', '#district_id', function (e) {
            var id = $(this).val();
            $.ajax({
                url: '<?php echo e(url('get-sub-location')); ?>',
                type: 'get',
                data: {
                    'id': $(this).val(),
                    'type': 'district'
                },
                dataType: 'html',
                success: function (response) {
                    $('#ward_id').html(response);
                    getCoor(id, 'district');
                }
            });
        });

        $(document).on('click', '.btn-paid', function (e) {
            var that = this;
            var money_info_id = $(this).attr('data-money-info');
            $.ajax({
                url: '<?php echo e(url('admin2/room/paid-form')); ?>',
                type: 'get',
                data: {
                    money_info_id: money_info_id
                },
                dataType: 'json',
                success: function (response) {
                    $('#form-paid-' + money_info_id).html(response.data);
                    $('.datepicker').datepicker({
                        format: 'dd/mm/yyyy',
                        autoclose: true
                    });

//                $(that).parent().parent().find('.col-md-12').first().next().html(response.data);
                }
            })
        });

        $(document).on('click', '.btn-save-payment', function (e) {
            e.preventDefault();

            var data = $(this).closest('form').serialize();

            $.ajax({
                url: '<?php echo e(url('admin2/room/spend')); ?>',
                type: 'post',
                data: data,
                dataType: 'json',
                beforeSend: function () {
                    $('#payment-items').addClass('ht-on-loading');
                },
                success: function (response) {
                    $('#payment-items').removeClass('ht-on-loading');
                    if (response.status == 1) {
                        swal('Thành công', 'Thanh toán thành công', 'success');
                        $('#detail-money-info').modal('hide');
                        table.ajax.reload();
                    } else {
                        swal('Thông báo', response.message, 'warning');
                    }
                }
            });

        });

        $(document).on('click', '.btn-cancel-paid', function (e) {
            e.preventDefault();

            $(this).parent().parent().parent().parent().remove();

        });

        $(document).on('click', '.btn-edit-payment', function (e) {
            e.preventDefault();
            var transaction_id = $(this).attr('data-payment');
            var that = this;

            $.ajax({
                url: '<?php echo e(url('admin2/room/get-edit-payment')); ?>',
                type: 'get',
                data: {
                    transaction_id: transaction_id
                },
                dataType: 'json',
                success: function (response) {
                    $(that).closest('td').prev().html(response.html);
                    $(that).closest('td').html(response.button);
                }
            });


        });


        $(document).on('click', '.btn-save-edit-payment', function (e) {
            e.preventDefault();
            var transaction_id = $(this).attr('data-transaction');
            var amount = $(this).closest('td').prev().find('input').val();
            var that = this;

            $.ajax({
                url: '<?php echo e(url('admin2/room/edit-payment')); ?>',
                type: 'post',
                data: {
                    transaction_id: transaction_id,
                    amount: amount
                },
                dataType: 'json',
                success: function (response) {
                    if (response.status == 1) {
                        swal('Thành công', '', 'success');
                    } else {
                        swal('Thông báo', response.message, 'warning');
                    }
                    $(that).closest('td').prev().html(response.html);
                    $(that).closest('td').html(response.button);
                }
            });
        });

        $(document).on('click', '.btn-cancel-edit-payment', function (e) {
            e.preventDefault();
            var transaction_id = $(this).attr('data-transaction');
            var that = this;

            $.ajax({
                url: '<?php echo e(url('admin2/room/cancel-edit-payment')); ?>',
                type: 'get',
                data: {
                    transaction_id: transaction_id,
                },
                dataType: 'json',
                success: function (response) {
                    $(that).closest('td').prev().html(response.html);
                    $(that).closest('td').html(response.button);
                }
            });
        });

        //    $(document).on('change', '#ward_id', function (e) {
        //        var id = $(this).val();
        //        getCoor(id, 'ward');
        //    });

        $(document).on('click', '.print-money-info', function (e) {
            e.preventDefault();
            var month = $(this).attr('data-date');
            var id = $(this).attr('data-money-info');

            $.ajax({

                url: '<?php echo e(url('admin2/money/check-number-money-info')); ?>',
                type: 'get',
                data: {
                    id: id
                },
                dataType: 'json',
                success: function (response) {
                    if (response.data > 1) {
                        bootbox.confirm({
                            message: "Bạn có muốn in gộp toàn bộ hoá đơn của tháng " + month + " không ?",
                            buttons: {
                                confirm: {
                                    label: 'Có',
                                    className: 'btn-success'
                                },
                                cancel: {
                                    label: 'Không',
                                    className: 'btn-danger'
                                }
                            },


                            callback: function (result) {
                                if (result == true) {
                                    window.open('<?php echo e(url('admin/money/print-all/')); ?>' + '/' + id);
                                } else {
                                    window.open('<?php echo e(url('admin/money/print/')); ?>' + '/' + id);
                                }
                            }
                        });
                    } else {
                        window.open('<?php echo e(url('admin/money/print/')); ?>' + '/' + id);
                    }
                }

            });


        });

        var g_money_info_id;
        $(document).on('click', '.update-money-info', function (e) {
            var money_info_id = $(this).attr('data-money-info');
            g_money_info_id = money_info_id;
            $.ajax({
                url: '<?php echo e(url('admin2/room/update-money-info')); ?>',
                data: {
                    money_info_id: money_info_id,
                },
                type: 'get',
                beforeSend: function (xhr) {

                    xhrthat = xhr;
                    $.ajax({
                        url: '<?php echo e(url('admin2/room/check-payment-status-money-info')); ?>',
                        data: {
                            money_info_id: money_info_id,
                        },
                        type: 'get',
                        dataType: 'json',
                        success: function (response) {
                            if (response.status == 0) {
                                swal('Thông báo', response.message, 'info');
                                xhrthat.abort();
                            } else {
                                $('#update-money-info-content').addClass('ht-on-loading');
                                $('#update-money-info-modal').modal('show');
                            }
                        }
                    });
                },
                dataType: 'json',
                success: function (response) {
                    if (response.status == 1) {
                        $('#update-money-info-content').removeClass('ht-on-loading').html(response.data);

                        $('.datepicker').datepicker({
                            format: 'dd/mm/yyyy',
                            language: 'vi',
                            autoclose: true,
                        }).on('changeDate', function (e) {
                            update_money_info(money_info_id);
                            //   get_stat();
                        });
                    } else {
                        swal('Thông báo', response.message, 'info');
                    }
                }
            })
        });

        $(document).on('blur', '#money-info-discount, #value-money-info', function () {
            update_money_info(g_money_info_id);
        });

        function update_money_info(money_info_id) {
            var start_date = $('#start_date_money_info').val();
            var end_date = $('#end_date_money_info').val();
            var discount = $('#money-info-discount').val();
            var value_money_info = $('#value-money-info').val();

            $.ajax({
                url: '<?php echo e(url('admin2/room/cal-money-info')); ?>',
                data: {
                    start_date: start_date,
                    end_date: end_date,
                    money_info_id: money_info_id,
                    discount: discount,
                    value_money_info: value_money_info
                },
                type: 'post',
                dataType: 'json',
                success: function (response) {
                    if (response.status == 1) {
                        $('#money-info-amount').val(response.data);
                    } else {
                        swal('Thông báo', response.message, 'info');
                    }
                }
            })
        }

        $(document).on('click', '#btn-save-money-info-update', function (e) {
            e.preventDefault();
            var data = $('#form-update-money-info').serialize();
            $.ajax({
                url: '<?php echo e(url('admin2/room/update-money-info-room')); ?>',
                type: 'post',
                data: data,
                dataType: 'json',
                beforeSend: function () {
                    $('#update-money-info-content').addClass('ht-on-loading');
                },
                success: function (response) {
                    $('#update-money-info-content').removeClass('ht-on-loading');
                    if (response.status == 1) {
                        $('#update-money-info-modal').modal('hide');
                        swal('Thông báo', 'Dữ liệu đã được cập nhật', 'success');
                        table.ajax.reload();
                    } else {
                        swal('Thông báo', response.message, 'info');
                    }
                }
            });
        });
    </script>

<?php $__env->stopPush(); ?>
<?php echo $__env->make('admin2', \Illuminate\Support\Arr::except(get_defined_vars(), array('__data', '__path')))->render(); ?>