<?php 
use Magento\Framework\App\Action\Action;
$_productCollection = $this->getProducts();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$timer_active = $this->getConfig('activate_pricecountdown');
$count = 1;
$_current_timer = time();
$itemsDefault = $this->getConfig('itemsDefault');
$itemsDesktop = $this->getConfig('itemsDesktop');
$itemsDesktopSmall = $this->getConfig('itemsDesktopSmall');
$itemsTablet = $this->getConfig('itemsTablet');
$itemsMobile = $this->getConfig('itemsMobile');
$speed = $this->getConfig('speed');
$owl_margin = $this->getConfig('owl_margin');
$auto = (1 == $this->getConfig('auto')) ? 'true' : 'false';
$next_back = (1 == $this->getConfig('next_back')) ? 'true' : 'false';
$owl_dots = (1 == $this->getConfig('owl_dots')) ? 'true' : 'false';
$owl_loop = (1 == $this->getConfig('owl_loop')) ? 'true' : 'false';
$owl_center = (1 == $this->getConfig('owl_center')) ? 'true' : 'false';
$width = ($this->getConfig('width')) ? $this->getConfig('width') : 500;
$height = ($this->getConfig('height')) ? $this->getConfig('height') : 500;
$enabled_hover = $this->getConfig('enabled_hover');
$image_type = $this->getConfig('image_type');
$image_type_hover = $this->getConfig('image_type_hover');
$hide_tocart = (1 == $this->getConfig('hide_tocart')) ? 'hide_tocart' : '';
$hide_tocompare = (1 == $this->getConfig('hide_tocompare')) ? 'hide_tocompare' : '';
$hide_towishlist = (1 == $this->getConfig('hide_towishlist')) ? 'hide_towishlist' : '';
$hide_quickview = (1 == $this->getConfig('hide_quickview')) ? 'hide_quickview' : '';

