/**
 * Show a system prompt before redirecting to a URL.
 * Used for delete links/etc.
 * @param	text	The prompt, i.e. are you sure?
 * @param	url		The url to redirect to.
 */
function pmpro_askfirst(text, url) {
	var answer = window.confirm(text);

	if (answer) {
		window.location = url;
	}
}

/**
 * Deprecated in v2.1
 * In case add-ons/etc are expecting the non-prefixed version.
 */
if (typeof askfirst !== 'function') {
	function askfirst(text, url) {
		return pmpro_askfirst(text, url);
	}
}

/*
 * Toggle elements with a specific CSS class selector.
 * Used to hide/show sub settings when a main setting is enabled.
 * @since v2.1
 */
function pmpro_toggle_elements_by_selector(selector, checked) {
	if (checked === undefined) {
		jQuery(selector).toggle();
	} else if (checked) {
		jQuery(selector).show();
	} else {
		jQuery(selector).hide();
	}
}

/*
 * Find inputs with a custom attribute pmpro_toggle_trigger_for,
 * and bind change to toggle the specified elements.
 * @since v2.1
 */
jQuery(document).ready(function () {
	jQuery('input[pmpro_toggle_trigger_for]').on('change', function () {
		pmpro_toggle_elements_by_selector(jQuery(this).attr('pmpro_toggle_trigger_for'), jQuery(this).prop('checked'));
	});
});

// Admin Settings Code.
jQuery(document).ready(function () {
	pmpro_admin_prep_click_events();
});

// Function to prep click events for admin settings.
function pmpro_admin_prep_click_events() {
	/*
	 * Toggle content within the settings sections boxes.
	 * @since 2.9
	 */
	jQuery('button.pmpro_section-toggle-button').on('click', function (event) {
		event.preventDefault();

		let thebutton = jQuery(event.target).parents('.pmpro_section').find('button.pmpro_section-toggle-button');
		let buttonicon = thebutton.children('.dashicons');
		let section = thebutton.closest('.pmpro_section');
		let sectioninside = section.children('.pmpro_section_inside');

		//let visibility = container.data('visibility');
		//let activated = container.data('activated');
		if (buttonicon.hasClass('dashicons-arrow-down-alt2')) {
			// Section is not visible. Show it.
			jQuery(sectioninside).show();
			jQuery(buttonicon).removeClass('dashicons-arrow-down-alt2');
			jQuery(buttonicon).addClass('dashicons-arrow-up-alt2');
			jQuery(section).attr('data-visibility', 'shown');
			jQuery(thebutton).attr('aria-expanded', 'true');
		} else {
			// Section is visible. Hide it.
			jQuery(sectioninside).hide();
			jQuery(buttonicon).removeClass('dashicons-arrow-up-alt2');
			jQuery(buttonicon).addClass('dashicons-arrow-down-alt2');
			jQuery(section).attr('data-visibility', 'hidden');
			jQuery(thebutton).attr('aria-expanded', 'false');
		}
	});
}

// Hide the popup if clicked outside the popup.
jQuery(document).on('click', function (e) {
	// Check if the clicked element is the close button or outside the pmpro-popup-wrap
	if ( jQuery(e.target).closest('.pmpro-popup-wrap').length === 0 ) {
		jQuery('.pmpro-popup-overlay').hide();
	}
});

/** JQuery to hide the notifications. */
jQuery(document).ready(function () {
	jQuery(document).on('click', '.pmpro-notice-button.notice-dismiss', function () {
		var notification_id = jQuery(this).val();
		var nonce = jQuery(this).data('nonce');

		var postData = {
			action: 'pmpro_hide_notice',
			notification_id: notification_id,
			nonce: nonce
		}

		jQuery.ajax({
			type: "POST",
			data: postData,
			url: ajaxurl,
			success: function (response) {
				///console.log( notification_id );
				jQuery('#' + notification_id).hide();
			}
		})

	});
});

/* jQuery to hide the pause notification if the secondary button is pressed */
jQuery(document).ready(function () {
	jQuery('#hide_pause_notification_button').click(function () {
		jQuery('#hide_pause_notification .notice-dismiss').click();
	});
});

/*
 * Create Webhook button for Stripe on the payment settings page.
 */
jQuery(document).ready(function () {
	// Check that we are on payment settings page.
	if (!jQuery('#stripe_publishablekey').length || !jQuery('#stripe_secretkey').length || !jQuery('#pmpro_stripe_create_webhook').length) {
		return;
	}

	// Disable the webhook buttons if the API keys aren't complete yet.
	jQuery('#stripe_publishablekey,#stripe_secretkey').on('change keyup', function () {
		pmpro_stripe_check_api_keys();
	});
	pmpro_stripe_check_api_keys();

	// AJAX call to create webhook.
	jQuery('#pmpro_stripe_create_webhook').on('click', function (event) {
		event.preventDefault();

		var postData = {
			action: 'pmpro_stripe_create_webhook',
			secretkey: pmpro_stripe_get_secretkey(),
		}
		jQuery.ajax({
			type: "POST",
			data: postData,
			url: ajaxurl,
			success: function (response) {
				response = jQuery.parseJSON(response);
				///console.log( response );

				jQuery('#pmpro_stripe_webhook_notice').parent('div').removeClass('error')
				jQuery('#pmpro_stripe_webhook_notice').parent('div').removeClass('notice-success')

				if (response.notice) {
					jQuery('#pmpro_stripe_webhook_notice').parent('div').addClass(response.notice);
				}
				if (response.message) {
					jQuery('#pmpro_stripe_webhook_notice').html(response.message);
				}
				if (response.success) {
					jQuery('#pmpro_stripe_create_webhook').hide();
				}
			}
		})
	});

	// AJAX call to delete webhook.
	jQuery('#pmpro_stripe_delete_webhook').on('click', function (event) {
		event.preventDefault();

		var postData = {
			action: 'pmpro_stripe_delete_webhook',
			secretkey: pmpro_stripe_get_secretkey(),
		}

		jQuery.ajax({
			type: "POST",
			data: postData,
			url: ajaxurl,
			success: function (response) {
				response = jQuery.parseJSON(response);
				///console.log( response );

				jQuery('#pmpro_stripe_webhook_notice').parent('div').removeClass('error')
				jQuery('#pmpro_stripe_webhook_notice').parent('div').removeClass('notice-success')

				if (response.notice) {
					jQuery('#pmpro_stripe_webhook_notice').parent('div').addClass(response.notice);
				}
				if (response.message) {
					jQuery('#pmpro_stripe_webhook_notice').html(response.message);
				}
				if (response.success) {
					jQuery('#pmpro_stripe_create_webhook').show();
				}
			}
		})
	});

	// AJAX call to rebuild webhook.
	jQuery('#pmpro_stripe_rebuild_webhook').on('click', function (event) {
		event.preventDefault();

		var postData = {
			action: 'pmpro_stripe_rebuild_webhook',
			secretkey: pmpro_stripe_get_secretkey(),
		}

		jQuery.ajax({
			type: "POST",
			data: postData,
			url: ajaxurl,
			success: function (response) {
				response = jQuery.parseJSON(response);
				///console.log( response );

				jQuery('#pmpro_stripe_webhook_notice').parent('div').removeClass('error')
				jQuery('#pmpro_stripe_webhook_notice').parent('div').removeClass('notice-success')

				if (response.notice) {
					jQuery('#pmpro_stripe_webhook_notice').parent('div').addClass(response.notice);
				}
				if (response.message) {
					jQuery('#pmpro_stripe_webhook_notice').html(response.message);
				}
				if (response.success) {
					jQuery('#pmpro_stripe_create_webhook').hide();
				}
			}
		})
	});
});

