/* global fusionBuilderConfig, ajaxurl */
( function( jQuery ) {
	var currentPage = 1,
		totalPages  = 0;

	'use strict';

	jQuery( document ).ready( function() {

		// Fix for forms list table pagination: when a page number is entered and
		// Enter is pressed, redirect to the correct page preserving the page context.
		jQuery( '#fusion-library-data input.current-page, #avada-form-entries input.current-page' ).on( 'keydown', function( event ) {
			if ( 13 === event.which ) {
				event.preventDefault();

				var totalPages = parseInt( jQuery( '.total-pages' ).first().text(), 10 ) || 1,
					page       = Math.min( Math.max( parseInt( jQuery( this ).val(), 10 ) || 1, 1 ), totalPages ),
					url        = new URL( window.location.href );

				url.searchParams.set( 'paged', page );
				window.location.href = url.toString();
			}
		} );

		jQuery( '.fusion-remove-form-entry' ).on( 'click', function( event ) {
			var self = this;

			event.preventDefault();

			if ( window.confirm( fusionBuilderConfig.remove_entry_message ) ) { // eslint-disable-line no-alert
				jQuery.ajax( {
					type: 'POST',
					url: fusionBuilderConfig.ajaxurl,
					data: {
						action: 'fusion_remove_form_entry',
						fusion_entry_nonce: fusionBuilderConfig.fusion_entry_nonce,
						entry: jQuery( this ).data( 'key' )
					},
					complete: function( response ) {

						if ( 'object' === typeof response.responseJSON && 'undefined' !== response.responseJSON.success && response.responseJSON.success ) {
							jQuery( self ).closest( 'tr' ).remove();
						}

						setTimeout( function() {
							if ( ! jQuery( '.avada_page_avada-form-entries .row-actions' ).length ) {
								jQuery( '#fusion-form-export' ).addClass( 'disabled' );
							}
						}, 50 );
					}
				} );
			}
		} );

		// Export form entries button.
		jQuery( '#fusion-form-export' ).on( 'click', function( event ) {
			event.preventDefault();

			if ( jQuery( '.avada_page_avada-form-entries .row-actions' ).length ) {
				exportForms( jQuery( this ).data( 'form-id' ) );
			}

		} );

		function exportForms( formID ) {

			// Starting export, show status.
			if ( 0 === totalPages ) {
				jQuery( '#fusion-form-export' ).addClass( 'disabled' );
				jQuery( '#fusion-form-export-status' ).css( 'opacity', '1' );
			}

			jQuery.ajax( {
				url: ajaxurl,
				type: 'get',
				data: {
					action: 'fusion_form_export',
					formID: formID,
					currentPage: currentPage,
					totalPages: totalPages,
					nonce: jQuery( '#fusion-form-export-nonce' ).val()
				},
				dataType: 'json'
			} )
			.done( function( result ) {

				// Stop if error.
				if ( 'error_nonce' === result.status || ( 0 < totalPages && currentPage > totalPages ) ) {
					return;
				}

				// Export in progress.
				if ( 'export_processing' === result.status ) {
					currentPage++;

					// First step done.
					if ( 0 === totalPages ) {
						totalPages = result.total_pages;
					}

					// Update export status.
					jQuery( '#fusion-form-export-status-bar' ).css( 'width', ( currentPage / totalPages ) * 100 + '%' ); // eslint-disable-line no-mixed-operators

					exportForms( formID );
				}

				// Export done (last step processed).
				if ( 'export_done' === result.status ) {
					currentPage = 1;
					totalPages  = 0;

					// Reset status.
					jQuery( '#fusion-form-export-status' ).css( 'opacity', '0' );
					jQuery( '#fusion-form-export-status-bar' ).css( 'width', '0' );
					jQuery( '#fusion-form-export' ).removeClass( 'disabled' );

					// Trigger download and cleanup.
					window.location.href = jQuery( '#fusion-form-export' ).attr( 'href' ) + '&nonce=' + jQuery( '#fusion-form-export-nonce' ).val();
				}
			} );
		}

	} );
}( jQuery ) );
