<?php
/**
 * Wishlist Widget - Render Methods
 *
 * @package GGM_Commerce
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Methods included into GGM_Commerce_Widget_Wishlist via require inside the class body.
// All methods use $this to refer to the widget instance.

/**
 * Render the premium widget content.
 */
function ggmc_wishlist_render_premium_content( $widget ) {
	$settings = $widget->get_settings_for_display();

	$wishlist_handler  = GGM_Commerce_Wishlist::instance();
	$currency_options  = get_option( 'ggm_commerce_currency_options', array() );
	$currency_symbol   = isset( $currency_options['symbol'] ) ? $currency_options['symbol'] : '£';
	$currency_position = isset( $currency_options['position'] ) ? $currency_options['position'] : 'before';

	$wishlist_mode   = isset( $settings['wishlist_mode'] ) ? $settings['wishlist_mode'] : 'default';
	$is_logged_in    = is_user_logged_in();
	$sharing_enabled = $wishlist_handler->is_sharing_enabled();
	$is_multi_mode   = in_array( $wishlist_mode, array( 'all', 'dropdown' ), true );

	if ( $is_logged_in && $is_multi_mode ) {
		$wishlists = $wishlist_handler->get_wishlists();
	} elseif ( $is_logged_in && 'specific' === $wishlist_mode && ! empty( $settings['wishlist_id'] ) ) {
		$specific  = $wishlist_handler->get_wishlist( absint( $settings['wishlist_id'] ) );
		$wishlists = $specific ? array( $specific ) : array();
	} else {
		$default_wishlist = $wishlist_handler->get_default_wishlist();
		$wishlists        = $default_wishlist ? array( $default_wishlist ) : array();
	}

	if ( ! empty( $wishlists ) && ! $is_multi_mode ) {
		$display_wishlist = $wishlists[0];
		$total_count      = isset( $display_wishlist->item_count )
			? (int) $display_wishlist->item_count
			: $wishlist_handler->get_count( isset( $display_wishlist->id ) ? $display_wishlist->id : null );
	} else {
		$total_count = $wishlist_handler->get_count();
	}

	$title_source = isset( $settings['title_source'] ) ? $settings['title_source'] : 'wishlist_name';
	if ( 'wishlist_name' === $title_source && ! empty( $wishlists ) && ! $is_multi_mode ) {
		$title_text = $wishlists[0]->name;
	} else {
		$title_text = ! empty( $settings['title'] ) ? $settings['title'] : '';
	}

	?>
	<div class="ggmc-wishlist-wrapper"
		data-layout="<?php echo esc_attr( $settings['layout'] ); ?>"
		data-mode="<?php echo esc_attr( $wishlist_mode ); ?>"
		data-logged-in="<?php echo esc_attr( $is_logged_in ? '1' : '0' ); ?>">

		<?php if ( 'yes' === $settings['show_title'] && ! empty( $title_text ) ) : ?>
			<h2 class="ggmc-wishlist-title"><?php echo esc_html( $title_text ); ?>
				<span class="ggmc-wishlist-count">(<?php echo esc_html( $total_count ); ?>)</span>
			</h2>
		<?php endif; ?>

		<?php
		if ( $is_logged_in && $is_multi_mode && 'yes' === $settings['show_management'] ) :
			ggmc_wishlist_render_management_controls( $wishlists, $wishlist_handler );
		endif;

		if ( 'all' === $wishlist_mode && $is_logged_in ) :
			ggmc_wishlist_render_tabbed_view( $widget, $wishlists, $settings, $currency_symbol, $currency_position, $sharing_enabled );
		elseif ( 'dropdown' === $wishlist_mode && $is_logged_in ) :
			ggmc_wishlist_render_dropdown_view( $widget, $wishlists, $settings, $currency_symbol, $currency_position, $sharing_enabled );
		else :
			ggmc_wishlist_render_single_wishlist_view( $widget, $wishlists, $settings, $currency_symbol, $currency_position, $sharing_enabled );
		endif;

		if ( $is_logged_in && $is_multi_mode && 'yes' === $settings['show_management'] ) :
			ggmc_wishlist_render_management_modals();
		endif;

		if ( $is_logged_in && $is_multi_mode && 'yes' === $settings['show_move_copy'] ) :
			ggmc_wishlist_render_move_copy_modal( $wishlists );
		endif;

		if ( $is_logged_in && $is_multi_mode ) :
			?>
			<script>
			(function($) {
				var $wrapper = $('.ggmc-wishlist-wrapper[data-mode="<?php echo esc_js( $wishlist_mode ); ?>"]').last();

				$wrapper.on('click', '.ggmc-wishlist-tab:not(.ggmc-wishlist-tab-new)', function(e) {
					e.preventDefault();
					e.stopPropagation();
					var wishlistId = $(this).data('wishlist-id');
					if (!wishlistId) return;

					$wrapper.find('.ggmc-wishlist-tab').removeClass('active');
					$(this).addClass('active');

					$wrapper.find('.ggmc-wishlist-tab-panel').removeClass('active');
					$wrapper.find('.ggmc-wishlist-tab-panel[data-wishlist-id="' + wishlistId + '"]').addClass('active');
					$wrapper.find('.ggmc-wishlist-tab-actions').removeClass('active');
					$wrapper.find('.ggmc-wishlist-tab-actions[data-wishlist-id="' + wishlistId + '"]').addClass('active');
				});

				$wrapper.on('change', '.ggmc-wishlist-selector', function() {
					var wishlistId = $(this).val();
					if (!wishlistId) return;

					var url = new URL(window.location.href);
					url.searchParams.set('wishlist', wishlistId);
					window.location.href = url.toString();
				});
			})(jQuery);
			</script>
			<?php
		endif;
		?>
	</div>
	<?php
}

