(function($){
        

        // next step click from sif pop
        $( "#esig-cf7-create" ).click(function() {
        
 
                   var form_id= $('select[name="esig-cf7-form-id"]').val();
                   
                 
                   $("#esig-cf7-form-first-step").hide();
                   
                   // jquery ajax to get form field with nonce for security
                   var ajaxUrl = (typeof esigCf7Ajax !== 'undefined') ? esigCf7Ajax.ajaxurl : esigAjax.ajaxurl;
                   var nonce = (typeof esigCf7Ajax !== 'undefined') ? esigCf7Ajax.esig_cf7_nonce : '';
                   
                   jQuery.ajax({
                       url: ajaxUrl,
                       type: 'POST',
                       data: {
                           action: "esig_cf7_form_fields",
                           form_id: form_id,
                           esig_cf7_nonce: nonce
                       },
                       dataType: 'text', // Accept both JSON and HTML
                       success: function(data, textStatus, xhr) {
                           // Check if response is JSON (error) by trying to parse it
                           var isJson = false;
                           var jsonData = null;
                           
                           // Check Content-Type header first
                           var contentType = xhr.getResponseHeader('Content-Type') || '';
                           if (contentType.indexOf('application/json') !== -1) {
                               isJson = true;
                           }
                           
                           // Also check if response looks like JSON (starts with {)
                           if (!isJson && data.trim().charAt(0) === '{') {
                               isJson = true;
                           }
                           
                           // If it's JSON, try to parse it
                           if (isJson) {
                               try {
                                   jsonData = jQuery.parseJSON(data);
                                   if (jsonData && jsonData.success === false) {
                                       // It's an error response - show error message only
                                       var errorMessage = jsonData.data && jsonData.data.message ? jsonData.data.message : 'Error loading form fields. Please try again.';
                                       $("#esig-cf7-field-option").html('<div class="esig-cf7-error-message" style="color: #b32d2e; margin-bottom: 10px; padding: 10px; background: #fbeaea; border: 1px solid #e0b4b4; border-radius: 4px;">' + errorMessage + '</div>');
                                       $("#esig-cf7-form-first-step").show();
                                       // Show the error message for 3 seconds, then hide it and show the field step again
                                     
                                       $("#esig-cf7-second-step").hide();
                                       return;
                                   }
                               } catch (e) {
                                   // Failed to parse JSON, treat as HTML
                               }
                           }
                           
                           // Success - treat as HTML and insert into DOM
                           $("#esig-cf7-field-option").html(data);
                       },
                       error: function(xhr, status, error) {
                           // Try to parse error response as JSON to get error message
                           var errorMessage = 'Error loading form fields. Please try again.';
                           try {
                               var jsonData = jQuery.parseJSON(xhr.responseText);
                               if (jsonData && jsonData.data && jsonData.data.message) {
                                   errorMessage = jsonData.data.message;
                               }
                           } catch (e) {
                               // Use default error message
                           }
                               $("#esig-cf7-field-option").html('<div class="esig-cf7-error-message" style="color: #b32d2e; margin-bottom: 10px; padding: 10px; background: #fbeaea; border: 1px solid #e0b4b4; border-radius: 4px;">' + errorMessage + '</div>');
                               $("#esig-cf7-form-first-step").show();
                              
                           $("#esig-cf7-second-step").hide();
                       }
                   });
                   
                   $("#esig-cf7-second-step").show();                        
  
        });
 
        // contact for 7 add to document button clicked 
        $( "#esig-cf7-insert" ).click(function() {
         
 
                 var formid= $('select[name="esig-cf7-form-id"]').val();
                   
                 var field_id =$('select[name="esig_cf7_field_id"]').val();
                 
                if (field_id == "all") {
                        $('select#esig_cf7_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>[esigcf7 formid="' + formid + '" field_id="' + allField + '" ]</p>';
                                esig_sif_admin_controls.insertContent(return_text);
                        });
                }
                else {
                        var return_text = '[esigcf7 formid="' + formid + '" field_id="' + field_id + '" ]';
                        esig_sif_admin_controls.insertContent(return_text);
                }
                  
            
                 tb_remove();
                     
                   
        });
        
        
        //if overflow
        $('#select-cf7-form-list').click(function(){
            
            
          
            $(".chosen-drop").show(0, function () { 
				$(this).parents("div").css("overflow", "visible");
				});
            
            
            
        });
	
})(jQuery);