// Disable the webhook buttons if the API keys aren't complete yet.
function pmpro_stripe_check_api_keys() {
	if ((jQuery('#stripe_publishablekey').val().length > 0 && jQuery('#stripe_secretkey').val().length > 0) || jQuery('#live_stripe_connect_secretkey').val().length > 0) {
		jQuery('#pmpro_stripe_create_webhook').removeClass('disabled');
		jQuery('#pmpro_stripe_create_webhook').addClass('button-secondary');
	} else {
		jQuery('#pmpro_stripe_create_webhook').removeClass('button-secondary');
		jQuery('#pmpro_stripe_create_webhook').addClass('disabled');
	}
}

// User Fields Code.
jQuery(document).ready(function () {
	pmpro_userfields_prep_click_events();
});

// Function to prep click events.
function pmpro_userfields_prep_click_events() {
	// Whenever we make a change, warn the user if they try to navigate away.
	function pmpro_userfields_made_a_change() {
		window.onbeforeunload = function () {
			return true;
		};
		jQuery('#pmpro_userfields_savesettings').prop("disabled", false);
	}

	// Add group button.
	jQuery('#pmpro_userfields_add_group').unbind('click').on('click', function (event) {
		jQuery('#pmpro_userfields_add_group').parent('p').before(pmpro.user_fields_blank_group);
		pmpro_userfields_prep_click_events();
		jQuery('#pmpro_userfields_add_group').parent('p').prev().find('input').focus().select();
		pmpro_userfields_made_a_change();
	});

	// Delete group button.
	jQuery('.pmpro_userfield-group-actions button[name=pmpro_userfields_delete_group]').unbind('click').on('click', function (event) {
		var thegroup = jQuery(this).closest('.pmpro_userfield-group');
		var thename = thegroup.find('input[name=pmpro_userfields_group_name]').val();
		var answer;
		if (thename.length > 0) {
			answer = window.confirm('Delete the "' + thename + '" group?');
		} else {
			answer = window.confirm('Delete this group?');
		}
		if (answer) {
			thegroup.remove();
			pmpro_userfields_made_a_change();
		}
	});

	// Add field button.
	jQuery('button[name="pmpro_userfields_add_field"]').unbind('click').on('click', function (event) {
		var thefields = jQuery(event.target).closest('div.pmpro_userfield-group-actions').siblings('div.pmpro_userfield-group-fields');
		thefields.append(pmpro.user_fields_blank_field);
		pmpro_userfields_prep_click_events();
		thefields.children().last().find('.edit-field').click();
		thefields.children().last().find('input[name="pmpro_userfields_field_label"]').focus().select();
		pmpro_userfields_made_a_change();
	});

	// Delete field button.
	jQuery('.pmpro_userfield-field-options a.delete-field, .pmpro_userfield-field-actions .is-destructive').unbind('click').on('click', function (event) {
		var thefield = jQuery(this).closest('.pmpro_userfield-group-field');
		var thelabel = thefield.find('input[name=pmpro_userfields_field_label]').val();
		var answer;
		if (thelabel.length > 0) {
			answer = window.confirm('Delete the "' + thelabel + '" field?');
		} else {
			answer = window.confirm('Delete this unlabeled field?');
		}
		if (answer) {
			thefield.remove();
			pmpro_userfields_made_a_change();
		}
	});

	// Toggle groups.
	jQuery('button.pmpro_userfield-group-buttons-button-toggle-group, div.pmpro_userfield-group-header h3').unbind('click').on('click', function (event) {
		event.preventDefault();

		// Ignore if the text field was clicked.
		if (jQuery(event.target).prop('nodeName') === 'INPUT') {
			return;
		}

		// Find the toggle button and open or close.
		let thebutton = jQuery(event.target).parents('.pmpro_userfield-group').find('button.pmpro_userfield-group-buttons-button-toggle-group');
		let buttonicon = thebutton.children('.dashicons');
		let groupheader = thebutton.closest('.pmpro_userfield-group-header');
		let groupinside = groupheader.siblings('.pmpro_userfield-inside');

		if (buttonicon.hasClass('dashicons-arrow-up')) {
			// closing
			buttonicon.removeClass('dashicons-arrow-up');
			buttonicon.addClass('dashicons-arrow-down');
			groupinside.slideUp();
		} else {
			// opening
			buttonicon.removeClass('dashicons-arrow-down');
			buttonicon.addClass('dashicons-arrow-up');
			groupinside.slideDown();
		}
	});

	// Move group up.
	jQuery('.pmpro_userfield-group-buttons-button-move-up').unbind('click').on('click', function (event) {
		var thegroup = jQuery(this).closest('.pmpro_userfield-group');
		var thegroupprev = thegroup.prev('.pmpro_userfield-group');
		if (thegroupprev.length > 0) {
			thegroup.insertBefore(thegroupprev);
			pmpro_userfields_made_a_change();
		}
	});

	// Move group down.
	jQuery('.pmpro_userfield-group-buttons-button-move-down').unbind('click').on('click', function (event) {
		var thegroup = jQuery(this).closest('.pmpro_userfield-group');
		var thegroupnext = thegroup.next('.pmpro_userfield-group');
		if (thegroupnext.length > 0) {
			thegroup.insertAfter(thegroupnext);
			pmpro_userfields_made_a_change();
		}
	});

	// Open field.
	jQuery('a.edit-field').unbind('click').on('click', function (event) {
		var fieldcontainer = jQuery(this).parents('.pmpro_userfield-group-field');
		var fieldsettings = fieldcontainer.children('.pmpro_userfield-field-settings');

		fieldcontainer.removeClass('pmpro_userfield-group-field-collapse');
		fieldcontainer.addClass('pmpro_userfield-group-field-expand');
		fieldsettings.find('select[name=pmpro_userfields_field_type]').change();
		fieldsettings.show();
	});

	// Close field.
	jQuery('button.pmpro_userfields_close_field').unbind('click').on('click', function (event) {
		event.preventDefault();
		var fieldcontainer = jQuery(this).parents('.pmpro_userfield-group-field');
		var fieldsettings = fieldcontainer.children('.pmpro_userfield-field-settings');
		var fieldheading = fieldsettings.prev();
		// Update label, name, and type.
		fieldheading.find('span.pmpro_userfield-label').html(fieldsettings.find('input[name=pmpro_userfields_field_label]').val().replace(/(<([^>]+)>)/gi, ''));
		fieldheading.find('li.pmpro_userfield-group-column-name').html(fieldsettings.find('input[name=pmpro_userfields_field_name]').val());
		fieldheading.find('li.pmpro_userfield-group-column-type').html(fieldsettings.find('select[name=pmpro_userfields_field_type]').val());

		// Toggle
		fieldcontainer.removeClass('pmpro_userfield-group-field-expand');
		fieldcontainer.addClass('pmpro_userfield-group-field-collapse');
		fieldsettings.hide();
	});

	// Move field up.
	jQuery('.pmpro_userfield-field-buttons-button-move-up').unbind('click').on('click', function (event) {
		var thefield = jQuery(this).closest('.pmpro_userfield-group-field');
		var thefieldprev = thefield.prev('.pmpro_userfield-group-field');
		if (thefieldprev.length > 0) {
			thefield.insertBefore(thefieldprev);
			pmpro_userfields_made_a_change();
		}
	});

	// Move field down.
	jQuery('.pmpro_userfield-field-buttons-button-move-down').unbind('click').on('click', function (event) {
		var thefield = jQuery(this).closest('.pmpro_userfield-group-field');
		var thefieldnext = thefield.next('.pmpro_userfield-group-field');
		if (thefieldnext.length > 0) {
			thefield.insertAfter(thefieldnext);
			pmpro_userfields_made_a_change();
		}
	});

	// Duplicate field.
	jQuery('a.duplicate-field').unbind('click').on('click', function (event) {
		var thefield = jQuery(this).closest('.pmpro_userfield-group-field');
		thefield.clone(true).insertAfter(thefield); // clone( true ) to clone event handlers.
		pmpro_userfields_made_a_change();
	});

	// Toggle required at checkout field settings based on group settings.
	jQuery('select[name="pmpro_userfields_group_checkout"]').unbind('change').on('change', function () {
		var groupContainer = jQuery(this).closest('.pmpro_userfield-inside');
		var fieldSettings = groupContainer.find('.pmpro_userfield-group-fields');
		var requiredFields = fieldSettings.find('#pmpro_userfield-field-setting_required');

		// Toggle visibility based on group setting.
		if (jQuery(this).val() === 'yes') {
			requiredFields.show();
		} else {
			requiredFields.hide();
		}
	}).trigger('change');

	// Toggle field settings based on type.
	jQuery('select[name=pmpro_userfields_field_type]').on('change', function (event) {
		var fieldcontainer = jQuery(this).parents('.pmpro_userfield-group-field');
		var fieldsettings = fieldcontainer.children('.pmpro_userfield-field-settings');
		var fieldtype = jQuery(this).val();

		var fieldoptions = fieldsettings.find('#pmpro_userfield-field-setting_options');
		var fieldfiles = fieldsettings.find('#pmpro_userfield-row-settings_files');
		var fielddefault = fieldsettings.find('#pmpro_userfield-field-setting_default');

		// Hide all the field settings.
		fieldoptions.hide();
		fieldfiles.hide();
		fielddefault.hide();

		// Show the option field if needed.
		var optiontypes = ['checkbox_grouped', 'radio', 'select', 'select2', 'multiselect'];
		if (jQuery.inArray(fieldtype, optiontypes) > -1) {
			fieldoptions.show();
		}

		// Show the file field options if needed.
		if (fieldtype === 'file') {
			fieldfiles.show();
		}

		// Show the default field if needed.
		var defaulttypes = ['text', 'textarea', 'checkbox', 'radio', 'select', 'date', 'readonly', 'hidden', 'number'];
		if (jQuery.inArray(fieldtype, defaulttypes) > -1) {
			fielddefault.show();
		}
	});

	// Suggest name after leaving label field.
	jQuery('input[name=pmpro_userfields_field_label]').on('focusout', function (event) {
		var fieldcontainer = jQuery(this).parents('.pmpro_userfield-group-field');
		var fieldsettings = fieldcontainer.children('.pmpro_userfield-field-settings');
		var fieldname = fieldsettings.find('input[name=pmpro_userfields_field_name]');
		if (!fieldname.val()) {
			fieldname.val(jQuery(this).val().toLowerCase().replace(/[^a-z0-9]/gi, '_').replace(/(^\_+|\_+$)/mg, ''));
		}
	});

	// If we change a field, mark it as changed.
	jQuery('.pmpro_userfield-group input, .pmpro_userfield-group textarea, .pmpro_userfield-group select').on('change', function (event) {
		pmpro_userfields_made_a_change();
	});

	// Save User Field Settings
	jQuery('#pmpro_userfields_savesettings').unbind('click').on('click', function (event) {
		///event.preventDefault();
		// We have saved, so we no longer need to warn user if they try to navigate away.
		window.onbeforeunload = null;

		let field_groups = [];
		let group_names = [];
		let default_group_name = 'More Information';

		jQuery('.pmpro_userfield-group').each(function (index, value) {
			let group_name = jQuery(this).find('input[name=pmpro_userfields_group_name]').val();

			// Make sure name is not blank.
			if (group_name.length === 0) {
				group_name = default_group_name;
			}
			// Make sure name is unique.
			let count = 1;
			while (group_names.includes(group_name)) {
				count++;
				group_name = group_name.replace(/\(0-9*\)/, '');
				group_name = group_name + ' (' + String(count) + ')';
			}
			group_names.push(group_name);

			let group_checkout = jQuery(this).find('select[name=pmpro_userfields_group_checkout]').val();
			let group_profile = jQuery(this).find('select[name=pmpro_userfields_group_profile]').val();
			let group_description = jQuery(this).find('textarea[name=pmpro_userfields_group_description]').val();

			// Get level ids.
			let group_levels = [];
			jQuery(this).find('input[name="pmpro_userfields_group_membership[]"]:checked').each(function () {
				group_levels.push(parseInt(jQuery(this).attr('id').replace('pmpro_userfields_group_membership_', '')));
			});

			// Get fields.
			let group_fields = [];
			jQuery(this).find('div.pmpro_userfield-group-fields div.pmpro_userfield-field-settings').each(function () {
				let field_label = jQuery(this).find('input[name=pmpro_userfields_field_label]').val();
				let field_name = jQuery(this).find('input[name=pmpro_userfields_field_name]').val();
				let field_type = jQuery(this).find('select[name=pmpro_userfields_field_type]').val();
				let field_required = jQuery(this).find('select[name=pmpro_userfields_field_required]').val();
				let field_readonly = jQuery(this).find('select[name=pmpro_userfields_field_readonly]').val();
				let field_profile = jQuery(this).find('select[name=pmpro_userfields_field_profile]').val();
				let field_wrapper_class = jQuery(this).find('input[name=pmpro_userfields_field_class]').val();
				let field_element_class = jQuery(this).find('input[name=pmpro_userfields_field_divclass]').val();
				let field_hint = jQuery(this).find('textarea[name=pmpro_userfields_field_hint]').val();
				let field_options = jQuery(this).find('textarea[name=pmpro_userfields_field_options]').val();
				let field_allowed_file_types = jQuery(this).find('input[name=pmpro_userfields_field_allowed_file_types]').val();
				let field_max_file_size = jQuery(this).find('input[name=pmpro_userfields_field_max_file_size]').val();
				let field_default = jQuery(this).find('input[name=pmpro_userfields_field_default]').val();

				// Get level ids.
				let field_levels = [];
				jQuery(this).find('input[name="pmpro_userfields_field_levels[]"]:checked').each(function () {
					field_levels.push(parseInt(jQuery(this).attr('id').replace('pmpro_userfields_field_levels_', '')));
				});

				let field = {
					'label': field_label,
					'name': field_name,
					'type': field_type,
					'required': field_required,
					'readonly': field_readonly,
					'levels': field_levels,
					'profile': field_profile,
					'wrapper_class': field_wrapper_class,
					'element_class': field_element_class,
					'hint': field_hint,
					'options': field_options,
					'allowed_file_types': field_allowed_file_types,
					'max_file_size': field_max_file_size,
					'default': field_default
				};

				// Add to array. (Only if it has a label or name.)
				if (field.label.length > 0 || field.name.length > 0) {
					group_fields.push(field);
				}
			});

			// Set up the field group object.
			let field_group = {
				'name': group_name,
				'checkout': group_checkout,
				'profile': group_profile,
				'description': group_description,
				'levels': group_levels,
				'fields': group_fields
			};

			// Add to array.
			field_groups.push(field_group);
		});

		// console.log( field_groups );
		jQuery('#pmpro_user_fields_settings').val(JSON.stringify(field_groups));

		return true;
	});
}

