jQuery(document).ready(function ($) {
  // ---------------------- add dynamic fields to sms body
  $(document).on(
    "click",
    ".dynamic_fields_placeholder .eeavadaforms_field",
    function () {
      var text = $(this).attr("data-fieldid");
      text = "{{" + text + "}}";
      var teatarea_obj = $("#acf-field_658e7e01db05dava");
      teatarea_obj.val(teatarea_obj.val() + text);
    }
  );

  checkSelectValue();
  function checkSelectValue() {
    var selectValue = $("#acf-field_658dc7b96a7f5ava").val();
    if (selectValue !== "0") {
      selectFormOnChangeEvent(selectValue);
    }
  }

  // ---------------------- select form
  $("#acf-field_658dc7b96a7f5ava").on("change", function () {
    selectFormOnChangeEvent($(this).val());
  });

  $("#acf-field_658ef2e89bd46ava").on("change", function () {
    pattern_variables_generator($(this).val());
  });

  function pattern_variables_generator(value) {
    var fieldsArray = ["description", "date", "time", "name", "url"];
    var payamakpanel_result = "";
    counter = 0;
    var resultArray = [];
    for (var fieldValue of value) {
      var isField = fieldsArray.includes(fieldValue);
      if (!isField) {
        resultArray.push("<span>" + fieldValue + "</span>");
      } else {
        resultArray.push("<span>" + fieldValue + "</span>");
      }
      payamakpanel_result += "<span>" + "{" + counter + "}" + "</span>";
      counter++;
    }

    var IppanelSpanHtml = resultArray.join("");
    var PayamakPanelSpanHtml = payamakpanel_result;

    $("#ippanel .panel_vars").html(IppanelSpanHtml);
    $("#payamakpanel .panel_vars").html(PayamakPanelSpanHtml);
  }

  function selectFormOnChangeEvent(selected_formid) {
    $("#acf-field_659057a750ce8ava").val(selected_formid);

    jQuery.ajax({
      url: ajaxurl,
      type: "POST",
      data: {
        action: "eeavadaforms_get_form_fields",
        form_id: selected_formid,
        settingspageid: eeavadaforms_get_url_parameter("post"),
      },
      beforeSend: function () {
        $("#optionsloadingwrapper").css("display", "flex");
      },
      success: function (response) {
        
        if (response.success) {
          // set normal sms variables
          $(".dynamic_fields_placeholder").html(response.data.normal);

          // set recipients variables
          var recipients_items = Object.entries(response.data.recipients).map(
            ([key, value]) => {
              return `<option value="${key}">${value}</option>`;
            }
          );
          $("#acf-field_658fdd83aa83eava").html(recipients_items);

          $("#acf-field_658fdd83aa83eava").val(
            response.data.selected_recipients
          );
          $("#acf-field_658fdd83aa83eava").trigger("change");

          var send_model = response.data.send_model;
          if (send_model == "pattern") {
            var select2data = response.data.select2data;
            $("#acf-field_658ef2e89bd46ava").select2({
              data: JSON.parse(select2data),
            });
            $("#acf-field_658ef2e89bd46ava").trigger("change");
          } else {
            var pattern_items = Object.entries(response.data.pattern).map(
              ([key, value]) => {
                return `<option value="${key}">${value}</option>`;
              }
            );
            $("#acf-field_658ef2e89bd46ava").html(pattern_items);
          }
        }
      },
      complete: function () {
        $("#optionsloadingwrapper").css("display", "none");
      },
    });
      
  }

  function eeavadaforms_get_url_parameter(sParam) {
    var sPageURL = window.location.search.substring(1),
      sURLVariables = sPageURL.split("&"),
      sParameterName,
      i;

    for (i = 0; i < sURLVariables.length; i++) {
      sParameterName = sURLVariables[i].split("=");

      if (sParameterName[0] === sParam) {
        return sParameterName[1] === undefined
          ? true
          : decodeURIComponent(sParameterName[1]);
      }
    }
    return false;
  }
});
