<?php $__env->startSection('content'); ?>
    <?php Fun::lang()?>
    <style>
        .no-boder td {
            border: none !important;
        }
    </style>
    <div class="page">
        <div class=container-fluid>
            <div class="top-content">
                <div class="row">
                    <div class="col-sm-12 col-xs-12">
                        <div class="title-add">
                            <div class="pull-left">
                                <h3 class="set_menu_name"></h3>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <br>&nbsp;
            <br>
            <div class="wrapper-table">
                <table id="print_table" class="table table-striped table-hover">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th><?php echo e(__('administrator.invoice_number')); ?></th>
                        <th><?php echo e(__('administrator.request_by')); ?></th>
                        <th><?php echo e(__('administrator.request_to')); ?></th>
                        <th><?php echo e(__('administrator.date')); ?></th>
                        <th><?php echo e(__('administrator.status')); ?></th>
                        <th><?php echo e(__('administrator.approve_date')); ?></th>
                        <th><?php echo e(__('administrator.confirm_received')); ?></th>
                        <th><?php echo e(__('administrator.action')); ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
    <script type="text/javascript">
        let $table = $('#print_table');

        function getTable() {
            $table.DataTable().destroy();
            $table.DataTable({
                "oLanguage": {
                    "sSearch": "<?php echo e(__('administrator.search')); ?>:",
                    "sLengthMenu": "<?php echo e(__('administrator.display')); ?> _MENU_ <?php echo e(__('administrator.record_per_page')); ?>",
                    "sZeroRecords": "<?php echo e(__('administrator.Nothing_foundsorry')); ?>",
                    "sInfo": "<?php echo e(__('administrator.showing')); ?> _START_ <?php echo e(__('administrator.to')); ?> _END_ <?php echo e(__('administrator.of')); ?> _TOTAL_ <?php echo e(__('administrator.record')); ?>",
                    "sInfoEmpty": "<?php echo e(__('administrator.showing')); ?> 0 <?php echo e(__('administrator.to')); ?> 0 <?php echo e(__('administrator.of')); ?> 0 <?php echo e(__('administrator.record')); ?>",
                    "sInfoFiltered": "(<?php echo e(__('administrator.filter_form')); ?> _MAX_ <?php echo e(__('administrator.total_records')); ?>)",
                    "oPaginate": {
                        "sFirst": "<?php echo e(__('administrator.first')); ?>",
                        "sLast": "<?php echo e(__('administrator.last')); ?>",
                        "sNext": "<?php echo e(__('administrator.next')); ?>",
                        "sPrevious": "<?php echo e(__('administrator.previous')); ?>"
                    },
                },
                ordering: false,
                serverSide: true,
                lengthMenu: [20, 30, 50, 100, 200, 500, 1000],
                ajax: {
                    url: "<?php echo e(route('request-stock.index')); ?>",
                    data: {
                        type: '<?php echo e(request('type')); ?>'
                    }
                },
                columns:
                    [
                        {data: 'DT_RowIndex', 'searchable': false},
                        {data: 'invoice_number', 'name': 'nso007_item_request.invoice_number'},
                        {data: 'request_by', 'searchable': false},
                        {data: 'request_to', 'searchable': false},
                        {data: 'date', 'searchable': false},
                        {data: 'status', 'searchable': false},
                        {data: 'approve_date', 'searchable': false},
                        {data: 'confirm_status', 'searchable': false},
                        {data: 'action', 'searchable': false},

                    ],

                "initComplete": function (settings, json) {
                }
            });
        }

        $(document).ready(function () {
            getTable();
            $('.select2').on('change', function () {
                let supplier = $('select[name=supplier]').val();
                let type = $('select[name=type]').val();
                getTable(supplier, type);
            });
            $table.on('click', ' .show_request', function () {
                let id = $(this).data('id');
                let can = $(this).data('can');
                let confirm_status = $(this).data('confirm_status');
                let status = $(this).data('status');
                let user_id = $(this).data('user_id');
                $.ajax({
                    url: $(this).data('href'),
                    method: 'get',
                    dataType: 'json',
                    success: function (data) {
                        $.confirm({
                            title: '',
                            theme: 'modern',
                            useBootstrap: false,
                            boxWidth: '50%',
                            content: data.view,
                            buttons: {
                                close: {
                                    text: '<?php echo e(__('administrator.close')); ?>',
                                    btnClass: 'button_warning',
                                },
                                print: {
                                    text: '<?php echo e(__('administrator.print')); ?>',
                                    btnClass: 'button_info',
                                    keys: ['enter'],
                                    action: function () {
                                        $('.page-purchases').printThis({
                                            loadCSS: "<?php echo e(asset('assets/css/pos-invoice.css')); ?>",
                                            afterPrint: function () {
                                                window.location.reload();
                                            }
                                        });
                                        return false;
                                    },
                                },
                                <?php if(Gate::check('Request Stock Approve')): ?>
                                approve: {
                                    text: '<?php echo e(__('administrator.approve')); ?>',
                                    btnClass: 'button_success approve',
                                    action: function () {
                                        $.confirm({
                                            title: '<?php echo e(__('administrator.please_complete_information_below')); ?>',
                                            boxWidth: '35%',
                                            useBootstrap: false,
                                            content: `
                                                   <table width="100%" class="delivery-form table no-boder">
                                                        <tr>
                                                            <td width="25%"><?php echo e(__('administrator.warehouse')); ?><span class="text-danger">*</span></td>
                                                            <td>  <?php echo Form::select('warehouse', $warehouse,null, ['placeholder'=>__('administrator.warehouse').'*','class' => 'form-control']); ?></td>
                                                        </tr>
                                                        <tr>
                                                            <td width="25%"><?php echo e(__('administrator.note')); ?></td>
                                                            <td><?php echo Form::textarea('note', null, ['placeholder'=>__('administrator.note'),'class' => 'form-control no-resize','rows'=>'5']); ?></td>
                                                        </tr>
                                                    </table>
                                                    `,
                                            buttons: {
                                                cancel: {
                                                    text: '<?php echo e(__('administrator.cancel')); ?>',
                                                    btnClass: 'button_warning',
                                                    action: function () {

                                                    }
                                                },
                                                approve: {
                                                    text: '<?php echo e(__('administrator.approve')); ?>',
                                                    btnClass: 'button_primary',
                                                    action: function () {
                                                        let self = this;
                                                        let warehouse = self.$content.find('select[name=warehouse]').val();
                                                        let note = self.$content.find('textarea[name=note]').val();
                                                        $('button').prop('disabled', true)
                                                        $.ajax({
                                                            url: '<?php echo e(route('request-stock-update-status',1)); ?>',
                                                            data: {
                                                                id,
                                                                warehouse,
                                                                note
                                                            },
                                                            success: function (data) {
                                                                if (data.status === 'success') {
                                                                    window.location.reload();
                                                                }
                                                            },
                                                            error: function (xhr) {
                                                                alertErrorFormController(xhr);
                                                            }
                                                        });
                                                        return false;
                                                    }
                                                },
                                            }
                                        });
                                        return false;
                                    }
                                },
                                reject: {
                                    text: '<?php echo e(__('administrator.reject')); ?>',
                                    btnClass: 'button_danger approve',
                                    action: function () {

                                        $.confirm({
                                            title: '<?php echo e(__('administrator.are_you_sure')); ?>',
                                            content: '',
                                            buttons: {
                                                submit: {
                                                    text: '<?php echo e(__('administrator.reject')); ?>',
                                                    btnClass: 'button_danger',
                                                    action: function () {
                                                        let self = this;
                                                        $('button').prop('disabled', true)
                                                        $.ajax({
                                                            url: '<?php echo url("request-stock-update-status",2); ?>',
                                                            data: {
                                                                id
                                                            },
                                                            async: false,
                                                            success: function (data) {
                                                                if (data.status === 'fail') {
                                                                    if (data.count > 0) {
                                                                        $.alert({
                                                                            theme: 'modern',
                                                                            title: '<?php echo e(__('administrator.warning')); ?>',
                                                                            content: data.message,
                                                                        });
                                                                    }
                                                                } else {
                                                                    window.location.reload();
                                                                }
                                                            },
                                                            error: function (xhr) {
                                                                setTimeout(function () {
                                                                    $('button').prop('disabled', false)
                                                                }, 4000)
                                                                alertErrorFormController(xhr);
                                                            }

                                                        });
                                                        return false;
                                                    }
                                                },
                                                close: {
                                                    text: '<?php echo e(__('administrator.close')); ?>',
                                                    btnClass: 'button_warning',
                                                    action: function () {

                                                    }
                                                },
                                            }
                                        });
                                        return false;
                                    }
                                },
                                <?php endif; ?>
                                confirm_received: {
                                    text: '<?php echo e(__('administrator.confirm_received')); ?>',
                                    btnClass: 'button_success confirm_status',
                                    keys: ['enter'],
                                    action: function () {
                                        $.confirm({
                                            title: '<?php echo e(__('administrator.confirm_received')); ?>',
                                            theme: 'modern',
                                            content: `<?php echo e(__('administrator.are_you_sure')); ?><br>
                                         <table width="100%" class="delivery-form table no-boder">
                                                        <tr>
                                                            <td width="25%"><?php echo e(__('administrator.warehouse')); ?><span class="text-danger">*</span></td>
                                                            <td>  <?php echo Form::select('warehouse', $warehouse,null, ['placeholder'=>__('administrator.warehouse').'*','class' => 'form-control']); ?></td>
                                                        </tr>

                                                    </table>
                                        `,
                                            buttons: {
                                                cancel: {
                                                    btnClass: 'button_warning',
                                                    text: '<?php echo e(__('administrator.cancel')); ?>',
                                                },
                                                confirm_received: {
                                                    btnClass: 'button_primary btn_alert',
                                                    text: '<?php echo e(__('administrator.confirm_received')); ?>',
                                                    action: function () {
                                                        let warehouse_id = this.$content.find('select[name=warehouse]').val();
                                                            $('button').prop('disabled', true)
                                                        $.ajax({
                                                            url: '<?php echo route("request-stock-update-confirm",1); ?>',
                                                            method: 'POST',
                                                            data: {
                                                                id,
                                                                warehouse_id
                                                            },
                                                            success: function (data) {
                                                                if (data) {
                                                                    window.location.reload();
                                                                }
                                                            },
                                                            error(xhr) {
                                                                setTimeout(function () {
                                                                    $('button').prop('disabled', false)
                                                                }, 4000)
                                                                alertErrorFormController(xhr);
                                                            }
                                                        });
                                                        return false
                                                    }
                                                }
                                            },
                                        });
                                        return false
                                    }
                                },
                                seller_reject: {
                                    text: '<?php echo e(__('administrator.reject')); ?>',
                                    btnClass: 'button_danger confirm_status',
                                    keys: ['enter'],
                                    action: function () {
                                        $.confirm({
                                            title: '<?php echo e(__('administrator.reject')); ?>',
                                            theme: 'modern',
                                            content: '<?php echo e(__('administrator.are_you_sure')); ?>',
                                            buttons: {
                                                cancel: {
                                                    btnClass: 'button_warning',
                                                    text: '<?php echo e(__('administrator.cancel')); ?>',
                                                },
                                                reject: {
                                                    btnClass: 'button_danger btn_alert',
                                                    text: '<?php echo e(__('administrator.reject')); ?>',
                                                    action: function () {
                                                        $('button').prop('disabled', true)
                                                        $.ajax({
                                                            url: '<?php echo route("request-stock-update-confirm",2); ?>',
                                                            method: 'POST',
                                                            data: {
                                                                id: id
                                                            },
                                                            success: function (data) {
                                                                window.location.reload();
                                                            }
                                                        });
                                                    }
                                                }
                                            },
                                        });
                                        return false
                                    }
                                }
                            },
                        });
                        setTimeout(function () {
                            let user = parseInt('<?php echo e(Auth::user()->id); ?>');
                            if (can === 0) {
                                $(document).find('.approve').remove();
                            }
                            if (status !== 1 || confirm_status !== 0) {
                                $('.confirm_status').remove();
                            }
                            if (user_id !== user) {
                                $('.confirm_status').remove();
                            }
                        }, 10);

                    }
                });
            });
            $table.on('click', ' .btn_return', function () {
                getHtmlForm($(this).data('href'), '<?php echo e(__('administrator.return_item')); ?>', '700px', 1);
            });
        });
    </script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make(branchMenu(), \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/nsowos/public_html/shr-brand/resources/views/stock-inventory/item-request/index.blade.php ENDPATH**/ ?>