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

// @codingStandardsIgnoreFile
?>
<?php
/**
 * Product list template
 *
 * @var $block \Smartwave\Filterproducts\Block\
 */
?>
<?php
$_productCollection = $block->getLoadedProductCollection();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
$_portohelper = $this->helper('Smartwave\Porto\Helper\Data');
$review_model = $_portohelper->getModel('\Magento\Review\Model\Review');
$_category_config = $_portohelper->getConfig('porto_settings/category');
$_category_grid_config = $_portohelper->getConfig('porto_settings/category_grid');
$_product_label_config = $this->helper('Smartwave\Porto\Helper\Data')->getConfig('porto_settings/product_label');

$aspect_ratio = $this->getData("aspect_ratio");
if($aspect_ratio == null) {
    $aspect_ratio = $_category_config['aspect_ratio'];
}
$image_width = $this->getData("image_width");
if(!$image_width) {
    $image_width = ($_category_config['ratio_width'])?$_category_config['ratio_width']:300;
}
$image_height = $this->getData("image_height");
if($aspect_ratio)
    $image_height = $image_width;
if(!$image_height) {
    $image_height = ($_category_config['ratio_height'])?$_category_config['ratio_height']:300;
}
$masonry_layout = $this->getData("masonry_layout");
if($masonry_layout == null) {
    $masonry_layout = '1';
}
$product_type = $this->getData("product_type");
if($product_type == null) {
    $product_type = 1;
}
$masonry_id = $this->getData("name");
if($masonry_id == null) {
    $masonry_id = "masonry_1";
}
if($product_type == 1) {
    $product_type = 7;
}else if($product_type == 2) {
    $product_type = 8;
}else if($product_type == 3) {
    $product_type = 11;
}
$gap_width = $this->getData("gap_width");
if($gap_width == null) {
    $gap_width = 0;
}
$gap_width = $gap_width / 2;

