// 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 () {
//         $('#importTable').css('overflow', 'inherit');
//     });
//
//     table.on('hide.bs.dropdown', function () {
//         $('#importTable').css('overflow', 'auto');
//     });
//
//     $('#importTable').DataTable({
//         initComplete: function (settings, json) {
//             $('input[type="search"]').first().focus();
//         },
//         stateSave: true,
//         autoWidth: true,
//         paging: true,
//         ordering: true,
//         order: [[0, 'desc']],
//         // columnDefs: [
//         //     {"targets": 2, "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>>'
//
//     });
// });


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 () {
        $('#importTable').css('overflow', 'inherit');
    });

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

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

    });

    const openModalBtn = document.getElementById('open-import-modal-btn');
    const modalOverlay = document.getElementById('import-modal');

    // Кнопки типов импорта
    const standardImportBtn = document.getElementById('standard-import-btn');
    const cumasImportBtn = document.getElementById('cumas-import-btn');

    // Формы импорта
    const standardImportForm = document.getElementById('standard-import-form');
    const cumasImportForm = document.getElementById('cumas-import-form');

    if (modalOverlay) {
        const closeModalBtns = modalOverlay.querySelectorAll('.modal-close-btn');

        // Функция открытия модалки
        const openModal = () => {
            modalOverlay.classList.remove('d-none');
            // Скрываем обе формы при открытии модального окна
            // if (standardImportForm) standardImportForm.classList.add('d-none');
            if (cumasImportForm) cumasImportForm.classList.add('d-none');
        };

        // Функция закрытия модалки
        const closeModal = () => {
            if (standardImportForm) standardImportForm.classList.remove('d-none');
            if(cumasImportBtn && standardImportBtn) {
                cumasImportBtn.classList.add('button-outline');
                cumasImportBtn.classList.remove('button-blue');
                standardImportBtn.classList.add('button-blue');
                standardImportBtn.classList.remove('button-outline');
            }


            modalOverlay.classList.add('d-none');

        };

        // Открыть модалку по клику на кнопку
        if (openModalBtn) {
            openModalBtn.addEventListener('click', openModal);
        }

        // Закрыть модалку по клику на кнопку закрытия
        closeModalBtns.forEach(btn => {
            btn.addEventListener('click', closeModal);
        });

        // Закрыть модалку по клику на оверлей (вне контента)
        modalOverlay.addEventListener('click', (event) => {
            // Закрываем только если клик был именно на оверлее, а не на его дочерних элементах
            if (event.target === modalOverlay) {
                closeModal();
            }
        });

        // Закрыть модалку по нажатию Esc
        document.addEventListener('keydown', (event) => {
            if (event.key === 'Escape' && !modalOverlay.classList.contains('hidden')) {
                closeModal();
            }
        });

        // Переключение на обычный импорт
        if (standardImportBtn && standardImportForm && cumasImportForm && cumasImportBtn) {
            standardImportBtn.addEventListener('click', function () {
                standardImportForm.classList.remove('d-none');
                cumasImportForm.classList.add('d-none');
                standardImportBtn.classList.add('button-blue');
                standardImportBtn.classList.remove('button-outline');
                cumasImportBtn.classList.add('button-outline');
                cumasImportBtn.classList.remove('button-blue');
            });
        }

        // Переключение на импорт из CumasCRM
        if (cumasImportBtn && cumasImportForm && standardImportForm && standardImportBtn) {
            cumasImportBtn.addEventListener('click', function () {
                cumasImportForm.classList.remove('d-none');
                standardImportForm.classList.add('d-none');
                cumasImportBtn.classList.add('button-blue');
                cumasImportBtn.classList.remove('button-outline');
                standardImportBtn.classList.add('button-outline');
                standardImportBtn.classList.remove('button-blue');
            });
        }
    }


});