function pmpro_stripe_get_secretkey() {
	// We can't do the webhook calls with the Connect keys anyway,
	// so we just look for the legacy key here.
	if (jQuery('#stripe_secretkey').val().length > 0) {
		return jQuery('#stripe_secretkey').val();
	} else {
		return '';
	}
}

// EMAIL TEMPLATES.
jQuery(document).ready(function ($) {

	/* Variables */
	$template = $('#edit').val();

	$("#pmpro_submit_template_data").click(function () {
		pmpro_save_template()
	});

	$("#pmpro_reset_template_data").click(function () {
		pmpro_reset_template();
	});

	$("#pmpro_email_template_disable").click(function (e) {
		pmpro_disable_template();
	});

	$("#send_test_email").click(function (e) {
		pmpro_save_template().done(setTimeout(function () { pmpro_send_test_email(); }, '1000'));
	});

	function pmpro_save_template() {

		$("#pmpro_submit_template_data").attr("disabled", true);
		$(".status").hide();
		// console.log(template);

		$data = {
			template: $template,
			subject: $("#pmpro_email_template_subject").val(),
			body: $("#pmpro_email_template_body").val(),
			action: 'pmpro_email_templates_save_template_data',
			security: $('input[name=security]').val()
		};
		$.post(ajaxurl, $data, function (response) {
			if (response != 0) {
				$(".status_message_wrapper").addClass('updated');
			}
			else {
				$(".status_message_wrapper").addClass("error");
			}
			$("#pmpro_submit_template_data").attr("disabled", false);
			$(".status_message").html(response);
			$(".status_message").show();
			$('html, body').animate({ scrollTop : 0 }, 'fast');
		});

		return $.Deferred().resolve();
	}

	function pmpro_reset_template() {

		var r = confirm('Are you sure? Your current template settings will be deleted permanently.');

		if (!r) return false;

		$data = {
			template: $template,
			action: 'pmpro_email_templates_reset_template_data',
			security: $('input[name=security]').val()
		};
		$.post(ajaxurl, $data, function (response) {
			var template_data = $.parseJSON(response);
			$('#pmpro_email_template_subject').val(template_data['subject']);
			$('#pmpro_email_template_body').val(template_data['body']);
			$(".status_message_wrapper").addClass('updated');
			$(".status_message").html('Template Reset');
			$(".status_message").show();
			$('html, body').animate({ scrollTop : 0 }, 'fast');
		});

		return true;
	}

	function pmpro_disable_template() {

		//update wp_options
		data = {
			template: $template,
			action: 'pmpro_email_templates_disable_template',
			disabled: $("#pmpro_email_template_disable").is(":checked"),
			security: $('input[name=security]').val()
		};

		$.post(ajaxurl, data, function (response) {

			response = JSON.parse(response);

			//failure
			if (response['result'] == false) {
				$(".status_message_wrapper").addClass("error");
				$(".status_message").show().text("There was an error updating your template settings.");
			}
			else {
				if (response['status'] == 'true') {
					$(".status_message_wrapper").addClass("updated");
					$(".status_message").show().text("Template Disabled");
					$(".pmpro_tag-success").addClass("pmpro_tag-alert");
					$(".pmpro_tag-success").removeClass("pmpro_tag-success");
					$(".pmpro_tag-alert").text("Disabled");
				}
				else {
					$(".status_message_wrapper").addClass("updated");
					$(".status_message").show().text("Template Enabled");
					$(".pmpro_tag-alert").addClass("pmpro_tag-success");
					$(".pmpro_tag-alert").removeClass("pmpro_tag-alert");
					$(".pmpro_tag-success").text("Enabled");
				}
			}

			$('html, body').animate({ scrollTop : 0 }, 'fast');

			disabled = response['status'];

			pmpro_toggle_form_disabled(disabled);
		});
	}

	function pmpro_send_test_email() {

		data = {
			template: $template,
			email: $("#test_email_address").val(),
			action: 'pmpro_email_templates_send_test',
			security: $('input[name=security]').val()
		};

		$.post(ajaxurl, data, function (success) {

			if (success) {
				$(".status_message_wrapper").addClass("updated").removeClass("error");
				$(".status_message").show().text("Test email sent successfully.");
			}
			else {
				$(".status_message_wrapper").addClass("error").removeClass("updated");
				$(".status_message").show().text("Test email failed.");
			}

		})
	}

	function pmpro_toggle_form_disabled(disabled) {
		if (disabled == 'true') {
			$("#pmpro_email_template_disable").prop('checked', true);
			$("#pmpro_email_template_body").attr('readonly', 'readonly').attr('disabled', 'disabled');
			$("#pmpro_email_template_subject").attr('readonly', 'readonly').attr('disabled', 'disabled');
		}
		else {
			$("#pmpro_email_template_disable").prop('checked', false);
			$("#pmpro_email_template_body").removeAttr('readonly', 'readonly').removeAttr('disabled', 'disabled');
			$("#pmpro_email_template_subject").removeAttr('readonly', 'readonly').removeAttr('disabled', 'disabled');
		}

	}

});

