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

    let deleteUserId: string | number; // Объявляем переменную для хранения id

    // Обработчик события для кнопки DELETE
    $(document).on('click', '.button-delete-ts', function () {
        deleteUserId = $(this).data('id');
    });

    // Обработчик события для кнопки подтверждения удаления
    $('#confirmDeleteBtn').on('click', function () {
        if (deleteUserId) {
            const deleteUrl = `/adm/${getLang()}/license/remove/${deleteUserId}`;
            window.location.href = deleteUrl; // Перенаправляем на URL удаления
        } else {
            console.error('No user ID set for deletion');
        }
    });
});