$grid_height = $this->getData("grid_height");
if($grid_height == null) {
    $grid_height = 600;
}
$product_grid_layout_arr = array();
if ( '1' == $masonry_layout ) {
    $product_grid_layout_arr = array(
        array( 'height' => '1', 'width' => '1-2', 'width_md' => '1' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1-2' ),
        array( 'height' => '1', 'width' => '1-4', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1-2' ),
    );
}
if ( '2' == $masonry_layout ) {
    $product_grid_layout_arr = array(
        array( 'height' => '2-3', 'width' => '1-2', 'width_md' => '1' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1-2' ),
        array( 'height' => '1', 'width' => '1-4', 'width_md' => '1-2' ),
        array( 'height' => '1-3', 'width' => '1-2', 'width_md' => '1' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1-2' ),
    );
}
if ( '3' == $masonry_layout ) {
    $product_grid_layout_arr = array(
        array( 'height' => '1', 'width' => '1-2', 'width_md' => '1' ),
        array( 'height' => '1', 'width' => '1-4', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1-2' ),
    );
}
if ( '4' == $masonry_layout ) {
    $product_grid_layout_arr = array(
        array( 'height' => '1-2', 'width' => '1-3', 'width_md' => '1' ),
        array( 'height' => '1-2', 'width' => '5-12', 'width_md' => '1' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1' ),
        array( 'height' => '1-2', 'width' => '1-3', 'width_md' => '1' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1' ),
        array( 'height' => '1-2', 'width' => '5-12', 'width_md' => '1' ),
    );
}
if ( '5' == $masonry_layout ) {
    $product_grid_layout_arr = array(
        array( 'height' => '1', 'width' => '2-5', 'width_md' => '1', 'width_lg' => '1-2' ),
        array( 'height' => '1', 'width' => '1-5', 'width_md' => '1-2', 'width_lg' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-5', 'width_md' => '1-2', 'width_lg' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-5', 'width_md' => '1-2', 'width_lg' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-5', 'width_md' => '1-2', 'width_lg' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-5', 'width_md' => '1-2', 'width_lg' => '1-2' ),
    );
}
if ( '6' == $masonry_layout ) {
    $product_grid_layout_arr = array(
        array( 'height' => '2-3', 'width' => '1-2', 'width_md' => '1', 'width_lg' => '1-2' ),
        array( 'height' => '2-3', 'width' => '1-4', 'width_md' => '1-2', 'width_lg' => '1-2' ),
        array( 'height' => '1', 'width' => '1-4', 'width_md' => '1-2', 'width_lg' => '1-2' ),
        array( 'height' => '1-3', 'width' => '1-4', 'width_md' => '1-2', 'width_lg' => '1-2' ),
        array( 'height' => '1-3', 'width' => '1-4', 'width_md' => '1-2', 'width_lg' => '1-2' ),
        array( 'height' => '1-3', 'width' => '1-4', 'width_md' => '1-2', 'width_lg' => '1-2' ),
    );
}
if ( '7' == $masonry_layout ) {
    $product_grid_layout_arr = array(
        array( 'height' => '1', 'width' => '1-2', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-2', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-2', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-4', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-2', 'width_md' => '1-2' ),
        array( 'height' => '1', 'width' => '1-2', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-2', 'width_md' => '1-2' ),
    );
}
if ( '8' == $masonry_layout ) {
    $product_grid_layout_arr = array(
        array( 'height' => '1', 'width' => '1-3', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-3', 'width_md' => '1-2' ),
        array( 'height' => '1', 'width' => '1-3', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-3', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-6', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-3', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-3', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'width' => '1-6', 'width_md' => '1-2' ),
    );
}
if ( '9' == $masonry_layout ) {
    $product_grid_layout_arr = array(
        array( 'height' => '1', 'width' => '2-5', 'width_md' => '1', 'width_lg' => '2-3' ),
        array( 'height' => '1-2', 'height_md' => '1-2', 'width' => '1-5', 'width_md' => '1-2', 'width_lg' => '1-3' ),
        array( 'height' => '1-2', 'height_md' => '1-2', 'width' => '1-5', 'width_md' => '1-2', 'width_lg' => '1-3' ),
        array( 'height' => '1-2', 'height_md' => '1-2', 'width' => '1-5', 'width_md' => '1-2', 'width_lg' => '1-2' ),
        array( 'height' => '1-2', 'height_md' => '1-2', 'width' => '1-5', 'width_md' => '1-2', 'width_lg' => '1-2' ),
        array( 'height' => '1-2', 'height_md' => '1-2', 'width' => '1-5', 'width_md' => '1-2', 'width_lg' => '1-2' ),
        array( 'height' => '1-2', 'height_md' => '1-2', 'width' => '1-5', 'width_md' => '1-2', 'width_lg' => '1-2' ),
    );
}
if ( '10' == $masonry_layout ) {
    $product_grid_layout_arr = array(
        array( 'height' => '1-2', 'height_md' => '1-2', 'width' => '2-3', 'width_md' => '1' ),
        array( 'height' => '1', 'height_md' => '1', 'width' => '1-3', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'height_md' => '1-2', 'width' => '1-3', 'width_md' => '1-2' ),
        array( 'height' => '1-2', 'height_md' => '1-2', 'width' => '1-3', 'width_md' => '1-2' ),
    );
}
$product_limit = count($product_grid_layout_arr);
?>
<?php if (!$_productCollection->count()): ?>
    <div class="message info empty"><div><?php echo __('We can\'t find products matching the selection.') ?></div></div>
<?php else: ?>
    <?php
    $viewMode = 'grid';
    $image = 'category_page_grid';
    $hover_image = 'category_page_grid-hover';
    $showDescription = false;
    $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
    ?>
    <div id="<?php echo $masonry_id; ?>" class="wrapper <?php echo $viewMode; ?> products-<?php echo $viewMode; ?> masonry-grid">
        <?php $iterator = 1; ?>
        <div class="filterproducts products list items product-items">
            <?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
            <?php foreach ($_productCollection as $_product): ?>
                <?php if($product_limit >= $iterator): ?>
                <?php echo($iterator++ == 1) ? '<div class="item product product-item '.$_portohelper->getMasonryItemClass($product_grid_layout_arr[$iterator-2]).'">' : '</div><div class="item product product-item '.$_portohelper->getMasonryItemClass($product_grid_layout_arr[$iterator-2]).'">' ?>
                <div class="product-item-info type<?php echo $product_type; ?>" data-container="product-grid">
                    <?php // Product Image ?>
                    <div class="product photo product-item-photo">
                        <a href="<?php echo $_product->getProductUrl() ?>" tabindex="-1">
                        <?php
                            if($aspect_ratio)
                                $productImage = $_imagehelper->init($_product, $image)->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize($image_width);
                            else
                                $productImage = $_imagehelper->init($_product, $image)->resize($image_width, $image_height);
                            $productImageUrl = $productImage->getUrl();
                        ?>
                            <img class="product-image-photo default_image" src="<?php echo $productImageUrl; ?>" width="<?php echo $image_width; ?>" height="<?php echo $image_height; ?>" alt="<?php //echo $productImage->getLabel();?>"/>
                        <?php if($_category_config['alternative_image']): ?>
                        <?php
                            if($aspect_ratio)
                                $productHoverImage = $_imagehelper->init($_product, $hover_image)->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize($image_width);
                            else
                                $productHoverImage = $_imagehelper->init($_product, $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>
                        <?php
                            $product_label = "";
                            if($_product_label_config['sale_label']) {
                                $orgprice = $_product->getPrice();
                                $specialprice = $_product->getSpecialPrice();
                                $specialfromdate = $_product->getSpecialFromDate();
                                $specialtodate = $_product->getSpecialToDate();
                                $today = time();
                                if(!$specialprice)
                                    $specialprice = $orgprice;
                                if($specialprice < $orgprice) {
                                    if((is_null($specialfromdate) && is_null($specialtodate)) || ($today >= strtotime($specialfromdate) && is_null($specialtodate)) || ($today <= strtotime($specialtodate) && is_null($specialfromdate)) || ($today >= strtotime($specialfromdate) && $today <= strtotime($specialtodate))){
                                        if($_product_label_config['sale_label_percent']) {
                                            $save_percent = 100-round(($specialprice/$orgprice)*100);
                                            $product_label .= '<div class="product-label sale-label">'.'-'.$save_percent.'%'.'</div>';
                                        } else {
                                            $product_label .= '<div class="product-label sale-label">'.$_product_label_config['sale_label_text'].'</div>';
                                        }
                                    }
                                }
                            }
                            if($_product_label_config['new_label']) {
                                $now = date("Y-m-d");
                                $newsFrom= substr($_product->getData('news_from_date')?$_product->getData('news_from_date'):'',0,10);
                                $newsTo=  substr($_product->getData('news_to_date')?$_product->getData('news_to_date'):'',0,10);

                                if ($newsTo != '' || $newsFrom != ''){
                                    if (($newsTo != '' && $newsFrom != '' && $now>=$newsFrom && $now<=$newsTo) || ($newsTo == '' && $now >=$newsFrom) || ($newsFrom == '' && $now<=$newsTo)) {
                                        $product_label .= '<div class="product-label new-label">'.$_product_label_config['new_label_text'].'</div>';
                                    }
                                }
                            }
                            if($product_label)
                                echo '<div class="product-labels">'.$product_label.'</div>';
                        ?>
                        <div class="product-item-inner">
                            <div class="product actions product-item-actions">
                                <div class="actions-primary">
                                    <?php if ($_product->isSaleable()): ?>
                                        <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                                        <form data-role="tocart-form" action="<?php echo $postParams['action']; ?>" method="post">
                                            <input type="hidden" name="product" value="<?php echo $postParams['data']['product']; ?>">
                                            <input type="hidden" name="<?php echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php 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 primary">
                                                <span><?php echo __('Add to Cart') ?></span>
                                            </button>
                                        </form>
                                    <?php else: ?>
                                        <?php if ($_product->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 if ($block->getMode() == 'grid'): ?>
                                <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()): ?>
                                    <a href="#"
                                       class="action towishlist actions-secondary"
                                       title="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
                                       aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
                                       data-post='<?php echo $block->getAddToWishlistParams($_product); ?>'
                                       data-action="add-to-wishlist"
                                       role="button">
                                        <span><?php echo __('Add to Wish List') ?></span>
                                    </a>
                                <?php endif; ?>
                                <?php endif; ?>
                                <?php if($_category_config['addtocompare']): ?>
                                <?php
                                $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');
                                ?>
                                <a href="#"
                                   class="action tocompare actions-secondary"
                                   title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                   aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                   data-post='<?php echo $compareHelper->getPostDataParams($_product); ?>'
                                   role="button">
                                    <span><?php echo __('Add to Compare') ?></span>
                                </a>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    <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="<?php echo $_product->getProductUrl() ?>">
                                <?php echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
                            </a>
                        </strong>
                        <?php //$_category_config['rating_star'] = "";  //disabled at the moment ?>
                        <?php if($_category_config['rating_star']): ?>
                        <?php
                            $review_model->getEntitySummary($_product);
                            $review_html = $block->getReviewsSummaryHtml($_product, $templateType);
                        ?>
                        <?php if($review_html): ?>
                            <?php echo $block->getReviewsSummaryHtml($_product, $templateType); ?>
                        <?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->getProductPrice($_product) ?>
                        <?php endif; ?>
                        <?php echo $block->getProductDetailsHtml($_product); ?>
                    </div>
                </div>
                <?php echo($iterator == count($_productCollection)+1 || $product_limit+1 == $iterator) ? '</div>' : '' ?>
                <?php endif; ?>
            <?php endforeach; ?>
        </div>
        <style>
            .products-grid.masonry-grid#<?php echo $masonry_id; ?> .filterproducts {
                margin: -<?php echo $gap_width; ?>px;
            }
            .products-grid.masonry-grid#<?php echo $masonry_id; ?> .filterproducts .product-item {
                padding: <?php echo $gap_width; ?>px;
                margin-bottom: 0;
            }
            .products-grid.masonry-grid#<?php echo $masonry_id; ?> .filterproducts .product-item.height-1 {
                height: <?php echo $grid_height; ?>px;
            }
            .products-grid.masonry-grid#<?php echo $masonry_id; ?> .filterproducts .product-item.height-1-2 {
                height: <?php echo $grid_height / 2 ?>px;
            }
            .products-grid.masonry-grid#<?php echo $masonry_id; ?> .filterproducts .product-item.height-1-3 {
                height: <?php echo $grid_height / 3; ?>px;
            }
            .products-grid.masonry-grid#<?php echo $masonry_id; ?> .filterproducts .product-item.height-2-3 {
                height: <?php echo $grid_height * 2 / 3; ?>px;
            }
            @media (max-width: 767px) {
                .products-grid.masonry-grid#<?php echo $masonry_id; ?> .filterproducts .product-item.height_md-1 {
                    height: <?php echo $grid_height ?>px;
                }
                .products-grid.masonry-grid#<?php echo $masonry_id; ?> .filterproducts .product-item.height_md-1-2 {
                    height: <?php echo $grid_height / 2 ?>px;
                }
            }
        </style>
    </div>
    <script type="text/javascript">
    require([
        'jquery',
        'imagesloaded',
        'packery'
    ], function ($, imagesLoaded, Packery) {
        $.bridget( 'packery', Packery );
        var $container = $('.products-grid.masonry-grid#<?php echo $masonry_id; ?> .filterproducts').imagesLoaded(function(){
            $container.packery({
                itemSelector: ".product-item",
                percentPosition: true
            });
        });
    });
    </script>
    <?php if (!$block->isRedirectToCartEnabled()) : ?>
        <script type="text/x-magento-init">
        {
            "[data-role=tocart-form], .form.map.checkout": {
                "catalogAddToCart": {}
            }
        }
        </script>
    <?php endif; ?>
<?php endif; ?>
