/**
 * Script for the tools page.
 *
 * @package northmill-online-shared
 */

/* global northmill_online_shared_admin_tools */
jQuery( document ).ready(
	function () {
		new NMS_Tools();
	}
);


class NMS_Tools {
	constructor() {
		this.download_package_logs = this.download_package_logs.bind( this );
		this.download_nm_logs      = this.download_nm_logs.bind( this );

		jQuery( '.nm-download-package-logs' ).on( 'click', this.download_package_logs );
		jQuery( '.nm-download-nm-logs' ).on( 'click', this.download_nm_logs );
		jQuery( '.nm-uninstall-data-deletion' ).on( 'click', this.toggle_uninstall_data_deletion );
	}

	download_package_logs() {
		this.download_logs();
	}

	download_nm_logs() {
		this.download_logs( false );
	}

	download_logs( only_package_logs = true ) {
		NMS_AJAX.post(
			northmill_online_shared_admin_tools.download_logs_ajax,
			{ only_package_logs: only_package_logs },
			NMS_AJAX.success_reload,
			NMS_AJAX.fail_alert
		);
	}

	/**
	 * Toggle whether to remove data on plugin uninstallation.
	 */
	toggle_uninstall_data_deletion() {
		const $btn   = jQuery( this );
		const enable = Boolean( $btn.data( 'enable' ) );

		if (
			! confirm(
				enable ?
					northmill_online_shared_admin_tools.l10n.confirm_enable_uninstall_data_deletion :
					northmill_online_shared_admin_tools.l10n.confirm_disable_uninstall_data_deletion
			)
		) {
			return;
		}

		NMS_AJAX.post(
			northmill_online_shared_admin_tools.uninstall_data_deletion_ajax,
			{ enabled: enable },
			NMS_AJAX.success_reload,
			NMS_AJAX.fail_alert
		);
	}
}
