import Ajax from '../../components/ajax.js';
import AlbaranComponent from '../../components/albaranComponent.js';

$(() => {

    const dropdown = document.querySelector(".dropdown-language");
    const activeItem = dropdown.querySelector(".dropdown-menu .dropdown-item");
    let locale = 'es';
    if (activeItem) {
        locale = activeItem.getAttribute("data-language");
    }

    new Ajax('/translate/getTranslation', { locale: locale, translationFile: ['Albaran'] }, {},
        function (translations) {
            window.language = JSON.parse(translations);
            new Ajax(
                '/albaranes/getAlbaran',
                {
                    id: $('#id').val()
                },
                {},
                function (data, headers, success, error, type) {
                    if (success) {
                        const albaran = new AlbaranComponent(data.data, '/logistica/listAlbaranes');
                        albaran.mount('#albaranContainer');
                        $('.accordion-button').trigger('click');

                    } else {
                        popErrorAlert(error);
                    }
                },
                function (data, headers, success, error, type) {
                    popErrorAlert(error);
                }
            ).get();
        },
        function (error) {
            console.log("Error getting translations:", error);
        }
    ).post();


});