(function ($) {
	'use strict';

	var strings = window.wppsActivityLogAdmin && window.wppsActivityLogAdmin.strings
		? window.wppsActivityLogAdmin.strings
		: {};

	$(function () {
		function syncSelectAll() {
			var total = $('#the-list input[type="checkbox"]').length;
			var checked = $('#the-list input[type="checkbox"]:checked').length;
			$('#cb-select-all-1, #cb-select-all-2').prop('checked', total > 0 && total === checked);
		}

		$('#cb-select-all-1, #cb-select-all-2').on('change', function () {
			$('#the-list input[type="checkbox"]').prop('checked', $(this).prop('checked'));
			syncSelectAll();
		});

		$('#the-list input[type="checkbox"]').on('change', function () {
			syncSelectAll();
		});

		function syncSeverityAvailability() {
			var hasAction = $('#filter_action').val() !== '';
			$('#filter_severity').prop('disabled', hasAction);
			$('.wpps-al-filter-note').toggle(hasAction);
		}

		$('#filter_action').on('change', syncSeverityAvailability);
		syncSeverityAvailability();

		$('#bulk-action-selector-top, #bulk-action-selector-bottom').on('change', function () {
			var val = $(this).val();
			$('#bulk-action-selector-top, #bulk-action-selector-bottom').val(val);
		});

		function submitBulkAction(action) {
			if (action === '-1') {
				alert(strings.selectBulkAction || 'Please select a bulk action.');
				return false;
			}

			var checked = $('#the-list input[type="checkbox"]:checked').length;
			if (checked === 0) {
				alert(strings.selectAtLeastOne || 'Please select at least one log entry.');
				return false;
			}

			if (action === 'delete') {
				if (!confirm(strings.confirmDelete || 'Are you sure you want to delete the selected logs? This action cannot be undone.')) {
					return false;
				}
			}

			$('#posts-filter').find('input[name="action2"]').remove();
			$('#posts-filter').find('input[name="action"]').remove();
			$('<input>').attr({ type: 'hidden', name: 'action', value: action }).appendTo('#posts-filter');
			$('#posts-filter').submit();
			return true;
		}

		$('#doaction').on('click', function (e) {
			e.preventDefault();
			submitBulkAction($('#bulk-action-selector-top').val());
		});

		$('#doaction2').on('click', function (e) {
			e.preventDefault();
			var action = $('#bulk-action-selector-bottom').val();
			if (action === '-1') {
				alert(strings.selectBulkAction || 'Please select a bulk action.');
				return false;
			}

			var checked = $('#the-list input[type="checkbox"]:checked').length;
			if (checked === 0) {
				alert(strings.selectAtLeastOne || 'Please select at least one log entry.');
				return false;
			}

			if (action === 'delete') {
				if (!confirm(strings.confirmDelete || 'Are you sure you want to delete the selected logs? This action cannot be undone.')) {
					return false;
				}
			}

			$('#posts-filter').find('input[name="action"]').remove();
			$('#posts-filter').find('input[name="action2"]').remove();
			$('<input>').attr({ type: 'hidden', name: 'action2', value: action }).appendTo('#posts-filter');
			$('#posts-filter').submit();
		});

		$('.wpps-clear-logs').on('click', function (e) {
			e.preventDefault();

			if (!confirm(strings.confirmClearAll || 'Are you sure you want to clear all logs? This action cannot be undone.')) {
				return;
			}

			var $button = $(this);
			var nonce = $button.data('nonce');
			var clearLabel = strings.clearAllLogs || 'Clear all logs';

			$button.prop('disabled', true).text(strings.clearing || 'Clearing...');

			$.ajax({
				url: ajaxurl,
				type: 'POST',
				data: {
					action: 'wpps_activity_log_clear',
					nonce: nonce
				},
				success: function (response) {
					if (response.success) {
						var url = new URL(window.location.href);
						url.searchParams.set('cleared', '1');
						window.location.href = url.toString();
					} else {
						alert(response.data && response.data.message ? response.data.message : (strings.errorClearing || 'Error clearing logs.'));
						$button.prop('disabled', false).text(clearLabel);
					}
				},
				error: function () {
					alert(strings.errorClearing || 'Error clearing logs.');
					$button.prop('disabled', false).text(clearLabel);
				}
			});
		});
	});
}(jQuery));
