//EDIT THEME
function changeBackImgTheme() {
  jQuery(".choose-img").on("click", function (event) {
    event.preventDefault();

    images = wp
      .media({
        tile: "Upload Image",
        multiple: false,
      })
      .open()
      .on("select", function () {
        var uploadedImages = images.state().get("selection").first();
        var getImageUrl = uploadedImages.toJSON().url;
        jQuery(".squirrly-strb-background-image").attr("src", getImageUrl);
        jQuery(".background-img").val(getImageUrl);
      });
  });
}

(function ($) {
  "use strict";
  $(document).ready(function () {
    $(".dropdown-button").click((e) => {
      $(e.currentTarget).siblings(".dropdown-pannel").toggleClass("open");
    });
    //
    $(".select-country").click((e) => {
      $(".dropdown-button-country").text($(e.currentTarget).text());
      if ($(".dropdown-pannel").hasClass("open"))
        $(".dropdown-pannel").removeClass("open");
    });
    //
    $(".select-channel").click((e) => {
      $(".dropdown-button-channel").text($(e.currentTarget).text());
      if ($(".dropdown-pannel").hasClass("open"))
        $(".dropdown-pannel").removeClass("open");
    });
    //
    let selected_channel = "google_shopping";
    $(".select-channel").change((e) => {
      selected_channel = $(e.currentTarget).val();
      console.log("mere");
      console.log(selected_channel);
    });
    $(".save-initial-input").click((e) => {
      let project_name_input = $(".project-name").val();
      //e.preventDefault();
      return $.post(sqQuery.ajaxurl, {
        strb_nonce: sqQuery.nonce,
        action: "save-initial-data",
        selected_channel: selected_channel,
        project_name: project_name_input,
      }).done(function (data) {
        //console.log(data);
      });
    });

    $(".save-field-mapping").click((e) => {
      //e.preventDefault();
      return $.post(sqQuery.ajaxurl, {
        strb_nonce: sqQuery.nonce,
        action: "save-field-mapping",
      }).done(function (data) {
        //console.log(data);
      });
    });

    $(".open-settings-pannel").click((e) => {
      $(".settings-pannel").css("display", "none");
      $(e.currentTarget)
        .parent()
        .siblings(".settings-pannel")
        .css("display", "block");
    });
    $(".delete-feed").click((e) => {
      let feed_name = $(e.currentTarget).attr("id");
      console.log(feed_name);

      $("." + feed_name).css("border", "1px solid red");
      return $.post(sqQuery.ajaxurl, {
        strb_nonce: sqQuery.nonce,
        action: "delete-feed",
        feed: feed_name,
      }).done(function (data) {
        $("." + feed_name).css("display", "none");
        console.log(data);
      });
    });

    $(document).click((e) => {
      if (!$(e.target).hasClass("open-settings-pannel"))
        $(".settings-pannel").css("display", "none");
    });

    $(".expand-fields").click(() => {
      $(".dropdown-btn").toggleClass("down");
      $(".show-no-req-fields").toggleClass("closed");
    });

    $(".add-prod-att-value").click((e) => {
      let className = $(e.currentTarget).attr("id");

      let tmpHTML =
        `<div clas="prod-att-row remove-${className}" style="display:flex;flex-wrap:wrap;width:30vw;margin-left:25px;">` +
        '<input class="" type="text" style="margin-left:10px;background: rgba(31, 105, 72, 0.2);border:none;font-weight:500;" placeholder="Product id"/>' +
        '<input class="" type="text" style="margin-left:20px;background: rgba(31, 105, 72, 0.2);border:none;font-weight:500;" placeholder="value"/>' +
        "</div>";
      $(`.${className}`).append(tmpHTML);
      console.log(tmpHTML);
    });

    $(".remove-att-val").click((e) => {
      let updatedClass = $(e.currentTarget).attr("class").replace("r-", "");
      updatedClass = updatedClass.replace(" remove-att-val", "");

      let removetmpHTML =
        `<div clas="prod-att-row remove-${updatedClass}" style="display:flex;flex-wrap:wrap;width:30vw;margin-left:25px;">` +
        '<input class="" type="text" style="margin-left:10px;background: rgba(31, 105, 72, 0.2);border:none;font-weight:500;" placeholder="Product id"/>' +
        '<input class="" type="text" style="margin-left:20px;background: rgba(31, 105, 72, 0.2);border:none;font-weight:500;" placeholder="value"/>' +
        "</div>";

      //$(removetmpHTML).remove();
      $(`.prod-att-row`).css("display", "none");
      //$(".prod-att-row").remove();
      console.log(updatedClass);
    });
  });
})(jQuery);