/**
 * Render management controls bar.
 */
function ggmc_wishlist_render_management_controls( $wishlists, $wishlist_handler ) {
	$max_wishlists = $wishlist_handler->get_max_wishlists();
	$can_create    = count( $wishlists ) < $max_wishlists;
	?>
	<div class="ggmc-wishlist-management-controls">
		<button type="button" class="ggmc-wishlist-management-btn ggmc-wishlist-create-btn"
			<?php echo ! $can_create ? 'disabled' : ''; ?>
			title="<?php
			/* translators: %d: maximum number of wishlists allowed */
			echo ! $can_create ? esc_attr( sprintf( __( 'Maximum of %d wishlists reached', 'ggm-commerce' ), $max_wishlists ) ) : '';
		?>">
			<i class="eicon-plus-circle-o"></i> <?php esc_html_e( 'New Wishlist', 'ggm-commerce' ); ?>
		</button>
		<span class="ggmc-wishlist-limit-info">
			<?php
			echo esc_html(
				sprintf(
					/* translators: 1: current count, 2: maximum count */
					__( '%1$d / %2$d lists used', 'ggm-commerce' ),
					count( $wishlists ),
					$max_wishlists
				)
			);
			?>
		</span>
	</div>
	<?php
}

/**
 * Render tabbed view for multiple wishlists.
 */
function ggmc_wishlist_render_tabbed_view( $widget, $wishlists, $settings, $currency_symbol, $currency_position, $sharing_enabled ) {
	if ( empty( $wishlists ) ) {
		ggmc_wishlist_render_empty_message( $settings );
		return;
	}

	$active_wishlist_id = null;

	if ( ! empty( $_GET['wishlist'] ) ) {
		$requested_id = absint( $_GET['wishlist'] );
		foreach ( $wishlists as $wl ) {
			if ( absint( $wl->id ) === $requested_id ) {
				$active_wishlist_id = $requested_id;
				break;
			}
		}
	}

	if ( ! $active_wishlist_id ) {
		foreach ( $wishlists as $wl ) {
			if ( (int) $wl->is_default === 1 ) {
				$active_wishlist_id = absint( $wl->id );
				break;
			}
		}
	}

	if ( ! $active_wishlist_id ) {
		$active_wishlist_id = absint( $wishlists[0]->id );
	}
	?>
	<div class="ggmc-wishlist-tabs">
		<div class="ggmc-wishlist-tabs-nav" role="tablist">
			<?php foreach ( $wishlists as $wishlist ) : ?>
				<div class="ggmc-wishlist-tab <?php echo absint( $wishlist->id ) === $active_wishlist_id ? 'active' : ''; ?>"
					data-wishlist-id="<?php echo esc_attr( $wishlist->id ); ?>"
					role="tab"
					aria-selected="<?php echo absint( $wishlist->id ) === $active_wishlist_id ? 'true' : 'false'; ?>"
					aria-controls="wishlist-panel-<?php echo esc_attr( $wishlist->id ); ?>">
					<span class="ggmc-tab-label"><?php echo esc_html( $wishlist->name ); ?>
						<span class="tab-count">(<?php echo esc_html( $wishlist->item_count ); ?>)</span>
					</span>
					<?php if ( (int) $wishlist->is_default === 1 ) : ?>
						<i class="eicon-star ggmc-default-star" title="<?php esc_attr_e( 'Default wishlist', 'ggm-commerce' ); ?>"></i>
					<?php endif; ?>
				</div>
			<?php endforeach; ?>
		</div>

		<?php foreach ( $wishlists as $wishlist ) : ?>
			<div class="ggmc-wishlist-tab-actions <?php echo absint( $wishlist->id ) === $active_wishlist_id ? 'active' : ''; ?>"
				data-wishlist-id="<?php echo esc_attr( $wishlist->id ); ?>">
				<button type="button" class="ggmc-wishlist-rename-btn" data-wishlist-id="<?php echo esc_attr( $wishlist->id ); ?>" title="<?php esc_attr_e( 'Rename', 'ggm-commerce' ); ?>">
					<i class="eicon-edit"></i> <?php esc_html_e( 'Rename', 'ggm-commerce' ); ?>
				</button>
				<?php if ( (int) $wishlist->is_default !== 1 ) : ?>
					<button type="button" class="ggmc-wishlist-set-default-btn" data-wishlist-id="<?php echo esc_attr( $wishlist->id ); ?>" title="<?php esc_attr_e( 'Set as Default', 'ggm-commerce' ); ?>">
						<i class="eicon-star-o"></i> <?php esc_html_e( 'Set as Default', 'ggm-commerce' ); ?>
					</button>
				<?php endif; ?>
				<?php if ( count( $wishlists ) > 1 ) : ?>
					<button type="button" class="ggmc-wishlist-delete-btn" data-wishlist-id="<?php echo esc_attr( $wishlist->id ); ?>" title="<?php esc_attr_e( 'Delete', 'ggm-commerce' ); ?>">
						<i class="eicon-trash-o"></i> <?php esc_html_e( 'Delete', 'ggm-commerce' ); ?>
					</button>
				<?php endif; ?>
			</div>
		<?php endforeach; ?>

		<div class="ggmc-wishlist-tab-panels">
			<?php foreach ( $wishlists as $wishlist ) : ?>
				<?php
				$wh    = GGM_Commerce_Wishlist::instance();
				$items = $wh->get_items( $wishlist->id );
				?>
				<div class="ggmc-wishlist-tab-panel <?php echo absint( $wishlist->id ) === $active_wishlist_id ? 'active' : ''; ?>"
					id="wishlist-panel-<?php echo esc_attr( $wishlist->id ); ?>"
					data-wishlist-id="<?php echo esc_attr( $wishlist->id ); ?>"
					role="tabpanel"
					aria-labelledby="tab-<?php echo esc_attr( $wishlist->id ); ?>">
					<?php ggmc_wishlist_render_panel_content( $widget, $wishlist, $items, $settings, $currency_symbol, $currency_position, $sharing_enabled ); ?>
				</div>
			<?php endforeach; ?>
		</div>
	</div>
	<?php
}

