<?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' ][ 'list_widget_image_width' ];
	$height			 = $at_settings[ 'product_images' ][ 'list_widget_image_height' ];
	$showCategory	 = $at_settings[ 'products_listing' ][ 'category_name' ];
	$showOutofstock	 = $at_settings[ 'products_listing' ][ 'outofstock' ];
	$showAvailability = $this->getShowStockStatus();
	$hidePrice = $this->getHidePrice();
	$hideName = $this->getHideName();
	$showTitle		 = $this->getShowTitle();
	$titleTag		 = $this->getTitleTag() ?: 'strong';
	$classes		 = [];
	$classes[]		 = 'products-grid__buttons-below products-widget__list products-widget__list-long';
	if($this->getBorderedStyle()){
		$classes[]		 = 'product-border-' . $this->getBorderedStyle();
	}
	$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->getReviewCount() ? 'show-review-count' : '';
	$classes[]		 = $this->getTitleSideLine() ? 'title--sideline' : '';
	$classes[]		=  $this->getCustomClass();
	$type			 = 'widget-product-list';
	$mode			 = 'list';
	$image			 = 'ox_list_widget_product_thumbnail';
	$image_hover	 = 'ox_list_widget_product_thumbnail_hover';
	$items			 = $block->getProductCollection()->getItems();
	$showWishlist	 = $this->getShowWishlist();
	$showCompare	 = $this->getShowCompare();
	$showCart		 = $this->getShowAddtocart();
	$showReview		 = $this->getShowReview();
	$templateType	 = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
	$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>';
	$showNum = $this->getShowNum();
	
	?>
	<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 row no-gutters <?= /* @noEscape */ $type ?>">
					<?php $iterator = 1; ?>
					<?php foreach ( $items as $_item ): ?>
						<?= /* @noEscape */ ($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>
						<?php $productOutOfStock = $_item->isAvailable() ? '' : ' product-item-unavailable'; ?>
						<?php $productImage = $img_helper->getResizedImageHover($_item, $image, $image_hover, [$width, $height]); ?>
						<div class="product-item-info d-flex flex-wrap align-items-center <?= $escaper->escapeHtmlAttr($productOutOfStock) ?>">
							<?php if($showNum): ?>
							<div class="list-product-number d-flex align-items-center justify-content-center a-center"><span class="h1"><?php echo ($iterator - 1)  ?></span></div>
							<?php endif; ?>
							<div class="product-grid__image-wrapper">
								<a href="<?= $block->escapeUrl( $block->getProductUrl( $_item ) ) ?>" class="product-item-photo">
									<?= $productImage->toHtml() ?>	
								</a>
								<?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  align-items-center product-item-details__first">
								<div class="product-item__col">
									<?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 ( !$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 ( $templateType && $showReview ): ?>
										<?= $block->getReviewsSummaryHtml( $_item, $templateType )
										?>
									<?php endif; ?>			

									<?php if ( $showAvailability && $_item->isAvailable()): ?>
										<div class="stock-info-block">
											<div class="stock available"><span><?= $block->escapeHtml(__('In stock')) ?></span></div>
										</div>
									<?php endif; ?>
								</div>

							</div>
							<div class="product-item-details d-flex  align-items-center product-item-details__right">
								<?php if ( !$hidePrice ) : ?>
									<div class="product-item__col">
										<?php								
										echo $block->getProductPriceHtml( $_item, $type );
										echo $block->getMSRPHtml( $_item );
										?>
									</div>
								<?php endif; ?>
								<?php if ($showWishlist || $showCompare || $showCart): ?>
									<div class="product-item__col product actions product-item-actions">
										<?php if ($_item->isSaleable()): ?>
											<?php if ($showCart): ?>
												<div class="actions-primary">	
													<?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>
												</div>
											<?php endif; ?>
										<?php else :?>
											<?php if (!$_item->isAvailable() && $showOutofstock) :?>
												<div class="stock unavailable"><span><?= $block->escapeHtml(__('Out of stock')) ?></span></div>
											<?php endif; ?>
										<?php endif; ?>
										
										<?php if ( $showWishlist || $showCompare ): ?>
											<div class="actions-secondary d-inline-flex" 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>
								<?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; ?>
	</div>
<?php endif; ?>