?>
<div class="rokan-price-count-down <?= $hide_tocart ?> <?= $hide_tocompare ?> <?= $hide_towishlist ?> <?= $hide_quickview ?>">
	<?php			
		if ($block->getMode() == 'grid') {
			$viewMode = 'grid';
			$image = 'category_page_grid';
			$showDescription = false;
			$templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
		} else {
			$viewMode = 'list';
			$image = 'category_page_list';
			$showDescription = true;
			$templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::FULL_VIEW;
		}
		$pos = $block->getPositioned();
		
	?>
	<?php if($this->getConfig('title') || $this->getConfig('see_all') || $this->getConfig('short_description')){?>
		<div class="rokan-title <?php if($this->getConfig('short_description')){?>has-short-des<?php endif; ?> <?php if($this->getConfig('see_all')){?>has-see-all<?php endif; ?>">
			<?php if($this->getConfig('title')): ?><h3 class="module-title"><?php echo $this->getConfig('title')?><?php if($this->getConfig('see_all')){?><a class="see-all" href="<?php echo $this->getConfig('see_all')?>"><?= __('See All') ?></a><?php }?></h3><?php endif; ?>
			<?php if($this->getConfig('short_description')){?>
				<div class="rokan-short-description">
					<p class="module-short-description"><?php echo $this->getConfig('short_description')?></p>
				</div>
			<?php }?>
		</div>
	<?php }?>
	<?php if(!$_productCollection->getSize()): ?> 
		<div class="rokan-onslate-slider">	
			<p class="note-msg"><?php echo __('There are no products matching the selection.') ?></p>
		</div>
	<?php else: ?>
		<?php
		if(!$rows = $this->getConfig('row_show'))
			$rows = 1;
		?>	
		<div class="products wrapper grid products-grid"> 
			<ul class="products list items product-items owl-carousel owl-theme" data-rtl="false" data-items="<?php echo $itemsDefault; ?>" data-bigdesktop="<?php echo $itemsDesktop; ?>" data-smalldesktop="<?php echo $itemsDesktopSmall; ?>" data-bigtablet="<?php echo $itemsTablet; ?>" data-tablet="<?php echo $itemsTablet; ?>" data-smalltablet="<?php echo $itemsTablet; ?>" data-mobile="<?php echo $itemsMobile; ?>" data-margin="<?php echo $owl_margin; ?>" data-loop="<?php echo $owl_loop; ?>" data-center="<?php echo $owl_center; ?>" data-mousedrag="true" data-touchdrag="true" data-stagepadding="1" data-nav="<?php echo $next_back; ?>" data-navnext="" data-navprev="" data-rewind="true" data-dots="<?php echo $owl_dots ?>" data-lazyload="false" data-autoplay="<?php echo $auto ?>" data-speed="<?php echo $speed ?>">
				<?php $_collectionSize = $_productCollection->count() ?>
				<?php $i=0; foreach ($_productCollection as $_product): ?>
					<?php if($i %$rows == 0) echo '<li class="item-load item product product-item">';  
						$pec_ = $this->helper('Rokanthemes\Superdeals\Helper\Data')->showLableSalePriceCategory($_product, true);
					?>
						<div class="product-item-info flex-layout no-wrap">
							<?php
							$resizedImageUrl = $block->resizeImage($_product, $image_type, $width, $height)->getUrl();
							?>
							<?php // Product Image ?>
							<div class="product-thumb">
								<a href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
									<span class="image0 image-switch">
										<img class="product-image" src="<?= $resizedImageUrl ?>" width="<?= $width ?>" height="<?= $height ?>" alt="<?php echo $_product->getName();?>">
									</span>
									<?php if($enabled_hover == 1){ 
										$resizedImageHoverUrl = $block->resizeImage($_product, $image_type_hover, $width, $height)->getUrl();
									?>
										<span class="image1 image_hover">
											<img class="product-image" src="<?= $resizedImageHoverUrl ?>" width="<?= $width ?>" height="<?= $height ?>" alt="<?php echo $_product->getName();?>">
										</span>
									<?php } ?>
								</a>
							</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="<?= $block->escapeUrl($_product->getProductUrl()) ?>">
										<?= /* @noEscape */ $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
									</a>
								</strong>
								<?= $block->getReviewsSummaryHtml($_product, $templateType) ?>
								<div class="info-price">
									<?= /* @noEscape */ $block->getProductPrice($_product) ?>
								</div>
								<?php if ( $_product->getFinalPrice() < $_product->getPrice() ): ?>
									<span class="product-label onsale">
										<span><?= $block->escapeHtml(__('Sale')) ?></span> <span class="value"><?= 100 - round(($_product->getFinalPrice() / $_product->getPrice() * 100)) ?>% <?= $block->escapeHtml(__('Off')) ?></span> 
									</span>
								<?php endif; ?>
								<?php if ($this->getConfig('description') == 1) :?>
									<div class="product description product-item-description">
										<?php 
											$maxlength = $this->getConfig('maxlength');
											if($maxlength){
												$short_description = $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description');
												if (strlen($short_description) > $maxlength){
													$short_description = substr($short_description, 0, $maxlength) . '...';
												}
												echo $short_description;
											}else{
												echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description');
											}
										?>
									</div>
								<?php endif; ?> 
								
								<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"
													  data-product-sku="<?= $block->escapeHtmlAttr($_product->getSku()) ?>"
													  action="<?= $block->escapeUrl($postParams['action']) ?>"
													  method="post">
													<input type="hidden"
														   name="product"
														   value="<?= /* @noEscape */ $postParams['data']['product'] ?>">
													<input type="hidden" name="<?= /* @noEscape */ Action::PARAM_NAME_URL_ENCODED ?>"
														   value="<?= /* @noEscape */ $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
													<?= $block->getBlockHtml('formkey') ?>
													<button type="submit"
															title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>"
															class="action tocart primary">
														<span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
													</button>
												</form>
											<?php else :?>
												<?php if ($_product->isAvailable()) :?>
													<div class="stock available"><span><?= $block->escapeHtml(__('In stock')) ?></span></div>
												<?php else :?>
													<div class="stock unavailable"><span><?= $block->escapeHtml(__('Out of stock')) ?></span></div>
												<?php endif; ?>
											<?php endif; ?>
										</div>
										<div data-role="add-to-links" class="actions-secondary">
											<?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare'); ?>
											<a href="#"
											   class="action tocompare"
											   title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
											   aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
											   data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataParams($_product); ?>'
											   role="button">
												<span><?php /* @escapeNotVerified */ echo __('Add to Compare') ?></span>
											</a>
											<?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()): ?>
												<a href="#" class="action towishlist"
													title=""
													aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
													data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_product); ?>'
													data-action="add-to-wishlist"
													role="button">
													<?php echo $block->escapeHtml(__('Wish List')); ?>
												</a>
											<?php endif; ?>
										</div>
									</div>
									<div class="count-down deal-timer">
										<div class="time_circles">
										<?php if($timer_active == 1){ ?>
										<?php $_product = $_product->load($_product->getId()) ?>
										<?php $toDate = $_product->getSpecialTODate();?>
										<?php $fromDate = $_product->getSpecialFromDate(); ?>
										<?php if(($_product->getPrice() > $_product->getSpecialPrice() || $_product->getTypeId() == 'bundle') && $_product->getTimershow() == 1) { ?>
											<?php if($_product->getSpecialPrice()!= 0 || $_product->getSpecialPrice()!= "" ) { ?>
													<div id="countbox_<?php echo $count; ?>" class="timer-list"></div>
													<?php
														if($_product->getTypeId() == 'bundle')
														{
															echo '<h1>Discount up to '.(100 - $_product->getSpecialPrice()*1).'% </h1>';
														}
													?> 
													<script>
													var DisplayFormat = "<span class='countdown-section'><span class='countdown-amount'>%%D%%</span><span class='countdown-period'><?php echo __('Day') ?></span></span><span class='countdown-section'><span class='countdown-amount'>%%H%%</span><span class='countdown-period'><?php echo __('Hrs') ?></span></span><span class='countdown-section'><span class='countdown-amount'>%%M%%</span><span class='countdown-period'><?php echo __('Min') ?></span></span><span class='countdown-section'><span class='countdown-amount'>%%S%%</span><span class='countdown-period'><?php echo __('Sec') ?></span></span>";
														var dthen<?php echo $count; ?> = new Date("<?php echo Date("m/d/Y", strtotime($toDate)).' 11:59:00 PM'; ?>");
														start = "<?php echo Date("m/d/y h:i:s A", $_current_timer); ?>";
														start_date = Date.parse(start);
														var dnow<?php echo $count; ?> = new Date(start_date);
														if(CountStepper>0)
															ddiff= new Date((dnow<?php echo $count; ?>)-(dthen<?php echo $count; ?>));
														else
															ddiff = new Date((dthen<?php echo $count; ?>)-(dnow<?php echo $count; ?>));
														//gsecs<?php echo $count; ?> = Math.floor(ddiff.valueOf()/1000);
														<?php if(strtotime($fromDate) <= $_current_timer){ ?>
															gsecs<?php echo $count; ?> = Math.floor(ddiff.valueOf()/1000);
														<?php }else{ ?>
															gsecs<?php echo $count; ?> = -1;
														<?php } ?>

														var iid<?php echo $count; ?> = "countbox_<?php echo $count; ?>";
														CountBack(gsecs<?php echo $count; ?>,"countbox_"+j, j);
														j++;
													</script>
													<?php $count = $count + 1; ?>
													<?php
												}
											}
										} ?>
										</div>
									</div>
								</div>
							</div>
						<?php $i++;?>
					</div>
				<?php if($i %$rows == 0) echo "</li>"; ?>                  
				<?php endforeach; ?>
					<?php if($i %$rows != 0) echo "</li>"; ?>                
			</ul>
		</div>
	<?php if (!$block->isRedirectToCartEnabled()) : ?> 
		<script>
		{
			"[data-role=tocart-form], .form.map.checkout": {
				"catalogAddToCart": {}
			}
		}
		</script>
	<?php endif; ?>
<?php endif; ?>
</div>