<?php

use Prodigy\Includes\Models\Prodigy_Products;
use Prodigy\Includes\Support\Customizer\Prodigy_Customizer;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
$image_ratio = Prodigy_Customizer::get_images_ratio();
?>
<div class="prodigy-custom-template">
	<div class="row">
		<div class="col-lg-8 offset-lg-2">
			<div class="row prodigy-product-list">
				<!-- map columns -->
				<!-- col-sm-<number_of_columns> -->
				<!-- number_of_columns - 12 / one of [1, 2, 3, 4]  -->
				<?php foreach ( $products as $product ) : ?>
					<?php
						$img           = $product->attributes->{'versions-image-url'};
						$title         = $product->attributes->{'name'};
						$price         = $product->attributes->{'price'};
						$sale_price    = $product->attributes->{'sale-price'};
						$local_product = Prodigy_Products::get_product_meta_by_remote_id( 'prodigy_remote_product_id', (int) $product['id'] );
						$columns       = $args['columns'] ?? 4;
					?>
				<div class="col-sm-6 col-md-<?php echo 12 / (int) $columns; ?> ">
					<div class="prodigy-related__products-item">
						<?php if ( isset( $local_product->post_id ) ) : ?>
						<a href="<?php echo esc_url( get_permalink( $local_product->post_id ) ); ?>"
							class="prodigy-related__products-img"
							rel="nofollow noopener"
							<?php /* translators: %s: product title */ ?>
							aria-label="<?php echo esc_attr( sprintf( __( 'View details for product %s', 'prodigy-commerce' ), $title ) ); ?>"
						>
							<?php if ( isset( $img ) ) : ?>
							<img loading="lazy" width="60"
								srcset="<?php echo esc_attr( $img->thumbnails ); ?> 1x, <?php echo esc_attr( $img->thumbnails_retina ); ?> 2x"
								src="<?php echo esc_url( $img->thumbnails ); ?>"
								alt="<?php echo esc_attr( $title ); ?>"
							>
							<?php else : ?>
							<img loading="lazy" width="60" src="<?php echo esc_url( $placeholder_url ); ?>" alt="<?php echo esc_attr( 'Empty image', 'prodigy-commerce' ); ?>">
							<?php endif; ?>
						</a>
						<?php endif; ?>
						<div>
							<?php if ( isset( $local_product->post_id ) ) : ?>
							<a href="<?php echo esc_url( get_permalink( $local_product->post_id ) ); ?>"
								rel="nofollow noopener"
								<?php /* translators: %s: product title */ ?>
								aria-label="<?php echo esc_attr( sprintf( __( 'View details for product %s', 'prodigy-commerce' ), $title ) ); ?>"
								class="prodigy-related__products-item-title"
							>
								<?php echo esc_html( $title ); ?>
							</a>
							<?php endif; ?>
							<div class="prodigy-related__products-item-price">
								<?php do_action( 'prodigy_products_price_block', array( 'attributes' => $product ) ); ?>
							</div>
						</div>
					</div>
				</div>
				<?php endforeach; ?>
				<!-- map columns -->
			</div>
		</div>
	</div>
</div>
