$(".user-select-two").select2({
  ajax: {
    url: mpPanelBaseUrl + "/user-request",
    type: "post",
    dataType: 'json',
    delay: 250,
    data: function (params) {
      return {
        search: $.trim(params.term)
      };
    },
    processResults: function (data) {
      return {
        results: data
      };
    },
    cache: true
  },
  language: {
    inputTooShort: function() {
      return "جستجو در کاربران";
    },
    noResults: function () {
      return "جستجو نتیجه‌ای نداشت";
    }
  },
  dir: "rtl",
  minimumInputLength: 1,
  templateResult: formatUser,
  templateSelection: formatUserSelection
});

function formatUser (user) {
  if (user.loading) {
    return user.text;
  }

  let $container = $(
    "<div class='select2-result clearfix'>" +
        "<div class='select2-result-id'></div>" +
        "<div class='select2-result-meta'>" +
            "<div class='select2-result-text'></div>" +
            "<div class='select2-result-summary'></div>" +
        "</div>" +
        "<div class='select2-result-user-phone'></div>" +
    "</div>"
  );

  $container.find(".select2-result-text").text(user.text);
  $container.find(".select2-result-summary").text(user.summary);
  $container.find(".select2-result-id").text(user.id);
  $container.find(".select2-result-user-phone").text(user.phone);

  return $container;
}

function formatUserSelection (user) {
  return user.text + " " + user.phone;
}


$(".role-filter-select-two").select2({
  dir: "rtl",
  language: {
    inputTooShort: function() {
      return "نقش کاربر";
    },
    noResults: function () {
      return "جستجو نتیجه‌ای نداشت";
    }
  },
  minimumInputLength: 0,
  templateResult: formatConfirm,
  templateSelection: formatConfirmSelection
});

function formatConfirm (item) {
  if (item.loading) {
    return item.text;
  }

  let $container = $(
    "<div class='select2-result clearfix'>" +
      "<div class='select2-result-meta'>" +
        "<div class='select2-result-text'></div>" +
        "<div class='select2-result-summary'></div>" +
      "</div>" +
    "</div>"
  );

  $container.find(".select2-result-text").text(item.text);
  $container.find(".select2-result-summary").text(item.summary);

  return $container;
}

function formatConfirmSelection (item) {
  return item.text ;
}