/**
 * Render dropdown view for multiple wishlists.
 */
function ggmc_wishlist_render_dropdown_view( $widget, $wishlists, $settings, $currency_symbol, $currency_position, $sharing_enabled ) {
	if ( empty( $wishlists ) ) {
		ggmc_wishlist_render_empty_message( $settings );
		return;
	}

	$active_wishlist = null;

	if ( ! empty( $_GET['wishlist'] ) ) {
		$requested_id = absint( $_GET['wishlist'] );
		foreach ( $wishlists as $wishlist ) {
			if ( absint( $wishlist->id ) === $requested_id ) {
				$active_wishlist = $wishlist;
				break;
			}
		}
	}

	if ( ! $active_wishlist ) {
		foreach ( $wishlists as $wishlist ) {
			if ( (int) $wishlist->is_default === 1 ) {
				$active_wishlist = $wishlist;
				break;
			}
		}
	}

	if ( ! $active_wishlist ) {
		$active_wishlist = $wishlists[0];
	}

	$active_wishlist_id = absint( $active_wishlist->id );
	?>
	<div class="ggmc-wishlist-dropdown-wrapper">
		<div class="ggmc-wishlist-dropdown-header">
			<label for="ggmc-wishlist-selector"><?php esc_html_e( 'Select Wishlist:', 'ggm-commerce' ); ?></label>
			<select id="ggmc-wishlist-selector" class="ggmc-wishlist-selector ggmc-wishlist-dropdown-select">
				<?php foreach ( $wishlists as $wishlist ) : ?>
					<option value="<?php echo esc_attr( $wishlist->id ); ?>" <?php selected( $wishlist->id, $active_wishlist_id ); ?>>
						<?php
						echo esc_html( $wishlist->name );
						echo ' (' . esc_html( $wishlist->item_count ) . ')';
						if ( (int) $wishlist->is_default === 1 ) {
							echo ' - ' . esc_html__( 'Default', 'ggm-commerce' );
						}
						?>
					</option>
				<?php endforeach; ?>
			</select>

			<div class="ggmc-wishlist-dropdown-actions">
				<button type="button" class="ggmc-wishlist-rename-btn" data-wishlist-id="<?php echo esc_attr( $active_wishlist->id ); ?>" title="<?php esc_attr_e( 'Rename', 'ggm-commerce' ); ?>">
					<i class="eicon-edit"></i>
				</button>
				<?php if ( ! (int) $active_wishlist->is_default ) : ?>
					<button type="button" class="ggmc-wishlist-set-default-btn" data-wishlist-id="<?php echo esc_attr( $active_wishlist->id ); ?>" title="<?php esc_attr_e( 'Set as Default', 'ggm-commerce' ); ?>">
						<i class="eicon-star-o"></i>
					</button>
				<?php endif; ?>
				<?php if ( count( $wishlists ) > 1 ) : ?>
					<button type="button" class="ggmc-wishlist-delete-btn" data-wishlist-id="<?php echo esc_attr( $active_wishlist->id ); ?>" title="<?php esc_attr_e( 'Delete', 'ggm-commerce' ); ?>">
						<i class="eicon-trash-o"></i>
					</button>
				<?php endif; ?>
			</div>
		</div>

		<div class="ggmc-wishlist-dropdown-content" data-wishlist-id="<?php echo esc_attr( $active_wishlist->id ); ?>">
			<?php
			$wh    = GGM_Commerce_Wishlist::instance();
			$items = $wh->get_items( $active_wishlist->id );
			ggmc_wishlist_render_panel_content( $widget, $active_wishlist, $items, $settings, $currency_symbol, $currency_position, $sharing_enabled );
			?>
		</div>
	</div>
	<?php
}

