<?php $attribute = NULL ?>
<?php if($this->getData('attribute')!=''): ?>
	<?php $attribute = $this->getData('attribute') ?>
<?php endif ?>
<?php $_productCollection = $this->getProductByCategories($this->getCategoryIds(), $attribute) ?>
<?php $builderHelper = $this->helper('MGS\Fbuilder\Helper\Data'); ?>
<?php $blockType = $this->getData('block_type'); ?>
<?php $template = 'MGS_Fbuilder::products/grid_item.phtml'; ?>

<div class="content-block <?= $blockType ?>-product-cat-block">
	<?php // Block Content  ?>
	<?php if (isset($_productCollection) && (count($_productCollection))): ?>
		
		<?php $blockId = rand() . time(); ?>
		<?php $useSlider = $this->getUseSlider(); ?>
		<?php $items = $perRowDefault = $this->getPerrow(); ?>
		<?php $perRowTablet = $this->getPerrowTablet(); ?>
		<?php $perRowMobile = $this->getPerrowMobile(); ?>
		
		<?php if($useSlider && $this->hasData('number_row') && $this->getNumberRow()>1): ?>
			<?php $slideMultiRow = true; ?>
			<?php $slideBy = 1 ?>
			<?php $columnCount = $perRowDefault * $this->getNumberRow(); ?>
		<?php else: ?>
			<?php $slideBy = $this->getSlideBy() ?>
			<?php $slideMultiRow = false; ?>
		<?php endif ?>
		
		<?php if($useSlider): ?>
		
		<?php 
			$settingMode = 'multiple'; 
			$navPosition = $this->getNavigationPosition();
	
			$navType = $builderHelper->getStoreConfig('fbuilder/'.$settingMode.'_slider/navigation');
			if($navType=='image'){
				$nextIcon = $builderHelper->getStoreConfig('fbuilder/'.$settingMode.'_slider/next_image');
				$prevIcon = $builderHelper->getStoreConfig('fbuilder/'.$settingMode.'_slider/prev_image');
				$navHtml = '["<span><img src=\''.$builderHelper->getMediaUrl().'mgs/fbuilder/slider/'.$prevIcon.'\'/></span>", "<span><img src=\''.$builderHelper->getMediaUrl().'mgs/fbuilder/slider/'.$nextIcon.'\'/></span>"]';
			}elseif($navType=='font'){
				$nextIcon = $builderHelper->getStoreConfig('fbuilder/'.$settingMode.'_slider/next_font_class');
				$prevIcon = $builderHelper->getStoreConfig('fbuilder/'.$settingMode.'_slider/prev_font_class');
				$navHtml = '["<span><em class=\''.$prevIcon.'\'></em></span>", "<span><em class=\''.$nextIcon.'\'></em></span>"]';
			}else {
				$navHtml = '["<span></span>", "<span></span>"]';
				
			}
		?>
		<?php endif ?>
		
		<div class="block widget mgs-products <?= $blockType ?>-products-grid products-grid product-items">
			<div id="<?= $blockType ?>-products<?= $blockId; ?>" class="items<?php if($useSlider): ?> owl-carousel mgs-owl-carousel mgs-carousel-<?= $settingMode ?> nav-type-<?= $navType ?> nav-position-<?= $navPosition ?> <?php if($this->getHideNav() && ((($items==1) && ($navPosition!='middle-outside')) || (($items>1) && ($navPosition=='middle-inside')))): ?> autohide-nav<?php endif ?> dot-position-<?= $this->getPaginationPosition()?><?php else: ?> line row-<?= $perRowDefault ?> row-tablet-<?= $perRowTablet ?> row-mobile-<?= $perRowMobile ?><?php endif ?>">
				<?php $i=0; $j=0; foreach ($_productCollection as $_product): $i++; ?>
					
					<?php if($slideMultiRow): ?>
						<?php if ($j++ % $columnCount == 0): ?>
						<div class="line row-<?= $perRowDefault ?> row-tablet-<?= $perRowTablet ?> row-mobile-<?= $perRowMobile ?>">
						<?php endif ?>
					<?php endif ?>
					
							<div class="product product-item <?php if($slideMultiRow || !$this->hasData('number_row')): ?>col<?php endif ?>">
								<?= $this->getLayout()
									->createBlock('MGS\Fbuilder\Block\Products\ProductItem')
									->setProductId($_product->getId())
									->setBlockData($this->getData())
									->setTemplate($template)
									->toHtml(); ?>
												
							</div>
							
					<?php if($slideMultiRow): ?>
						<?php if ($j % $columnCount == 0 || $j == count($_productCollection)): ?>
						</div>
						<?php endif ?>
					<?php endif ?>
					
				<?php endforeach; ?>
			</div>
		</div>
		
		<?php if($useSlider): ?>
			<?php $autoPlay = $this->getAutoplay() ? 'true' : 'false'; ?>
			<?php $stopAuto = $this->getStopAuto() ? 'true' : 'false'; ?>
			<?php $nav = $this->getNavigation() ? 'true' : 'false'; ?>
			<?php $dot = $this->getPagination() ? 'true' : 'false'; ?>
			<?php $rtl = $this->getRtl() ? 'true' : 'false'; ?>
			<?php $loop = $this->getLoop() ? 'true' : 'false'; ?>
			<?php $slideBy = $slideBy ? $slideBy : 1; ?>
			
			<script type="text/javascript">
				require([
					'jquery',
					'mgsowlcarousel'
				], function ($) {
					var owl<?= $blockType ?>cattab<?= $blockId ?> = $('#<?= $blockType ?>-products<?= $blockId ?>').owlCarousel({
						<?php if($slideMultiRow): ?>
							items: 1,
						<?php else: ?>
							items: <?= $perRowDefault; ?>,
						<?php endif ?>
						autoplay: <?= $autoPlay ?>,
						autoplayHoverPause: <?= $stopAuto ?>,
						nav: <?= $nav ?>,
						dots: <?= $dot ?>,
						slideBy: <?= $slideBy ?>,
						rtl: <?= $rtl; ?>,
						loop: <?= $loop; ?>,
						<?php if(!$slideMultiRow):?>margin: <?= $this->getSlideMargin() ?>,<?php endif ?>
						<?php if($navPosition=='top-left' || $navPosition=='top-right'): ?>
							onInitialized: function(){
								$("#<?= $blockType ?>-products<?= $blockId ?> .owl-nav").attr('style', 'top:<?= $this->getNavTop() ?>px');
							},
						<?php endif ?>
						<?php if(!$slideMultiRow): ?>
						responsive:{
							0:{
								items:<?= $perRowMobile; ?>,
								nav: false,
							},
							571:{
								items:<?= $perRowTablet; ?>
							},
							1200:{
								items:<?= $perRowDefault; ?>
							}
						},
						<?php endif ?>
						navText: <?= $navHtml ?>,
						autoHeight:true
					});
				});
			</script>
		<?php endif ?>
	<?php else: ?>
		<div class="message info empty"><div><?= /* @escapeNotVerified */ __('We can\'t find products matching the selection.') ?></div></div>
	<?php endif ?>
</div>