document.addEventListener('DOMContentLoaded', function() {

    const moreMenuButton = document.getElementById('more-menu-button');
    const moreMenuDropdown = document.getElementById('more-menu-dropdown');
    const languageSelectorButton = document.getElementById('language-selector-button');
    const languageOptions = document.getElementById('language-options');


    function toggleDropdown(button, dropdown) {
        button.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            dropdown.classList.toggle('hidden');
        });
    }

    if (moreMenuButton && moreMenuDropdown) {
        toggleDropdown(moreMenuButton, moreMenuDropdown);
    } else {
        console.error('More menu button or dropdown not found');
    }

    if (languageSelectorButton && languageOptions) {
        toggleDropdown(languageSelectorButton, languageOptions);
    } else {
        console.error('Language selector button or options not found');
    }

    // 点击页面其他地方时关闭下拉菜单
    document.addEventListener('click', function(e) {
        if (moreMenuDropdown && !moreMenuButton.contains(e.target) && !moreMenuDropdown.contains(e.target)) {
            moreMenuDropdown.classList.add('hidden');
        }
        if (languageOptions && !languageSelectorButton.contains(e.target) && !languageOptions.contains(e.target)) {
            languageOptions.classList.add('hidden');
        }
    });

    // 处理语言选择
    const languageForm = document.getElementById('language-form');
    if (languageForm) {
        const languageButtons = languageForm.querySelectorAll('button[name="language"]');
        languageButtons.forEach(button => {
            button.addEventListener('click', function() {
                languageForm.submit();
            });
        });
    } else {
        console.error('Language form not found');
    }
});