/**
 * Render single wishlist view.
 */
function ggmc_wishlist_render_single_wishlist_view( $widget, $wishlists, $settings, $currency_symbol, $currency_position, $sharing_enabled ) {
	if ( empty( $wishlists ) ) {
		ggmc_wishlist_render_empty_message( $settings );
		return;
	}

	$wishlist = $wishlists[0];
	$wh       = GGM_Commerce_Wishlist::instance();
	$items    = $wh->get_items( isset( $wishlist->id ) ? $wishlist->id : null );

	ggmc_wishlist_render_panel_content( $widget, $wishlist, $items, $settings, $currency_symbol, $currency_position, $sharing_enabled );
}

/**
 * Render wishlist panel content (shared by all views).
 */
function ggmc_wishlist_render_panel_content( $widget, $wishlist, $items, $settings, $currency_symbol, $currency_position, $sharing_enabled ) {
	$wishlist_id  = isset( $wishlist->id ) ? $wishlist->id : null;
	$show_actions = ( 'yes' === $settings['show_clear_all'] && ! empty( $items ) ) ||
		( 'yes' === $settings['show_sharing'] && $sharing_enabled && is_user_logged_in() );
	?>
	<div class="ggmc-wishlist-panel-header">
		<?php if ( $show_actions ) : ?>
			<div class="ggmc-wishlist-actions">
				<?php if ( 'yes' === $settings['show_clear_all'] && ! empty( $items ) ) : ?>
					<button type="button" class="ggmc-wishlist-clear-all" data-wishlist-id="<?php echo esc_attr( $wishlist_id ); ?>">
						<i class="eicon-trash-o"></i> <?php esc_html_e( 'Clear All', 'ggm-commerce' ); ?>
					</button>
				<?php endif; ?>

				<?php if ( 'yes' === $settings['show_sharing'] && $sharing_enabled && is_user_logged_in() ) : ?>
					<button type="button" class="ggmc-wishlist-share-btn" data-wishlist-id="<?php echo esc_attr( $wishlist_id ); ?>">
						<i class="eicon-share"></i> <?php esc_html_e( 'Share', 'ggm-commerce' ); ?>
					</button>
					<?php if ( ! empty( $wishlist->share_token ) ) : ?>
						<div class="ggmc-share-link-container" style="display: none;">
							<input type="text" class="ggmc-share-link-input"
								value="<?php echo esc_url( add_query_arg( 'wishlist_token', $wishlist->share_token, home_url( '/wishlist/' ) ) ); ?>"
								readonly />
							<button type="button" class="ggmc-copy-share-link" title="<?php esc_attr_e( 'Copy link', 'ggm-commerce' ); ?>">
								<i class="eicon-copy"></i>
							</button>
						</div>
					<?php endif; ?>
				<?php endif; ?>
			</div>
		<?php endif; ?>
	</div>

	<?php if ( empty( $items ) ) : ?>
		<div class="ggmc-wishlist-empty">
			<div class="ggmc-wishlist-empty-icon"><i class="eicon-heart-o"></i></div>
			<p class="ggmc-wishlist-empty-message"><?php echo esc_html( $settings['empty_message'] ); ?></p>
		</div>
	<?php else : ?>
		<?php
		switch ( $settings['layout'] ) {
			case 'grid':
				ggmc_wishlist_render_grid_layout( $widget, $items, $settings, $currency_symbol, $currency_position );
				break;
			case 'list':
				ggmc_wishlist_render_list_layout( $widget, $items, $settings, $currency_symbol, $currency_position );
				break;
			default:
				ggmc_wishlist_render_table_layout( $widget, $items, $settings, $currency_symbol, $currency_position );
				break;
		}
		?>
	<?php endif; ?>
	<?php
}

/**
 * Render empty wishlist message.
 */
function ggmc_wishlist_render_empty_message( $settings ) {
	?>
	<div class="ggmc-wishlist-empty">
		<div class="ggmc-wishlist-empty-icon"><i class="eicon-heart-o"></i></div>
		<p class="ggmc-wishlist-empty-message"><?php echo esc_html( $settings['empty_message'] ); ?></p>
	</div>
	<?php
}

/**
 * Render management modals.
 */
