<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis
// phpcs:disable Magento2.Files.LineLength.MaxExceeded
?>
<?php if (($_products = $block->getProductCollection()) && $_products->getSize()) :?>
    <div class="block widget block-new-products-list">
        <div class="block-title">
            <strong><?= $block->escapeHtml(__('New Products')) ?></strong>
        </div>
        <div class="block-content">
            <?php $suffix = $block->getNameInLayout(); ?>
            <ol class="product-items" id="widget-new-products-<?= $block->escapeHtmlAttr($suffix) ?>">
                <?php foreach ($_products->getItems() as $_product) :?>
                    <li class="product-item">
                        <div class="product-item-info">
                            <a class="product-item-photo" href="<?= $block->escapeUrl($_product->getProductUrl()) ?>"
                               title="<?= /* @noEscape */ $block->stripTags($_product->getName(), null, true) ?>">
                                <?= $block->getImage($_product, 'side_column_widget_product_thumbnail')->toHtml() ?>
                            </a>
                            <div class="product-item-details">
                                <strong class="product-item-name">
                                    <a href="<?= $block->escapeUrl($_product->getProductUrl()) ?>"
                                       title="<?= /* @noEscape */ $block->stripTags($_product->getName(), null, true) ?>)"
                                       class="product-item-link">
                                        <?= /* @noEscape */ $this->helper(Magento\Catalog\Helper\Output::class)->productAttribute($_product, $_product->getName(), 'name') ?>
                                    </a>
                                </strong>
                                <?= $block->getProductPriceHtml($_product, '-widget-new-' . $suffix) ?>
                                <div class="product-item-actions">
                                    <div class="actions-primary">
                                        <?php if ($_product->isSaleable()) :?>
                                            <?php if (!$_product->getTypeInstance()->isPossibleBuyFromList($_product)) :?>
                                                <button type="button" title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>"
                                                        class="action tocart primary"
                                                        data-mage-init='{"redirectUrl":{"url":"<?= $block->escapeUrl($block->getAddToCartUrl($_product)) ?>"}}'>
                                                    <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                                                </button>
                                            <?php else :?>
                                                <?php
                                                $postDataHelper = $this->helper(Magento\Framework\Data\Helper\PostHelper::class);
                                                $postData = $postDataHelper->getPostData($block->escapeUrl($block->getAddToCartUrl($_product)), ['product' => $_product->getEntityId()]);
                                                ?>
                                                <button type="button" title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>"
                                                        class="action tocart primary"
                                                        data-post='<?= /* @noEscape */ $postData ?>'>
                                                    <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                                                </button>
                                            <?php endif; ?>
                                        <?php else :?>
                                            <?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; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </li>
                <?php endforeach; ?>
            </ol>
            <?= $block->getPagerHtml() ?>
        </div>
    </div>
<?php endif; ?>
