jQuery(document).ready(function ($) {
    // Media Uploader for Company Logo
    $('#upload-btn').click(function (e) {
        e.preventDefault();
        var frame = wp.media({
            title: 'Select or Upload Company Logo',
            button: {
                text: 'Use this logo'
            },
            multiple: false
        });

        frame.on('select', function () {
            var attachment = frame.state().get('selection').first().toJSON();
            $('#image_url').val(attachment.url);
            $('#image-preview').attr('src', attachment.url);
        });

        frame.open();
    });

    // Remove Image
    $('#remove-btn').click(function (e) {
        e.preventDefault();
        $('#image_url').val('');
        $('#image-preview').attr('src', $('#image-preview').attr('data-default-src'));
    });

    // Dynamic Layout Preview Change
    $('#choose').change(function () {
        var selected_icon = $(this).find(':selected').attr('data-id');
        if (selected_icon) {
            $('#my_changing_image').attr('src', selected_icon);
        }
    });

    // --- NEW: Premium Bulk Export Button Shortcut ---
    if ($('body').hasClass('post-type-product') && $('body').hasClass('edit-php')) {
        // Create the shortcut button with improved spacing and alignment
        var btnHtml = '<a href="#" id="kgdc-bulk-pdf-btn" class="page-title-action" style="background: #0a5fa5 !important; color: #ffffff !important; border: none !important; font-weight: 600; padding: 0 16px; height: 30px; margin: 12px 0 0 10px; border-radius: 6px; box-shadow: 0 4px 10px rgba(10, 95, 165, 0.2); transition: all 0.3s ease; display: inline-flex; justify-content: center; align-items: center; vertical-align: top;">' +
            '<span class="dashicons dashicons-pdf" style="font-size: 17px; width: 17px; height: 17px; margin-right: 8px; margin-top: 0;"></span>' +
            '<span class="kgdc-btn-text">Export Full Catalog</span></a>';

        $('.wp-header-end').before(btnHtml);
        var $shortcutBtn = $('#kgdc-bulk-pdf-btn');

        // Update count and visibility on selection change
        function updateShortcutState() {
            var selectedCount = $('.wp-list-table input[name="post[]"]:checked').length;
            var $btnText = $shortcutBtn.find('.kgdc-btn-text');
            if (selectedCount > 0) {
                $btnText.html('Export Selected (' + selectedCount + ')');
            } else {
                $btnText.text('Export Full Catalog');
            }
        }

        // Listen for checkbox changes (including Select All)
        $(document).on('change', '.wp-list-table input[type="checkbox"]', function () {
            setTimeout(updateShortcutState, 10);
        });

        // Trigger the bulk action on click
        $shortcutBtn.on('click', function (e) {
            e.preventDefault();

            var selectedCount = $('.wp-list-table input[name="post[]"]:checked').length;

            // If nothing selected, use the dedicated Export All URL
            if (selectedCount === 0) {
                var currentUrl = new URL(window.location.href);
                currentUrl.searchParams.set('kgdc_export_all', '1');
                if (typeof kgdc_admin !== 'undefined' && kgdc_admin.export_nonce) {
                    currentUrl.searchParams.set('_wpnonce', kgdc_admin.export_nonce);
                }
                window.location.href = currentUrl.toString();
                return;
            }

            var $topSelector = $('#bulk-action-selector-top');
            if ($topSelector.find('option[value="export_to_pdf"]').length) {
                $topSelector.val('export_to_pdf');
                $('#posts-filter').submit();
            } else {
                alert('Export to PDF action not available.');
            }
        });

        // Hover animation
        $shortcutBtn.hover(
            function () { $(this).css('transform', 'translateY(-1px)').css('box-shadow', '0 6px 15px rgba(10, 95, 165, 0.3)'); },
            function () { $(this).css('transform', 'translateY(0)').css('box-shadow', '0 4px 10px rgba(10, 95, 165, 0.2)'); }
        );

        // Initial check
        updateShortcutState();
    }
});