function ggmc_wishlist_render_management_modals() {
	?>
	<!-- Create Wishlist Modal -->
	<div class="ggmc-wishlist-modal ggmc-create-wishlist-modal" style="display: none;">
		<div class="ggmc-wishlist-modal-overlay"></div>
		<div class="ggmc-wishlist-modal-content">
			<div class="ggmc-wishlist-modal-header">
				<h3><?php esc_html_e( 'Create New Wishlist', 'ggm-commerce' ); ?></h3>
				<button type="button" class="ggmc-wishlist-modal-close"><i class="eicon-close"></i></button>
			</div>
			<div class="ggmc-wishlist-modal-body">
				<label for="ggmc-new-wishlist-name"><?php esc_html_e( 'Wishlist Name', 'ggm-commerce' ); ?></label>
				<input type="text" id="ggmc-new-wishlist-name" class="ggmc-wishlist-name-input"
					placeholder="<?php esc_attr_e( 'e.g., Birthday Ideas', 'ggm-commerce' ); ?>" maxlength="100" />
			</div>
			<div class="ggmc-wishlist-modal-footer">
				<button type="button" class="ggmc-wishlist-modal-cancel"><?php esc_html_e( 'Cancel', 'ggm-commerce' ); ?></button>
				<button type="button" class="ggmc-wishlist-modal-confirm ggmc-create-wishlist-confirm"><?php esc_html_e( 'Create', 'ggm-commerce' ); ?></button>
			</div>
		</div>
	</div>

	<!-- Rename Wishlist Modal -->
	<div class="ggmc-wishlist-modal ggmc-rename-wishlist-modal" style="display: none;">
		<div class="ggmc-wishlist-modal-overlay"></div>
		<div class="ggmc-wishlist-modal-content">
			<div class="ggmc-wishlist-modal-header">
				<h3><?php esc_html_e( 'Rename Wishlist', 'ggm-commerce' ); ?></h3>
				<button type="button" class="ggmc-wishlist-modal-close"><i class="eicon-close"></i></button>
			</div>
			<div class="ggmc-wishlist-modal-body">
				<input type="hidden" id="ggmc-rename-wishlist-id" value="" />
				<label for="ggmc-rename-wishlist-name"><?php esc_html_e( 'New Name', 'ggm-commerce' ); ?></label>
				<input type="text" id="ggmc-rename-wishlist-name" class="ggmc-wishlist-name-input" maxlength="100" />
			</div>
			<div class="ggmc-wishlist-modal-footer">
				<button type="button" class="ggmc-wishlist-modal-cancel"><?php esc_html_e( 'Cancel', 'ggm-commerce' ); ?></button>
				<button type="button" class="ggmc-wishlist-modal-confirm ggmc-rename-wishlist-confirm"><?php esc_html_e( 'Save', 'ggm-commerce' ); ?></button>
			</div>
		</div>
	</div>

	<!-- Delete Wishlist Modal -->
	<div class="ggmc-wishlist-modal ggmc-delete-wishlist-modal" style="display: none;">
		<div class="ggmc-wishlist-modal-overlay"></div>
		<div class="ggmc-wishlist-modal-content">
			<div class="ggmc-wishlist-modal-header">
				<h3><?php esc_html_e( 'Delete Wishlist', 'ggm-commerce' ); ?></h3>
				<button type="button" class="ggmc-wishlist-modal-close"><i class="eicon-close"></i></button>
			</div>
			<div class="ggmc-wishlist-modal-body">
				<input type="hidden" id="ggmc-delete-wishlist-id" value="" />
				<p><?php esc_html_e( 'Are you sure you want to delete this wishlist?', 'ggm-commerce' ); ?></p>
				<label>
					<input type="checkbox" id="ggmc-move-items-to-default" checked />
					<?php esc_html_e( 'Move items to default wishlist', 'ggm-commerce' ); ?>
				</label>
			</div>
			<div class="ggmc-wishlist-modal-footer">
				<button type="button" class="ggmc-wishlist-modal-cancel"><?php esc_html_e( 'Cancel', 'ggm-commerce' ); ?></button>
				<button type="button" class="ggmc-wishlist-modal-confirm ggmc-delete-wishlist-confirm ggmc-btn-danger"><?php esc_html_e( 'Delete', 'ggm-commerce' ); ?></button>
			</div>
		</div>
	</div>
	<?php
}

/**
 * Render move/copy modal.
 */
