{% set sListId = oUIBlock.GetId() %}
{% set sListIDForVarSuffix = ('' ~ sListId)|sanitize(constant('utils::ENUM_SANITIZATION_FILTER_VARIABLE_NAME')) %}

var oTable{{ sListIDForVarSuffix }} = $('#{{ oUIBlock.GetId() }}').DataTable({
    language: {
        emptyTable: "{{ 'UI:Message:EmptyList:UseAdd'|dict_s }}"
    },
    scrollX: true,
    scrollCollapse: true,
    paging: false,
    filter: false,
    search: false,
    dom: "t",
    "order": [],
    {% if oUIBlock.GetOption("select_mode") is not empty %}
    {% if oUIBlock.GetOption("select_mode") != "custom" %}
    select: {
        style: "{% if oUIBlock.GetOption("select_mode") == "multiple" %}multi{% else %}single{% endif %}",
        info: false
    },
    {% endif %}
    columnDefs: [
        {orderable: false, targets: 0},
        {% if oUIBlock.HasRowActions() %}
            getRowActionsColumnDefinition('{{ oUIBlock.GetId() }}', {{ oUIBlock.GetColumnsCount() - 1 }}),
        {% endif %}
    ],
    {% endif %}
    drawCallback: function (settings) {
        if(settings.json)
        {
            $(this).closest('.ibo-panel').find('.ibo-datatable--result-count').html(settings.json.recordsTotal);
        }
        // Hiding pagination if only one page
        if ($(this).closest('.dataTables_wrapper').find('.dataTables_paginate:last .paginate_button:not(.previous):not(.next)').length < 2)
        {
            $(this).closest('.dataTables_wrapper').find('.dataTables_paginate, .dataTables_info').hide();
        }
        else
        {
            $(this).closest('.dataTables_wrapper').find('.dataTables_paginate, .dataTables_info').show();
        }

        // Disable hyperlinks if necessary
        {% if oUIBlock.GetOption("disable_hyperlinks") is same as true or (oUIBlock.GetOption("select_mode") is not empty and oUIBlock.GetOption("disable_hyperlinks") is not same as false) %}
        $("#{{ oUIBlock.GetId() }} a").on('click', function (e) {
            e.preventDefault();
        });
        {% endif %}
    },
    createdRow: function (row, data, dataIndex) {
        if (data['@class'] !== undefined)
        {
            $(row).addClass(data['@class']);
        }
    },

    initComplete: function () {
        if (this.api().page.info().pages < 2)
        {
            this.closest('.dataTables_wrapper').find('.dataTables_length').hide();
        }

        // Set header and body datatables status as loaded
        this.attr('data-status', 'loaded');
        this.closest('.dataTables_scroll').find('.dataTables_scrollHead .ibo-datatable').attr('data-status', 'loaded');
    },
});

{% if (oUIBlock.GetOption('bFullscreen')) %}
var heightS{{ sListIDForVarSuffix }} = 0;
var heightD{{ sListIDForVarSuffix }} = 0;
if ($("#{{ oUIBlock.GetId() }}").height() > 0)
{
    heightD{{ sListIDForVarSuffix }} = $("#ibo-main-content").height()-$("#ibo-main-content")[0].scrollHeight+$("#{{ oUIBlock.GetId() }}").height();
    heightS{{ sListIDForVarSuffix }} = $("#ibo-main-content").height();
    if (heightD{{ sListIDForVarSuffix }} > 200)
    {
        $("#{{ oUIBlock.GetId() }}_wrapper").find(".dataTables_scrollBody").height(heightD{{ sListIDForVarSuffix }});
    }
}
{% endif %}

if (window.ResizeObserver)
{
    let oFromTable{{ sListIDForVarSuffix }}ResizeTimeout = null;
    const oFromTable{{ sListIDForVarSuffix }}Resize = new ResizeObserver(function () {
        clearTimeout(oFromTable{{ sListIDForVarSuffix }}ResizeTimeout);
        oFromTable{{ sListIDForVarSuffix }}ResizeTimeout = setTimeout(function () {
            $('#{{ oUIBlock.GetId() }}').DataTable().columns.adjust();
        }, 120);
    });
    oFromTable{{ sListIDForVarSuffix }}Resize.observe($('#{{ oUIBlock.GetId() }}')[0]);

    {% if (oUIBlock.GetOption('bFullscreen')) %}
    let heightScreen{{ sListIDForVarSuffix }} = heightS{{ sListIDForVarSuffix }};
    let heightDatatable{{ sListIDForVarSuffix }} = heightD{{ sListIDForVarSuffix }};
    let oFromScreen{{ sListIDForVarSuffix }}ResizeTimeout = null;
    const oFromScreen{{ sListIDForVarSuffix }}Resize = new ResizeObserver(function () {
        clearTimeout(oFromScreen{{ sListIDForVarSuffix }}ResizeTimeout);
        oFromScreen{{ sListIDForVarSuffix }}ResizeTimeout = setTimeout(function () {
            if ($("#{{ oUIBlock.GetId() }}_wrapper").height() > 0)
            {
                if (heightDatatable{{ sListIDForVarSuffix }}> 0)
                {
                    let calcul = $("#ibo-main-content").height()+heightDatatable{{ sListIDForVarSuffix }}-heightScreen{{ sListIDForVarSuffix }};
                    if (calcul > 200)
                    {
                        $("#{{ oUIBlock.GetId() }}_wrapper").find(".dataTables_scrollBody").height(calcul);
                    }
                }
                else
                {
                    heightDatatable{{ sListIDForVarSuffix }} = $("#ibo-main-content").height()-$("#ibo-main-content")[0].scrollHeight+$("#{{ oUIBlock.GetId() }}_wrapper").height();
                    heightScreen{{ sListIDForVarSuffix }} = $("#ibo-main-content").height();
                    if (heightDatatable{{ sListIDForVarSuffix }} > 200)
                    {
                        $("#{{ oUIBlock.GetId() }}_wrapper").find(".dataTables_scrollBody").height(heightDatatable{{ sListIDForVarSuffix }});
                    }
                }

            }
        }, 120);
    });
    oFromScreen{{ sListIDForVarSuffix }}Resize.observe($('#ibo-main-content')[0]);

    {% endif %}

}

{% include 'base/components/datatable/row-actions/handler.js.twig' %}