<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
use Magento\Framework\App\Action\Action;

// @codingStandardsIgnoreFile

?>
<?php
/**
 * Product list template
 *
 * @var $block \MGS\InstantSearch\Block\SearchResult\ListProduct
 */
?>
<?php
$_productCollection = $block->getLoadedProductCollection();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$limit = $block->getPositionLimit();
$themeHelper = $this->helper('MGS\ThemeSettings\Helper\Config');
$lazyLoad = $themeHelper->getStoreConfig('themesettings/general/lazy_load');
$baseImage = $this->getViewFileUrl('MGS_ThemeSettings::images/blank.png');
?>
<?php $_imagehelper = $this->helper('Magento\Catalog\Helper\Image'); ?>
<?php $size = $themeHelper->getImageSize(); ?>
<?php $padding = $themeHelper->getImagePadding(); ?>
<div class="block block-title">
    <h4><?php echo __('Products') ?></h4>
    <?php if($_productCollection->count() && $_productCollection->getSize() > $limit): ?>
        <div class="view-more">
            <a href="<?php /* @escapeNotVerified */ echo $block->getResultUrl() ?>"><?php /* @escapeNotVerified */ echo $block->getViewMoreLabel() ?></a>
        </div>
    <?php endif; ?>
</div>
<?php if (!$_productCollection->count()): ?>
    <div class="message info empty"><div><?php /* @escapeNotVerified */ echo __('We can\'t find products matching the selection.') ?></div></div>
<?php else: ?>
    <?php
        $viewMode = 'grid';
        $imageDisplayArea = 'category_page_grid';
        $showDescription = $block->showShortDescription();
        $showReview = $block->showReview();
        $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
        /**
         * Position for actions regarding image size changing in vde if needed
         */
        $pos = $block->getPositioned() ?? '';
    ?>
    <div class="products wrapper <?php /* @escapeNotVerified */ echo $viewMode; ?> products-<?php /* @escapeNotVerified */ echo $viewMode; ?>">
        <?php $iterator = 1; ?>
        <ol class="products list items product-items ">
            <?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
            <?php $i=0; foreach ($_productCollection as $_product): $i++ ?>
                <li class="item product product-item">
                    <div class="product-item-info" data-container="product-grid">
                        <?php
                            $image = 'category_page_grid';
                        ?>

                        <?php $_productNameStripped = $block->stripTags($_product->getName(), null, true); ?>
                        <?php $productImage = $_imagehelper->init($_product, $image)->resize($size['width'], $size['height'])->getUrl(); ?>
                        <?php if(!$lazyLoad): ?>
                            <?php $baseImage = $productImage ?>
                        <?php endif ?>
                        <?php // Product Image ?>
                        <div class="product-top">
                            <a href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>" class="product photo product-item-photo"  style="padding-bottom: <?php echo $padding; ?>;" tabindex="-1">
                                <img src="<?php echo $baseImage ?>" alt="<?php echo $_productNameStripped ?>" class="img-responsive product-image-photo img-thumbnail<?php if($lazyLoad): ?> arrow-lazy<?php endif ?>" data-src="<?php echo $productImage ?>"/>
                            </a>
                            <?php echo $themeHelper->getProductLabel($_product) ?>
                            <div class="product-item-inner <?= $pos; ?>">
                                <div class="product actions product-item-actions"<?php echo strpos($pos, $viewMode . '-actions') ? $position : ''; ?>>
                                    <div class="actions-primary"<?php echo strpos($pos, $viewMode . '-primary') ? $position : ''; ?>>
                                        <?php if ($_product->isSaleable() && $_product->isAvailable()): ?>
                                            <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                            <form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="post">
                                                <input type="hidden" name="product" value="<?php /* @escapeNotVerified */ echo $postParams['data']['product']; ?>">
                                                <input type="hidden" name="<?php /* @escapeNotVerified */ echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */ echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
                                                <?php echo $block->getBlockHtml('formkey')?>
                                                <button type="submit"
                                                        title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                                        class="action tocart  btn-cart">
                                                    <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                                </button>
                                            </form>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="product details product-item-details">
                            <?php
                                $_productNameStripped = $block->stripTags($_product->getName(), null, true);
                            ?>
                            <h5 class="product name product-item-name">
                                <a class="product-item-link"
                                   href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>">
                                    <?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
                                </a>
                            </h5>
                            <?php echo $showReview ? $block->getReviewsSummaryHtml($_product, $templateType) : ''; ?>
                            <?php /* @escapeNotVerified */ echo $block->getProductPrice($_product) ?>
                            <?php if ($showDescription):?>
                                <div class="product description product-item-description">
                                    <?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </li>
            <?php endforeach; ?>
        </ol>
    </div>
    <?php if (!$block->isRedirectToCartEnabled()) : ?>
        <script type="text/x-magento-init">
        {
            "[data-role=tocart-form], .form.map.checkout": {
                "catalogAddToCart": {}
            }
        }
        </script>
    <?php endif; ?>
<?php endif; ?>
