<?php

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

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

<?php if ( ! empty( $products ) ) : ?>
	<div class="prodigy-custom-template">
		<h2 class="prodigy-related__products-title">
			<?php if ( $heading ) : ?>
				<?php echo esc_html( $heading ); ?>
			<?php endif; ?>
		</h2>
	</div>
<?php endif; ?>

<div class="related-products__container prodigy-custom-template container-fluid">
	<input type="hidden" class="feature-product-columns-number-js" value="<?php echo esc_attr( $settings['columns'] ) ?? 4; ?>">
	<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 ) :
			$product = (array) $product;
			?>
			<div class="prodigy-product-list__item">
				<?php if ( ! empty( $product['sale-price'] ) && Prodigy_Widget_Options::$bool_mapper[ $settings['sale'] ] ) : ?>
					<div class="<?php echo esc_attr( $sale_classname ); ?>"><span><?php esc_html_e( 'Sale', 'prodigy-commerce' ); ?></span></div>
				<?php endif; ?>
				<div class="prodigy-product-list__item-container">
					<div <?php echo ( $image_ratio !== '' ) ? 'style="padding-top: ' . esc_attr( $image_ratio ) . '%"' : ''; ?> class="prodigy-product-list__link-wrp">
						<a class="prodigy-product-list__item-preview" href="<?php echo esc_url( $product['local_url'] ?? '' ); ?>"
							rel="nofollow noopener"
						>
							<span class="prodigy-visually-hidden"><?php echo esc_html( $product['name'] ?? '' ); ?></span>
							<i class="icon icon-image" aria-hidden="true" role="presentation"></i>
							<?php echo get_product_logo_image_template( $product ); ?>
						</a>
					</div>
					<?php if ( $settings['category'] === 'yes' ) : ?>
						<p class="prodigy-product-list__item-category">
							<?php echo esc_html( $product['category'] ); ?>
						</p>
					<?php endif; ?>
					<p class="prodigy-product-list__item-title">
						<a href="<?php echo esc_attr( $product['local_url'] ?? '' ); ?>"
							rel="nofollow noopener">
							<?php echo esc_html( $product['name'] ); ?>
						</a>
					</p>
					<?php if ( Prodigy_Widget_Options::$bool_mapper[ $settings['rating'] ] ) : ?>
						<div class="prodigy-product-list__item-rating">
							<div class="prodigy-star-rating">
								<?php echo wp_kses( pg_get_star_rating_html( $product['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' => $product ) ); ?>
					<?php endif; ?>
				</div>
			</div>
		<?php endforeach; ?>
	</div>
	<div class="prodigy-pagination">
		<?php Prodigy_Pagination::prodigy_pagination( $pagination ?? array(), $pagination['pages'] ?? 1 ); ?>
	</div>
</div>
