<?php
/**
 * Copyright © 2015 Ihor Vansach (ihor@magefan.com). All rights reserved.
 * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
 *
 * 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() && $this->displayProducts()) {
    $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 = true;
    $templateType = null;
    $description = false;
    $showAddTo = true;
}
?>

<?php if ($exist):?>
<?php
        $imageHelper = $this->helper('Magento\Catalog\Helper\Image');
        $themeHelper = $this->helper('Codazon\ThemeLayoutPro\Helper\Data');
        $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');
        $wishlistHelper = $this->helper('Magento\Wishlist\Helper\Data');
        
        $width = $themeHelper->getConfig('images/category/product_image_width')?:150;
        $height = $themeHelper->getConfig('images/category/product_image_height')?:150;
        $paddingBottom = $height*100/$width;
        $cartWidget = $block->isRedirectToCartEnabled()?'':'data-mage-init=\'{"catalogAddToCart":{}}\'';
        $showWishlist = $showAddTo && (bool)($themeHelper->isDisplayOnList('wishlist'));
        $showCompare = $showAddTo && (bool)($themeHelper->isDisplayOnList('compare'));
        $showCart = $showCart && (bool)($themeHelper->isDisplayOnList('addtocart'));
        
        $widget = ['themewidgets' => [
            'codazon.slider' => ['sliderConfig' => [
                'dots'          => false,
                'lazyload'      => true,
                'margin'        => 20,
                'nav'           => true,
                'responsive'    => $themeHelper->getSliderItemsPerRowArray('2')
            ]]
        ]];
    ?>
    <?php if ($type == 'related' || $type == 'upsell'): ?>
        <?php if ($type == 'related'): ?>
            <div class="block <?php echo $class; ?>" data-mage-init='{"relatedProducts":{"relatedCheckbox":".related.checkbox"}}' data-limit="<?php echo $limit; ?>" data-shuffle="<?php echo $shuffle; ?>">
        <?php else: ?>
            <div class="block <?php echo $class; ?>" data-mage-init='{"upsellProducts":{}}' data-limit="<?php echo $limit; ?>" data-shuffle="<?php echo $shuffle; ?>">
        <?php endif; ?>
    <?php else: ?>
        <div class="block <?php echo $class; ?>">
    <?php endif; ?>
    <div class="block-title title">
        <strong id="block-<?php echo $class?>-heading" role="heading" aria-level="2"><?php echo $title; ?></strong>
    </div>
    <div class="block-content content" aria-labelledby="block-<?php echo $class?>-heading">
        <div class="products product-list-style-17 base-slider wrapper grid products-grid products-<?php echo $type; ?>">
            <div class="products list items product-items owl-carousel" data-mage-init='<?php echo json_encode($widget) ?>'>
                <?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'): ?>
                    <?php echo($iterator++ == 1) ? '<div class="item product product-item" style="display: none;">' : '</div><div class="item product product-item" style="display: none;">' ?>
                <?php else: ?>
                    <?php echo($iterator++ == 1) ? '<div class="item product product-item">' : '</div><div class="item product product-item">' ?>
                <?php endif; ?>
                <div class="product-item-info <?php echo $available; ?>">
                    <div class="product-item-top">
                        <?php echo '<!-- ' . $image . '-->' ?>
                        <?php
                            $mainImage = $_item->getData('small_image');
                            $hoveredImage = $_item->getData('thumbnail');
                        ?>
                        <a href="<?php echo $block->getProductUrl($_item) ?>" class="product photo product-item-photo">
                            <?php //echo $block->getImage($_item, $image)->toHtml(); ?>
                            <span class="product-image-container">
                                <span class="product-image-wrapper" style="padding-bottom: <?= $paddingBottom ?>%;">
                                    <img class="product-image-photo main-img owl-lazy" data-src="<?php echo $imageHelper->init($_item, $image)->setImageFile($mainImage)->resize($width, $height)->getUrl() ?>" />
                                    <img class="product-image-photo hovered-img owl-lazy" data-src="<?php echo $imageHelper->init($_item, $image)->setImageFile($hoveredImage)->resize($width, $height)->getUrl() ?>" />
                                </span>
                            </span>
                        </a>
                        
                        
                        <?php $i = 1; ?>
                        <div class="button-hover-wrap hidden-xs">
                            <?php if ($showCart): ?>
                            <div class="cart-wrap">
                                <div class="cart-wrap">
                                    <?php if ($_item->isSaleable()): ?>
                                        <?php $postParams = $block->getAddToCartPostParams($_item); ?>
                                        <form <?= $cartWidget ?> data-role="tocart-form" action="<?= $block->getAddToCartUrl($_item); ?>" method="post">
                                            <?php echo $block->getBlockHtml('formkey')?>
                                            <input type="hidden" name="product" value="<?= $_item->getId() ?>">
                                            <button type="submit"
                                                    title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                                    class="action tocart primary">
                                                <span><?= __('Add to Cart') ?></span>
                                                <label>&nbsp;</label>
                                            </button>
                                        </form>
                                    <?php else: ?>
                                        <?php if ($_item->getIsSalable()): ?>
                                            <button onclick="document.location.href = '<?= $_item->getProductUrl(); ?>'" class="action tocart primary stock available <?php echo "effect-{$i}"; $i++; ?>"><span><?= __('In stock') ?></span></button>
                                        <?php else: ?>
                                            <button class="action tocart primary disabled stock unavailable <?php echo "effect-{$i}"; $i++; ?>" disabled><span><?= __('Out of stock') ?></span></button>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                </div>
                            </div>
                            <?php endif ?>
                            <?php if ($wishlistHelper->isAllow() && $showWishlist): ?>
                                <a href="#"
                                   data-post='<?= $block->getAddToWishlistParams($_item); ?>'
                                   class="action towishlist <?php echo "effect-{$i}"; $i++; ?>" data-action="add-to-wishlist"
                                   title="<?= __('Add to Wish List') ?>">
                                    <span><?= __('Add to Wish List') ?></span>
                                </a>
                            <?php endif; ?>
                            <?php if ($block->getAddToCompareUrl() && $showCompare): ?>
                                <a href="#" class="action tocompare <?php echo "effect-{$i}"; $i++; ?>"
                                   data-post='<?= $compareHelper->getPostDataParams($_item);?>'
                                   title="<?= __('Add to Compare') ?>">
                                    <span><?= __('Add to Compare') ?></span>
                                </a>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="product details product-item-details">
                        <strong class="product name product-item-name"><a class="product-item-link" title="<?php echo $block->escapeHtml($_item->getName()) ?>" href="<?php echo $block->getProductUrl($_item) ?>">
                            <?php echo $block->escapeHtml($_item->getName()) ?></a>
                        </strong>

                        <?php echo $block->getProductPrice($_item); ?>

                            <?php if ($templateType): ?>
                                <?php echo $block->getReviewsSummaryHtml($_item, $templateType) ?>
                            <?php endif; ?>
                            
                            <?php if ($showAddTo || $showCart): ?>
                            <div class="button-hover-wrap visible-xs">
                                <div class="product-item-inner">
                                    <div class="product actions product-item-actions">
                                        <?php if ($showCart): ?>
                                            <div class="actions-primary">
                                                <?php if ($_item->isSaleable()): ?>
                                                    <form <?= $cartWidget ?> data-role="tocart-form" action="<?= $block->getAddToCartUrl($_item); ?>" method="post">
                                                        <?php echo $block->getBlockHtml('formkey')?>
                                                        <input type="hidden" name="product" value="<?= $_item->getId() ?>">
                                                        <button type="submit"
                                                                title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                                                class="action tocart primary">
                                                            <span><?= __('Add to Cart') ?></span>
                                                        </button>
                                                    </form>
                                                <?php else: ?>
                                                    <?php if ($_item->getIsSalable()): ?>
                                                        <div class="stock available"><span><?php echo __('In stock') ?></span></div>
                                                    <?php else: ?>
                                                        <div class="stock unavailable"><span><?php echo __('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')): ?>
                                                    <?php echo $addToBlock->setProduct($_item)->getChildHtml(); ?>
                                                <?php endif; ?>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php echo($iterator == count($items)+1) ? '</div>' : '' ?>
                <?php endforeach ?>
            </div>
        </div>
    </div>
</div>
<?php endif;?>