function ggmc_wishlist_render_move_copy_modal( $wishlists ) {
	?>
	<!-- Move/Copy Items Modal -->
	<div class="ggmc-wishlist-modal ggmc-move-copy-modal" style="display: none;">
		<div class="ggmc-wishlist-modal-overlay"></div>
		<div class="ggmc-wishlist-modal-content">
			<div class="ggmc-wishlist-modal-header">
				<h3><?php esc_html_e( 'Move or Copy Item', 'ggm-commerce' ); ?></h3>
				<button type="button" class="ggmc-wishlist-modal-close"><i class="eicon-close"></i></button>
			</div>
			<div class="ggmc-wishlist-modal-body">
				<input type="hidden" id="ggmc-move-copy-product-id" value="" />
				<input type="hidden" id="ggmc-move-copy-from-wishlist" value="" />

				<div class="ggmc-move-copy-options">
					<label>
						<input type="radio" name="ggmc-move-copy-action" value="move" checked />
						<?php esc_html_e( 'Move to another wishlist', 'ggm-commerce' ); ?>
					</label>
					<label>
						<input type="radio" name="ggmc-move-copy-action" value="copy" />
						<?php esc_html_e( 'Copy to another wishlist', 'ggm-commerce' ); ?>
					</label>
				</div>

				<label for="ggmc-move-copy-target"><?php esc_html_e( 'Select Wishlist:', 'ggm-commerce' ); ?></label>
				<select id="ggmc-move-copy-target" class="ggmc-move-copy-target-select">
					<?php foreach ( $wishlists as $wishlist ) : ?>
						<option value="<?php echo esc_attr( $wishlist->id ); ?>">
							<?php
							echo esc_html( $wishlist->name );
							if ( (int) $wishlist->is_default === 1 ) {
								echo ' (' . esc_html__( 'Default', 'ggm-commerce' ) . ')';
							}
							?>
						</option>
					<?php endforeach; ?>
				</select>
			</div>
			<div class="ggmc-wishlist-modal-footer">
				<button type="button" class="ggmc-wishlist-modal-cancel"><?php esc_html_e( 'Cancel', 'ggm-commerce' ); ?></button>
				<button type="button" class="ggmc-wishlist-modal-confirm ggmc-move-copy-confirm"><?php esc_html_e( 'Confirm', 'ggm-commerce' ); ?></button>
			</div>
		</div>
	</div>
	<?php
}

/**
 * Render table layout.
 */
function ggmc_wishlist_render_table_layout( $widget, $items, $settings, $currency_symbol, $currency_position ) {
	?>
	<table class="ggmc-wishlist-table">
		<thead>
			<tr>
				<?php if ( 'yes' === $settings['show_remove_button'] ) : ?>
					<th class="wishlist-col-remove"></th>
				<?php endif; ?>
				<?php if ( 'yes' === $settings['show_image'] ) : ?>
					<th class="wishlist-col-image"><?php esc_html_e( 'Image', 'ggm-commerce' ); ?></th>
				<?php endif; ?>
				<th class="wishlist-col-name"><?php esc_html_e( 'Product', 'ggm-commerce' ); ?></th>
				<?php if ( 'yes' === $settings['show_price'] ) : ?>
					<th class="wishlist-col-price"><?php esc_html_e( 'Price', 'ggm-commerce' ); ?></th>
				<?php endif; ?>
				<?php if ( 'yes' === $settings['show_stock_status'] ) : ?>
					<th class="wishlist-col-stock"><?php esc_html_e( 'Stock', 'ggm-commerce' ); ?></th>
				<?php endif; ?>
				<?php if ( 'yes' === $settings['show_add_to_cart'] ) : ?>
					<th class="wishlist-col-actions"></th>
				<?php endif; ?>
			</tr>
		</thead>
		<tbody>
			<?php foreach ( $items as $product ) : ?>
				<tr class="ggmc-wishlist-item" data-product-id="<?php echo esc_attr( $product->id ); ?>">
					<?php if ( 'yes' === $settings['show_remove_button'] ) : ?>
						<td class="wishlist-col-remove">
							<button type="button" class="ggmc-wishlist-remove ggmc-remove-from-wishlist"
								data-product-id="<?php echo esc_attr( $product->id ); ?>"
								title="<?php esc_attr_e( 'Remove from wishlist', 'ggm-commerce' ); ?>">
								<i class="eicon-close"></i>
							</button>
						</td>
					<?php endif; ?>
					<?php if ( 'yes' === $settings['show_image'] ) : ?>
						<td class="wishlist-col-image">
							<?php if ( ! empty( $product->image_url ) ) : ?>
								<img src="<?php echo esc_url( $product->image_url ); ?>" alt="<?php echo esc_attr( $product->name ); ?>" class="wishlist-product-image">
							<?php else : ?>
								<div class="wishlist-no-image"><i class="eicon-image-bold"></i></div>
							<?php endif; ?>
						</td>
					<?php endif; ?>
					<td class="wishlist-col-name">
						<a href="<?php echo esc_url( home_url( '/product-details/' . $product->slug ) ); ?>" class="wishlist-product-name">
							<?php echo esc_html( $product->name ); ?>
						</a>
						<?php if ( ! empty( $product->sku ) ) : ?>
							<span class="wishlist-product-sku"><?php echo esc_html( 'SKU: ' . $product->sku ); ?></span>
						<?php endif; ?>
					</td>
					<?php if ( 'yes' === $settings['show_price'] ) : ?>
						<td class="wishlist-col-price">
							<?php echo esc_html( ggmc_wishlist_format_price( $product->effective_price, $currency_symbol, $currency_position ) ); ?>
							<?php if ( $product->sale_price && $product->sale_price < $product->price ) : ?>
								<del class="wishlist-original-price">
									<?php echo esc_html( ggmc_wishlist_format_price( $product->price, $currency_symbol, $currency_position ) ); ?>
								</del>
							<?php endif; ?>
						</td>
					<?php endif; ?>
					<?php if ( 'yes' === $settings['show_stock_status'] ) : ?>
						<td class="wishlist-col-stock">
							<?php ggmc_wishlist_render_stock_status( $product ); ?>
						</td>
					<?php endif; ?>
					<?php if ( 'yes' === $settings['show_add_to_cart'] ) : ?>
						<td class="wishlist-col-actions">
							<button type="button" class="ggmc-wishlist-add-to-cart ggmc-add-to-cart-btn"
								data-product-id="<?php echo esc_attr( $product->id ); ?>"
								<?php echo 'outofstock' === $product->stock_status ? 'disabled' : ''; ?>>
								<?php
								if ( ! empty( $settings['add_to_cart_icon']['value'] ) ) {
									ggmc_render_icon( $settings['add_to_cart_icon'], array( 'aria-hidden' => 'true' ) );
								} else {
									echo '<i class="fas fa-shopping-cart" aria-hidden="true"></i>';
								}
								?>
								<?php esc_html_e( 'Add to Cart', 'ggm-commerce' ); ?>
							</button>
						</td>
					<?php endif; ?>
				</tr>
			<?php endforeach; ?>
		</tbody>
	</table>
	<?php
}

