<?php
/**
 * Wishlist page template (included from shortcode).
 *
 * Variables: $data (array), $settings (array), $multiple_enabled (bool), $skynsmfa_grid_columns (int), $skynsmfa_bulk_enabled (bool).
 *
 * @package SKYNSMFA_Smart_Favorites
 */

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

$skynsmfa_layout = isset( $settings['skynsmfa_page_layout'] ) && in_array( $settings['skynsmfa_page_layout'], array( 'table', 'grid', 'compact' ), true )
	? $settings['skynsmfa_page_layout']
	: 'table';

$skynsmfa_grid_columns = isset( $skynsmfa_grid_columns ) ? max( 1, min( 6, absint( $skynsmfa_grid_columns ) ) ) : 4;
$skynsmfa_bulk_enabled = isset( $skynsmfa_bulk_enabled ) ? (bool) $skynsmfa_bulk_enabled : false;
$skynsmfa_page_attrs   = '';

if ( 'grid' === $skynsmfa_layout ) {
	$skynsmfa_page_attrs = ' style="' . esc_attr( '--skynsmfa-grid-columns:' . $skynsmfa_grid_columns . ';' ) . '"';
}

?>
<div class="skynsmfa-wishlist-page skynsmfa-wishlist-page--layout-<?php echo esc_attr( $skynsmfa_layout ); ?>"<?php echo $skynsmfa_page_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built with esc_attr above. ?>>


	<?php if ( $multiple_enabled ) : ?>
		<div class="skynsmfa-wishlist-controls">
			<button type="button" class="button button-secondary skynsmfa-create-list-btn">
				<?php echo esc_html__( 'Create new list', 'skynet-smart-favorites' ); ?>
			</button>
		</div>
	<?php endif; ?>

	<?php if ( empty( $data ) ) : ?>
		<p class="skynsmfa-empty"><?php echo esc_html__( 'Your wishlist is currently empty.', 'skynet-smart-favorites' ); ?></p>
	<?php else : ?>
		<?php foreach ( $data as $skynsmfa_list_key => $skynsmfa_items ) : ?>
			<?php
			if ( ! is_array( $skynsmfa_items ) ) {
				continue;
			}
			$skynsmfa_list_name = \SKYNSMFA\Wishlist\Session::instance()->get_list_display_name( (string) $skynsmfa_list_key );
			?>
			<section class="skynsmfa-wishlist-box" data-wishlist-key="<?php echo esc_attr( (string) $skynsmfa_list_key ); ?>">
				<div class="skynsmfa-wishlist-box__head">
					<h3 class="skynsmfa-wishlist-box__title"><?php echo esc_html( $skynsmfa_list_name ); ?></h3>
					<div class="skynsmfa-wishlist-box__actions">
						<?php if ( $skynsmfa_bulk_enabled && ! empty( $skynsmfa_items ) ) : ?>
							<button type="button" class="button skynsmfa-add-all-cart-btn" data-wishlist-key="<?php echo esc_attr( (string) $skynsmfa_list_key ); ?>">
								<?php echo esc_html__( 'Add all to cart', 'skynet-smart-favorites' ); ?>
							</button>
						<?php endif; ?>
						<button type="button" class="button button-secondary skynsmfa-rename-list-btn" data-wishlist-key="<?php echo esc_attr( (string) $skynsmfa_list_key ); ?>" data-list-name="<?php echo esc_attr( $skynsmfa_list_name ); ?>">
							<?php echo esc_html__( 'Rename list', 'skynet-smart-favorites' ); ?>
						</button>
						<?php if ( $multiple_enabled && 'default' !== $skynsmfa_list_key ) : ?>
							<button type="button" class="button button-link-delete skynsmfa-delete-list-btn" data-wishlist-key="<?php echo esc_attr( (string) $skynsmfa_list_key ); ?>">
								<?php echo esc_html__( 'Delete list', 'skynet-smart-favorites' ); ?>
							</button>
						<?php endif; ?>
					</div>
				</div>

				<?php if ( empty( $skynsmfa_items ) ) : ?>
					<p class="skynsmfa-wishlist-box__empty"><?php echo esc_html__( 'No items in this list yet.', 'skynet-smart-favorites' ); ?></p>
				<?php else : ?>
					<div class="skynsmfa-wishlist-table-wrap">
						<table class="shop_table shop_table_responsive skynsmfa-wishlist-table">
							<thead>
								<tr>
									<?php if ( $skynsmfa_bulk_enabled ) : ?>
									<th class="product-select"><input type="checkbox" class="skynsmfa-select-all" aria-label="<?php echo esc_attr__( 'Select all', 'skynet-smart-favorites' ); ?>"></th>
									<?php endif; ?>
									<th class="product-name"><?php echo esc_html__( 'Product', 'skynet-smart-favorites' ); ?></th>
									<th class="product-price"><?php echo esc_html__( 'Price', 'skynet-smart-favorites' ); ?></th>
									<th class="product-action"><?php echo esc_html__( 'Action', 'skynet-smart-favorites' ); ?></th>
									<th class="product-remove"><span class="screen-reader-text"><?php echo esc_html__( 'Remove', 'skynet-smart-favorites' ); ?></span></th>
								</tr>
							</thead>
							<tbody>
								<?php
								foreach ( $skynsmfa_items as $skynsmfa_product_id => $skynsmfa_item_data ) :
									$skynsmfa_product_id = absint( $skynsmfa_product_id );
									if ( $skynsmfa_product_id <= 0 ) {
										continue;
									}
									$skynsmfa_variation_id = 0;
									if ( is_array( $skynsmfa_item_data ) && isset( $skynsmfa_item_data['variation_id'] ) ) {
										$skynsmfa_variation_id = absint( $skynsmfa_item_data['variation_id'] );
									}
									$skynsmfa_parent_product = wc_get_product( $skynsmfa_product_id );
									if ( ! $skynsmfa_parent_product ) {
										continue;
									}
									$skynsmfa_needs_variation = $skynsmfa_parent_product->is_type( 'variable' ) && $skynsmfa_variation_id <= 0;
									$skynsmfa_display_id      = $skynsmfa_variation_id > 0 ? $skynsmfa_variation_id : $skynsmfa_product_id;
									$skynsmfa_product         = wc_get_product( $skynsmfa_display_id );
									if ( ! $skynsmfa_product ) {
										$skynsmfa_product = $skynsmfa_parent_product;
									}
									?>
									<tr class="skynsmfa-wishlist-item" data-product-id="<?php echo esc_attr( (string) $skynsmfa_product_id ); ?>" data-variation-id="<?php echo esc_attr( (string) $skynsmfa_variation_id ); ?>" data-wishlist-key="<?php echo esc_attr( (string) $skynsmfa_list_key ); ?>">
										<?php if ( $skynsmfa_bulk_enabled ) : ?>
										<td class="product-select" data-title="<?php echo esc_attr__( 'Select', 'skynet-smart-favorites' ); ?>">
											<input type="checkbox" class="skynsmfa-item-select" value="<?php echo esc_attr( (string) $skynsmfa_product_id ); ?>" aria-label="<?php echo esc_attr__( 'Select item', 'skynet-smart-favorites' ); ?>">
										</td>
										<?php endif; ?>
										<td class="product-name" data-title="<?php echo esc_attr__( 'Product', 'skynet-smart-favorites' ); ?>">
											<div class="skynsmfa-product-cell">
												<div class="skynsmfa-product-cell__thumb">
													<?php echo wp_kses_post( $skynsmfa_product->get_image( 'woocommerce_thumbnail' ) ); ?>
												</div>
												<div class="skynsmfa-product-cell__details">
													<a class="skynsmfa-product-cell__title" href="<?php echo esc_url( $skynsmfa_product->get_permalink() ); ?>"><?php echo esc_html( $skynsmfa_product->get_name() ); ?></a>
												</div>
											</div>
										</td>
										<td class="product-price" data-title="<?php echo esc_attr__( 'Price', 'skynet-smart-favorites' ); ?>">
											<?php echo wp_kses_post( $skynsmfa_product->get_price_html() ); ?>
										</td>
										<td class="product-action" data-title="<?php echo esc_attr__( 'Action', 'skynet-smart-favorites' ); ?>">
											<?php if ( $skynsmfa_needs_variation ) : ?>
												<a class="button" href="<?php echo esc_url( $skynsmfa_parent_product->get_permalink() ); ?>">
													<?php echo esc_html__( 'Select options', 'skynet-smart-favorites' ); ?>
												</a>
											<?php elseif ( $skynsmfa_product->is_in_stock() ) : ?>
												<button type="button" class="button skynsmfa-move-to-cart-btn" data-product-id="<?php echo esc_attr( (string) $skynsmfa_product_id ); ?>" data-wishlist-key="<?php echo esc_attr( (string) $skynsmfa_list_key ); ?>">
													<?php echo esc_html__( 'Add to cart', 'skynet-smart-favorites' ); ?>
												</button>
											<?php else : ?>
												<span class="skynsmfa-out-of-stock"><?php echo esc_html__( 'Out of stock', 'skynet-smart-favorites' ); ?></span>
											<?php endif; ?>
										</td>
										<td class="product-remove" data-title="<?php echo esc_attr__( 'Remove', 'skynet-smart-favorites' ); ?>">
											<a href="#" class="skynsmfa-remove-btn" aria-label="<?php echo esc_attr__( 'Remove this item', 'skynet-smart-favorites' ); ?>" data-product-id="<?php echo esc_attr( (string) $skynsmfa_product_id ); ?>" data-wishlist-key="<?php echo esc_attr( (string) $skynsmfa_list_key ); ?>">&times;</a>
										</td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					</div>

					<?php if ( $skynsmfa_bulk_enabled ) : ?>
					<div class="skynsmfa-wishlist-actions">
						<button type="button" class="button button-primary skynsmfa-bulk-move-btn" data-wishlist-key="<?php echo esc_attr( (string) $skynsmfa_list_key ); ?>">
							<?php echo esc_html__( 'Move selected to cart', 'skynet-smart-favorites' ); ?>
						</button>
					</div>
					<?php endif; ?>
				<?php endif; ?>
			</section>
		<?php endforeach; ?>
	<?php endif; ?>
</div>
