(function ($, window, document, undefined) {
  "use strict";

  /* Ajax search & category filter for product select */

  function finch_cart_product_select_ajax_search( $this, $search_query, $search_category ){
    var $options         = $this.find(".edith-product-select-options"),
        $options_name    = $this.find(".edith-product-select-option-name").attr("name"),
        $current_post    = $this.find(".edith-current-product-id").val();

    wp.ajax.send({
      ajax_option: "fire_fc_product_select_search",
      data: {
        action         : "fire_fc_product_select_search",
        search_query   : $search_query,
        search_category: $search_category,
        options_name   : $options_name,
        current_post   : $current_post,
        _wpnonce       : Finch_Cart_Vars.nonce,
      },
      beforeSend: function (xhr) {
        $this.addClass("finchcart_ps_loading");
        $options.html('<div class="finchcart_dot_pulse"></div>');
      },
      success: function (res) {
        $this.removeClass("finchcart_ps_loading");

        // Implode new response
        $options.html(res);
        edith_field_product_select_sorter();
      },
      error: function (error) {
        alert(error);
      },
    });
  }

  $(".edith-field-product_select").each(function () {
    var $this             = $(this),
        $search_box       = $this.find(".edith-product-select-search"),
        $product_category = $this.find(".edith-product-select-category"),
        $options          = $this.find(".edith-product-select-options"),
        $options_name     = $this.find(".edith-product-select-option-name").attr("name"),
        $search_query     = '',
        $search_category  = '';

    $this.find(".edith-chosen").edith_chosen();

    $product_category.on('change', function() {
      console.log( this.value );
      $search_category = this.value;
      finch_cart_product_select_ajax_search( $this, $search_query, $search_category );
    });

    $search_box.keyup(function () {
      if ($(this).val().length > 2) {
        var $search_query = $(this).val();
        finch_cart_product_select_ajax_search( $this, $search_query, $search_category );
      }

      if ($(this).val().length == 0) {
        finch_cart_product_select_ajax_search( $this, '', $search_category );
      }
    });
  });

  function edith_field_product_select_sorter() {
    $(".edith-field-product_select").each(function () {
      var $this         = $(this),
          $enabled      = $this.find(".edith-enabled"),
          $has_disabled = $this.find(".edith-disabled"),
          $disabled     = $has_disabled.length ? $has_disabled : false,
          $options_name = $this.find(".edith-product-select-option-name").attr("name");

      $enabled.sortable({
        connectWith: $disabled,
        placeholder: "ui-sortable-placeholder",
        update: function (event, ui) {
          var $el = ui.item.find("input");

          if (ui.item.parent().hasClass("edith-enabled")) {
            $el.attr("name", $el.attr("name").replace("disabled", "enabled"));
          } else {
            $el.attr("name", $el.attr("name").replace("enabled", "disabled"));
          }
        },
      });

      if ($disabled) {
        $disabled.sortable({
          connectWith: $enabled,
          placeholder: "ui-sortable-placeholder",
          update: function (event, ui) {},
        });
      }
    });
  }
  //
  // Document ready and run scripts
  //
  $(document).ready(function () {
    //$(".edith-field-product_select").addClass("edith-field-sorter");
    edith_field_product_select_sorter();
  });
})(jQuery, window, document);
