(function ($) {
	$(document).ready(
		function () {
			var selector = 'a[href*="plugins.php?action=deactivate"][href*="' + wawpDeactData.deactPattern + '"]';
			var originalHref = null;
			var $backdrop = $("<div class='wawp-deact-backdrop'></div>").appendTo("body");
			var modalHtml = 
				"<div class='wawp-deact-modal' role='dialog' aria-modal='true'>" +
					"<div class='wawp-deact-card'>" +
						"<div class='hd'>" + (wawpDeactData.title || "Quick feedback before deactivating Wawp?") + "</div>" +
						"<div class='bd'><p class='description'>" + (wawpDeactData.desc || "Choose a reason (optional) — it really helps us improve.") + "</p>" +
							"<label class='opt'><input type='radio' name='wawp_reason' value='I am just testing/temporary'> " + (wawpDeactData.opt_test || "I am just testing / temporary") + "</label>" +
							"<label class='opt'><input type='radio' name='wawp_reason' value='Found a bug / not working'> " + (wawpDeactData.opt_bug || "Found a bug / not working") + "</label>" +
							"<label class='opt'><input type='radio' name='wawp_reason' value='Missing a feature I need'> " + (wawpDeactData.opt_feature || "Missing a feature I need") + "</label>" +
							"<label class='opt'><input type='radio' name='wawp_reason' value='Too complex / hard to use'> " + (wawpDeactData.opt_complex || "Too complex / hard to use") + "</label>" +
							"<label class='opt'><input type='radio' name='wawp_reason' value='Switched to another plugin'> " + (wawpDeactData.opt_switch || "Switched to another plugin") + "</label>" +
							"<label class='opt'><input type='radio' name='wawp_reason' value='other'> " + (wawpDeactData.opt_other || "Other") + "</label>" +
							"<div class='opt-other' style='display:none'>" +
								"<input type='text' class='regular-text' id='wawp_other_text' placeholder='" + (wawpDeactData.placeholder || "Type your reason...") + "' />" +
							"</div>" +
						"</div>" +
						"<div class='ft'>" +
							"<button type='button' class='button button-secondary' id='wawp_cancel'>" + (wawpDeactData.btn_cancel || "Cancel") + "</button>" +
							"<button type='button' class='button' id='wawp_skip'>" + (wawpDeactData.btn_skip || "Deactivate without feedback") + "</button>" +
							"<button type='button' class='button button-primary' id='wawp_submit'>" + (wawpDeactData.btn_submit || "Submit & Deactivate") + "</button>" +
						"</div>" +
					"</div>" +
				"</div>";
			var $modal = $(modalHtml).appendTo("body");

			$(document).on(
				"click",
				selector,
				function (e) {
					e.preventDefault();
					originalHref = this.href;
					$backdrop.show();
					$modal.css("display", "flex");
				}
			);

			$modal.on(
				"change",
				"input[name=wawp_reason]",
				function () {
					if (this.value === "other") {
						$(".opt-other", $modal).slideDown(120);
					} else {
						$(".opt-other", $modal).slideUp(120);
					}
				}
			);

			$("#wawp_cancel").on(
				"click",
				function () {
					$modal.hide();
					$backdrop.hide();
					originalHref = null;
				}
			);

			$("#wawp_skip").on(
				"click",
				function () {
					if (originalHref) {
						window.location = originalHref;
					}
				}
			);

			$("#wawp_submit").on(
				"click",
				function () {
					var val = $modal.find("input[name=wawp_reason]:checked").val() || "";
					var other = (val === "other") ? $("#wawp_other_text").val() : "";
					$.post(
						ajaxurl,
						{
							action: "wawp_save_deactivation_reason",
							_wpnonce: wawpDeactData.nonce,
							reason: val,
							other_text: other
						}
					).always(
						function () {
							if (originalHref) {
								window.location = originalHref;
							}
						}
					);
				}
			);
		}
	);
})(jQuery);
