<?php
/* Template Name: Related shortcode */

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

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} ?>
<?php
$placeholder_url = plugin_dir_url( PRODIGY_PLUGIN_PATH . 'web/admin/images/placeholder.png' ) . 'placeholder.png';
$columns         = $args['columns'] ?? 4;

$image_ratio = Prodigy_Customizer::get_images_ratio();
?>
<div class="prodigy-custom-template">
	<div class="row">
		<div class="col-lg-12">
			<div class="prodigy-related">
				<input type="hidden" class="feature-product-columns-number-js" value="<?php echo esc_attr( $columns ); ?>">
				<div class="prodigy-related__products-container related-products-js">
					<?php
					foreach ( $products as $product ) :
						$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'] );
						?>
					<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 ) ); ?>"
							rel="nofollow noopener"
							class="prodigy-related__products-img"
						>
							<span class="prodigy-visually-hidden"><?php echo esc_html( $title ); ?></span>
							<?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_html( $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"
								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>

					<?php endforeach; ?>
				</div>
			</div>
		</div>
	</div>

</div>
