/**
 * Admin Bar Manager Scripts
 *
 * @package Polanger_Dashboard_Tools_Pro
 * @since 3.3.0
 */

(function($) {
    'use strict';

    $(document).ready(function() {
        
        // Toggle visibility icon on checkbox change
        $(document).on('change', '.pdt-abm-hide-toggle input', function() {
            var $item = $(this).closest('.pdt-abm-item');
            var $icon = $(this).siblings('.dashicons');
            
            if ($(this).is(':checked')) {
                $item.addClass('pdt-abm-item-hidden');
                $icon.removeClass('dashicons-visibility').addClass('dashicons-hidden');
            } else {
                $item.removeClass('pdt-abm-item-hidden');
                $icon.removeClass('dashicons-hidden').addClass('dashicons-visibility');
            }
        });
        
        // Toggle rename input
        $(document).on('click', '.pdt-abm-rename-btn', function(e) {
            e.preventDefault();
            var $item = $(this).closest('.pdt-abm-item');
            var $renameSection = $item.children('.pdt-abm-item-rename');
            
            $renameSection.slideToggle(200);
            $(this).toggleClass('active');
        });
        
        // Expand/collapse children
        $(document).on('click', '.pdt-abm-expand-btn', function(e) {
            e.preventDefault();
            var $item = $(this).closest('.pdt-abm-item');
            var $children = $item.children('.pdt-abm-item-children');
            var $icon = $(this).find('.dashicons');
            
            $children.slideToggle(200);
            $icon.toggleClass('dashicons-arrow-right-alt2 dashicons-arrow-down-alt2');
            $(this).attr('aria-expanded', $children.is(':visible'));
        });
        
        // Expand all button
        $(document).on('click', '#pdt-abm-expand-all', function(e) {
            e.preventDefault();
            $('.pdt-abm-item-children').slideDown(200);
            $('.pdt-abm-expand-btn .dashicons').removeClass('dashicons-arrow-right-alt2').addClass('dashicons-arrow-down-alt2');
            $('.pdt-abm-expand-btn').attr('aria-expanded', 'true');
        });
        
        // Collapse all button
        $(document).on('click', '#pdt-abm-collapse-all', function(e) {
            e.preventDefault();
            $('.pdt-abm-item-children').slideUp(200);
            $('.pdt-abm-expand-btn .dashicons').removeClass('dashicons-arrow-down-alt2').addClass('dashicons-arrow-right-alt2');
            $('.pdt-abm-expand-btn').attr('aria-expanded', 'false');
        });
        
        // Search/filter items
        $(document).on('input', '#pdt-abm-search', function() {
            var search = $(this).val().toLowerCase();
            
            if (search === '') {
                $('.pdt-abm-item').show();
                return;
            }
            
            $('.pdt-abm-item').each(function() {
                var $item = $(this);
                var title = $item.find('.pdt-abm-item-title').first().text().toLowerCase();
                var id = $item.find('.pdt-abm-item-id').first().text().toLowerCase();
                
                if (title.indexOf(search) > -1 || id.indexOf(search) > -1) {
                    $item.show();
                    // Show parent items too
                    $item.parents('.pdt-abm-item').show();
                    $item.parents('.pdt-abm-item-children').show();
                } else {
                    // Only hide if no matching children
                    var hasMatchingChild = $item.find('.pdt-abm-item').filter(function() {
                        var childTitle = $(this).find('.pdt-abm-item-title').first().text().toLowerCase();
                        var childId = $(this).find('.pdt-abm-item-id').first().text().toLowerCase();
                        return childTitle.indexOf(search) > -1 || childId.indexOf(search) > -1;
                    }).length > 0;
                    
                    if (!hasMatchingChild) {
                        $item.hide();
                    }
                }
            });
        });
        
    });
    
})(jQuery);