/**
 * Render grid layout.
 */
function ggmc_wishlist_render_grid_layout( $widget, $items, $settings, $currency_symbol, $currency_position ) {
	$columns = isset( $settings['columns'] ) ? $settings['columns'] : '4';
	?>
	<div class="ggmc-wishlist-grid" style="--wishlist-columns: <?php echo esc_attr( $columns ); ?>;">
		<?php foreach ( $items as $product ) : ?>
			<div class="ggmc-wishlist-item ggmc-wishlist-grid-item" data-product-id="<?php echo esc_attr( $product->id ); ?>">
				<?php if ( 'yes' === $settings['show_remove_button'] ) : ?>
					<button type="button" class="ggmc-wishlist-remove ggmc-remove-from-wishlist"
						data-product-id="<?php echo esc_attr( $product->id ); ?>"
						title="<?php esc_attr_e( 'Remove from wishlist', 'ggm-commerce' ); ?>">
						<i class="eicon-close"></i>
					</button>
				<?php endif; ?>

				<?php if ( 'yes' === $settings['show_image'] ) : ?>
					<div class="wishlist-product-image-wrapper">
						<a href="<?php echo esc_url( home_url( '/product-details/' . $product->slug ) ); ?>">
							<?php if ( ! empty( $product->image_url ) ) : ?>
								<img src="<?php echo esc_url( $product->image_url ); ?>" alt="<?php echo esc_attr( $product->name ); ?>" class="wishlist-product-image">
							<?php else : ?>
								<div class="wishlist-no-image"><i class="eicon-image-bold"></i></div>
							<?php endif; ?>
						</a>
					</div>
				<?php endif; ?>

				<div class="wishlist-product-info">
					<a href="<?php echo esc_url( home_url( '/product-details/' . $product->slug ) ); ?>" class="wishlist-product-name">
						<?php echo esc_html( $product->name ); ?>
					</a>

					<?php if ( 'yes' === $settings['show_price'] ) : ?>
						<div class="wishlist-product-price">
							<?php echo esc_html( ggmc_wishlist_format_price( $product->effective_price, $currency_symbol, $currency_position ) ); ?>
							<?php if ( $product->sale_price && $product->sale_price < $product->price ) : ?>
								<del class="wishlist-original-price">
									<?php echo esc_html( ggmc_wishlist_format_price( $product->price, $currency_symbol, $currency_position ) ); ?>
								</del>
							<?php endif; ?>
						</div>
					<?php endif; ?>

					<?php if ( 'yes' === $settings['show_stock_status'] ) : ?>
						<div class="wishlist-stock-status">
							<?php ggmc_wishlist_render_stock_status( $product ); ?>
						</div>
					<?php endif; ?>

					<?php if ( 'yes' === $settings['show_add_to_cart'] ) : ?>
						<button type="button" class="ggmc-wishlist-add-to-cart ggmc-add-to-cart-btn"
							data-product-id="<?php echo esc_attr( $product->id ); ?>"
							<?php echo 'outofstock' === $product->stock_status ? 'disabled' : ''; ?>>
							<?php
							if ( ! empty( $settings['add_to_cart_icon']['value'] ) ) {
								ggmc_render_icon( $settings['add_to_cart_icon'], array( 'aria-hidden' => 'true' ) );
							} else {
								echo '<i class="fas fa-shopping-cart" aria-hidden="true"></i>';
							}
							?>
							<?php esc_html_e( 'Add to Cart', 'ggm-commerce' ); ?>
						</button>
					<?php endif; ?>
				</div>
			</div>
		<?php endforeach; ?>
	</div>
	<?php
}

