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

(function($) {
    'use strict';

    $(document).ready(function() {
        var i18n = window.pdtAdminBarI18n || {};
        
        // Media uploader for logo
        var mediaUploader;
        
        $('#pdt-upload-logo').on('click', function(e) {
            e.preventDefault();
            
            if (mediaUploader) {
                mediaUploader.open();
                return;
            }
            
            mediaUploader = wp.media({
                title: i18n.mediaTitle || '',
                button: {
                    text: i18n.mediaButtonText || ''
                },
                multiple: false,
                library: {
                    type: 'image'
                }
            });
            
            mediaUploader.on('select', function() {
                var attachment = mediaUploader.state().get('selection').first().toJSON();
                
                $('#pdt-custom-logo-url').val(attachment.url);
                $('#pdt-logo-preview').html('<img src="' + attachment.url + '" alt="' + (i18n.customLogoAlt || '') + '" />').addClass('has-logo');
                $('#pdt-remove-logo').show();
            });
            
            mediaUploader.open();
        });
        
        // Remove logo
        $('#pdt-remove-logo').on('click', function(e) {
            e.preventDefault();
            
            $('#pdt-custom-logo-url').val('');
            $('#pdt-logo-preview').html('<span class="dashicons dashicons-format-image"></span>').removeClass('has-logo');
            $(this).hide();
        });
        
        // Toggle custom logo section based on hide checkbox
        function toggleLogoSections() {
            var isHidden = $('#pdt-hide-wp-logo').is(':checked');

            if (isHidden) {
                $('#pdt-custom-logo-section').addClass('pdt-ab-section-dimmed');
                $('#pdt-logo-link-section').addClass('pdt-ab-section-dimmed');
                $('#pdt-logo-submenu-section').addClass('pdt-ab-section-dimmed');
            } else {
                $('#pdt-custom-logo-section').removeClass('pdt-ab-section-dimmed');
                $('#pdt-logo-link-section').removeClass('pdt-ab-section-dimmed');
                $('#pdt-logo-submenu-section').removeClass('pdt-ab-section-dimmed');
            }
        }
        
        $('#pdt-hide-wp-logo').on('change', toggleLogoSections);
        $('#pdt-custom-logo-url').on('change', toggleLogoSections);
        
        // Initial state
        toggleLogoSections();
        
        // Add has-logo class if logo exists
        if ($('#pdt-custom-logo-url').val() !== '') {
            $('#pdt-logo-preview').addClass('has-logo');
        }
    });
    
})(jQuery);
