window.jQuery(document).ready(function(r){var o=wp.i18n.__,a=r(".tutor-search-loader"),s=r(".tutor-search-result"),n=r(".tutor-search-selected"),u=r("input.tutor-search-input"),t=r("#tutor-manual-enrollment-form"),e=r("#enrollment-modal"),i=r('select[name="course_id"]'),c=r(".tutor-btn-submit");function d(){var t=[];return n.find("[data-user-id]").each(function(){t.push(r(this).data("user-id"))}),t}u.keyup(function(o){var r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:500,a=null;return function(){var t=this,e=arguments;clearTimeout(a),a=setTimeout(function(){o.apply(t,e)},r)}}(function(){var t,e,o=r(this).val();""===o?(s.html(""),a.removeClass("tutor-animation-spin tutor-icon-circle-notch")):(a.addClass("tutor-animation-spin tutor-icon-circle-notch"),t=[],n.find("[data-user-id]").each(function(){t.push(r(this).data("user-id"))}),e=r('select[name="course_id"]').val(),r.ajax({url:window._tutorobject.ajaxurl,type:"POST",dataType:"json",data:{term:o,action:"tutor_search_students",course_id:e,shortlisted:t},success:function(t){a.removeClass("tutor-animation-spin tutor-icon-circle-notch"),s.show(),s.html(t.data)}}))},500)),e.on("click",".tutor-add-student",function(){var t,e;r(".tutor-add-student").hasClass("is-loading")||(r(this).addClass("is-loading"),r(".tutor-add-student").css("pointer-events","none"),t=r(this).data("user_id"),e=d(),t&&!isNaN(t)&&e.push(t),r.ajax({url:window._tutorobject.ajaxurl,type:"POST",dataType:"json",data:{action:"tutor_search_students",selection:1,shortlisted:e},success:function(t){a.removeClass("is-loading"),n.html(t.data),s.hide(),u.val("").focus(),r(".tutor-add-student").css("pointer-events","auto")}}))}),e.on("click",".tutor-btn-remove-student",function(){r(this).parent().closest(".tutor-user-item").remove()}),i.change(function(){""===r(this).val()?u.attr("disabled","disabled"):(u.removeAttr("disabled").focus(),n.empty())}),t.submit(function(t){t.preventDefault();var t=i.val(),e=d();t?0==e.length?tutor_toast(o("Error!","tutor-pro"),"Please select student for enroll","error"):(c.addClass("is-loading").attr("disabled","disabled"),r.ajax({url:window._tutorobject.ajaxurl,type:"POST",dataType:"json",data:{action:"tutor_enroll_bulk_student",course_id:t,student_ids:e.join(",")},success:function(t){c.removeClass("is-loading").removeAttr("disabled"),t.success?(r("button[data-tutor-modal-close]").last().click(),tutor_toast(o("Success","tutor-pro"),t.message,"success"),window.location.reload()):tutor_toast(o("Error!","tutor-pro"),t.message,"error")}})):tutor_toast(o("Error!","tutor-pro"),"Please select a course","error")}),r("#select2_search_user_ajax").select2({allowClear:!0,minimumInputLength:1,placeholder:o("Search students","tutor-pro"),language:{inputTooShort:function(){return o("Please add 1 or more character","tutor")}},escapeMarkup:function(t){return t},ajax:{url:window._tutorobject.ajaxurl,type:"POST",dataType:"json",delay:1e3,data:function(t){return{term:t.term,action:"tutor_json_search_students"}},processResults:function(t){var o=[];return t&&r.each(t,function(t,e){o.push({id:t,text:e})}),{results:o}},cache:!0}})});