<?php

/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
// @codingStandardsIgnoreFile
use Magento\Framework\App\Action\Action;

/** @var \Magento\CatalogWidget\Block\Product\ProductsList $block */
?>
<?php if ( $exist = ($block->getProductCollection() && $block->getProductCollection()->getSize()) ): ?>
	<?php
	$_helper = $this->helper(\Magento\Catalog\Helper\Output::class);
	$theme_helper	 = $this->helper( \Olegnax\Athlete2\Helper\ProductList::class );
	$img_helper = $this->helper(\Olegnax\Athlete2\Helper\ProductImage::class);
	$at_settings	 = $theme_helper->getModuleConfig();
	$ajax_addtocart	 = $at_settings[ 'products_listing' ][ 'catalog_ajax_add_to_cart' ];
	$width			 = $at_settings[ 'product_images' ][ 'listing_image_width' ];
	$height			 = $at_settings[ 'product_images' ][ 'listing_image_height' ];
	$showCategory	 = $at_settings[ 'products_listing' ][ 'category_name' ];
	$showOutofstock	 = $at_settings[ 'products_listing' ][ 'outofstock' ];
	$showAvailability = $this->getShowStockStatus();
	$showDescMoreLink = false;
	$showBrand		 = $at_settings[ 'products_listing' ][ 'brand' ];
	$descSource		 = $at_settings['products_listing']['description_on_grid_source'];
	if ( $showBrand ) {
		$brandImageSize		 = '';
		$altBrandImage		 = !$at_settings[ 'products_listing' ][ 'brand_image_alt' ];
		$brandImageWidth	 = $at_settings[ 'products_listing' ][ 'brand_image_width' ];
		$brandImageHeight	 = $at_settings[ 'products_listing' ][ 'brand_image_height' ] ?: $at_settings[ 'products_listing' ][ 'brand_image_width' ];
		if ( $brandImageWidth && $brandImageHeight ) {
			$brandImageSize = [$brandImageWidth, $brandImageHeight];
		}
	}
	$thumbSlider = $this->getThumbCarousel();
	if($thumbSlider){
		$thumbSliderShowDots = $this->getThumbCarouselShowDots();
		$thumbSliderDotsPos = $this->getThumbCarouselDotsPos();
		$thumbOptions['carousel'] = true;
		$thumbOptions['selected_only'] = $this->getThumbCarouselLogic() ?: false;
		$thumbOptions['max_items'] = $this->getThumbCarouselMaxItems() ?: '';
		$thumbOptions['min_items'] = $minThumbs = $this->getThumbCarouselMinItems() ?: 2;
		$img_helper->setOptions($thumbOptions);
	}
	$hidePrice = $this->getHidePrice();
	$hideName = $this->getHideName();
    $showSwatches = $this->getShowSwatches();
	$showTitle		 = $this->getShowTitle();
	$titleTag		 = $this->getTitleTag() ?: 'strong';
	$classes		 = [];
	if($this->getBorderedStyle()){
		$classes[]		 = 'product-border-' . $this->getBorderedStyle();
	}
	$classes[]		 = 'products-grid__layout-default products-grid__style6 products-grid__details-bottom products-grid__big-btns';
	$classes[]		 = 'product-columns-' . $this->getColumnsDesktop();
	$classes[]		 = 'product-columns-l-' . $this->getColumnsDesktopSmall();
	$classes[]		 = 'product-columns-m-' . $this->getColumnsTablet();
	$classes[]		 = 'product-columns-s-' . $this->getColumnsMobile();
	$classes[]		 = 'title--align-' . $this->getTitleAlign();
	$classes[]		 = $this->getProductsLayoutCentered() ? 'align-details-center' : '';
	$classes[]		 = $this->getReviewCount() ? 'show-review-count' : '';
	$classes[]		 = $this->getQuickviewPosition() ? ('quickview-' . $this->getQuickviewPosition()) : '';
	$classes[]		=  $this->getCustomClass();
	$classes[]		 = $this->getTitleSideLine() ? 'title--sideline' : '';
	$classes[] 		 = $thumbSlider ? ('thumb-slider-on') : '';
	$type			 = 'widget-product-grid';
	$mode			 = 'grid';
	$image			 = 'new_products_content_widget_grid';
	$image_hover	 = 'category_page_grid_hover';
	$items			 = $block->getProductCollection()->getItems();
	$showWishlist	 = $this->getShowWishlist();
	$showCompare	 = $this->getShowCompare();
	$showCart		 = $this->getShowAddtocart();
	$showQuickview	 = $this->getShowQuickview() && $theme_helper->isActivePlugin( 'Olegnax_Quickview' );
	$quickviewBtnClass  = $this->getQuickviewButtonStyle() ?: '';
	$showReview		 = $this->getShowReview();
	$templateType	 = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
	$description	 = $this->getShowDesc();
	$cartIcon = '<svg version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" class="a2-icon-cart"><use xlink:href="#a2-cart-icon"></use></svg>';
	$compareIcon	 = '<svg version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" class="a2-icon-compare"><use xlink:href="#a2-compare-icon"></use></svg>';
	$wishlistIcon	 = '<svg version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" class="a2-icon-wishlist"><use xlink:href="#a2-wishlist-icon"></use></svg>';

	$gridBannerEnabled	= false;
	if($block->getBannersEnabled()){
		$bannersList = $block->getBannersData();
		$gridBannerEnabled = is_array($bannersList) && !empty($bannersList);
	}
	?>
	<div id="ox_<?= $block->getNameInLayout() ?>" class="block widget block-products-list <?= /* @noEscape */ $mode ?> <?= $block->escapeHtmlAttr(implode( ' ', $classes) )
	?>">
		<?php if ( $showTitle && $block->getTitle() ): ?>
			<div class="block-title">
				<<?= $titleTag ?>><?= $block->escapeHtml( __( $block->getTitle() ) ) ?></<?= $titleTag ?>>
			</div>
		<?php endif ?>
		<div class="block-content">
			<div class="products-<?= /* @noEscape */ $mode ?> <?= /* @noEscape */ $mode ?>">
				<ol class="product-items <?= /* @noEscape */ $type ?>">
					<?php $iterator = 1; ?>
					<?php foreach ( $items as $_item ): ?>
						<?php if ($gridBannerEnabled): ?>
							<?php if(array_key_exists($iterator, $bannersList)): ?>			
									<li class="product-item product-item-with-banner">
										<?= $block->getGridBanner($bannersList[$iterator]) ?>
									</li>
								<?php $iterator++; ?>
							<?php endif; ?>
						<?php endif; ?>
						<?= /* @noEscape */ ($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>
						<?php $productOutOfStock = $_item->isAvailable() ? '' : ' product-item-unavailable'; ?>
						<div class="product-item-info <?= $escaper->escapeHtmlAttr($productOutOfStock) ?>">
							<div class="product-grid__image-wrapper">
								<a href="<?= $block->escapeUrl( $block->getProductUrl( $_item ) ) ?>" class="product-item-photo">
									<?php
										if($thumbSlider){
											$productImage = $img_helper->getResizedCatalogImages($_item, $image, $image_hover, [$width, $height] );
										} else{
											/* not necessarily anymore but left it in case some data is passed in arg for image with hover template */
											$productImage = $img_helper->getResizedImageHover($_item, $image, $image_hover, [$width, $height]);
										}
										$_thumbSlider = $thumbSlider && (bool)($img_helper->getImagesCount() >= $minThumbs);
									?>
									<?= $productImage->toHtml() ?>	
								</a>
								<div class="product-item-inner">
									<a href="<?= $block->escapeUrl( $block->getProductUrl( $_item ) ) ?>" aria-label="View Product" class="overlay-link"><?= $block->escapeHtml( $_item->getName() ) ?></a>
									<?php if($_thumbSlider): ?>
										<?php if($thumbSliderDotsPos != 'below'){ echo $img_helper->getThumbImageDots($thumbSliderShowDots); } ?>
										<?= $block->fetchView($block->getTemplateFile('Magento_Catalog::product/list/thumb-arrows.phtml')) ?>
									<?php endif; ?>
								</div>
								<?php if ( $theme_helper->isActivePlugin( 'Olegnax_ProductLabel' ) ) {
									echo $this->helper( 'Olegnax\ProductLabel\Helper\Helper' )->showLabels( $_item );
								}  ?>
							</div>
							<div class="product-grid-overlay"></div>
							<div class="product-item-details -top actions-stretch">
								<?php if( $_thumbSlider && $thumbSliderDotsPos === 'below'){ echo $img_helper->getThumbImageDots($thumbSliderShowDots); } ?>
								<?php if ( $showBrand && $theme_helper->isActivePlugin( 'Olegnax_BrandSlider' ) ) { ?>
									<div class="ox-product-grid__brand-wrap">
									<?php echo $this->helper( 'Olegnax\BrandSlider\Helper\Helper' )->getProductBrandImage( $_item, $altBrandImage, $brandImageSize ); ?>
									</div>
								<?php }?>

								<?php if ( $showCategory && $category = $theme_helper->getLastCategory( $_item ) ) : ?>
								<div class="ox-product-grid__categories"><a href="<?= $category->getUrl(); ?>" class="ox-product-grid__category-link"><?= $block->escapeHtmlAttr($category->getName()); ?></a></div>
								<?php endif; ?>
								
								<?php if ( $templateType && $showReview ): ?>
									<?= $block->getReviewsSummaryHtml( $_item, $templateType )
									?>
								<?php endif; ?>	

								<?php if ( !$hideName ) : ?>
									<strong class="product-item-name">
										<a title="<?= $block->escapeHtml( $_item->getName() ) ?>"
										href="<?= $block->escapeUrl( $block->getProductUrl( $_item ) ) ?>"
										class="product-item-link">
									<?= $block->escapeHtml( $_item->getName() ) ?>
										</a>
									</strong>
								<?php endif; ?>

								<?php 
								if($descSource === 'catalog-short'){
									$descContent = $_helper->productAttribute($_item,  $_item->getShortDescriptionCatalog(), 'short_description_catalog');
								} else {
									$descContent = $_item->getShortDescription();
								} ?>
								<?php if ($description && !empty($descContent)):?>
									<div class="product description product-item-description">
										<?= /* @noEscape */ $descContent ?>
										<?php if($showDescMoreLink): ?>
										<a href="<?= $block->escapeUrl($block->getProductUrl( $_item)) ?>"
										title="<?= $block->escapeHtml( $_item->getName() ) ?>"
										class="action more"><?= $block->escapeHtml(__('Learn More')) ?></a>
										<?php endif; ?>
									</div>
								<?php endif; ?>
													
								<?php if ($_item->isAvailable()): ?>			
									<?php if ( $showAvailability ): ?>
										<div class="stock-info-block">
											<div class="stock available"><span><?= $escaper->escapeHtml(__('In stock')) ?></span></div>
										</div>
									<?php endif; ?>
								<?php else:?>
									<div class="stock-info-block">
										<div class="stock unavailable"><span><?= $escaper->escapeHtml(__('Out of stock')) ?></span></div>
									</div>
								<?php endif; ?>

								<?php if ($showSwatches):?>
                                    <?= $block->getProductDetailsHtml($_item) ?>
                                <?php endif; ?>
							</div>
							<div class="product-item-details -footer actions-stretch actions-stretch-2edges">
								<?php
								if ( !$hidePrice ) :
								echo $block->getProductPriceHtml( $_item, $type );
								echo $block->getMSRPHtml( $_item );
								endif;
								?>
							
								<?php if ( $showCart || $showWishlist || $showCompare || $showQuickview ): ?>
									<div class="product actions product-item-actions  show-text">
										<?php if ( $showWishlist || $showCompare ): ?>
											<div class="actions-secondary" data-role="add-to-links">
												<?php if ( $this->helper( \Magento\Wishlist\Helper\Data::class )->isAllow() && $showWishlist ): ?>
													<a href="#"
													   data-post='<?= /* @noEscape */ $block->getAddToWishlistParams( $_item ) ?>' class="action towishlist" data-action="add-to-wishlist" title="<?= $block->escapeHtmlAttr( __( 'Add to Wish List' ) ) ?>">
													   <?= $wishlistIcon ?><span><?= $block->escapeHtml( __( 'Add to Wish List' ) ) ?></span>
													</a>
												<?php endif; ?>
												<?php if ( $block->getAddToCompareUrl() && $showCompare ): ?>
													<?php $compareHelper = $this->helper( \Magento\Catalog\Helper\Product\Compare::class ); ?>
													<a href="#" class="action tocompare" data-post='<?= /* @noEscape */ $compareHelper->getPostDataParams( $_item ) ?>' title="<?= $block->escapeHtmlAttr( __( 'Add to Compare' ) ) ?>">
														<?= $compareIcon ?><span><?= $block->escapeHtml( __( 'Add to Compare' ) ) ?></span>
													</a>
												<?php endif; ?>
											</div>
										<?php endif; ?>
										<div class="actions-primary">	
											<?php
											if ( $showQuickview ) {
												echo $this->helper( 'Olegnax\Quickview\Helper\Helper' )->getButton( $_item, 'button quick-view ' . $quickviewBtnClass );
											}
											?>
											<?php if ($showCart && $_item->isSaleable()): ?>
												<?php $postParams = $block->getAddToCartPostParams( $_item ); ?>
												<form data-role="tocart-form" data-product-sku="<?= $block->escapeHtmlAttr($_item->getSku()) ?>" action="<?= $block->escapeUrl($postParams['action']) ?>" method="post">
													<input type="hidden" name="product" value="<?= $block->escapeHtmlAttr($postParams['data']['product']) ?>">
													<input type="hidden" name="<?= /* @noEscape */ Action::PARAM_NAME_URL_ENCODED ?>" value="<?= /* @noEscape */ $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
													<?= $block->getBlockHtml( 'formkey' ) ?>
													<button type="submit"
															title="<?= $block->escapeHtmlAttr( __( 'Add to Cart' ) ) ?>"
															class="action tocart primary">
														<?= $cartIcon ?><span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
													</button>
												</form>
											<?php endif; ?>
										</div>
									</div>								

								<?php endif; ?>

							</div>
						</div>
						<?= ($iterator == count( $items ) + 1) ? '</li>' : '' ?>
					<?php endforeach ?>
				</ol>
			</div>
			<?= $block->getPagerHtml() ?>
		</div>
		<?php if ( isset( $_item ) && $showCart && !$block->isRedirectToCartEnabled() && !$ajax_addtocart ): ?>
			<script type="text/x-magento-init">
				{
				"[data-role=tocart-form], .form.map.checkout": {
				"catalogAddToCart": {
				"product_sku": "<?= /* @NoEscape */ str_replace('"','\"', $_item->getSku() ) ?>"
				}
				}
				}
			</script>
		<?php endif; ?>
		<?php if($thumbSlider): ?>
		<script type="text/x-magento-init">{"*":{"ox-thumb-carousel":{}}}</script> 
		<?php endif; ?>
	</div>
<?php endif; ?>
