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

$(function () {

    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 uploadTable = $('#uploadTable').DataTable({
        initComplete: function (settings, json) {
            $('input[type="search"]').first().focus();
        },
        stateSave: true,
        autoWidth: true,
        paging: true,
        ordering: true,
        order: [[0, "desc"]],
        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>>',
        columnDefs: [
            {
                targets: 0,
                type: 'num',
            }
        ]
    });

    const uploadGuestTable = $('#uploadGuestTable').DataTable({
        initComplete: function (settings, json) {
            $('input[type="search"]').first().focus();
        },
        stateSave: true,
        autoWidth: true,
        paging: true,
        ordering: true,
        order: [[0, "desc"]],
        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>>',
        columnDefs: [
            {
                targets: 0,
                type: 'num',
            }
        ]
    });

    const downloadTable = $('#downloadTable').DataTable({
        initComplete: function (settings, json) {
            $('input[type="search"]').first().focus();
        },
        stateSave: true,
        autoWidth: true,
        paging: true,
        ordering: true,
        order: [[0, "desc"]],
        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>>',
        columnDefs: [
            {
                targets: 0,
                type: 'num',
            }
        ]
    });


    // Изменение overflow для таблицы
    const tableResponsive = $('.table-responsive');
    tableResponsive.on('show.bs.dropdown', function () {
        $('#uploadTable').css("overflow", "inherit");
    });

    tableResponsive.on('hide.bs.dropdown', function () {
        $('#uploadTable').css("overflow", "auto");
    });

    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';
    }

});
