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

// @codingStandardsIgnoreFile

?>
<?php
/**
 * Template for displaying new products widget
 *
 * @var $block \Magento\Catalog\Block\Product\Widget\NewWidget
 */
if ($exist = ($block->getProductCollection() && $block->getProductCollection()->getSize())) {
    $type = 'widget-product-carousel';

    $mode = 'grid';

    $image = 'new_products_content_widget_grid';
    $title = __('Latest Products');
    $items = $block->getProductCollection()->getItems();

    $showWishlist = true;
    $showCompare = true;
    $showCart = true;
    $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
    $description = false;
}
$data = $this->helper('Megnor\Framework\Helper\Data');
?>

<?php if ($exist):?>
    <div class="block widget block-new-products <?php /* @escapeNotVerified */ echo $mode; ?>">
       	<div class="block-title tabbernav_product collection-title">
            <h2><a role="heading" aria-level="2"><span><?php /* @escapeNotVerified */ echo $title; ?></span></a></h2>
        </div>
        <div class="block-new-content">
            <?php /* @escapeNotVerified */ echo '<!-- ' . $image . '-->' ?>
            <div class="products wrapper products-<?php /* @escapeNotVerified */ echo $mode; ?> <?php /* @escapeNotVerified */ echo $mode; ?>">
                <ol class="products list items product-items <?php /* @escapeNotVerified */ echo $type; ?>">
              
					<?php $iterator = 1; ?>
                    <?php foreach ($items as $_item): ?>
                        <?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="item product product-item">' : '</li><li class="item product product-item">' ?>
                        <div class="product-item-info">
                            <div class="product-item-image">
									<a href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>" class="product-item-photo">
										<?php echo $block->getImage($_item, $image)->toHtml(); ?>
									</a>


                                <?php
                                    $is_new = $data->checkProductIsNew($_item);
                                    if($is_new) { ?>
                                        <span class="new-label"><?php echo __("New");?></span>
                                    <?php } ?>

                                    <?php
                                        
                                        $specialprice = $_item->getSpecialPrice();
                                        $specialPriceFromDate = $_item->getSpecialFromDate();
                                        $specialPriceToDate = $_item->getSpecialToDate();
                                        $today =  time();
                                        if ($specialprice) {
                                            if($today >= strtotime($specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime($specialPriceFromDate) && is_null($specialPriceToDate)) {
                                                $originalPrice = $_item->getPrice();
                                                $finalPrice = $_item->getFinalPrice();
                                                $percentage = $percentage = round(($originalPrice - $finalPrice) * 100 / $originalPrice);
                                        ?>
                                        <span class="sale-label"><?php echo $percentage."%";?></span>
                                        <?php
                                        }
                                    }
                                ?>
								<?php if ($templateType): ?>
                                    <?php echo $block->getReviewsSummaryHtml($_item, $templateType,true) ?>
                                <?php endif; ?>	
							</div>
                            <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
                                echo $block->getProductPriceHtml($_item, $type);
                                ?>
                                <div class="product-item-inner">

                                        <?php if ($showWishlist || $showCompare || $showCart): ?>
                                           
                                           <?php if ($showWishlist): ?>
                                            <div class="actions-secondary wishlist">
                                                <div class="product-item-actions">
                                                    <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow() && $showWishlist): ?>
                                                        <a href="#"
                                                           data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_item); ?>'
                                                           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; ?>
                                                    
                                                </div>
                                            </div>
                                            <?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" title="Out of Stock"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
                                                        <?php endif; ?>
                                                    <?php endif; ?>
                                                </div>
                                            <?php endif; ?>
                                            <?php if ($showCompare): ?>
                                                <div class="actions-secondary compare">
                                                   <div class="product-item-actions">
                                                    <?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($_item);?>'
                                                           title="<?php /* @escapeNotVerified */ echo __('Add to Compare') ?>">
                                                            <span><?php /* @escapeNotVerified */ echo __('Add to Compare') ?></span>
                                                        </a>
                                                    <?php endif; ?>
                                                </div>
                                                </div>
                                            <?php endif; ?>

                                    </div>
                                        <?php endif; ?>
                                    </div>                               
                            </div>

                        <!-- </div> -->
                        <?php echo($iterator == count($items)+1) ? '</li>' : '' ?>
                    <?php endforeach ?>
                </ol>
            </div>
            <?php echo $block->getPagerHtml() ?>
        </div>
    </div>
<?php endif;?>