// Design Settings.
jQuery(document).ready(function () {
	// Preview color changes by updating the #pmpro_global_style_colors inline styles.
	jQuery('.pmpro_color_picker').on('change', function () {
		var baseColor = jQuery('#pmpro_base_color').val();
		var contrastColor = jQuery('#pmpro_contrast_color').val();
		var accentColor = jQuery('#pmpro_accent_color').val();

		jQuery('#pmpro_global_style_colors').html(':root { --pmpro--color--base: ' + baseColor + '; --pmpro--color--contrast: ' + contrastColor + '; --pmpro--color--accent: ' + accentColor + '; }');
	});
});

// Add Ons Page Code.
jQuery(document).ready(function () {

	// If we're on the add ons admin page
	if (jQuery('#pmpro-admin-add-ons-list').length) {

		// Scoped helper object for Add Ons page (keeps readability while avoiding duplication).
		var pmproAddOnsHelpers = {
			progressText: {
				'activate': 'Activating...',
				'install': 'Installing...',
				'update': 'Updating...',
				'deactivate': 'Deactivating...',
				'delete': 'Deleting...'
			},
			failText: {
				'activate': 'Could Not Activate.',
				'install': 'Could Not Install.',
				'update': 'Could Not Update.',
				'deactivate': 'Could Not Deactivate.',
				'delete': 'Could Not Delete.'
			},
			recountFilters: function(){
				var activeCount = jQuery('.add-on-container.add-on-active').length;
				var inactiveCount = jQuery('.add-on-container.add-on-inactive').length;
				var updateCount = jQuery('.add-on-container.add-on-needs-update').length;
					function setCount($link, count, hide){
					if(!$link.length){return;}
					var baseLabel = $link.data('baseLabel');
					if(!baseLabel){
						baseLabel = jQuery.trim($link.text()).replace(/\s*\(\d+\)$/,'');
						$link.data('baseLabel', baseLabel);
					}
					$link.text(count>0? baseLabel+' ('+count+')': baseLabel);
						if(hide){ $link.closest('li').toggle(count>0); }
				}
					// Hide tabs whose count is zero for all three: active, inactive, update.
					setCount(jQuery('.filter-links a[data-view="active"]'), activeCount, true);
					setCount(jQuery('.filter-links a[data-view="inactive"]'), inactiveCount, true);
					setCount(jQuery('.filter-links a[data-view="update"]'), updateCount, true);
					// If current link becomes hidden, switch to All.
					var $currentLink = jQuery('.filter-links a.current');
					if ($currentLink.length && !$currentLink.closest('li').is(':visible')) {
						jQuery('.filter-links a[data-view="all"]').trigger('click');
					} else if (window.location.hash) {
						var hashView = window.location.hash.replace('#','');
						var $hashLink = jQuery('.filter-links a[data-view="' + hashView + '"]');
						if ($hashLink.length && !$hashLink.closest('li').is(':visible')) {
							jQuery('.filter-links a[data-view="all"]').trigger('click');
						}
					}
			},
			applyCurrentFilter: function(){
				var $current = jQuery('.filter-links a.current');
				if($current.length){ $current.trigger('click'); }
			},
			// Ensure the actions dropdown exists and reflects current status (installed states only).
			buildOrUpdateMenu: function(container, status, plugin_file, nonce){
				var $details = container.find('> .add-on-item > .details');
				if(!$details.length){ return; }
				var $btn = $details.children('.dropdown-arrow');
				var $menu = $details.children('.pmpro-add-on-actions-menu');
				// Only show dropdown for installed plugins (active/inactive).
				if(status !== 'active' && status !== 'inactive'){
					$btn.remove();
					$menu.remove();
					return;
				}
				if(!$btn.length){
					$btn = jQuery(
						'<button type="button" class="dropdown-arrow" aria-haspopup="true" aria-expanded="false">\n'
						+ '\t<span class="screen-reader-text">Toggle actions menu</span>\n'
						+ '\t<span class="dashicons dashicons-ellipsis"></span>\n'
						+ '</button>'
					);
					$details.prepend($btn);
				}
				if(!$menu.length){
					$menu = jQuery(
						'<div class="pmpro-add-on-actions-menu" role="menu" aria-hidden="true">\n'
						+ '\t<ul></ul>\n'
						+ '</div>'
					);
					$btn.after($menu);
				}
				// Build menu items
				var $ul = $menu.children('ul');
				$ul.empty();
				// First item: Activate or Deactivate
				var firstAction = (status === 'inactive') ? 'activate' : 'deactivate';
				var firstLabel = (status === 'inactive') ? 'Activate' : 'Deactivate';
				$ul.append(
					'<li>'
					+ '\t<button type="button" role="menuitem" class="pmproAddOnActionButton action-' + firstAction + '">' + firstLabel + '</button>'
					+ '\t<input type="hidden" name="pmproAddOnAdminAction" value="' + firstAction + '" />'
					+ '\t<input type="hidden" name="pmproAddOnAdminTarget" value="' + plugin_file + '" />'
					+ '\t<input type="hidden" name="pmproAddOnAdminNonce" value="' + nonce + '" />'
					+ '</li>'
				);
				// Divider + Uninstall
				$ul.append(
					'<li>'
					+ '\t<button type="button" role="menuitem" class="pmproAddOnActionButton action-uninstall is-destructive">Uninstall</button>'
					+ '\t<input type="hidden" name="pmproAddOnAdminAction" value="delete" />'
					+ '\t<input type="hidden" name="pmproAddOnAdminTarget" value="' + plugin_file + '" />'
					+ '\t<input type="hidden" name="pmproAddOnAdminNonce" value="' + nonce + '" />'
					+ '</li>'
				);
			}
		};

		// Hide the license banner.
		jQuery('.pmproPopupCloseButton, .pmproPopupCompleteButton').click(function (e) {
			e.preventDefault();
			jQuery('.pmpro-popup-overlay').hide();
		});

		// Hide the popup banner if "ESC" is pressed.
		jQuery(document).keyup(function (e) {
			if (e.key === 'Escape') {
				jQuery('.pmpro-popup-overlay').hide();
			}
		});

		jQuery('#pmpro-admin-add-ons-list').on('click', '.pmproAddOnActionButton', function (e) {
			e.preventDefault();

			var button = jQuery(this);
			var container = button.closest('.add-on-container');

			// Make sure we only run once.
			if (button.hasClass('disabled')) {
				return;
			}
			button.addClass('disabled');

			// Pull the action that we are performing on this button.
			var action = button.siblings('input[name="pmproAddOnAdminAction"]').val();

			if ('license' === action) {
				// Get the add on name and the user's current license type and show banner.
				document.getElementById('addon-name').innerHTML = button.siblings('input[name="pmproAddOnAdminName"]').val();
				document.getElementById('addon-license').innerHTML = button.siblings('input[name="pmproAddOnAdminLicense"]').val();
				jQuery('.pmpro-popup-overlay').show();
				button.removeClass('disabled');
				return false;
			}

			// Remove checkmark
			button.removeClass('checkmarked');

			// Update button text (progress state)
			if (!pmproAddOnsHelpers.progressText[action]) {
				button.removeClass('disabled');
				return;
			}
			button.text(pmproAddOnsHelpers.progressText[action]);

			// Build AJAX payload for new class endpoints
			var target = button.siblings('input[name="pmproAddOnAdminTarget"]').val();
			var nonce = button.siblings('input[name="pmproAddOnAdminNonce"]').val();
			var ajaxAction = null;
			if (action === 'install') ajaxAction = 'pmpro_addon_install';
			if (action === 'update') ajaxAction = 'pmpro_addon_update';
			if (action === 'activate') ajaxAction = 'pmpro_addon_activate';
			if (action === 'deactivate') ajaxAction = 'pmpro_addon_deactivate';
			if (action === 'delete') ajaxAction = 'pmpro_addon_delete';

			// Send AJAX request
			jQuery.post(ajaxurl, {
				action: ajaxAction,
				nonce: nonce,
				target: target,
				slug: (action === 'install' ? target : ''),
			}).done(function(resp){
				if (!resp || !resp.success) {
					var msg = (resp && resp.data && resp.data.message) ? resp.data.message : 'Action failed.';
					// Filesystem credentials needed
					if (resp && resp.data && resp.data.code === 'pmpro_fs_credentials') {
						button.text('Credentials required...');
						if (window.wp && wp.updates && wp.updates.maybeRequestFilesystemCredentials) {
							wp.updates.maybeRequestFilesystemCredentials({});
						}
					} else {
						button.text(msg);
					}
					button.removeClass('disabled');
					return;
				}

				var plugin_file = (resp && resp.data && resp.data.plugin_file) ? resp.data.plugin_file : null;
				var slug = container.attr('id');
				if(!plugin_file && slug){ plugin_file = slug + '/' + slug + '.php'; }
				var mainButton = container.find('.action-button > .pmproAddOnActionButton');
				var mainActionInput = mainButton.siblings('input[name="pmproAddOnAdminAction"]');
				var mainTargetInput = mainButton.siblings('input[name="pmproAddOnAdminTarget"]');
				var statusLabel = container.find('.add-on-status strong');
				var dropdownBtn = container.find('.details > .dropdown-arrow');
				var dropdownMenu = container.find('.pmpro-add-on-actions-menu');
				var firstMenuBtn = dropdownMenu.find('button.pmproAddOnActionButton').first();
				var firstMenuActionInput = firstMenuBtn.siblings('input[name="pmproAddOnAdminAction"]');

				function setStatus(newStatus, statusText){
					container.removeClass('add-on-active add-on-inactive add-on-uninstalled');
					if(newStatus){ container.addClass('add-on-' + newStatus); }
					if(statusLabel.length){
						statusLabel.removeClass(function(i,c){ return (c.match(/status-\S+/g)||[]).join(' '); });
						statusLabel.attr('class','status-' + newStatus).text(statusText);
					}
					// Update data-search-view tokens
					var viewAttr = container.data('search-view');
					if(viewAttr){
						var parts = viewAttr.split(' ').filter(function(p){ return ['active','inactive','update'].indexOf(p) === -1; });
						if(newStatus === 'active') parts.push('active');
						if(newStatus === 'inactive') parts.push('inactive');
						if(container.hasClass('add-on-needs-update')) parts.push('update');
						var newView = parts.filter(function(v,i,a){ return a.indexOf(v)===i; }).join(' ');
						container.attr('data-search-view', newView);
						container.data('search-view', newView);
					}
				}

				// Handle each action result
				if (action === 'activate') {
					button.text('Activated').addClass('checkmarked');
					// Main button shows Active (disabled)
					if(mainButton.length){
						mainButton.text('Active').addClass('disabled').attr('disabled','disabled').attr('aria-disabled','true');
						mainActionInput.val('');
						mainTargetInput.val(plugin_file);
					}
					// Update dropdown first item to Deactivate
					if(firstMenuBtn.length){
						firstMenuBtn.text('Deactivate').removeClass('action-activate').addClass('action-deactivate');
						firstMenuActionInput.val('deactivate');
					}
					setStatus('active', 'Active');
				} else if (action === 'deactivate') {
					button.text('Deactivated');
					if(mainButton.length){
						mainButton.text('Activate').removeClass('disabled').removeAttr('disabled').attr('aria-disabled','false').removeClass('checkmarked');
						mainActionInput.val('activate');
						mainTargetInput.val(plugin_file);
					}
					if(firstMenuBtn.length){
						firstMenuBtn.text('Activate').removeClass('action-deactivate').addClass('action-activate');
						firstMenuActionInput.val('activate');
					}
					setStatus('inactive', 'Inactive');
				} else if (action === 'install') {
					button.text('Installed');
					// After install the plugin is inactive.
					if(mainButton.length){
						setTimeout(function(){
							mainButton.text('Activate').removeClass('disabled checkmarked');
							mainActionInput.val('activate');
							mainTargetInput.val(plugin_file);
						}, 600);
					}
					// Ensure actions menu exists/updated now that it's installed.
					pmproAddOnsHelpers.buildOrUpdateMenu(container, 'inactive', plugin_file, nonce);
					setStatus('inactive', 'Inactive');
				} else if (action === 'update') {
					button.text('Updated').addClass('checkmarked');
					container.removeClass('add-on-needs-update');
					// If active keep Active label, else ensure main button is Activate
					if(mainButton.length){
						if(container.hasClass('add-on-active')){
							mainButton.text('Active').addClass('disabled').attr('disabled','disabled').attr('aria-disabled','true');
						}else{
							mainButton.text('Activate').removeClass('disabled');
							mainActionInput.val('activate');
							mainTargetInput.val(plugin_file);
						}
					}
					// Update search view attribute removing update token
					var viewAttr2 = container.data('search-view');
					if(viewAttr2){
						var parts2 = viewAttr2.split(' ').filter(function(p){ return p !== 'update'; });
						container.attr('data-search-view', parts2.join(' '));
						container.data('search-view', parts2.join(' '));
					}
				} else if (action === 'delete') {
					button.text('Deleted').removeClass('checkmarked');
					// Switch to uninstalled state
					setStatus('uninstalled', 'Not Installed');
					if(mainButton.length){
						setTimeout(function(){
							mainButton.text('Install').removeClass('disabled checkmarked').removeAttr('disabled').attr('aria-disabled','false');
							mainActionInput.val('install');
							mainTargetInput.val(slug); // target is slug for install
						}, 600);
					}
					// Remove dropdown
					dropdownBtn.remove();
					dropdownMenu.remove();
				}

				pmproAddOnsHelpers.recountFilters();
				pmproAddOnsHelpers.applyCurrentFilter();

				// Re-enable after success for possible further actions (except transient states already swapped)
				setTimeout(function(){
					button.removeClass('disabled');
				}, 400);
			}).fail(function(){
				button.text(pmproAddOnsHelpers.failText[action] || 'Action failed.');
				button.removeClass('disabled').removeClass('checkmarked');
			});
		});
	}
});

