// WC Request a Quote (by Addify)
(function ($) {
  if (typeof wcpt_afrfq_params == "undefined") {
    wcpt_afrfq_params = {
      product_ids: [],
      view_quote_url: "",
      view_quote_label: "",
    };
  }

  // -- button status & switch html class
  $("body").on("wcpt_after_every_load", ".wcpt", function () {
    var $this__container = $(this);

    // switch native html class to wcpt html class on plugin buttons
    $(
      ".afrfqbt, .afrfqbt_single_page",
      wcpt_util.get_uninit_rows($this__container)
    )
      .removeClass("afrfqbt afrfqbt_single_page")
      .addClass("wcpt-afrfqbt"); // override with WCPT handler

    // remove afrfqbt success message
    $(".added_quote_pro").remove();

    // set added status
    afrfqbt_status($this__container);
  });

  setTimeout(afrfqbt_status, 500); // some external script removes 'added' html class

  function afrfqbt_status($container) {
    // $container will be $('.wcpt-afrfqbt'), $row, $wcpt or $body
    if (typeof $container === "undefined") {
      $container = $("body");
    }

    var $raq_buttons = $container.is(".wcpt-afrfqbt")
      ? $container
      : $(".wcpt-afrfqbt", wcpt_util.get_uninit_rows($container));

    $raq_buttons.each(function () {
      var $raq_button = $(this),
        $row = $raq_button.closest(".wcpt-row"),
        id = false,
        product_type = $row.attr("data-wcpt-type"),
        $prev_view_link = $raq_button.siblings(
          ".wcpt-afrfqbt-view-quote-wrapper"
        );

      if (product_type === "variable") {
        id = $row.data("wcpt_variation_id");
      } else if (product_type === "variation") {
        id = $row.attr("data-wcpt-variation-id");
      } else {
        id = $row.attr("data-wcpt-product-id");
      }

      if (-1 !== $.inArray(parseInt(id), wcpt_afrfq_params.product_ids)) {
        $raq_button.addClass("added");

        if (!$prev_view_link.length) {
          $raq_button.after(
            '<div class="wcpt-afrfqbt-view-quote-wrapper"><a href="' +
              wcpt_afrfq_params.view_quote_url +
              '" class="wcpt-afrfqbt-view-quote">' +
              wcpt_afrfq_params.view_quote_label +
              "</a></div>"
          );
        }
      } else {
        $raq_button.removeClass("added");
        $prev_view_link.remove();
      }
    });
  }

  // -- ev: 'select_variation' - enable / disable button
  $(function () {
    $("body").on(
      "select_variation",
      ".wcpt-product-type-variable",
      function () {
        var $this = $(this),
          variation_id = $this.data("wcpt_variation_id"),
          $raq_button = $(".wcpt-afrfqbt", $this);

        afrfqbt_status($this);

        if (variation_id) {
          // enable button
          $raq_button.removeClass("disabled");
        } else {
          // disable button
          $raq_button.addClass("disabled");
        }
      }
    );
  });

  // -- ajax req
  $("body").on("click", ".wcpt-afrfqbt", function (e) {
    e.preventDefault();

    var $this = $(this),
      $row = $this.closest(".wcpt-row"),
      product_id = $row.attr("data-wcpt-product-id"),
      variation_id = false,
      variation_attributes = false,
      qty = 0;

    if ($(".cart .qty", $row).length) {
      qty = $(".cart .qty", $row).val();
    } else {
      qty = $(".qty", $row).val();
    }

    if (!qty) {
      qty = 1;
    }

    if ($row.hasClass("wcpt-product-type-variable")) {
      variation_id = $row.data("wcpt_variation_id");
      variation_attributes = $row.data("wcpt_attributes");
    } else if ($row.hasClass("wcpt-product-type-variation")) {
      variation_id = $row.attr("data-wcpt-variation-id");
      variation_attributes = $row.data("wcptVariationAttributes");
    }

    if ($row.hasClass("wcpt-product-type-variable") && !variation_id) {
      alert(wcpt_i18n.i18n_make_a_selection_text);
      return;
    }

    if ($this.hasClass("disabled")) {
      return;
    }

    $this.removeClass("added"); // avoid conflict with .loading animation

    if (variation_id) {
      var ajax_data = {
        action: "add_to_quote_single_vari",
        form_data: {
          product_id: product_id,
          variation_id: variation_id,
          "add-to-cart": product_id,
          quantity: qty,
        },
        nonce: afrfq_phpvars.nonce,
      };

      ajax_data.form_data = $.param(ajax_data.form_data);

      $.extend(ajax_data.form_data, variation_attributes);
    } else {
      var ajax_data = {
        action: "add_to_quote_single",
        form_data: "quantity=" + qty,
        product_id: product_id,
        nonce: afrfq_phpvars.nonce,
      };
    }

    $.ajax({
      url: afrfq_phpvars.admin_url,
      method: "POST",
      beforeSend: function () {
        $this.addClass("loading");
      },
      data: ajax_data,
    }).done(function (response) {
      // keep list of products in raq
      // upon after every load, refer to this list
      $this.removeClass("loading");

      if (variation_id) {
        wcpt_afrfq_params.product_ids.push(parseInt(variation_id));
      } else {
        wcpt_afrfq_params.product_ids.push(parseInt(product_id));
      }

      afrfqbt_status($this);

      if (response !== "success") {
        // menu mini quote
        $(".quote-li").replaceWith(response["mini-quote"]);
      }
    });
  });
})(jQuery);
