!function(e){"use strict";var a="open",t={modal:null,form:null,deactivateLink:null,isProcessing:!1,deactivateLinkHref:null,init:function(){"undefined"!=typeof MetFormDeactivation&&!1===MetFormDeactivation.show_modal||(this.cacheElements(),this.bindEvents(),this.syncFeedbackState())},cacheElements:function(){this.modal=e("#mf-deactivation-modal"),this.form=e("#mf-deactivation-form"),this.deactivateLink=e('#deactivate-metform, tr[data-slug="metform"] .deactivate a, a[href*="plugin=metform/metform.php"][href*="deactivate"], a[href*="metform.php"][href*="deactivate"]')},bindEvents:function(){var t=this;this.deactivateLink.length&&this.deactivateLink.on("click.mfModal",function(a){a.preventDefault(),t.deactivateLinkHref=e(this).attr("href"),e(".mf-deactivation-skip").data("deactivate-link",t.deactivateLinkHref),t.openModal()}),this.form.on("submit.mfModal",function(e){e.preventDefault(),t.submitForm()}),e(".mf-deactivation-close").on("click.mfModal",function(){t.closeModal()}),e(".mf-deactivation-skip").on("click.mfModal",function(){var a=e(this).data("deactivate-link");a&&(window.location.href=a)}),this.form.on("change.mfModal",'input[name="reason"]',function(){t.hideAllFeedbacks(),t.showFeedbackForReason(e(this).closest(".mf-deactivation-radio-item").data("reason-key"))}),this.modal.on("click.mfModal",function(a){e(a.target).is(t.modal)&&t.closeModal()}),e(document).on("keydown.mfModal",function(e){"Escape"===e.key&&t.modal.hasClass(a)&&t.closeModal()})},openModal:function(){this.modal.addClass(a),e("body").css("overflow","hidden"),this.hideErrorMessage(),this.syncFeedbackState()},closeModal:function(){this.modal.removeClass(a),e("body").css("overflow",""),this.form[0].reset(),this.hideErrorMessage(),this.hideAllFeedbacks()},syncFeedbackState:function(){var e=this.form.find('input[name="reason"]:checked');e.length&&(this.hideAllFeedbacks(),this.showFeedbackForReason(e.closest(".mf-deactivation-radio-item").data("reason-key")))},submitForm:function(){var a=this;if(!this.isProcessing){var t=this.form.find('input[name="reason"]:checked'),i=t.val();if(i){var o=t.closest(".mf-deactivation-radio-item"),n=o.data("reason-key"),s={action:"metform_deactivation_feedback",metform_nonce:this.form.find('input[name="metform_nonce"]').val(),reason:i,reason_key:o.find(".mf-reason-key").val()||"",reason_label:o.find(".mf-reason-label").val()||i,feedback:o.find('textarea[name="feedback_'+n+'"]').val()||""};this.setProcessing(!0),e.ajax({type:"POST",url:MetFormDeactivation.ajaxurl,data:s,success:function(){},error:function(e,a,t){window.console.error("MetForm: deactivation feedback error —",t)},complete:function(){a.setProcessing(!1);var t=e(".mf-deactivation-skip").data("deactivate-link");t&&(window.location.href=t)}})}else this.showErrorMessage("Please select a reason first.")}},hideAllFeedbacks:function(){e(".mf-deactivation-radio-feedback").removeClass("active")},showFeedbackForReason:function(a){a&&e('.mf-deactivation-radio-item[data-reason-key="'+a+'"] .mf-deactivation-radio-feedback').addClass("active")},showErrorMessage:function(a){e("#mf-deactivation-error-message").html(a).slideDown(300)},hideErrorMessage:function(){var a=e("#mf-deactivation-error-message");a.slideUp(300,function(){a.html("")})},setProcessing:function(a){this.isProcessing=a,e(".mf-deactivation-submit").prop("disabled",a).toggleClass("loading",a)}};e(document).ready(function(){t.init()}),window.MetFormDeactivationModal=t}(jQuery);