<?php
/**
 *
 * SM Listing Tabs - Version 2.7.0
 * Copyright (c) 2017 YouTech Company. All Rights Reserved.
 * @license - Copyrighted Commercial Software
 * Author: YouTech Company
 * Websites: http://www.magentech.com
 */

use Magento\Framework\App\Action\Action;

if ($this->_isAjax()) {
    $products = $this->_ajaxLoad();
    $config   = $this->_getConfig();
} else {
    $config   = $this->getData('config');
    $products = $this->getData('products');
}
$_helper         = $this->helper('Magento\Catalog\Helper\Output');
$viewMode        = 'grid';
$image           = 'category_page_grid';
$showDescription = false;
$showWishlist    = true;
$showCompare     = true;
$showCart        = true;
$templateType    = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
$type_loadmore   = $config['type_show'] == 'loadmore' ? true : false;
$cls_mark        = $type_loadmore ? ' ltabs-mark ' : '';
$_config         = $this->helper('Sm\Themecore\Helper\Data');
$rowItems        = $config['row_items'];
$classRow        = "";
if ($rowItems) {
    $nb_rows = $rowItems;
} else {
    $nb_rows = 1;
}

if ($rowItems > 1) {
    $classRow = "multi-row";
} else {
    $classRow = "one-row";
}
$count_item = count($products);
if ($count_item) {
    /**
     * Position for actions regarding image size changing in vde if needed
     */
    $pos = 0;
    if ($block->getPositioned()) {
        $pos = $block->getPositioned();
    }
    ?>
    <div class="products wrapper <?php /* @escapeNotVerified */
    echo $classRow . " ";
    echo $viewMode; ?> products-<?php /* @escapeNotVerified */
    echo $viewMode; ?>">
        <?php $iterator = 1; ?>
        <div class="products list items product-items  <?php echo $type_loadmore == false ? ' owl-carousel owl-theme' : ''; ?>" <?php echo $type_loadmore ? 'data-liffect="fadeIn"' : ''; ?>>
            <?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
            <?php
            $j     = 0;
            $count = 0;

            foreach ($products as $_product):
                $count++;
                $j++;
                ?>
                <?php if (($count % $nb_rows == 1 || $nb_rows == 1) && $type_loadmore == false) { ?>
                <div class="item-row">
            <?php } ?>

                <div class="item product product-item <?php echo $cls_mark ?>">
                    <div class="product-item-info" data-container="product-grid">
                        <?php
                        $productImage = $block->getImage($_product, $image);
                        if ($pos != null) {
                            $position = ' style="left:' . $productImage->getWidth() . 'px;'
                                . 'top:' . $productImage->getHeight() . 'px;"';
                        }
                        ?>

                        <?php // Product Image
                        ?>
                        <div class="image-product">
                            <a href="<?php /* @escapeNotVerified */
                            echo $_product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
                                <?php echo $productImage->toHtml(); ?>
                            </a>

                            <?= $_config->getLabelProduct($_product); ?>
                        </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 /* @escapeNotVerified */
                                   echo $_product->getProductUrl() ?>">
                                    <?php /* @escapeNotVerified */
                                    echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
                                </a>
                            </strong>
                            <?php echo $block->getReviewsSummaryHtml($_product, $templateType); ?>
                            <?php /* @escapeNotVerified */
                            echo $block->getProductPrice($_product) ?>
                            <?php if ((int)$config['display_countdown'] && !empty($_product->getSpecialPrice()) && !empty($_product->getSpecialToDate())) {
                                $specialToDate = $_product->getSpecialToDate();
                                ?>
                                <div class="deals-countdown"
                                     data-timer="<?php echo date("Y/m/d H:i:s", strtotime($specialToDate)); ?>">
                                    <div class="deals-time time-day">
                                        <div class="num-time"></div>
                                        <div class="title-time"></div>
                                    </div>
                                    <div class="deals-time time-hours">
                                        <div class="num-time"></div>
                                        <div class="title-time"></div>
                                    </div>
                                    <div class="deals-time time-mins">
                                        <div class="num-time"></div>
                                        <div class="title-time"></div>
                                    </div>
                                    <div class="deals-time time-secs">
                                        <div class="num-time"></div>
                                        <div class="title-time"></div>
                                    </div>
                                </div>
                            <?php } ?>
                            <?php echo $block->getProductDetailsHtml($_product); ?>

                            <div class="product-item-inner">
                                <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()): ?>
                                            <?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]; ?>">
                                                <input type="hidden" name="form_key"
                                                       value="<?php echo $block->_getFormKey(); ?>"/>
                                                <button type="submit"
                                                        title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                                        class="action tocart primary">
													<span><?php /* @escapeNotVerified */
                                                        echo __('Add to Cart') ?></span>
                                                </button>
                                            </form>
                                        <?php else: ?>
                                            <?php if ($_product->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 if ($showWishlist || $showCompare): ?>
                                        <div class="actions-secondary" data-role="add-to-links">
                                            <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow() && $showWishlist): ?>
                                                <a href="#"
                                                   data-post='<?php /* @escapeNotVerified */
                                                   echo $block->getAddToWishlistParams($_product); ?>'
                                                   class="action towishlist" 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 if ($block->getAddToCompareUrl() && $showCompare): ?>
                                                <?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare'); ?>
                                                <a href="#" class="action tocompare"
                                                   data-post='<?php /* @escapeNotVerified */
                                                   echo $compareHelper->getPostDataParams($_product); ?>'
                                                   title="<?php /* @escapeNotVerified */
                                                   echo __('Add to Compare') ?>">
													<span><?php /* @escapeNotVerified */
                                                        echo __('Add to Compare') ?></span>
                                                </a>
                                            <?php endif; ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                                <?php if ($showDescription): ?>
                                    <div class="product description product-item-description">
                                        <?php /* @escapeNotVerified */
                                        echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                                        <a href="<?php /* @escapeNotVerified */
                                        echo $_product->getProductUrl() ?>" title="<?php /* @escapeNotVerified */
                                        echo $_productNameStripped ?>"
                                           class="action more"><?php /* @escapeNotVerified */
                                            echo __('Learn More') ?></a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
                <?php if (($count % $nb_rows == 0 || $count == $count_item) && $type_loadmore == false) { ?>
                </div>
            <?php } ?>
            <?php endforeach; ?>
        </div>
    </div>
    <?php
} else {
    if (!$this->_isAjax()) { ?>
        <div class="message info empty">
            <div><?php /* @escapeNotVerified */
                echo __('We can\'t find products matching the selection.') ?></div>
        </div>
    <?php }
} ?>
