(function($){
        

    // next step click from sif pop
    $( "#esig-forminator-create" ).click(function() {
      
               var form_id= $('select[name="esig_forminator_form_id"]').val();
               
               // Check if form_id is valid
               if (!form_id || form_id === '' || form_id === 'sddelect') {
                   alert('Please select a Forminator form first.');
                   return;
               }
             
               $("#esig-forminator-form-first-step").hide();
               
               // Show loading indicator
               var loadingHtml = '<div id="esig-forminator-loading" style="text-align: center; padding: 40px 20px;">' +
                   '<div style="display: inline-block; width: 40px; height: 40px; border: 4px solid #f3f3f3; border-top: 4px solid #3498db; border-radius: 50%; animation: esig-spin 1s linear infinite;"></div>' +
                   '<p style="margin-top: 15px; color: #666; font-size: 14px;">Loading form fields...</p>' +
                   '</div>';
               $("#esig-forminator-field-option").html(loadingHtml);
               $("#esig-forminator-second-step").show();
               
               // Add CSS animation for spinner if not already added
               if (!$('#esig-forminator-spinner-style').length) {
                   $('<style id="esig-forminator-spinner-style">@keyframes esig-spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }</style>').appendTo('head');
               }
               
               // jquery ajax to get form field with nonce for security
               var ajaxUrl = (typeof esigForminatorAjax !== 'undefined') ? esigForminatorAjax.ajaxurl : esigAjax.ajaxurl;
               var nonce = (typeof esigForminatorAjax !== 'undefined') ? esigForminatorAjax.esig_forminator_nonce : '';
               
               jQuery.ajax({
                   url: ajaxUrl,
                   type: 'POST',
                   data: {
                       action: "esig_forminator_form_fields",
                       form_id: form_id,
                       esig_forminator_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-forminator-form-first-step").show();
                                   $("#esig-forminator-second-step").hide();
                                   return;
                               }
                           } catch (e) {
                               // Failed to parse JSON, treat as HTML
                           }
                       }
                       
                       // Success - treat as HTML and insert into DOM
                       $("#esig-forminator-field-option").html(data);
                       $("#esig-forminator-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-forminator-form-first-step").show();
                       $("#esig-forminator-second-step").hide();
                   }
               });

    });

    // Helper function to escape shortcode attribute values
    function escapeShortcodeAttr(value) {
        if (!value) return '';
        // Convert to string and escape double quotes
        return String(value).replace(/"/g, '&quot;');
    }

    // forminator add to document button clicked 
    $( "#esig-forminator-insert" ).click(function() {

               var form_id= $('select[name="esig_forminator_form_id"]').val();
               
              // var field_id =$('select[name="esig_forminator_field_id"]').val();
               var label = $('select[name="esig_forminator_field_id"]').find(':selected').data('id');
               var type = $('select[name="esig_forminator_field_id"]').find(':selected').data('type');
               var field_id =$('select[name="esig_forminator_field_id"]').val();
               var displayType =$('select[name="esig_forminator_value_display_type"]').val();
 
               

               if(field_id =="all")
               {
                       
                        $('select[name="esig_forminator_field_id"]').find('option').each(function () {
                               
                               // Add $(this).val() to your list
                               let allField = $(this).val();
                               let allLabel = $(this).data("id");
                               let allType = $(this).data("type"); 
                               
                               if (allField == "all") return true;
                               var return_text = '<p>[esigforminator label="'+ escapeShortcodeAttr(allLabel) +'" field_type="'+ escapeShortcodeAttr(allType) +'" field_id="'+ escapeShortcodeAttr(allField) +'" formid="'+ escapeShortcodeAttr(form_id) +'" display="'+ escapeShortcodeAttr(displayType) +'"]</p>';
                               esig_sif_admin_controls.insertContent(return_text);
                       });
               }else{

                var return_text = ' [esigforminator label="'+ escapeShortcodeAttr(label) +'" field_type="'+ escapeShortcodeAttr(type) +'" field_id="'+ escapeShortcodeAttr(field_id) +'" formid="'+ escapeShortcodeAttr(form_id) +'" display="'+ escapeShortcodeAttr(displayType) +'"] ';
                esig_sif_admin_controls.insertContent(return_text);
              }
        
         tb_remove();
                 
               
    });
    
    
    //if overflow
    $('#select-forminator-form-list').click(function(){
        
        
      
        $(".chosen-drop").show(0, function () { 
            $(this).parents("div").css("overflow", "visible");
            });
        
        
        
    });

})(jQuery);



