<div id="custom-translation-table">
    [{$translationList}]
</div>

<script>
    (function () {
        const $table = $('custom-translation-table');
        const $newTranslationButton = $('navbar_item_[{idoit\Module\Pro\SettingPage\CustomTranslation\CustomTranslation::NAVMODE_NEW_TRANSLATION}]');
        const $exportButton = $('navbar_item_[{idoit\Module\Pro\SettingPage\CustomTranslation\CustomTranslation::NAVMODE_EXPORT}]');
        const $importButton = $('navbar_item_[{idoit\Module\Pro\SettingPage\CustomTranslation\CustomTranslation::NAVMODE_IMPORT}]');

        if ($newTranslationButton) {
            $newTranslationButton.writeAttribute('onclick', null);

            $newTranslationButton.on('click', function () {
                get_popup('custom_translation_new', '', 590, 250, { });
            });
        }

        if ($exportButton) {
            $exportButton.writeAttribute('onclick', null);

            $exportButton.on('click', function () {
                if ($table.select('[name="id[]"]:checked').length === 0) {
                    idoit.Notify.info('[{isys type="lang" ident="LC__LANGUAGE_EXPORT__SELECT_ONE"}]');
                    return;
                }

                if ($table.select('[name="id[]"]:checked').length !== 1) {
                    idoit.Notify.info('[{isys type="lang" ident="LC__LANGUAGE_EXPORT__SELECT_ONLY_ONE"}]');
                    return;
                }

                const targetUrl = document.location.href.parseQuery();

                targetUrl['[{$smarty.const.C__GET__NAVMODE}]'] = '[{idoit\Module\Pro\SettingPage\CustomTranslation\CustomTranslation::NAVMODE_EXPORT}]';
                targetUrl.id = $table.down('[name="id[]"]:checked').getValue();

                document.location.href = '?' + Object.toQueryString(targetUrl);
            });
        }

        if ($importButton) {
            $importButton.writeAttribute('onclick', null);

            $importButton.on('click', function () {
                get_popup('custom_translation_import', '', 700, 300, { redirectUrl: document.location.href });
            });
        }
    })();
</script>
