<?php

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

use Prodigy\Includes\Helpers\Prodigy_Formatting;
use Prodigy\Includes\Helpers\Prodigy_Platform_Settings;
use Prodigy\Includes\Helpers\Prodigy_Widget_Options;
use Prodigy\Includes\Helpers\Prodigy_Url_Helper;
use Prodigy\Includes\Prodigy_Pagination;

?>

<?php if ( empty( $products ) ) : ?>
	<div class="prodigy-products__no-sales-message">
		<p class="prodigy-products__no-sales-message-title">
			<?php esc_html_e( 'There are no products available at the moment.', 'prodigy-commerce' ); ?>
		</p>
	</div>
<?php else : ?>

<div class="feature-products__container prodigy-custom-template">
	<input type="hidden" class="is-featured-products-js">
	<div class="prodigy-product-list__grid prodigy-product-list__grid-<?php echo esc_attr( $columns ); ?>">
		<?php foreach ( $products as $key => $product ) : ?>
			<?php $attributes = $product['attributes']; ?>
			<div class="prodigy-product-list__item">
				<?php if ( ! empty( $attributes['sale-price'] ) && Prodigy_Widget_Options::$bool_mapper[ $attr['sale'] ] ) : ?>
					<div class="<?php echo esc_attr( $attr['products_sale_classname'] ?? '' ); ?>">
						<?php esc_html_e( 'SALE', 'prodigy-commerce' ); ?>
					</div>
				<?php endif; ?>
				<div class="prodigy-product-list__item-container">
					<div <?php echo ( $attr['image_ratio'] !== '' ) ? 'style="padding-top: ' . esc_attr( $attr['image_ratio'] ) . '%"' : ''; ?>
							class="prodigy-product-list__link-wrp">
						<?php
						$product_url = Prodigy_Url_Helper::get_product_url_by_remote_id( (int) $product['id'] ?? '' );
						$logo_html   = get_product_logo_image_template( $attributes );
						?>
						<a class="prodigy-product-list__item-preview"
							rel="nofollow noopener"
							href="<?php echo esc_url( $product_url ); ?>"
						>
							<span class="prodigy-visually-hidden"><?php echo esc_html( $attributes['name'] ?? '' ); ?></span>
							<i class="icon icon-image" aria-hidden="true" role="presentation"></i>
							<?php echo $logo_html; ?>
						</a>
					</div>
					<?php if ( $attr['category'] === 'yes' ) : ?>
						<p class="prodigy-product-list__item-category">
						<?php echo esc_html( $attributes['categories-list'] ?? '' ); ?>
						</p>
					<?php endif; ?>
					<p class="prodigy-product-list__item-title">
						<a href="<?php echo esc_url( $product_url ); ?>"
							rel="nofollow noopener">
							<?php echo esc_html( $attributes['name'] ?? '' ); ?>
						</a>
					</p>
					<?php if ( Prodigy_Widget_Options::$bool_mapper[ $attr['rating'] ?? false ] ) : ?>
						<div class="prodigy-product-list__item-rating">
							<div class="prodigy-star-rating">
								<?php echo wp_kses( pg_get_star_rating_html( $attributes['rating'] ?? '' ), Prodigy_Formatting::get_allowed_html() ); ?>
							</div>
						</div>
					<?php endif; ?>
					<?php if ( Prodigy_Platform_Settings::is_redemption_store_status() ) : ?>
						<?php do_action( 'prodigy_products_price_block', array( 'attributes' => $attributes ) ); ?>
					<?php endif; ?>
			<?php
			if (
			! $product['attributes']['has-personalization'] && $product['attributes']['without-variants'] &&
			( isset( $product['relationships']['logos']['data'] ) && empty( $product['relationships']['logos']['data'] ) ) &&
			isset( $attr['buynow'] ) && Prodigy_Widget_Options::$bool_mapper[ $attr['buynow'] ] &&
			( isset( $product['attributes']['tiered-price'] ) && ! $product['attributes']['tiered-price'] ) ) :
				?>
						<a href="<?php echo esc_url( '/buy-now?_wpnonce=' . $buy_now_nonce . '&product-id=' . $product['id'] ); ?>"
							role="button"
							class="prodigy-buynow-button prodigy-main-button"
							aria-label="<?php esc_attr_e( 'Buy this product now', 'prodigy-commerce' ); ?>">
								<?php esc_html_e( 'Buy now', 'prodigy-commerce' ); ?>
						</a>
					<?php endif; ?>
				</div>
			</div>
		<?php endforeach; ?>
	</div>
	<div class="prodigy-pagination">
		<?php Prodigy_Pagination::prodigy_pagination( $pagination['pagination'] ?? array(), $pagination['pagination']['pages'] ?? 1 ); ?>
	</div>
</div>
<?php endif; ?>
