(function () {
    const STORAGE_KEY = 'intern_sidebar_open';

    document.addEventListener('DOMContentLoaded', function () {
        const toggle = document.getElementById('sidebar-toggle');
        const sidebar = document.getElementById('sidebar-nav');
        const overlay = document.getElementById('sidebar-overlay');

        if (!toggle || !sidebar || !overlay) return;

        function openSidebar() {
            sidebar.classList.add('open');
            overlay.classList.add('open');
            toggle.classList.add('open');
            localStorage.setItem(STORAGE_KEY, 'true');
        }

        function closeSidebar() {
            sidebar.classList.remove('open');
            overlay.classList.remove('open');
            toggle.classList.remove('open');
            localStorage.setItem(STORAGE_KEY, 'false');
        }

        function toggleSidebar() {
            if (sidebar.classList.contains('open')) {
                closeSidebar();
            } else {
                openSidebar();
            }
        }

        toggle.addEventListener('click', toggleSidebar);
        overlay.addEventListener('click', closeSidebar);

        // Keyboard shortcut: press 'M' to toggle
        document.addEventListener('keydown', function (e) {
            if (e.key.toLowerCase() === 'm' && !e.ctrlKey && !e.altKey && !e.metaKey) {
                const tag = document.activeElement?.tagName;
                if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return;
                toggleSidebar();
            }
        });

        // Restore state
        if (localStorage.getItem(STORAGE_KEY) === 'true') {
            openSidebar();
        }
    });
})();

function changeFreigabe(freigabe) {

    const params = new URLSearchParams();
    params.append('freigabe', freigabe);
    params.append('type', siteType);
    params.append('csrf_token', window.CSRF_TOKEN);

    fetch('/intern/scripts/ajax-update_selected_freigabe.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: params
    })
    .then(res => res.json())
    .then(data => {
        if (data.success) {
            window.location.reload(); // reload page to reflect changes
        } else {
            alert('Error: ' + data.data);
        }
    })
    .catch(err => {
        console.error(err);
        alert('AJAX request failed');
    });
}

$(document).on('click', '.sidebar-nav .selectTriggerSidebar', function (e) {
    e.stopPropagation();
    $(this).closest('.customSelect').toggleClass('open');
});

$(document).on('click', '.sidebar-nav .selectOptionsSidebar li', function (e) {
    e.stopPropagation();
    const $item       = $(this);
    const $container  = $item.closest('.customSelect');
    const val         = $item.data('value');
    const text        = $item.text();
    const $input      = $container.find('.selectValue');
    const $label      = $container.find('.selectLabel');
    const $allOptions = $container.find('.selectOptions li');

    $allOptions.removeClass('selected');

    $item.addClass('selected');

    $input.val(val);
    $label.text(text);

    $container.removeClass('open');

    changeFreigabe(val);
});

$(document).on('click', () => $('.sidebar-nav .customSelect').removeClass('open'));