/**
 * Waitlytics — Admin JavaScript
 *
 * @package Waitlytics
 */
(function($) {
	'use strict';

	var WTLY_Admin = {

		init: function() {
			this.bindEvents();
		},

		bindEvents: function() {
			$(document).on('click', '.wtly-delete-subscriber', this.deleteSubscriber);
			$(document).on('change', '.wtly-bulk-action-select', this.handleBulkAction);
			$(document).on('click', '.wtly-tab-link', this.switchTab);
		},

		/**
		 * Delete a single subscriber.
		 */
		deleteSubscriber: function(e) {
			e.preventDefault();

			if (!window.confirm(wtlyData.i18n.confirm)) {
				return;
			}

			var $btn = $(this);
			var $row = $btn.closest('tr');
			var subscriberId = $btn.data('id');

			$btn.prop('disabled', true);

			$.ajax({
				url: wtlyData.restUrl + 'subscribers/' + subscriberId,
				type: 'DELETE',
				beforeSend: function(xhr) {
					xhr.setRequestHeader('X-WP-Nonce', wtlyData.nonce);
				},
				success: function(response) {
					if (response.success) {
						$row.fadeOut(300, function() {
							$(this).remove();
						});
					} else {
						WTLY_Admin.showNotice(wtlyData.i18n.error, 'error');
						$btn.prop('disabled', false);
					}
				},
				error: function() {
					WTLY_Admin.showNotice(wtlyData.i18n.error, 'error');
					$btn.prop('disabled', false);
				}
			});
		},

		/**
		 * Handle bulk action selection.
		 */
		handleBulkAction: function() {
			var action = $(this).val();

			if (!action) {
				return;
			}

			var $checked = $('.wtly-subscriber-table input[type="checkbox"]:checked');
			if (0 === $checked.length) {
				return;
			}

			if ('delete' === action) {
				if (!window.confirm(wtlyData.i18n.confirm)) {
					$(this).val('');
					return;
				}

				$checked.each(function() {
					var $row = $(this).closest('tr');
					var subscriberId = $row.data('subscriber-id');

					$.ajax({
						url: wtlyData.restUrl + 'subscribers/' + subscriberId,
						type: 'DELETE',
						beforeSend: function(xhr) {
							xhr.setRequestHeader('X-WP-Nonce', wtlyData.nonce);
						},
						success: function() {
							$row.fadeOut(300, function() {
								$(this).remove();
							});
						}
					});
				});
			}

			$(this).val('');
		},

		/**
		 * Tab switching (for settings sub-tabs if needed).
		 */
		switchTab: function(e) {
			e.preventDefault();
			var target = $(this).attr('href');

			if (target) {
				window.location.href = target;
			}
		},

		/**
		 * Show a temporary admin notice.
		 *
		 * @param {string} message Notice text.
		 * @param {string} type    'success' or 'error'.
		 */
		showNotice: function(message, type) {
			var noticeClass = 'success' === type ? 'notice-success' : 'notice-error';
			var $notice = $('<div class="notice ' + noticeClass + ' is-dismissible"><p></p></div>');

			$notice.find('p').text(message);
			$('.wtly-wrap .wtly-nav-tabs').after($notice);

			setTimeout(function() {
				$notice.fadeOut(300, function() {
					$(this).remove();
				});
			}, 5000);
		}
	};

	$(function() {
		WTLY_Admin.init();
	});

})(jQuery);