// Add On Action Dropdown (toggle + accessibility)
jQuery(document).ready(function(){
		// If we're on the add ons admin page
	if (jQuery('#pmpro-admin-add-ons-list').length) {
	
		var $doc = jQuery(document);

		function closeAllAddonMenus(except){
			jQuery('#pmpro-admin-add-ons-list .add-on-item .dropdown-arrow').attr('aria-expanded','false');
			jQuery('#pmpro-admin-add-ons-list .add-on-item .pmpro-add-on-actions-menu').attr('aria-hidden','true');
			jQuery('#pmpro-admin-add-ons-list .add-on-item').removeClass('is-open');
			if (except) {
				except.attr('aria-expanded','true')
					.closest('.add-on-item').addClass('is-open')
					.find('.pmpro-add-on-actions-menu').attr('aria-hidden','false');
			}
		}

		$doc.on('click', '#pmpro-admin-add-ons-list .add-on-item .dropdown-arrow', function(e){
			e.preventDefault();
			var $btn = jQuery(this);
			var isOpen = $btn.attr('aria-expanded') === 'true';
			if (isOpen) {
				closeAllAddonMenus();
			} else {
				closeAllAddonMenus($btn);
				// Focus first actionable item for keyboard users
				setTimeout(function(){
					var $first = $btn.next('.pmpro-add-on-actions-menu').find('button:not([disabled])').first();
					if ($first.length) { $first.trigger('focus'); }
				}, 15);
			}
		});

		// Keyboard navigation inside menu
		$doc.on('keydown', '#pmpro-admin-add-ons-list .pmpro-add-on-actions-menu', function(e){
			var $items = jQuery(this).find('button:not([disabled])');
			if (!$items.length) { return; }
			var idx = $items.index(document.activeElement);
			if (e.key === 'ArrowDown') { e.preventDefault(); idx = (idx + 1) % $items.length; $items.eq(idx).focus(); }
			else if (e.key === 'ArrowUp') { e.preventDefault(); idx = (idx - 1 + $items.length) % $items.length; $items.eq(idx).focus(); }
			else if (e.key === 'Home') { e.preventDefault(); $items.eq(0).focus(); }
			else if (e.key === 'End') { e.preventDefault(); $items.eq($items.length - 1).focus(); }
			else if (e.key === 'Escape') { e.preventDefault(); closeAllAddonMenus(); }
		});

		// Close when clicking outside
		$doc.on('click', function(e){
			if (!jQuery(e.target).closest('#pmpro-admin-add-ons-list .add-on-item .pmpro-add-on-actions-menu, #pmpro-admin-add-ons-list .add-on-item .dropdown-arrow').length) {
				closeAllAddonMenus();
			}
		});

		// Close on Escape from anywhere
		$doc.on('keyup', function(e){ if (e.key === 'Escape') { closeAllAddonMenus(); } });
	}
});

