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

?>
<?php
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis

/**
 * Product list template
 *
 * @var $block \Magento\Catalog\Block\Product\ListProduct
 */
?>
<?php
$_productCollection = $block->getLoadedProductCollection();
$_helper = $this->helper(Magento\Catalog\Helper\Output::class);
?>
<?php if (!$_productCollection->count()) :?>
    <div class="message info empty"><div><?= $block->escapeHtml(__('We can\'t find products matching the selection.')) ?></div></div>
<?php else :?>
    <?= $block->getToolbarHtml() ?>
    <?= $block->getAdditionalHtml() ?>
    <?php
    if ($block->getMode() == 'grid') {
        $viewMode = 'grid';
        $imageDisplayArea = 'category_page_grid';
        $imageDisplayAreaHover = 'category_page_grid_hover';
        $showDescription = false;
        $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
    } else {
        $viewMode = 'list';
        $imageDisplayArea = 'category_page_list';
        $imageDisplayAreaHover = 'category_page_list_hover';
        $showDescription = true;
        $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::FULL_VIEW;
    }
    /**
     * Position for actions regarding image size changing in vde if needed
     */
    $pos = $block->getPositioned();
    ?>
    <div class="products wrapper <?= /* @noEscape */ $viewMode ?> products-<?= /* @noEscape */ $viewMode ?>">
        <ol class="products list items product-items container-products-switch" data-view-mode="4">
            <?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
            <?php foreach ($_productCollection as $_product) :?>
            <li class="item product product-item">
                <div class="product-item-info" data-container="product-<?= /* @noEscape */ $viewMode ?>">
                    <?php
                    $productImage = $block->getImage($_product, $imageDisplayArea);
                    if ($pos != null) {
                        $position = ' style="left:' . $productImage->getWidth() . 'px;'
                            . 'top:' . $productImage->getHeight() . 'px;"';
                    }
                    ?>
                    <?php // Product Image ?>
					<?php if ($block->getMode() == 'list') { ?><div class="product photo product-item-photo"><?php } ?>
						<a href="<?= $block->escapeUrl($_product->getProductUrl()) ?>"
						   class="product-photo photo product-item-photo product-list-image"
						   tabindex="-1">
							<?= $productImage->toHtml() ?>
							<?php $productImageHover = $block->getImage($_product, $imageDisplayAreaHover); ?>
							<img class="image_hover" src="<?= $productImageHover->getImageUrl(); ?>" />
						</a>
						<div class="product-label">
							<?php if ( $_product->getFinalPrice() < $_product->getPrice() ): ?>
								<span class="onsale">
									<span class="value"><?= -100 + round(($_product->getFinalPrice() / $_product->getPrice() * 100)) ?>%</span>
								</span>
							<?php endif; ?>
							<?php $todayDate = time(); 
								$timeStartNew = strtotime($_product->getData('news_from_date'));
								$timeEndNew = strtotime($_product->getData('news_to_date')); ?>
							<?php if ( $timeEndNew >= $todayDate && $timeStartNew <= $todayDate): ?>
								<span class="newlabel">
									<span><?php echo __('New') ?></span>
								</span>
							<?php endif; ?>
						</div>
						<?php if ($block->getMode() == 'list') { ?>
							<?php if ($_product->isAvailable()) :?>
								<?= $block->getProductDetailsHtml($_product) ?>
							<?php endif; ?>
						<?php } ?>
					<?php if ($block->getMode() == 'list') { ?></div><?php } ?>
                    <div class="product details product-item-details">
						<?php if ($block->getMode() == 'list') { ?><div class="flex-layout no-wrap-desktop space-between"><?php } ?>
							<?php if ($block->getMode() == 'list') { ?><div class="list-product-infor"><?php } ?>
								<?php if ($block->getMode() == 'grid') { ?>
									<?php if ($_product->isAvailable()) :?>
										<?= $block->getProductDetailsHtml($_product) ?>
									<?php endif; ?>
								<?php } ?>
								<?php
									$_productNameStripped = $block->stripTags($_product->getName(), null, true);
								?>
								<strong class="product name product-item-name">
									<a class="product-item-link"
									   href="<?= $block->escapeUrl($_product->getProductUrl()) ?>">
										<?= /* @noEscape */ $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
									</a>
								</strong>
								<?= $block->getReviewsSummaryHtml($_product, $templateType) ?>
								<?php if ($block->getMode() == 'grid') { ?>
									<?= /* @noEscape */ $block->getProductPrice($_product) ?>
								<?php } ?>
								<div class="product-item-inner">
									<?php if ($showDescription) :?>
										<div class="product description product-item-description">
											<?= /* @noEscape */ $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
											<a href="<?= $block->escapeUrl($_product->getProductUrl()) ?>"
											   title="<?= /* @noEscape */ $_productNameStripped ?>"
											   class="action more"><?= $block->escapeHtml(__('Learn More')) ?></a>
										</div>
									<?php endif; ?> 
									<?php if ($block->getMode() == 'grid') { ?>
										<div class="product actions product-item-actions"<?= strpos($pos, $viewMode . '-actions') ? $block->escapeHtmlAttr($position) : '' ?>>
											<div class="actions-primary visible-hover"<?= strpos($pos, $viewMode . '-primary') ? $block->escapeHtmlAttr($position) : '' ?>>
												<?php if ($_product->isSaleable()) :?>
													<?php $postParams = $block->getAddToCartPostParams($_product); ?>
													<a href="<?= $block->escapeUrl($_product->getProductUrl()) ?>" type="submit"
														title="<?= $block->escapeHtmlAttr(__('View Details')) ?>"
														class="action tocart primary" style="text-align: center;"><?= $block->escapeHtml(__('View Details')) ?></a>
												<?php else :?>
													<?php if ($_product->isAvailable()) :?>
														<div class="stock available"><span><?= $block->escapeHtml(__('In stock')) ?></span></div>
													<?php else :?>
														<div class="stock unavailable"><span><?= $block->escapeHtml(__('Out of stock')) ?></span></div>
													<?php endif; ?>
												<?php endif; ?>
											</div>
											<!-- <div data-role="add-to-links" class="actions-secondary visible-hover"<?= strpos($pos, $viewMode . '-secondary') ? $block->escapeHtmlAttr($position) : '' ?>>
												<?php if ($addToBlock = $block->getChildBlock('addto')) :?>
													<?= $addToBlock->setProduct($_product)->getChildHtml() ?>
												<?php endif; ?>
											</div> -->
										</div>
									<?php } ?>
								</div>
							<?php if ($block->getMode() == 'list') { ?></div><?php } ?>
							<?php if ($block->getMode() == 'list') { ?>
								<div class="action-column-right">
									<?php if ($block->displayProductStockStatus()) :?>
										<?php if ($_product->isAvailable()) :?>
											<div class="stock available" title="<?= $block->escapeHtmlAttr(__('Availability')) ?>">
												<span><?= $block->escapeHtml(__('In stock')) ?></span>
											</div>
										<?php else :?>
											<div class="stock unavailable" title="<?= $block->escapeHtmlAttr(__('Availability')) ?>">
												<span><?= $block->escapeHtml(__('Out of stock')) ?></span>
											</div>
										<?php endif; ?>
									<?php endif; ?>
									<?= /* @noEscape */ $block->getProductPrice($_product) ?>
									<div class="product actions product-item-actions"<?= strpos($pos, $viewMode . '-actions') ? $block->escapeHtmlAttr($position) : '' ?>>
										<!-- <div data-role="add-to-links" class="actions-secondary visible-hover"<?= strpos($pos, $viewMode . '-secondary') ? $block->escapeHtmlAttr($position) : '' ?>>
											<?php if ($addToBlock = $block->getChildBlock('addto')) :?>
												<?= $addToBlock->setProduct($_product)->getChildHtml() ?>
											<?php endif; ?>
										</div> -->
										<div class="actions-primary visible-hover"<?= strpos($pos, $viewMode . '-primary') ? $block->escapeHtmlAttr($position) : '' ?>>
											<?php if ($_product->isSaleable()) :?>
												<?php $postParams = $block->getAddToCartPostParams($_product); ?>
												<a href="<?= $block->escapeUrl($_product->getProductUrl()) ?>" type="submit"
														title="<?= $block->escapeHtmlAttr(__('View Details')) ?>"
														class="action tocart primary" style="text-align: center;"><?= $block->escapeHtml(__('View Details')) ?></a>
											<?php else :?>
												<?php if ($_product->isAvailable()) :?>
													<div class="stock available"><span><?= $block->escapeHtml(__('In stock')) ?></span></div>
												<?php else :?>
													<div class="stock unavailable"><span><?= $block->escapeHtml(__('Out of stock')) ?></span></div>
												<?php endif; ?>
											<?php endif; ?>
										</div>
									</div>
								</div>
							<?php } ?>
						<?php if ($block->getMode() == 'list') { ?></div><?php } ?>
                    </div>
                </div>
            </li>
            <?php endforeach; ?>
        </ol>
    </div>
    <?= $block->getToolbarHtml() ?>
    <?php if (!$block->isRedirectToCartEnabled()) :?>
        <script type="text/x-magento-init">
        {
            "[data-role=tocart-form], .form.map.checkout": {
                "catalogAddToCart": {
                    "product_sku": "<?= $block->escapeJs($_product->getSku()) ?>"
                }
            }
        }
        </script>
    <?php endif; ?>
<?php endif; ?>
