

(function($){
        

        // next step click from sif pop
        $( "#esig-formidableform-create" ).click(function() {
                 var form_id= $('select[name="esig-formidableform-id"]').val();
                 $("#esig-formidable-form-first-step").hide();
                 // jquery ajax to get form field with nonce for security
                 var ajaxUrl = (typeof esigFormidableAjax !== 'undefined') ? esigFormidableAjax.ajaxurl : esigAjax.ajaxurl;
                 var nonce = (typeof esigFormidableAjax !== 'undefined') ? esigFormidableAjax.esig_formidableform_nonce : '';
                 
                 jQuery.ajax({
                     url: ajaxUrl,
                     type: 'POST',
                     data: {
                         action: "esig_formidableform_fields",
                         form_id: form_id,
                         esig_formidableform_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.';
                                     alert(errorMessage);
                                     $("#esig-formidable-form-first-step").show();
                                     $("#esig-formidableform-second-step").hide();
                                     return;
                                 }
                             } catch (e) {
                                 // Failed to parse JSON, treat as HTML
                             }
                         }
                         
                         // Success - treat as HTML and insert into DOM
                         $("#esig-formidable-field-option").html(data);
                         $("#esig-formidableform-second-step").show();
                     },
                     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
                         }
                         alert(errorMessage);
                         $("#esig-formidable-form-first-step").show();
                         $("#esig-formidableform-second-step").hide();
                     }
                 });
  
        });
 
       
        $( "#esig-formidable-insert" ).click(function() {

                
                 var formid= $('select[name="esig-formidableform-id"]').val();
                 var field_id =$('select[name="esig_formidableform_field_id"]').val();

                 var displayType = $('select[name="esig_formidable_value_display_type"]').val();

                 if(field_id =="all")
                 {
                         //$("#esig_formidableform_field_id").each(function () {
                         $('select#esig_formidableform_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>[esigformidable formid="' + formid + '" field_id="' + allField + '" display="' + displayType + '" ]</p>';
                                 esig_sif_admin_controls.insertContent(return_text);
                         });
                 }
                 else 
                 {
                         var return_text = '[esigformidable formid="' + formid + '" field_id="' + field_id + '" display="' + displayType + '" ] ';
                         esig_sif_admin_controls.insertContent(return_text);
                 }
                
                 tb_remove();
                     
                   
        });
        
        
        //if overflow
        $('#select-formidable-form-list').click(function(){
       
         $(".chosen-drop").show(0, function () { 
	$(this).parents("div").css("overflow", "visible");
	});
            
            
            
        });
	
})(jQuery);