/**
 * Add/Edit Member Page
 */
window.addEventListener("DOMContentLoaded", () => {
	const tabs = document.querySelectorAll('#pmpro-edit-user-div [role="tab"]');
	const tabList = document.querySelector('#pmpro-edit-user-div [role="tablist"]');
	const inputs = document.querySelectorAll('#pmpro-edit-user-div input, #pmpro-edit-user-div textarea, #pmpro-edit-user-div select');

	if ( tabs && tabList ) {
		// Track whether an input has been changed.
		let inputChanged = false;
		inputs.forEach((input) => {
			input.addEventListener('change', function(e) {
				inputChanged = true;
			});
		});

		// Add a click event handler to each tab
		tabs.forEach((tab) => {
			tab.addEventListener("click", function (e) {
				if ( pmpro_changeTabs(e, inputChanged ) ) {
					// If we changed tabs, reset the inputChanged flag.
					inputChanged = false;

					// Hide the PMPro message.
					const pmproMessage = document.querySelector('#pmpro_message');
					if ( pmproMessage ) {
						pmproMessage.style.display = 'none';
					}
				}
			});
		});

		// Enable arrow navigation between tabs in the tab list
		let tabFocus = 0;
		tabList.addEventListener("keydown", (e) => {
		// Move Down
		if (e.key === "ArrowDown" || e.key === "ArrowUp") {
			tabs[tabFocus].setAttribute("tabindex", -1);
			if (e.key === "ArrowDown") {
			tabFocus++;
			// If we're at the end, go to the start
			if (tabFocus >= tabs.length) {
				tabFocus = 0;
			}
			// Move Up
			} else if (e.key === "ArrowUp") {
			tabFocus--;
			// If we're at the start, move to the end
			if (tabFocus < 0) {
				tabFocus = tabs.length - 1;
			}
			}

			tabs[tabFocus].setAttribute("tabindex", 0);
			tabs[tabFocus].focus();
		}
		});

		// Enable the button to show more tabs.
		document.addEventListener('click', function(e) {
			const moreTabsToggle = e.target.closest('[role="showmore"]');
			if (moreTabsToggle) {
				e.preventDefault();
				const parent = moreTabsToggle.parentNode;
				const grandparent = parent.parentNode;
				grandparent.querySelectorAll('[role="tab"]').forEach((t) => t.style.display = 'block');
				parent.style.display = 'none';
			}
		});

		// If the visible panel's corresponding tab is hidden, show all tabs.
		const visiblePanel = document.querySelector('#pmpro-edit-user-div [role="tabpanel"]:not([hidden])');
		if ( visiblePanel ) {
			const visibleTab = document.querySelector(`[aria-controls="${visiblePanel.id}"]`);
			if ( visibleTab.style.display === 'none' ) {
				const moreTabsToggle = document.querySelector('[role="showmore"]');
				moreTabsToggle.click();
			}
		}

	}

});

