/**
 * STRYKE Backup – Admin JavaScript
 *
 * Handles AJAX deletion of backup log entries.
 *
 * @package StrykeWP\Modules\Backup
 */

/* global jQuery, stryke_ajax */

jQuery( document ).ready( function ( $ ) {
	'use strict';

	$( '.stryke-delete-log-link' ).on( 'click', function ( e ) {
		e.preventDefault();

		var $link = $( this );
		var logId = $link.data( 'id' );

		if ( ! window.confirm( stryke_ajax.confirm_text ) ) {
			return;
		}

		$link.text( 'Lösche…' ).css( 'pointer-events', 'none' );

		$.ajax( {
			url:  stryke_ajax.ajax_url,
			type: 'POST',
			data: {
				action:   'stryke_delete_backup',
				security: stryke_ajax.nonce,
				id:       logId
			},
			success: function ( response ) {
				if ( response.success ) {
					$link.closest( 'tr' ).fadeOut( 300, function () {
						$( this ).remove();
					} );
				} else {
					window.alert( response.data.message );
					$link.text( 'Löschen' ).css( 'pointer-events', '' );
				}
			},
			error: function () {
				window.alert( 'Ein unerwarteter Fehler ist aufgetreten.' );
				$link.text( 'Löschen' ).css( 'pointer-events', '' );
			}
		} );
	} );
} );
