/* Sidebar and Header Interaction Logic */

const Sidebar = {
    init: function() {
        const _this = this;
        
        // Mobile Toggle Click
        $('#mobileToggle, #sidebarOverlay').on('click', function() {
            _this.toggleMobile();
        });
        
        // Close sidebar on link click (mobile only)
        $('.nav-link').on('click', function() {
            if (window.innerWidth <= 1024) {
                _this.toggleMobile();
            }
        });

        // Filter Menu based on permissions
        this.filterMenu();
    },

    filterMenu: function() {
        // Initial filter of the sidebar
        Utils.applyPermissions();
        
        // Hide empty groups (Optional Polish)
        $('.nav-group-title').each(function() {
            const nextItems = $(this).nextUntil('.nav-group-title', '.nav-item:visible');
            if (nextItems.length === 0) {
                $(this).hide();
            } else {
                $(this).show();
            }
        });
    },
    
    toggleMobile: function() {
        $('#sidebar').toggleClass('show');
        $('#sidebarOverlay').toggleClass('show');
    },
    
    // Set active link in sidebar
    setActive: function(page) {
        $('.nav-link').removeClass('active');
        $(`.nav-link[data-page="${page}"]`).addClass('active');
        
        // Update header title based on active link
        const title = $(`.nav-link[data-page="${page}"]`).text().trim();
        $('#pageTitle').text(title);
    }
};

$(function() {
    Sidebar.init();
});
