(function($){
        

        // next step click from sif pop
        $("#esig-ninja-create").click(function() {
 
                var form_id = $('select[name="esig_ninja_form_id"]').val();
                
                // Hide first step, show second step
                $("#esig-ninja-form-first-step").hide();
                $("#esig-nf-second-step").show();
                
                // Show loading only on first load
                var isFirstLoad = $("#esig-nf-field-option").is(':empty') || $("#esig-nf-field-option").html().trim() === '';
                
                if (isFirstLoad) {
                        $("#esig-ninja-loading-container").show();
                }
                
                // AJAX to get form fields
                // Security: Include nonce in AJAX request
                // Try esigNinjaAjax first, then fallback to esigAjax
                var nonce = (typeof esigNinjaAjax !== 'undefined' && esigNinjaAjax.nonce) 
                    ? esigNinjaAjax.nonce 
                    : (typeof esigAjax !== 'undefined' && esigAjax._wpnonce) 
                        ? esigAjax._wpnonce 
                        : '';
                var ajaxUrl = (typeof esigNinjaAjax !== 'undefined' && esigNinjaAjax.ajaxurl) 
                    ? esigNinjaAjax.ajaxurl 
                    : (typeof esigAjax !== 'undefined' && esigAjax.ajaxurl) 
                        ? esigAjax.ajaxurl 
                        : ajaxurl;
                jQuery.post(ajaxUrl, { 
                        action: "esig_ninja_form_fields", 
                        form_id: form_id,
                        nonce: nonce
                }, function(data) {
                        
                        // Hide and remove loading message
                        $("#esig-ninja-loading-container").fadeOut(200, function() {
                                $(this).remove();
                        });
                        
                        // Insert field options
                        $("#esig-nf-field-option").html(data);
                        
                        // Show all elements with proper targeting and spacing
                        setTimeout(function() {
                                // Get DOM elements directly - use step2 button ID!
                                var fieldOption = document.getElementById('esig-nf-field-option');
                                var displayType = document.getElementById('select-ninja-field-display-type');
                                var buttonWrap = document.getElementById('upload_ninja_button_step2');
                                
                                // Force inline styles with !important via setAttribute - add proper spacing
                                if (fieldOption) {
                                        fieldOption.setAttribute('style', 'display: block !important; visibility: visible !important; opacity: 1 !important; margin: 15px 0 !important;');
                                }
                                if (displayType) {
                                        displayType.setAttribute('style', 'display: block !important; visibility: visible !important; opacity: 1 !important; margin: 15px 0 !important;');
                                }
                                if (buttonWrap) {
                                        buttonWrap.setAttribute('style', 'display: block !important; visibility: visible !important; opacity: 1 !important; margin: 20px 0 !important;');
                                        
                                        // Also force the button inside visible
                                        var button = buttonWrap.querySelector('#esig-ninja-insert');
                                        if (button) {
                                                button.setAttribute('style', 'display: inline-block !important; visibility: visible !important; opacity: 1 !important;');
                                        }
                                }
                                
                        }, 100);
                        
                        // Re-initialize chosen for the dropdowns
                        setTimeout(function() {
                                if (jQuery.fn.chosen) {
                                        try {
                                                $("#esig-nf-field-option .chosen-select").chosen('destroy');
                                                $("#select-ninja-field-display-type .chosen-select").chosen('destroy');
                                        } catch(e) {}
                                        
                                        $("#esig-nf-field-option .chosen-select").chosen();
                                        $("#select-ninja-field-display-type .chosen-select").chosen();
                                }
                        }, 150);
                        
                }, "html").fail(function(xhr, status, error) {
                        $("#esig-ninja-loading-container").html('<span style="color: red;">Error loading fields. Please try again.</span>');
                });
  
        });
 
        // ninja add to document button clicked 
        $(document).on("click", "#esig-ninja-insert", function(e) {
                e.preventDefault();
 
                   var form_id= $('input[name="esig_nf_form_id"]').val() ;
                   var field_id =$('select[name="esig_nf_field_id"]').val();
                   var displayType =$('select[name="esig_ninja_value_display_type"]').val();
                   // 

                if (field_id == "all") {
                        //$("#esig_formidableform_field_id").each(function () {
                        $('select#esig_nf_field_id').find('option').each(function () {

                                // Add $(this).val() to your list
                                let allField = $(this).val();
                                if (allField == "all") return true;
                                var return_text = '<p> [esigninja formid="' + form_id + '" field_id="' + allField + '" display="' + displayType + '" ] </p>';
                                tinymce.get('document_content').insertContent(return_text);
                        });
                }
                else {
                        var return_text = ' [esigninja formid="' + form_id + '" field_id="' + field_id + '" display="' + displayType + '" ] ';
                        tinymce.get('document_content').insertContent(return_text);
                }   
            
             tb_remove();
                     
                   
        });
        
        
        //if overflow
        $('#select-ninja-form-list').click(function(){
            
            
          
            $(".chosen-drop").show(0, function () { 
				$(this).parents("div").css("overflow", "visible");
				});
            
            
            
        });

          // display  gravity form option popup
        $("#wpesign__ninja-sif-popup").on("click", function(e) {

                e.preventDefault();
               
                tb_show( "+ Ninja form option", "#TB_inline?inlineId=esig-ninja-option", false );
                

        });
	
})(jQuery);



