<?php
/**
 * Products template
 *
 * @var \Ves\Megamenu\Block\Widget\Products $block
 */
?>
<?php
use Magento\Catalog\Block\Product\ReviewRendererInterface;
$_productCollection = [];
$helper = $this->helper("Ves\Megamenu\Helper\Data");
$imgHelper = $this->helper("Ves\Megamenu\Helper\Image");
if ($exist = ($collection = $block->getProductCollection() && $block->getProductCollection()->getSize())) {
	$gridtype = 'widget-new-grid';
	//$type = '-ves-megamenu-list-' . time().rand();
	$mode = 'grid';
	$image = 'new_products_content_widget_grid';
	$layoutType = $this->getConfig("layout_type");
	if($layoutType == "list") {
		$gridtype = 'widget-new-list';
		//$type = '-ves-megamenu-list-' . time().rand();
		$mode = 'list';
		$image = 'new_products_content_widget_list';
	}
    $type = $gridtype;
	$title = $this->getConfig("widget_title");
	$items = $block->getProductCollection()->getItems();

	$productCollection = $block->getProductCollection();
	$number_item_percolumn = (int)$this->getConfig("number_item_percolumn", 1);
	$total = $productCollection->count();
	if ($total%$number_item_percolumn == 0) {
		$column = $total/$number_item_percolumn;
	} else {
		$column = floor($total/$number_item_percolumn)+1;
	}

	$i = $x = 0;
	foreach ($productCollection as $_product) {
		if ($i<$column) {
			$i++;
		} else {
			$i = 1;
			$x++;
		}
		$_productCollection[$i][$x] = $_product;
	}

	$showWishlist    = $this->getConfig("show_wishlist");
	$showCompare     = $this->getConfig("show_compare");
	$showCart        = $this->getConfig("show_addtocart");
	$templateType    = ReviewRendererInterface::DEFAULT_VIEW;
	$description     = $this->getConfig("show_short_description");
	$show_name       = $this->getConfig("show_name");
	$show_price      = $this->getConfig("show_price");
	$show_review     = $this->getConfig("show_review");
	$show_image      = $this->getConfig("show_image");
	$show_new_label  = $this->getConfig("show_new_label");
	$show_sale_label = $this->getConfig("show_sale_label");
	$image_width     = $this->getConfig('image_width',150);
	$image_height    = (int)$this->getConfig('image_height');
	$item_height     = (int)$this->getConfig('item_height');

	//If image width is not specified, use default values
	if ($image_width <= 0) {
		$image_width = 150;
		$image_height = 150;
	}
	$catViewKeepFrame = TRUE;

	if ($helper->getCoreRegistry()->registry('catViewKeepFrame') === NULL) {
		$helper->getCoreRegistry()->register('catViewKeepFrame', $catViewKeepFrame);
	}
}
$large_max_items    = (int)$this->getConfig('large_max_items',6);
$large_items        = (int)$this->getConfig('large_items',5);
$portrait_items     = (int)$this->getConfig('portrait_items',4);
$tablet_items       = (int)$this->getConfig('tablet_items',4);
$tablet_small_items = (int)$this->getConfig('tablet_small_items',2);
$mobile_items       = (int)$this->getConfig('mobile_items',1);
$blockId            = time().rand();
$_helper            = $this->helper('Magento\Catalog\Helper\Output');
$short_max_char     = (int)$this->getConfig('short_max_char',100);
$show_learnmore     = $this->getConfig('show_learnmore');
$block_width        = (int)$this->getConfig('block_width');
$show_sku			= $this->getConfig('show_sku');
?>
<?php if ($exist): ?>
	<div class="block widget megamenu-widget products-widget <?= $this->getData('addition_class') ?>">
		<?php if ($title != '') : ?>
		<div class="block-title">
			<strong><?= $title; ?></strong>
		</div>
		<?php endif; ?>
		<div class="block-content">
			<?php if($layoutType != "list") : ?>
                <?php if( $this->getConfig("nav") && count($_productCollection) > 0 ) : ?>
                <div class="owl-nav">
                    <a href="javascript:void(0)" data-owlid="menuowl-<?= $blockId ?>" class="owl-left"><i class="fa fa-angle-left"></i></a>
                    <a href="javascript:void(0)" data-owlid="menuowl-<?= $blockId ?>" class="owl-right"><i class="fa fa-angle-right"></i></a>
                </div>
                <?php endif; ?>
			<?php endif; ?>
			<div class="<?php if ($layoutType == "list") : ?>products-list list <?php else : ?>products-grid grid megamenuowl-play<?php endif; ?>">
				<ol id="menuowl-<?= $blockId ?>" class="product-items <?php if ($layoutType != "list") : ?>owl-carousel<?php endif; ?> <?= $gridtype; ?> megamenu-products<?= $blockId ?>"
					data-items="<?= $this->getConfig("column", 3); ?>"
					data-mousedrag="<?= $this->getConfig("mousedrag")?"true":"false"; ?>"
					data-pulldrag="<?= $this->getConfig("pulldrag")?"true":"false"; ?>"
					data-stagepadding="<?= (int)$this->getConfig("stagepadding") ?>"
					data-lazyload="<?= $this->getConfig("lazyload")?"true":"false"; ?>"
					data-rtl="<?= $this->getConfig("rtl")?"true":"false"; ?>"
					data-center="<?= $this->getConfig("center")?"true":"false"; ?>"
					data-margin="<?= (int)$this->getConfig("margin") ?>"
					data-autoplay="<?= $this->getConfig("autoplay")?"true":"false"; ?>"
					data-autoplay-timeout="<?= $this->getConfig("autoplay_timeout", 3000); ?>"
					data-dot="<?= $this->getConfig("dots")?"true":"false"; ?>"
					data-loop="<?= $this->getConfig("loop")?"true":"false"; ?>"
					data-nav = "false"
					data-autoplay-pauonhover="<?= $this->getConfig("autoplay_hoverpause")?"true":"false"; ?>"
					data-mobile-items="<?= $mobile_items; ?>"
					data-tablet-small-items="<?= $tablet_small_items; ?>"
					data-tablet-items="<?= $tablet_small_items; ?>"
					data-portrait-items="<?= $portrait_items; ?>"
					data-large-items="<?= $large_items; ?>"
					data-large-max-items="<?= $large_max_items; ?>"
					<?php if ($block_width) : ?>style="width: <?= $block_width; ?>px !important"<?php endif; ?>>
						<?php $iterator = 1; ?>
						<?php foreach ($_productCollection as $_columnCollection) : ?>
						<?=($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>
						<?php foreach ($_columnCollection as $_item): ?>
                            <?php /** @var \Magento\Catalog\Model\Product $_item */?>
							<div class="product-block" <?php if ($item_height) : echo "style='height:".$item_height."px;max-width: 100%;'"; endif; ?>>
								<div class="product-item-info">
									<?php if ($show_new_label || $show_sale_label) : ?>
									<div class="icon">
										<?php
										if ($show_sale_label) :
											$specialprice = $_item->getSpecialPrice();
											$specialPriceFromDate = $_item->getSpecialFromDate();
											$specialPriceToDate = $_item->getSpecialToDate();
											$today =  time();
											if ($specialprice && $specialprice != $_item->getPrice()) :
                                                $specialPriceToDateTime = !empty($specialPriceToDate) ? strtotime($specialPriceToDate) : strtotime("+1 day");
                                                $specialPriceFromDateTime = !empty($specialPriceFromDate) ? strtotime($specialPriceFromDate) : strtotime("-1 day");
												if(($today >= $specialPriceFromDateTime && $today <= $specialPriceToDateTime) || ($today >= $specialPriceFromDateTime && is_null($specialPriceToDate))) :
												?>
													<span class="onsale"><?= __("Sale");?></span>
												<?php
												endif;
											endif;
										endif;
										if ($show_new_label) :
											$is_new = $block->checkProductIsNew( $_item );
											if ($is_new) : ?>
                                                <span class="new-icon"><?= __("New");?></span>
                                                <?php
                                            endif;
									    endif;
									?>
								</div>
								<?php endif; ?>
								<?php if($show_image) : ?>
								<?php $_image = $imgHelper->getImg($_item, $image_width, $image_height, 'category_page_grid'); ?>
								<?php if($_image) : ?>
								<div class="product-image">
									<a href="<?= $block->getProductUrl($_item) ?>" class="product-item-photo">
										<img alt="<?= $_image->getLabel() ?>" <?php if ($this->getConfig("lazyload")) { ?> class="owl-lazy" data-src="<?= $_image->getUrl(); ?>" <?php } else { ?> src="<?= $_image->getUrl(); ?>"<?php } ?> width="<?= $image_width ?>" height="<?= $image_height ?>"
										/>
									</a>
									<?php if ($showWishlist || $showCompare || $showCart): ?>
										<div class="product-item-actions">
											<?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":"<?= $block->getAddToCartUrl($_item) ?>"}}'
															type="button" title="<?= __('Add to Cart') ?>">
															<span><?= __('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='<?= $postData; ?>'
														type="button" title="<?= __('Add to Cart') ?>">
														<span><?= __('Add to Cart') ?></span>
													</button>
												<?php endif; ?>
											<?php else: ?>
												<?php if ($_item->getIsSalable()): ?>
													<div class="stock available"><span><?= __('In stock') ?></span></div>
												<?php else: ?>
													<div class="stock unavailable"><span><?= __('Out of stock') ?></span></div>
												<?php endif; ?>
											<?php endif; ?>
										</div>
									<?php endif; ?>
									<?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='<?= $block->getAddToWishlistParams($_item); ?>'
												class="action towishlist" data-action="add-to-wishlist"
												title="<?= __('Add to Wish List') ?>">
												<span><?= __('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='<?= $compareHelper->getPostDataParams($_item);?>'
											title="<?= __('Add to Compare') ?>">
											<span><?= __('Add to Compare') ?></span>
										</a>
									<?php endif; ?>
								</div>
							<?php endif; ?>
						</div>
					<?php endif; ?>
				</div>
				<?php endif; ?>
				<?php endif; ?>
				<div class="product-item-details">
					<?php if($show_name) : ?>
					<strong class="product-item-name">
						<a title="<?= $block->escapeHtml($_item->getName()) ?>"
							href="<?= $block->getProductUrl($_item) ?>"
							class="product-item-link">
							<?= $block->escapeHtml($_item->getName()) ?>
						</a>
					</strong>
					<?php endif; ?>
					<?php if ($show_sku) : ?>
						<span><?= __("Sku: ").$_item->getSku(); ?></span>
					<?php endif; ?>

					<?php // Short Description ?>
					<?php $shortDescription = $_helper->productAttribute($_item, $_item->getShortDescription(), 'short_description'); ?>
					<?php if ($description && $shortDescription) : ?>
					<div class="product-item-inner">
						<div class="product-item-description">
							<?php $short_description = $helper->subString($shortDescription, $short_max_char, '...'); ?>
							<?= $short_description; ?>
							<?php if($show_learnmore && $shortDescription!=''){ ?>
							<a href="<?= $_item->getProductUrl() ?>" title="<?= $_item->getName() ?>" class="action more">
								<?= __('Learn More') ?>
							</a>
							<?php } ?>
						</div>
					</div>
					<?php endif; ?>

					<?php // Price ?>
					<?php if($show_price) : ?>
					<?= $block->getVesProductPriceHtml($_item, $type) ?>
					<?php endif; ?>
					<?php if ($show_review && $templateType): ?>
						<?= $block->getReviewsSummaryHtml($_item, $templateType, true) ?>
					<?php endif; ?>
				</div>
			</div>
		</div>
	<?php endforeach ?>
	<?= ($iterator == count($items)+1) ? '</li>' : '' ?>
	<?php endforeach; ?>
</ol>
</div>
</div>
</div>
<?php endif; ?>
