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

$(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 languageOptions = {
        'de': {
            searchPlaceholder: 'Suchen',
            lengthMenu: 'Einträge anzeigen _MENU_'
        },
        'en': {
            searchPlaceholder: 'Search',
            lengthMenu: 'Show _MENU_ entries'
        },
        'es': {
            searchPlaceholder: 'Buscar',
            lengthMenu: 'Mostrar _MENU_ entradas'
        },
        'ru': {
            searchPlaceholder: 'Поиск',
            lengthMenu: 'Показать _MENU_ записей'
        }
    };

    const lang = getLang();
    const langOptions = languageOptions[lang] || languageOptions['en']; // используем английский по умолчанию


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

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

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

    $('#contactTable').DataTable({
        initComplete: function (settings, json) {
            $('input[type="search"]').first().focus();
        },
        stateSave: true,
        autoWidth: true,
        paging: true,
        ordering: true,
        order: [[3, 'asc']],
        columnDefs: [
            { targets: 5, orderable: false },
            { targets: 6, orderable: false },
        ],
        language: {
            search: '',
            searchPlaceholder: langOptions.searchPlaceholder,
            lengthMenu: langOptions.lengthMenu,
        },
        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>>'

    });
});
