jQuery( document ).ready(
	function ($) {
		// Force Wawp Actions to the top of the sidebar
		function moveWwawpBoxToTop() {
			var box = $( '#wawp_order_actions' );
			if (box.length) {
				var container = $( '#side-sortables' );
				if (container.length) {
					// Only move if it's not already at the very top
					if (box.index() !== 0) {
						box.prependTo( container );
					}
				}
			}
		}

		moveWwawpBoxToTop();
		// Additional checks to ensure it stays at the top
		setTimeout( moveWwawpBoxToTop, 100 );
		setTimeout( moveWwawpBoxToTop, 500 );

		function updateWawpButtonVisibility( $select ) {
			if ( ! $select.length ) {
				return;
			}
			var $option = $select.find( 'option:selected' );
			var $container = $select.closest( '.wawp-order-actions-container, .wawp-order-list-actions, td' );
			if ( ! $option.length ) {
				$container.find( '.wawp-send-btn' ).addClass( 'wawp-hide' );
				return;
			}
			var waWeb  = $option.attr( 'data-wa-web' ) === '1';
			var waMeta = $option.attr( 'data-wa-meta' ) === '1';
			var email  = $option.attr( 'data-email' ) === '1';

			$container.find( '.wawp-send-btn.whatsapp-web' ).toggleClass( 'wawp-hide', ! waWeb );
			$container.find( '.wawp-send-btn.whatsapp-meta' ).toggleClass( 'wawp-hide', ! waMeta );
			$container.find( '.wawp-send-btn.email' ).toggleClass( 'wawp-hide', ! email );
		}

		// Initialize all dropdowns
		function initWawpVisibility() {
			$( '#wawp_status_select, .wawp-list-status-select' ).each(
				function () {
					updateWawpButtonVisibility( $( this ) );
				}
			);
		}

		initWawpVisibility();
		setTimeout( initWawpVisibility, 500 );

		// Handle change events
		$( document ).on(
			'change',
			'#wawp_status_select, .wawp-list-status-select',
			function () {
				updateWawpButtonVisibility( $( this ) );
			}
		);

		$( document ).on(
			'click',
			'.wawp-send-btn',
			function (e) {
				e.preventDefault();

				var $btn    = $( this );
				var orderId = $btn.data( 'order-id' );
				var gateway = $btn.data( 'gateway' );
				var $container = $btn.closest( '.wawp-order-actions-container, .wawp-order-list-actions, td' );
				var status  = $container.find( 'select' ).val();
				var $msg    = $container.find( '.wawp-action-message, #wawp_action_message' );

				if ($btn.hasClass( 'disabled' )) {
					return;
				}

				$btn.addClass( 'disabled' ).attr( 'disabled', true );
				var originalHtml = $btn.html();
				
				if ( ! $btn.hasClass( 'wawp-btn-icon-only' ) ) {
					$btn.text( wawp_order_actions.sending );
				} else {
					$btn.css( 'opacity', '0.6' );
				}
				
				$msg.hide().removeClass( 'success error' );

				$.ajax(
					{
						url: wawp_order_actions.ajax_url,
						type: 'POST',
						data: {
							action: 'wawp_send_manual_notification',
							nonce: wawp_order_actions.nonce,
							order_id: orderId,
							status: status,
							gateway: gateway
						},
						success: function (response) {
							if (response.success) {
								$msg.html( wawp_order_actions.success ).addClass( 'success' ).fadeIn();
							} else {
								$msg.html( response.data && response.data.message ? response.data.message : wawp_order_actions.error ).addClass( 'error' ).fadeIn();
							}
						},
						error: function () {
							$msg.html( wawp_order_actions.error ).addClass( 'error' ).fadeIn();
						},
						complete: function () {
							$btn.removeClass( 'disabled' ).removeAttr( 'disabled' );
							if ( ! $btn.hasClass( 'wawp-btn-icon-only' ) ) {
								$btn.html( originalHtml );
							} else {
								$btn.css( 'opacity', '' );
							}
						}
					}
				);
			}
		);
	}
);
