@extends(branchMenu())
@section('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>{{__('administrator.invoice_number')}}</th>
                        <th>{{__('administrator.request_by')}}</th>
                        <th>{{__('administrator.request_to')}}</th>
                        <th>{{__('administrator.date')}}</th>
                        <th>{{__('administrator.status')}}</th>
                        <th>{{__('administrator.approve_date')}}</th>
                        <th>{{__('administrator.confirm_received')}}</th>
                        <th>{{__('administrator.action')}}</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

@endsection
@section('script')
    <script type="text/javascript">
        let $table = $('#print_table');

        function getTable() {
            $table.DataTable().destroy();
            $table.DataTable({
                "oLanguage": {
                    "sSearch": "{{__('administrator.search')}}:",
                    "sLengthMenu": "{{__('administrator.display')}} _MENU_ {{__('administrator.record_per_page')}}",
                    "sZeroRecords": "{{__('administrator.Nothing_foundsorry')}}",
                    "sInfo": "{{__('administrator.showing')}} _START_ {{__('administrator.to')}} _END_ {{__('administrator.of')}} _TOTAL_ {{__('administrator.record')}}",
                    "sInfoEmpty": "{{__('administrator.showing')}} 0 {{__('administrator.to')}} 0 {{__('administrator.of')}} 0 {{__('administrator.record')}}",
                    "sInfoFiltered": "({{__('administrator.filter_form')}} _MAX_ {{__('administrator.total_records')}})",
                    "oPaginate": {
                        "sFirst": "{{__('administrator.first')}}",
                        "sLast": "{{__('administrator.last')}}",
                        "sNext": "{{__('administrator.next')}}",
                        "sPrevious": "{{__('administrator.previous')}}"
                    },
                },
                ordering: false,
                serverSide: true,
                lengthMenu: [20, 30, 50, 100, 200, 500, 1000],
                ajax: {
                    url: "{{ route('request-stock.index') }}",
                    data: {
                        type: '{{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: '{{__('administrator.close')}}',
                                    btnClass: 'button_warning',
                                },
                                print: {
                                    text: '{{__('administrator.print')}}',
                                    btnClass: 'button_info',
                                    keys: ['enter'],
                                    action: function () {
                                        $('.page-purchases').printThis({
                                            loadCSS: "{{asset('assets/css/pos-invoice.css')}}",
                                            afterPrint: function () {
                                                window.location.reload();
                                            }
                                        });
                                        return false;
                                    },
                                },
                                @if (Gate::check('Request Stock Approve'))
                                approve: {
                                    text: '{{__('administrator.approve')}}',
                                    btnClass: 'button_success approve',
                                    action: function () {
                                        $.confirm({
                                            title: '{{__('administrator.please_complete_information_below')}}',
                                            boxWidth: '35%',
                                            useBootstrap: false,
                                            content: `
                                                   <table width="100%" class="delivery-form table no-boder">
                                                        <tr>
                                                            <td width="25%">{{__('administrator.warehouse')}}<span class="text-danger">*</span></td>
                                                            <td>  {!! Form::select('warehouse', $warehouse,null, ['placeholder'=>__('administrator.warehouse').'*','class' => 'form-control']) !!}</td>
                                                        </tr>
                                                        <tr>
                                                            <td width="25%">{{__('administrator.note')}}</td>
                                                            <td>{!! Form::textarea('note', null, ['placeholder'=>__('administrator.note'),'class' => 'form-control no-resize','rows'=>'5']) !!}</td>
                                                        </tr>
                                                    </table>
                                                    `,
                                            buttons: {
                                                cancel: {
                                                    text: '{{__('administrator.cancel')}}',
                                                    btnClass: 'button_warning',
                                                    action: function () {

                                                    }
                                                },
                                                approve: {
                                                    text: '{{__('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: '{{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: '{{__('administrator.reject')}}',
                                    btnClass: 'button_danger approve',
                                    action: function () {

                                        $.confirm({
                                            title: '{{__('administrator.are_you_sure')}}',
                                            content: '',
                                            buttons: {
                                                submit: {
                                                    text: '{{__('administrator.reject')}}',
                                                    btnClass: 'button_danger',
                                                    action: function () {
                                                        let self = this;
                                                        $('button').prop('disabled', true)
                                                        $.ajax({
                                                            url: '{!! 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: '{{__('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: '{{__('administrator.close')}}',
                                                    btnClass: 'button_warning',
                                                    action: function () {

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

                                                    </table>
                                        `,
                                            buttons: {
                                                cancel: {
                                                    btnClass: 'button_warning',
                                                    text: '{{__('administrator.cancel')}}',
                                                },
                                                confirm_received: {
                                                    btnClass: 'button_primary btn_alert',
                                                    text: '{{__('administrator.confirm_received')}}',
                                                    action: function () {
                                                        let warehouse_id = this.$content.find('select[name=warehouse]').val();
                                                            $('button').prop('disabled', true)
                                                        $.ajax({
                                                            url: '{!! 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: '{{__('administrator.reject')}}',
                                    btnClass: 'button_danger confirm_status',
                                    keys: ['enter'],
                                    action: function () {
                                        $.confirm({
                                            title: '{{__('administrator.reject')}}',
                                            theme: 'modern',
                                            content: '{{__('administrator.are_you_sure')}}',
                                            buttons: {
                                                cancel: {
                                                    btnClass: 'button_warning',
                                                    text: '{{__('administrator.cancel')}}',
                                                },
                                                reject: {
                                                    btnClass: 'button_danger btn_alert',
                                                    text: '{{__('administrator.reject')}}',
                                                    action: function () {
                                                        $('button').prop('disabled', true)
                                                        $.ajax({
                                                            url: '{!! route("request-stock-update-confirm",2) !!}',
                                                            method: 'POST',
                                                            data: {
                                                                id: id
                                                            },
                                                            success: function (data) {
                                                                window.location.reload();
                                                            }
                                                        });
                                                    }
                                                }
                                            },
                                        });
                                        return false
                                    }
                                }
                            },
                        });
                        setTimeout(function () {
                            let user = parseInt('{{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'), '{{__('administrator.return_item')}}', '700px', 1);
            });
        });
    </script>
@stop