function pmpro_changeTabs( e, inputChanged ) {
	e.preventDefault();

	if ( inputChanged ) {
		const answer = window.confirm('You have unsaved changes. Are you sure you want to switch tabs?');
		if ( ! answer ) {
			return false;
		}
	}

	const target = e.target;
	const parent = target.parentNode;
	const grandparent = parent.parentNode;

	// Remove all current selected tabs
	parent
	.querySelectorAll('[aria-selected="true"]')
	.forEach((t) => t.setAttribute("aria-selected", false));

	// Set this tab as selected
	target.setAttribute("aria-selected", true);

	// Hide all tab panels
	grandparent
	.querySelectorAll('[role="tabpanel"]')
	.forEach((p) => p.setAttribute("hidden", true));

	// Show the selected panel
	grandparent.parentNode
	.querySelector(`#${target.getAttribute("aria-controls")}`)
	.removeAttribute("hidden");

	// Update the URL to include the panel URL in the pmpro_member_edit_panel attribute.
	const fullPanelName = target.getAttribute('aria-controls');
	// Need to convert pmpro-member-edit-xyz-panel to xyz.
	const panelSlug = fullPanelName.replace(/^pmpro-member-edit-/, '').replace(/-panel$/, '');
	const url = new URL(window.location.href);
	url.searchParams.set('pmpro_member_edit_panel', panelSlug);
	window.history.pushState({}, '', url);

	return true;
}

/**
 * Edit Order Page
 */
jQuery(document).ready(function () {
	jQuery('.pmpro_admin-pmpro-orders select#membership_id').select2();
});