/**
 * 404Zen admin: inline row actions.
 */
( function ( $ ) {
	'use strict';

	function post( action, data ) {
		return $.post( lgbfAdmin.ajaxUrl, $.extend( { action: action, nonce: lgbfAdmin.nonce }, data ) );
	}

	function rowOf( el ) {
		return $( el ).closest( 'tr' );
	}

	function fadeOutRow( $row ) {
		$row.css( 'background', '#fcf0f1' ).fadeOut( 400, function () {
			$( this ).remove();
		} );
	}

	$( document ).on( 'click', '.lgbf-action', function ( e ) {
		e.preventDefault();

		var $link = $( this );
		var id = $link.data( 'id' );
		var action = $link.data( 'action' );
		var $row = rowOf( this );

		if ( action === 'edit' ) {
			$row.find( '.lgbf-inline-edit' ).toggle().find( '.lgbf-new-url' ).trigger( 'focus' );
			return;
		}

		if ( action === 'unlink' && ! window.confirm( lgbfAdmin.i18n.confirmUnlink ) ) {
			return;
		}
		// Dismiss is applied instantly (non-blocking) and offers an inline Undo.

		$link.text( lgbfAdmin.i18n.working );

		post( 'lgbf_' + action, { link_id: id } )
			.done( function ( response ) {
				if ( ! response.success ) {
					window.alert( ( response.data && response.data.message ) || lgbfAdmin.i18n.error );
					return;
				}
				if ( action === 'recheck' ) {
					window.location.reload();
					return;
				}
				if ( action === 'dismiss' ) {
					showUndoNotice( id );
				}
				fadeOutRow( $row );
			} )
			.fail( function () {
				window.alert( lgbfAdmin.i18n.error );
			} );
	} );

	function showUndoNotice( id ) {
		$( '.lgbf-undo-notice' ).remove();
		var $notice = $( '<div class="notice notice-success lgbf-undo-notice"><p></p></div>' );
		$notice.find( 'p' )
			.text( lgbfAdmin.i18n.dismissed + ' ' )
			.append(
				$( '<a href="#" class="lgbf-undo-dismiss"></a>' )
					.attr( 'data-id', id )
					.text( lgbfAdmin.i18n.undo )
			);
		var $anchor = $( '.lgbf-wrap hr.wp-header-end' );
		if ( $anchor.length ) {
			$anchor.after( $notice );
		} else {
			$( '.lgbf-wrap h1' ).first().after( $notice );
		}
	}

	$( document ).on( 'click', '.lgbf-undo-dismiss', function ( e ) {
		e.preventDefault();
		var $undoLink = $( this );
		$undoLink.text( lgbfAdmin.i18n.working );
		post( 'lgbf_undismiss', { link_id: $undoLink.data( 'id' ) } )
			.done( function () {
				window.location.reload();
			} )
			.fail( function () {
				window.alert( lgbfAdmin.i18n.error );
			} );
	} );

	$( document ).on( 'click', '.lgbf-save-url', function () {
		var $box = $( this ).closest( '.lgbf-inline-edit' );
		var $row = rowOf( this );
		var id = $box.data( 'id' );
		var newUrl = $box.find( '.lgbf-new-url' ).val();

		$( this ).prop( 'disabled', true ).text( lgbfAdmin.i18n.working );

		post( 'lgbf_edit_url', { link_id: id, new_url: newUrl } )
			.done( function ( response ) {
				if ( ! response.success ) {
					window.alert( ( response.data && response.data.message ) || lgbfAdmin.i18n.error );
					$box.find( '.lgbf-save-url' ).prop( 'disabled', false ).text( 'Save' );
					return;
				}
				fadeOutRow( $row );
			} )
			.fail( function () {
				window.alert( lgbfAdmin.i18n.error );
				$box.find( '.lgbf-save-url' ).prop( 'disabled', false ).text( 'Save' );
			} );
	} );

	$( document ).on( 'click', '.lgbf-cancel-edit', function () {
		$( this ).closest( '.lgbf-inline-edit' ).hide();
	} );

	$( document ).on( 'click', '.zen-hide-task', function () {
		var $row = $( this ).closest( '.zen-issue' );
		post( 'lgbf_hide_task', { task: $( this ).data( 'task' ) } )
			.done( function ( response ) {
				if ( response.success ) {
					$row.slideUp( 200, function () {
						$( this ).remove();
					} );
				}
			} );
	} );
} )( jQuery );

/* Newsletter opt-in card (explicit consent only) */
( function ( $ ) {
	'use strict';
	$( document ).on( 'click', '.lgbf-optin-submit', function () {
		var $card = $( this ).closest( '.lgbf-optin-card' );
		var email = $card.find( '.lgbf-optin-email' ).val();
		$( this ).prop( 'disabled', true ).text( lgbfAdmin.i18n.working );
		$.post( lgbfAdmin.ajaxUrl, { action: 'lgbf_subscribe', nonce: lgbfAdmin.nonce, email: email } )
			.done( function ( r ) {
				if ( r.success ) {
					$card.find( '.lgbf-optin-form, .lgbf-optin-note, .zen-card-sub' ).remove();
					$card.append( '<p class="lgbf-optin-note">✓ ' + ( lgbfAdmin.i18n.subscribed || 'Subscribed. Thank you!' ) + '</p>' );
				} else {
					window.alert( ( r.data && r.data.message ) || lgbfAdmin.i18n.error );
					$card.find( '.lgbf-optin-submit' ).prop( 'disabled', false ).text( 'Subscribe' );
				}
			} );
	} );
} )( jQuery );
