import $ from 'jquery';
import 'datatables.net-bs5';
import('bootstrap');

$(function () {
    function getLang() {
        if (window.location.href.indexOf("/de/") > -1) {
            return 'de';
        }
        if (window.location.href.indexOf("/en/") > -1) {
            return 'en';
        }
        if (window.location.href.indexOf("/es/") > -1) {
            return 'es';
        }
        if (window.location.href.indexOf("/ru/") > -1) {
            return 'ru';
        }
        return 'de';
    }

    const userRole: string = $('#role').val() as string;
    $('#user_roles').val(userRole);

    const table = $('.table-responsive');

    table.on('show.bs.dropdown', function () {
        $('#userTable').css('overflow', 'inherit');
    });

    table.on('hide.bs.dropdown', function () {
        $('#userTable').css('overflow', 'auto');
    });

    let deleteLink : string;
    $(document).on('click', '.button-delete-ts', function () {
        deleteLink = $(this).data('delete-link');
    });

    $('#confirmDeleteBtn').on('click', function () {
        if (deleteLink) {
            console.log(deleteLink)
            window.location.href = deleteLink;
        } else {
            console.error('No user ID set for deletion');
        }
    });

    $('#userTable').DataTable({
        initComplete: function (settings, json) {
            $('input[type="search"]').first().focus();
        },
        stateSave: true,
        autoWidth: true,
        paging: true,
        ordering: true,
        order: [[0, 'asc']],
        columnDefs: [
            { targets: 4, orderable: false },
            { targets: 5, orderable: false },
        ],
        language: {
            search: '',
            searchPlaceholder: 'Search',
        },
        lengthMenu: [[5, 10, 25, 50, -1], [5, 10, 25, 50, 100]],
        pageLength: 5,

        dom: '<"table-header-container"<"show-entire-custom"l><"search-section-custom"f>>rt<"table-footer-container"<"show-entire-custom"i><"pages-section-custom"p>>'

    });
});
