<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

use Magento\Catalog\ViewModel\Product\Listing\PreparePostData;
use Magento\Framework\App\ActionInterface;

/** @var \Magetop\Advancepreview\Block\ReWrites\Related $block */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>

<?php
//$exist = $block->getLastOpend();
$type = 'related';
$class = $type;

$image = 'related_products_list';
$title = __('Last Open');
$items = $block->getLastOpend();
$exist = count($items) > 0 ? true : false;
$limit = 0;
$shuffle = 0;
$isWeightedRandom = 0;
$canItemsAddToCart = $block->canItemsAddToCart();

$showAddTo = true;
$showCart = false;
$templateType = null;
$description = false;
?>

<?php if ($exist):?>

        <?php if ($type == 'related' || $type == 'upsell'):?>

            <?php if ($type == 'related'):?>
                <div class="block <?= $block->escapeHtmlAttr($class) ?>"
                    data-mage-init='{"relatedProducts":{"relatedCheckbox":".related.checkbox"}}'
                    data-limit="<?= $block->escapeHtmlAttr($limit) ?>"
                    data-shuffle="<?= /* @noEscape */ $shuffle ?>"
                    data-shuffle-weighted="<?= /* @noEscape */ $isWeightedRandom ?>">
            <?php else:?>
                <div class="block <?= $block->escapeHtmlAttr($class) ?>"
                data-mage-init='{"upsellProducts":{}}'
                data-limit="<?= $block->escapeHtmlAttr($limit) ?>"
                data-shuffle="<?= /* @noEscape */ $shuffle ?>"
                data-shuffle-weighted="<?= /* @noEscape */ $isWeightedRandom ?>">
            <?php endif; ?>
            <?php else:?>
                <div class="block <?= $block->escapeHtmlAttr($class) ?>">
                <?php endif; ?>
                <div class="block-title title">
                    <strong id="block-<?= $block->escapeHtmlAttr($class) ?>-heading" role="heading"
                            aria-level="2"><?= $block->escapeHtml($title) ?></strong>
                </div>
                <div class="block-content content" aria-labelledby="block-<?= $block->escapeHtmlAttr($class) ?>-heading">
                    <?php if ($type == 'related' && $canItemsAddToCart):?>
                        <div class="block-actions">
                            <?= $block->escapeHtml(__('Check items to add to the cart or')) ?>
                            <button type="button" class="action select"
                                    data-role="select-all"><span><?= $block->escapeHtml(__('select all')) ?></span></button>
                        </div>
                    <?php endif; ?>
                    <div class="products wrapper grid products-grid products-<?= $block->escapeHtmlAttr($type) ?>">
                        <ol class="products list items product-items">
                            <?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'):?>
                                    <li class="item product product-item"
                                    id="product-item_<?= /* @noEscape */ $_item->getId() ?>"
                                    data-shuffle-group="<?= $block->escapeHtmlAttr($_item->getPriority()) ?>" >
                                    <?= /* @noEscape */ $secureRenderer->renderStyleAsTag(
                                        'display:none;',
                                        'li#product-item_' . $_item->getId()
                                    ) ?>
                                <?php else:?>
                                    <li class="item product product-item">
                                <?php endif; ?>
                                <div class="product-item-info <?= /* @noEscape */ $available ?>">
                                    <?= /* @noEscape */ '<!-- ' . $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->escapeHtmlAttr($_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 ($canItemsAddToCart && !$_item->isComposite() && $_item->isSaleable()
                                            && $type == 'related'):?>
                                            <?php if (!$_item->getRequiredOptions()):?>
                                                <div class="field choice related">
                                                    <input
                                                        type="checkbox"
                                                        class="checkbox related"
                                                        id="related-checkbox<?= $block->escapeHtmlAttr($_item->getId()) ?>"
                                                        name="related_products[]"
                                                        value="<?= $block->escapeHtmlAttr($_item->getId()) ?>" />
                                                    <label class="label"
                                                           for="related-checkbox<?= $block->escapeHtmlAttr($_item->getId())
                                                           ?>"><span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                                                    </label>
                                                </div>
                                            <?php endif; ?>
                                        <?php endif; ?>

                                        <?php if ($showAddTo || $showCart):?>
                                            <?php // phpcs:disable ?>
                                            <div class="product actions product-item-actions">
                                                <?php if ($showCart):?>
                                                    <div class="actions-primary">
                                                        <?php if ($_item->isSaleable()):?>
                                                            <?php if (!$_item->getTypeInstance()->isPossibleBuyFromList($_item)):?>
                                                                <button
                                                                    class="action tocart primary"
                                                                    data-mage-init='{"redirectUrl": {"url": "<?= $block->escapeUrl($block->getAddToCartUrl($_item)) ?>"}}' type="button" title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>">
                                                                    <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                                                                </button>
                                                            <?php else :?>
                                                                <?php
                                                                /** @var $viewModel PreparePostData */
                                                                $viewModel = $block->getViewModel();
                                                                $postArray = $viewModel->getPostData(
                                                                    $block->escapeUrl($block->getAddToCartUrl($_item)),
                                                                    ['product' => $_item->getEntityId()]
                                                                );
                                                                $value = $postArray['data'][ActionInterface::PARAM_NAME_URL_ENCODED];
                                                                ?>
                                                                <form data-role="tocart-form"
                                                                      data-product-sku="<?= $block->escapeHtmlAttr($_item->getSku()) ?>"
                                                                      action="<?= $block->escapeUrl($block->getAddToCartUrl($_item)) ?>"
                                                                      method="post">
                                                                    <input type="hidden" name="product"
                                                                           value="<?= /* @noEscape */ (int)$_item->getEntityId() ?>">
                                                                    <input type="hidden"
                                                                           name="<?= /* @noEscape */ ActionInterface::PARAM_NAME_URL_ENCODED?>"
                                                                           value="<?= /* @noEscape */ $value ?>">
                                                                    <?= $block->getBlockHtml('formkey') ?>
                                                                    <button type="submit"
                                                                            title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>"
                                                                            class="action tocart primary">
                                                                        <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                                                                    </button>
                                                                </form>
                                                            <?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 ($showAddTo):?>
                                                    <div class="secondary-addto-links actions-secondary"
                                                         data-role="add-to-links">
                                                        <?php if ($addToBlock = $block->getChildBlock('addto')):?>
                                                            <?= $addToBlock->setProduct($_item)->getChildHtml() ?>
                                                        <?php endif; ?>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                            <?php // phpcs:enable ?>
                                        <?php endif; ?>
                                    </div>
                                </div>
                                </li>
                            <?php endforeach ?>
                        </ol>
                    </div>
                </div>
            </div>
        <?php if (!$block->isRedirectToCartEnabled() && $_item):?>
            <script type="text/x-magento-init">
            {
                "[data-role=tocart-form], .form.map.checkout": {
                    "catalogAddToCart": {
                        "product_sku": "<?= $block->escapeJs($_item->getSku()) ?>"
                    }
                }
            }
            </script>
     <?php endif;?>
<?php endif;?>