/**
 * Render list layout.
 */
function ggmc_wishlist_render_list_layout( $widget, $items, $settings, $currency_symbol, $currency_position ) {
	?>
	<div class="ggmc-wishlist-list">
		<?php foreach ( $items as $product ) : ?>
			<div class="ggmc-wishlist-item ggmc-wishlist-list-item" data-product-id="<?php echo esc_attr( $product->id ); ?>">
				<?php if ( 'yes' === $settings['show_image'] ) : ?>
					<div class="wishlist-product-image-wrapper">
						<a href="<?php echo esc_url( home_url( '/product-details/' . $product->slug ) ); ?>">
							<?php if ( ! empty( $product->image_url ) ) : ?>
								<img src="<?php echo esc_url( $product->image_url ); ?>" alt="<?php echo esc_attr( $product->name ); ?>" class="wishlist-product-image">
							<?php else : ?>
								<div class="wishlist-no-image"><i class="eicon-image-bold"></i></div>
							<?php endif; ?>
						</a>
					</div>
				<?php endif; ?>

				<div class="wishlist-product-content">
					<div class="wishlist-product-info">
						<a href="<?php echo esc_url( home_url( '/product-details/' . $product->slug ) ); ?>" class="wishlist-product-name">
							<?php echo esc_html( $product->name ); ?>
						</a>

						<?php if ( ! empty( $product->short_description ) ) : ?>
							<p class="wishlist-product-description"><?php echo esc_html( wp_trim_words( $product->short_description, 15 ) ); ?></p>
						<?php endif; ?>

						<?php if ( 'yes' === $settings['show_price'] ) : ?>
							<div class="wishlist-product-price">
								<?php echo esc_html( ggmc_wishlist_format_price( $product->effective_price, $currency_symbol, $currency_position ) ); ?>
								<?php if ( $product->sale_price && $product->sale_price < $product->price ) : ?>
									<del class="wishlist-original-price">
										<?php echo esc_html( ggmc_wishlist_format_price( $product->price, $currency_symbol, $currency_position ) ); ?>
									</del>
								<?php endif; ?>
							</div>
						<?php endif; ?>

						<?php if ( 'yes' === $settings['show_stock_status'] ) : ?>
							<div class="wishlist-stock-status">
								<?php ggmc_wishlist_render_stock_status( $product ); ?>
							</div>
						<?php endif; ?>
					</div>

					<div class="wishlist-product-actions">
						<?php if ( 'yes' === $settings['show_add_to_cart'] ) : ?>
							<button type="button" class="ggmc-wishlist-add-to-cart ggmc-add-to-cart-btn"
								data-product-id="<?php echo esc_attr( $product->id ); ?>"
								<?php echo 'outofstock' === $product->stock_status ? 'disabled' : ''; ?>>
								<?php
								if ( ! empty( $settings['add_to_cart_icon']['value'] ) ) {
									ggmc_render_icon( $settings['add_to_cart_icon'], array( 'aria-hidden' => 'true' ) );
								} else {
									echo '<i class="fas fa-shopping-cart" aria-hidden="true"></i>';
								}
								?>
								<?php esc_html_e( 'Add to Cart', 'ggm-commerce' ); ?>
							</button>
						<?php endif; ?>

						<?php if ( 'yes' === $settings['show_remove_button'] ) : ?>
							<button type="button" class="ggmc-wishlist-remove ggmc-remove-from-wishlist"
								data-product-id="<?php echo esc_attr( $product->id ); ?>"
								title="<?php esc_attr_e( 'Remove from wishlist', 'ggm-commerce' ); ?>">
								<i class="eicon-close"></i> <?php esc_html_e( 'Remove', 'ggm-commerce' ); ?>
							</button>
						<?php endif; ?>
					</div>
				</div>
			</div>
		<?php endforeach; ?>
	</div>
	<?php
}

/**
 * Render stock status badge.
 */
function ggmc_wishlist_render_stock_status( $product ) {
	$status_class = 'instock' === $product->stock_status ? 'in-stock' : 'out-of-stock';
	$status_text  = 'instock' === $product->stock_status ? __( 'In Stock', 'ggm-commerce' ) : __( 'Out of Stock', 'ggm-commerce' );
	echo '<span class="stock-status ' . esc_attr( $status_class ) . '">' . esc_html( $status_text ) . '</span>';
}

/**
 * Format price with currency symbol.
 */
function ggmc_wishlist_format_price( $price, $currency_symbol, $currency_position ) {
	$formatted = number_format( (float) $price, 2 );
	return 'before' === $currency_position ? $currency_symbol . $formatted : $formatted . $currency_symbol;
}
