<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * Template for displaying products list widget
 *
 * @var $block \Magento\CatalogWidget\Block\Product\ProductsList
 */
?>
<?php if ($exist = ($block->getProductCollection() && $block->getProductCollection()->getSize())):?>
<?php
$_productCollection = $block->getLoadedProductCollection();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');

$_category_config = $this->helper('Smartwave\Porto\Helper\Data')->getConfig('porto_settings/category');
$_category_grid_config = $this->helper('Smartwave\Porto\Helper\Data')->getConfig('porto_settings/category_grid');
?>
<?php
    $type = 'widget-product-grid';

    $mode = 'grid';

    $image = 'new_products_content_widget_grid';
    $hover_image = 'category_page_grid-hover';
    $title = $block->getTitle() ? __($block->getTitle()) : '';
    $items = $block->getProductCollection()->getItems();

    $showWishlist = true;
    $showCompare = true;
    $showCart = true;
    $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::DEFAULT_VIEW;
    $description = false;

    $image_width = ($_category_config['ratio_width'])?$_category_config['ratio_width']:300;
    $image_height = ($_category_config['ratio_height'])?$_category_config['ratio_height']:300;
?>
    <div class="block widget block-products-list <?php /* @escapeNotVerified */ echo $mode; ?>">
        <?php if ($title):?>
        <div class="block-title">
            <strong><?php /* @escapeNotVerified */ echo $title; ?></strong>
        </div>
        <?php endif ?>
        <div class="block-content">
            <?php /* @escapeNotVerified */ echo '<!-- ' . $image . '-->' ?>
            <div class="products-grid products-<?php /* @escapeNotVerified */ echo $mode; ?> <?php /* @escapeNotVerified */ echo $mode; ?>">
                <ol class="product-items <?php /* @escapeNotVerified */ echo $type; ?>">
                    <?php $iterator = 1; ?>
                    <?php foreach ($items as $_item): ?>
                        <?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>
                        <div class="product-item-info">
                            <a href="<?php echo $_item->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
                                <?php
                                    if($_category_config['aspect_ratio'])
                                        $productImage = $_imagehelper->init($_item, $image)->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize($image_width);
                                    else
                                        $productImage = $_imagehelper->init($_item, $image)->resize($image_width, $image_height);
                                    $productImageUrl = $productImage->getUrl();
                                ?>
                                    <img class="product-image-photo default_image" src="<?php echo $productImageUrl; ?>" alt="<?php echo $productImage->getLabel(); ?>"/>
                                <?php if($_category_config['alternative_image']): ?>
                                <?php
                                    if($_category_config['aspect_ratio'])
                                        $productHoverImage = $_imagehelper->init($_item, $hover_image)->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize($image_width);
                                    else
                                        $productHoverImage = $_imagehelper->init($_item, $hover_image)->resize($image_width, $image_height);
                                    $productHoverImageUrl = $productHoverImage->getUrl();
                                ?>
                                    <?php if($productImageUrl != str_replace("/thumbnail/","/small_image/",$productHoverImageUrl)): ?>
                                    <img class="product-image-photo hover_image" src="<?php echo $productHoverImageUrl; ?>" alt="<?php echo $productHoverImage->getLabel(); ?>"/>
                                    <?php endif; ?>
                                <?php endif; ?>
                            </a>
                            <div class="product-item-details">
                                <strong class="product-item-name">
                                    <a title="<?php echo $block->escapeHtml($_item->getName()) ?>"
                                       href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>"
                                       class="product-item-link">
                                        <?php echo $block->escapeHtml($_item->getName()) ?>
                                    </a>
                                </strong>

                                <?php //MNM ?>
                                <?php if($_product->getSpiPropertyShort()): ?>
                                    <div class="product-property-short">
                                        <?php echo $_helper->productAttribute($_product, $_product->getSpiPropertyShort(), 'spi_property_short'); ?>
                                    </div>
                                <?php endif; ?>
                                <?php
                                $content = '';

                                if(!($_product->getTypeId() == Magento\GroupedProduct\Model\Product\Type\Grouped::TYPE_CODE)) {
                                    $content = $_product->getAttributeText('spi_inhalt');
                                } else {
                                    $contents = [];
                                    foreach($_product->getTypeInstance()->getAssociatedProducts($_product) as $_child) {
                                        if($c = $_child->getAttributeText('spi_inhalt')) {
                                            $contents[] = $c;
                                        }
                                    }

                                    usort($contents, function($a, $b){
                                        return intval($a) > intval($b) ? -1 : 1;
                                    });

                                    $content = implode(', ', $contents);
                                }

                                if($content): ?>
                                    <div class="product-content">
                                        <?php echo __('Inhalt').': '.str_replace('1000ml', '1L', $content) ?>
                                    </div>
                                <?php endif; ?>
                                <?php //MNM ?>

                                <?php if($_category_config['rating_star']): ?>
                                <?php
                                    $review_html = $block->getReviewsSummaryHtml($_item, $templateType);
                                ?>
                                <?php if($review_html): ?>
                                    <?php echo $review_html; ?>
                                <?php else: ?>
                                <div class="product-reviews-summary short">
                                    <div class="rating-summary">
                                        <span class="label"><span>Rating:</span></span>
                                        <div class="rating-result" title="0%">
                                            <span style="width:0"><span>0%</span></span>
                                        </div>
                                    </div>
                                </div>
                                <?php endif; ?>
                                <?php endif; ?>

                                <?php if($_category_config['product_price']): ?>
                                <?php echo $block->getProductPriceHtml($_item, $type); ?>
                                <?php endif; ?>

                                <?php if($_category_config['actions']): ?>
                                <?php if ($showWishlist || $showCompare || $showCart): ?>
                                    <div class="product-item-actions">
                                        <?php if($_category_config['addtowishlist']): ?>
                                            <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow() && $showWishlist): ?>
                                                <a href="#"
                                                   data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_item); ?>'
                                                   class="action towishlist actions-secondary" data-action="add-to-wishlist"
                                                   title="<?php /* @escapeNotVerified */ echo __('Add to Wish List') ?>">
                                                    <span><?php /* @escapeNotVerified */ echo __('Add to Wish List') ?></span>
                                                </a>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                        <?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":"<?php /* @escapeNotVerified */ echo $block->getAddToCartUrl($_item) ?>"}}'
                                                                type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
                                                            <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                                        </button>
                                                    <?php else: ?>
                                                        <?php
                                                            $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
                                                            $postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
                                                        ?>
                                                        <button class="action tocart primary"
                                                                data-post='<?php /* @escapeNotVerified */ echo $postData; ?>'
                                                                type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
                                                            <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                                        </button>
                                                    <?php endif; ?>
                                                <?php else: ?>
                                                    <?php if ($_item->getIsSalable()): ?>
                                                        <div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
                                                    <?php else: ?>
                                                        <div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
                                                    <?php endif; ?>
                                                <?php endif; ?>
                                            </div>
                                        <?php endif; ?>
                                        <?php if($_category_config['addtocompare']): ?>
                                            <?php if ($block->getAddToCompareUrl() && $showCompare): ?>
                                                <?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');?>
                                                <a href="#" class="action tocompare actions-secondary"
                                                   data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataParams($_item);?>'
                                                   title="<?php /* @escapeNotVerified */ echo __('Add to Compare') ?>">
                                                    <span><?php /* @escapeNotVerified */ echo __('Add to Compare') ?></span>
                                                </a>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php echo($iterator == count($items)+1) ? '</li>' : '' ?>
                    <?php endforeach ?>
                </ol>
            </div>
            <?php echo $block->getPagerHtml() ?>
        </div>
    </div>
<?php endif;?>
