(function ($) {
  'use strict'

  $(document).ready(function () {
    // Função para remover scripts com base no src
    function removeScriptBySrc(src) {
      $('script').each(function () {
        if ($(this).attr('src') && $(this).attr('src').includes(src)) {
          $(this).remove()
        }
      })
    }

    removeScriptBySrc('crowdfunding-front.js')
  })

  $(document).ready(function () {
    const css = `
            .form-errors {
                color: #d9534f; /* Cor para mensagens de erro */
                margin-bottom: 10px;
            }
            .error-message, .success-message {
                font-size: 14px;
                margin: 5px 0;
            }
            .success-message {
                color: #1aab00; /* Cor para mensagens de sucesso */
            }
        `

    const style = document.createElement('style')
    style.type = 'text/css'

    if (style.styleSheet) {
      style.styleSheet.cssText = css
    } else {
      style.appendChild(document.createTextNode(css))
    }

    document.head.appendChild(style)
  })

  $(document).on('click', '#lkn-user-registration-btn', function (e) {
    const $btn = $(this)
    $btn.attr('disabled', 'disabled')

    let allFieldsFilled = true
    const $formErrors = $('#form-errors')
    const $formSuccess = $('#form-success')

    $formErrors.empty()
    $formSuccess.empty()

    $btn.closest('form').find('[required]').each(function () {
      if ($(this).val() === '') {
        allFieldsFilled = false
        $(this).addClass('field-error')
      } else {
        $(this).removeClass('field-error')
      }
    })

    if (!allFieldsFilled) {
      $formErrors.html('<p class="error-message">Por favor, preencha todos os campos obrigatórios.</p>')
      $btn.removeAttr('disabled')
      return
    }

    e.preventDefault()

    const registration_form_data = $btn.closest('form').serialize()
    $.ajax({
      type: 'POST',
      url: wpcf_ajax_object.ajax_url,
      data: registration_form_data,
      dataType: 'json', // Espera-se que a resposta seja JSON
      success: function (response) {
        if (response.success == 1) {
          // wpcf_modal(response.message);
          if (response.validation == 0) { // sem validação
            $formSuccess.html('<p class="sucess-message">' + response.message + '</p>')
            if (response.redirect != 'Nenhuma') {
              location.href = response.redirect
            }
          } else {
            $formSuccess.html('<p class="sucess-message">' + response.message + '</p>')
            if (response.redirect) {
              location.href = response.redirect
            }
          }
        } else {
          $btn.removeAttr('disabled')
          if (typeof grecaptcha !== 'undefined') {
            grecaptcha.reset()
          }
          $formErrors.html('<p class="error-message">' + response.message + '</p>')
        }
      },
      error: function (jqXHR, textStatus, errorThrown) {
        $btn.removeAttr('disabled')
        $formErrors.html('<p class="error-message">Ocorreu um erro ao processar sua solicitação.</p>')
      }
    })
  })
})(jQuery)
