@if (!defined('LOAD_DATATABLES'))
    @push('css')
        <link rel="stylesheet" href="{{ asset('assets/plugins/datatables.min.css') }}">
    @endpush

    @push('scripts')
        @include('boilerplate::load.moment')
        <script src="{{ asset('assets/plugins/datatables.min.js') }}"></script>
        <script src="{{ asset('vendor/datatables/buttons.server-side.js') }}"></script>

        <script>
            $.extend(true, $.fn.dataTable.defaults, {
                language: {
                    url: "/assets/plugins/datatables_lang/{{ \App::getLocale() }}.json"
                },
            });

            function removeAccents(data) {
                if (data.normalize) {
                    return data + ' ' + data.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
                }
                return data;
            }

            function reloadDatatable(name) {
                getDatatable(name).ajax.reload(null, false);
                // getDatatable(name).columns.adjust().draw();
            }

            function getDatatable(name) {
                return (typeof(window.LaravelDataTables) !== 'undefined') ? window.LaravelDataTables[name + "-table"] : false;
            }

            function getDatatableOrderArray(name) {
                var order = [];
                order['name'] = getDatatableOrderName(name);
                order['sort'] = getDatatableOrderSort(name);
                return order;
            }

            function getDatatableOrderJson(name) {
                return {
                    'name': getDatatableOrderName(name),
                    'sort': getDatatableOrderSort(name)
                };
            }

            function getDatatableOrderName(name) {
                var id = getDatatableOrderIndex(name);
                return id ? getDatatableColumnNameById(name, id) : false;
            }

            function getDatatableOrderIndex(name) {
                var table = getDatatable(name);
                var order = table.order();
                var col = order ? order[0][0] : false;
                return col;
            }

            function getDatatableOrderSort(name) {
                var table = getDatatable(name);
                var order = table.order();
                var sort = order[0][1];
                return sort;
            }

            function getDatatableState(name) {
                var table = getDatatable(name);
                return table ? table.state.loaded() : false;
            }

            function getDatatableColumnNameById(name, id) {
                var column = getDatatableColumnById(name, id);
                return column ? column.name : false;
            }

            function getDatatableColumnById(name, id) {
                var columns = getDatatableColumns(name);
                return columns ? columns[id] : false;
            }

            function getDatatableColumns(name) {
                var table = getDatatable(name);
                return table ? table.settings().init().columns : false;
            }

            function setAlertWhenFiltered(model) {
                var selector = '#' + model + '-filters';
                if (checkFilterHasValues(selector)) {
                    $(selector + '-badge').show();
                } else {
                    $(selector + '-badge').hide();
                }
            }

            function checkFilterHasValues(selector) {
                var tab = $(selector).serializeArray();
                var hasValue = false;
                for (item of tab) {
                    if (item.value) {
                        var selector2 = selector + ' input[name="' + item.name + '"]';
                        var isHidden = ($(selector2).attr('type') == 'hidden');
                        if (!isHidden) hasValue = true;
                    }
                }
                return hasValue;
            }

            (function() {
                var searchType = jQuery.fn.DataTable.ext.type.search;

                searchType.string = function(data) {
                    return !data ? '' : typeof data === 'string' ? removeAccents(data) : data;
                };

                searchType.html = function(data) {
                    return !data ? '' : typeof data === 'string' ? removeAccents(data.replace(/<.*?>/g, '')) : data;
                };
            });
        </script>
    @endpush

    @php(define('LOAD_DATATABLES', true))
@endif

@push('scripts')
    @if ($dataTable)
        {{ $dataTable->scripts() }}
    @endif
@endpush
