<script>
    $('#{{ $model }}-table').on('draw.dt', function() {

        var table = getDatatable("{{ $model }}");

        $('#{{ $model }}-table .btn-edit').off('click').click(function(e) {
            e.preventDefault();
            var id = $(this).data('id');
            @if (isset($edit_callback))
                {{ $edit_callback }}
            @else
                url = '{{ $route }}' + '/edit/' + id;
                openURL(url);
            @endif
        });

        $('#{{ $model }}-table .btn-show').off('click').click(function(e) {
            e.preventDefault();
            var id = $(this).data('id');
            @if (isset($show_callback))
                {{ $show_callback }}
            @else
                url = '{{ $route }}' + '/show/' + id;
                openURL(url);
            @endif
        });

        $('#{{ $model }}-table .btn-del').off('click').click(function(e) {
            e.preventDefault();
            var id = $(this).data('id');

            bootbox.confirm("{{ __('fg.confirmdelete') }}", function(result) {
                if (result === false) return;

                $.ajax({
                    url: '{{ $route }}' + '/destroy/' + id,
                    method: 'delete',
                    headers: {
                        'X-CSRF-TOKEN': '{{ csrf_token() }}'
                    },
                    success: function() {
                        @if (isset($delete_callback))
                            {{ $delete_callback }}
                        @else
                            table.draw();
                        @endif
                        growl("{{ __('fg.deletesuccess') }}", 'success');
                    }
                });
            });
        });

        {{ $callback ?? '' }}

        var state = table.state.loaded();
    });
</script>
