<?php
/**
 * Copyright © Magefan (support@magefan.com). All rights reserved.
 * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
 *
 * Glory to Ukraine! Glory to the heroes!
 */
?>
<?php
/**
 * Blog related posts template
 *
 * @var $block \Magefan\Blog\Block\Post\View\RelatedProducts
 */
?>

<?php
if ($exist = $block->getItems()->getSize()) {
    $type = 'related';
    $class = $type;

    $image = 'related_products_list';
    $title = __('Related Products');
    $items = $block->getItems();
    $limit = 0;
    $shuffle = 0;
    $canItemsAddToCart = true;

    $showWishlist = true;
    $showCompare = true;
    $showCart = false;
    $templateType = null;
    $description = false;
}
?>

<?php if ($exist):?>

    <?php if ($type == 'related' || $type == 'upsell'): ?>
        <?php if ($type == 'related'): ?>
            <div class="block <?= $block->escapeHtml($class); ?>"
                 data-mage-init='{"relatedProducts":{"relatedCheckbox":".related.checkbox"}}'
                 data-limit="<?= $block->escapeHtml($limit); ?>"
                 data-shuffle="<?= $block->escapeHtml($shuffle); ?>">
        <?php else: ?>
            <div class="block <?= $block->escapeHtml($class); ?>"
                 data-mage-init='{"upsellProducts":{}}'
                 data-limit="<?= $block->escapeHtml($limit); ?>"
                 data-shuffle="<?= $block->escapeHtml($shuffle); ?>">
        <?php endif; ?>
    <?php else: ?>
        <div class="block <?= $block->escapeHtml($class); ?>">
    <?php endif; ?>
    <div class="block-title title">
        <strong id="block-<?= $block->escapeHtml($class)?>-heading"
                role="heading"
                aria-level="2"><?= $block->escapeHtml($title); ?>
        </strong>
    </div>
    <div class="block-content content" aria-labelledby="block-<?= $block->escapeHtml($class)?>-heading">

        <div class="products wrapper grid products-grid products-<?= $block->escapeHtml($type); ?>">
            <ol class="products list items product-items">
                <?php $iterator = 1; ?>
                <?php foreach ($items as $_item): ?>
                    <?php $available = ''; ?>
                    <?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
                        <?php if (!$_item->getRequiredOptions()): ?>
                            <?php $available = 'related-available'; ?>
                    <?php endif; ?>
                <?php endif; ?>
                    <?php if ($type == 'related' || $type == 'upsell'): ?>
                        <?= ($iterator++ == 1) ?
                            '<li class="item product product-item" >' :
                            '</li><li class="item product product-item" >' ?>
                <?php else: ?>
                    <?= ($iterator++ == 1) ?
                            '<li class="item product product-item">' :
                            '</li><li class="item product product-item">' ?>
                <?php endif; ?>
                <div class="product-item-info <?= $block->escapeHtml($available); ?>">
                    <?= '<!-- ' . $block->escapeHtml($image) . '-->' ?>
                    <a href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>"
                       class="product photo product-item-photo">
                        <?= $block->getImage($_item, $image)->toHtml(); ?>
                    </a>
                    <div class="product details product-item-details">
                        <strong class="product name product-item-name">
                            <a class="product-item-link"
                               title="<?= $block->escapeHtml($_item->getName()) ?>"
                               href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>">
                            <?= $block->escapeHtml($_item->getName()) ?></a>
                        </strong>

                        <?= /*@noEscape*/ $block->getProductPrice($_item); ?>

                            <?php if ($templateType): ?>
                                <?= $block->getReviewsSummaryHtml($_item, $templateType) ?>
                            <?php endif; ?>

                            <?php if ($showWishlist || $showCompare || $showCart): ?>
                                <div class="product actions product-item-actions">
                                    <?php if ($showCart): ?>
                                        <div class="actions-primary">
                                            <?php if ($_item->isSaleable()): ?>
                                                <?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>
                                                    <button class="action tocart primary"
                                                            data-mage-init='{"redirectUrl": {"url": "<?= $block->escapeUrl($block->getAddToCartUrl($_item)) ?>"}}'
                                                            type="button"
                                                            title="<?= $block->escapeHtml(__('Add to Cart')) ?>">
                                                        <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                                                    </button>
                                                <?php else: ?>
                                                    <?php $postDataHelper = $this->helper(Magento\Framework\Data\Helper\PostHelper::class);
                                                            $postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()]);
                                                    ?>
                                                    <button class="action tocart primary"
                                                            data-post='<?= $block->escapeHtml($postData); ?>'
                                                            type="button" title="<?= $block->escapeHtml(__('Add to Cart')) ?>">
                                                        <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                                                    </button>
                                                <?php endif; ?>
                                            <?php else: ?>
                                                <?php if ($_item->getIsSalable()): ?>
                                                    <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>
                                    <?php endif; ?>

                                    <?php if ($showWishlist || $showCompare): ?>
                                        <div class="secondary-addto-links actions-secondary" data-role="add-to-links">
                                            <?php if ($this->helper(Magento\Wishlist\Helper\Data::class)->isAllow() && $showWishlist): ?>
                                                <a href="#"
                                                   data-post='<?= $block->escapeHtml($block->getAddToWishlistParams($_item)); ?>'
                                                   class="action towishlist"
                                                   data-action="add-to-wishlist"
                                                   title="<?= $block->escapeHtml(__('Add to Wish List')) ?>">
                                                    <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='<?= $block->escapeHtml($compareHelper->getPostDataParams($_item));?>'
                                                   data-role="add-to-links"
                                                   title="<?= $block->escapeHtml(__('Add to Compare')); ?>">
                                                    <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>
    </div>
</div>
<?php endif;?>
