<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Blog
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

use Magento\Catalog\Block\Product\ReviewRendererInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Action\Action;
use Mageplaza\Blog\Block\Post\RelatedProduct;
use Magento\Framework\Escaper;

/** @var $block RelatedProduct */
/** @var Escaper $escaper */
$_productCollection = $block->getLoadedProductCollection();
$_helper            = $block->getHelper();

?>
<?php if (!$_productCollection->count()) : ?>
    <div class="message info empty">
        <div><?= $escaper->escapeHtml(__('We can\'t find products matching the selection.')) ?></div>
    </div>
<?php else : ?>
    <?= $block->getToolbarHtml() ?>
    <?= $block->getAdditionalHtml() ?>
    <?php
    if ($block->getMode() == 'grid') {
        $viewMode         = '';
        $imageDisplayArea = 'category_page_grid';
        $showDescription  = false;
        $templateType     = ReviewRendererInterface::SHORT_VIEW;
    } else {
        $viewMode         = 'list';
        $imageDisplayArea = 'category_page_list';
        $showDescription  = true;
        $templateType     = ReviewRendererInterface::FULL_VIEW;
    }
    $pos = $block->getPositioned();
    ?>
    <div class="products wrapper <?= /* @noEscape */ $viewMode ?> products-<?= /* @noEscape */ $viewMode ?>">
        <?php if ($block->relatedMode() !== '1') : ?>
            <div class="products list items product-items mt-6 grid grid-cols-2 gap-4 md:grid-cols-3 lg:grid-cols-4">
                <?php /** @var Product $_product */ ?>
                <?php foreach ($_productCollection as $_product) : ?>
                    <div 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 ?>
                            <a href="<?= $escaper->escapeUrl($block->getParentProductUrl($_product)) ?>"
                               class="product photo product-item-photo"
                               tabindex="-1">
                                <?= $productImage->toHtml() ?>
                            </a>
                            <div class="product details product-item-details">
                                <?php
                                $_productNameStripped = $block->stripTags($_product->getName(), null, true);
                                ?>
                                <strong class="product name product-item-name">
                                    <a class="product-item-link"
                                       href="<?= $escaper->escapeUrl($block->getParentProductUrl($_product)) ?>">
                                        <?= /* @noEscape */
                                        $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
                                    </a>
                                </strong>
                                <?= $block->getReviewsSummaryHtml($_product, $templateType) ?>
                                <?= /* @noEscape */
                                $block->getProductPrice($_product) ?>
                                <?= $block->getProductDetailsHtml($_product) ?>
                                <div class="product-item-inner">
                                    <div class="product actions product-item-actions"
                                        <?= strpos((string) $pos, $viewMode . '-actions')
                                            ? $escaper->escapeHtmlAttr($position) : '' ?>>
                                        <div class="actions-primary"
                                            <?= strpos((string) $pos, $viewMode . '-primary')
                                                ? $escaper->escapeHtmlAttr($position) : '' ?>>
                                            <?php if ($_product->isSaleable()) : ?>
                                                <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                                <form data-role="tocart-form"
                                                      data-product-sku="<?= $escaper->escapeHtml($_product->getSku()) ?>"
                                                      action="<?= $escaper->escapeUrl($postParams['action']) ?>"
                                                      method="post">
                                                    <input type="hidden" name="product"
                                                           value="<?= /* @noEscape */
                                                           $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="<?= $escaper->escapeHtmlAttr(__('Add to Cart')) ?>"
                                                            class="action tocart btn btn-primary">
                                                        <span><?= $escaper->escapeHtml(__('Add to Cart')) ?></span>
                                                    </button>
                                                </form>
                                            <?php else : ?>
                                                <?php if ($_product->isAvailable()) : ?>
                                                    <div class="stock available">
                                                        <span><?= $escaper->escapeHtml(__('In stock')) ?></span></div>
                                                <?php else : ?>
                                                    <div class="stock unavailable">
                                                        <span><?= $escaper->escapeHtml(__('Out of stock')) ?></span></div>
                                                <?php endif; ?>
                                            <?php endif; ?>
                                        </div>
                                        <div data-role="add-to-links" class="actions-secondary"
                                            <?= strpos((string) $pos, $viewMode . '-secondary')
                                                ? $escaper->escapeHtmlAttr($position) : '' ?>>
                                            <?php if ($addToBlock = $block->getChildBlock('addto')) : ?>
                                                <?= $addToBlock->setProduct($_product)->getChildHtml() ?>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                    <?php if ($showDescription) : ?>
                                        <div class="product description product-item-description">
                                            <?= /* @noEscape */
                                            $_helper->productAttribute(
                                                $_product,
                                                $_product->getShortDescription(),
                                                'short_description'
                                            ) ?>
                                            <a href="<?= $escaper->escapeUrl($block->getParentProductUrl($_product)) ?>"
                                               title="<?= /* @noEscape */
                                               $_productNameStripped ?>"
                                               class="action more"><?= $escaper->escapeHtml(__('Learn More')) ?></a>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
            <style>
                .page-products .product-item-info {
                    width: 220px;
                    padding: 20px;
                }

            </style>
        <?php else : ?>
            <div class="">
                <?php /** @var $_product Product */ ?>
                <section id="products-carousel" class="splide" aria-label="Related Products">
                    <div class="splide__track">
                        <ul class="splide__list">
                            <?php foreach ($_productCollection as $_product) : ?>
                                <li class="splide__slide">
                                    <div class="item product product-item mp-blog-slider-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 ?>
                                            <a href="<?= $escaper->escapeUrl($block->getParentProductUrl($_product)) ?>"
                                               class="product photo product-item-photo"
                                               tabindex="-1">
                                                <?= $productImage->toHtml() ?>
                                            </a>
                                            <div class="product details product-item-details">
                                                <?php
                                                $_productNameStripped = $block->stripTags($_product->getName(), null,
                                                    true);
                                                ?>
                                                <strong class="product name product-item-name">
                                                    <a class="product-item-link"
                                                       href="<?= $escaper->escapeUrl($block->getParentProductUrl($_product)) ?>">
                                                        <?= /* @noEscape */
                                                        $_helper->productAttribute($_product, $_product->getName(),
                                                            'name') ?>
                                                    </a>
                                                </strong>
                                                <?= $block->getReviewsSummaryHtml($_product, $templateType) ?>
                                                <?= /* @noEscape */
                                                $block->getProductPrice($_product) ?>
                                                <?= $block->getProductDetailsHtml($_product) ?>
                                                <div class="product-item-inner">
                                                    <div class="product actions product-item-actions"
                                                        <?= strpos((string) $pos, $viewMode . '-actions')
                                                            ? $escaper->escapeHtmlAttr($position) : '' ?>>
                                                        <div class="actions-primary"
                                                            <?= strpos((string) $pos, $viewMode . '-primary')
                                                                ? $escaper->escapeHtmlAttr($position) : '' ?>>
                                                            <?php if ($_product->isSaleable()) : ?>
                                                                <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                                                <form data-role="tocart-form"
                                                                      data-product-sku="<?= $escaper->escapeHtml($_product->getSku()) ?>"
                                                                      action="<?= $escaper->escapeUrl($postParams['action']) ?>"
                                                                      method="post">
                                                                    <input type="hidden"
                                                                           name="product"
                                                                           value="<?= /* @noEscape */
                                                                           $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="<?= $escaper->escapeHtmlAttr(__('Add to Cart')) ?>"
                                                                            class="action tocart btn btn-primary">
                                                                        <span><?= $escaper->escapeHtml(__('Add to Cart')) ?></span>
                                                                    </button>
                                                                </form>
                                                            <?php else : ?>
                                                                <?php if ($_product->isAvailable()) : ?>
                                                                    <div class="stock available">
                                                                        <span><?= $escaper->escapeHtml(__('In stock')) ?></span>
                                                                    </div>
                                                                <?php else : ?>
                                                                    <div class="stock unavailable">
                                                                        <span><?= $escaper->escapeHtml(__('Out of stock')) ?></span>
                                                                    </div>
                                                                <?php endif; ?>
                                                            <?php endif; ?>
                                                        </div>
                                                        <div data-role="add-to-links" class="actions-secondary"
                                                            <?= strpos((string) $pos, $viewMode . '-secondary')
                                                                ? $escaper->escapeHtmlAttr($position) : '' ?>>
                                                            <?php if ($addToBlock = $block->getChildBlock('addto')) : ?>
                                                                <?= $addToBlock->setProduct($_product)->getChildHtml() ?>
                                                            <?php endif; ?>
                                                        </div>
                                                    </div>
                                                    <?php if ($showDescription) : ?>
                                                        <div class="product description product-item-description">
                                                            <?= /* @noEscape */
                                                            $_helper->productAttribute(
                                                                $_product,
                                                                $_product->getShortDescription(),
                                                                'short_description'
                                                            ) ?>
                                                            <a href="<?= $escaper->escapeUrl($block->getParentProductUrl($_product)) ?>"
                                                               title="<?= /* @noEscape */
                                                               $_productNameStripped ?>"
                                                               class="action more"><?= $escaper->escapeHtml(__('Learn More')) ?></a>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </section>
            </div>
            <script>
                document.addEventListener('DOMContentLoaded', function () {
                    new Splide('#products-carousel', {
                        perPage: 3,
                        breakpoints: {
                            1028: {
                                perPage: 2
                            }
                        }
                    }).mount();
                });
            </script>
        <?php endif; ?>
    </div>
    <?= $block->getToolbarHtml() ?>
    <?php if (!$block->isRedirectToCartEnabled()) : ?>
        <script type="text/x-magento-init">
            {
                "[data-role=tocart-form], .form.map.checkout": {
                    "catalogAddToCart": {
                        "product_sku": "<?= $escaper->escapeJs($_product->getSku()) ?>"
                    }
                }
            }

        </script>
    <?php endif; ?>
<?php endif; ?>
<style>

    .product-item .price-box {
        margin: 10px 0 25px;
    }


    .item.product.product-item.mp-blog-slider-item {
        width: 85%;
        text-align: center;
        min-height: 450px;
        margin-top: 5px;
    }
    .product-item-info:hover, .products-grid .product-item-info.active {

        background: #ffffff;
        box-shadow: 3px 4px 4px 0 rgba(0, 0, 0, 0.3);
        border: 1px solid #bbbbbb;
        margin: -10px;
        padding: 18px;
        position: relative;
        z-index: 9;
    }

    .product-item-info:hover .btn.btn-primary {
        display: block;
    }

    .btn.btn-primary {
        display: none;
    }

    .products-grid .product-item-actions {
        margin: -10px 0 10px;
    }

</style>
