<?php

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

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

?>

<?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="prodigy-feature-products-slider prodigy-product-list-js prodigy-custom-template">
	<input type="hidden" class="is-featured-products-js<?php echo esc_attr( $attr['idWidget'] ?? '' ); ?>">
	<div class="prodigy-homepage__slider prodigy-homepage-slider-related">
		<div class="related-products-js<?php echo esc_attr( $attr['idWidget'] ?? '' ); ?> prodigy-homepage__slider-container
			<?php
			if ( Prodigy_Widget_Options::$bool_mapper[ $attr['slider_hide_arrows'] ] || Prodigy_Widget_Options::$bool_mapper[ $attr['slider_hide_arrows'] ] ) {
				echo ' prodigy-homepage__slider-container--hidden-arrows';
			}
			?>
		">
			<?php foreach ( $products as $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'] ?? '' ); ?>">
					<span><?php esc_html_e( 'Sale', 'prodigy-commerce' ); ?>
			</span>
					</div>
				<?php endif; ?>
				<div class="prodigy-product-list__item-container">
				<?php
				$product_url = Prodigy_Url_Helper::get_product_url_by_remote_id( (int) $product['id'] ?? '' );
				$logo_html   = get_product_logo_image_template( $attributes );
				?>
				<div <?php echo ( isset( $attr['image_ratio'] ) && $attr['image_ratio'] !== '' ) ? 'style="padding-top: ' . esc_attr( $attr['image_ratio'] ) . '%"' : ''; ?>
					class="prodigy-product-list__link-wrp">
					<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 ( Prodigy_Widget_Options::$bool_mapper[ $attr['category'] ] ) : ?>
					<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 ( $attr['rating'] === 'yes' ) : ?>
					<div class="prodigy-product-list__item-rating">
						<div class="prodigy-star-rating">
							<?php echo wp_kses( pg_get_star_rating_html( $attributes['rating'] ?? 0 ), 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 role="button"
						aria-label="<?php esc_attr_e( 'Buy this product now', 'prodigy-commerce' ); ?>"
						class="prodigy-buynow-button prodigy-main-button"
						href="<?php echo esc_url( '/buy-now?_wpnonce=' . $buy_now_nonce . '&product-id=' . $product['id'] ); ?>"
					>
						<?php esc_html_e( 'Buy now', 'prodigy-commerce' ); ?>
					</a>
					<?php endif; ?>
				</div>
			</div>
			<?php endforeach; ?>
		</div>
	</div>
	</div>
<?php endif; ?>

<script>
	jQuery(document).ready(function ($) {
		var idwidget = "<?php echo esc_attr( $attr['idWidget'] ?? '' ); ?>";
		let is_feature_products = $('.is-featured-products-js' + idwidget).length;

		if (!is_feature_products) {
			var left = 'prodigy-related__products-nav prodigy-related__products-nav--prev icon icon-arrow-left prodigy-related__products-nav--sm';
			var right = 'prodigy-related__products-nav prodigy-related__products-nav--next icon icon-arrow-right prodigy-related__products-nav--sm';
		} else {
			var left = 'prodigy-related__products-nav prodigy-related__products-nav--prev icon icon-arrow-left';
			var right = 'prodigy-related__products-nav prodigy-related__products-nav--next icon icon-arrow-right';
		}

		$('.related-products-js' + idwidget).not('.slick-initialized').slick({
			prevArrow: "<button type='button' aria-label='<?php echo esc_attr( __( 'Previous slide', 'prodigy-commerce' ) ); ?>' class='" + left + '\'></button>',
			nextArrow: "<button type='button' aria-label='<?php echo esc_attr( __( 'Next slide', 'prodigy-commerce' ) ); ?>' class='" + right + '\'></button>',
			dots: false,
			arrows: true,
			mobileFirst: true,
			responsive: [
				{
					breakpoint: 767,
					settings: {
						variableWidth: false,
						slidesToShow: 3,
						slidesToScroll: 3,
					},
				},
				{
					breakpoint: 1168,
					settings: {
						slidesToShow: <?php echo esc_attr( $columns ); ?>,
						slidesToScroll: <?php echo esc_attr( $columns ); ?>,
						variableWidth: false,
						arrows: true,
					},
				},
			],
		});
	});
</script>
