/**
 * WDS Biometric 2FA — Azioni di emergenza per l'admin sul profilo di un
 * altro utente: rimuovere un dispositivo bloccato, o riportare il metodo
 * su Email per sbloccare subito l'accesso.
 */
( function () {
	'use strict';

	if ( typeof spidyBiometricAdminEmergency === 'undefined' ) {
		return;
	}

	var status = document.getElementById( 'spidy-wp-admin-status' );

	function setStatus( message, isError ) {
		if ( ! status ) {
			return;
		}
		status.textContent = message;
		status.style.color = isError ? '#b32d2e' : '#1a7f37';
	}

	document.querySelectorAll( '.spidy-wp-admin-remove-device' ).forEach( function ( btn ) {
		btn.addEventListener( 'click', function () {
			if ( ! window.confirm( spidyBiometricAdminEmergency.i18n.confirmRemove ) ) {
				return;
			}

			var credentialId = btn.getAttribute( 'data-credential-id' );
			var formData = new FormData();
			formData.append( 'action', 'spidy_wp_remove_credential' );
			formData.append( 'nonce', spidyBiometricAdminEmergency.nonce );
			formData.append( 'credential_id', credentialId );
			formData.append( 'target_user_id', spidyBiometricAdminEmergency.targetUserId );

			fetch( spidyBiometricAdminEmergency.ajaxUrl, { method: 'POST', body: formData, credentials: 'same-origin' } )
				.then( function ( res ) { return res.json(); } )
				.then( function ( data ) {
					if ( data.success ) {
						btn.closest( 'li' ).remove();
						setStatus( data.data && data.data.message ? data.data.message : 'Fatto.', false );
					} else {
						setStatus( ( data.data && data.data.message ) ? data.data.message : spidyBiometricAdminEmergency.i18n.genericError, true );
					}
				} )
				.catch( function () {
					setStatus( spidyBiometricAdminEmergency.i18n.genericError, true );
				} );
		} );
	} );

	var resetButton = document.getElementById( 'spidy-wp-admin-reset-button' );

	if ( resetButton ) {
		resetButton.addEventListener( 'click', function () {
			if ( ! window.confirm( spidyBiometricAdminEmergency.i18n.confirmReset ) ) {
				return;
			}

			resetButton.disabled = true;

			var formData = new FormData();
			formData.append( 'action', 'spidy_wp_admin_emergency_reset' );
			formData.append( 'nonce', spidyBiometricAdminEmergency.nonce );
			formData.append( 'target_user_id', spidyBiometricAdminEmergency.targetUserId );

			fetch( spidyBiometricAdminEmergency.ajaxUrl, { method: 'POST', body: formData, credentials: 'same-origin' } )
				.then( function ( res ) { return res.json(); } )
				.then( function ( data ) {
					resetButton.disabled = false;
					if ( data.success ) {
						setStatus( data.data && data.data.message ? data.data.message : 'Fatto.', false );
						resetButton.style.display = 'none';
					} else {
						setStatus( ( data.data && data.data.message ) ? data.data.message : spidyBiometricAdminEmergency.i18n.genericError, true );
					}
				} )
				.catch( function () {
					resetButton.disabled = false;
					setStatus( spidyBiometricAdminEmergency.i18n.genericError, true );
				} );
		} );
	}
} )();
