<?php $portfolios = $this->getPortfolios() ?>
<?php $helper = $this->helper('MGS\Portfolio\Helper\Data'); ?>
<div class="content-block portfolio-block portfolio-list-block">
	<?php if($this->hasData('mgs_panel_title') || $this->hasData('mgs_panel_note')): ?>
		<div class="content-heading">
			<?php if($this->hasData('mgs_panel_title')): ?>
				<h2 class="title"><?php echo $helper->decodeHtmlTag($this->getData('mgs_panel_title')) ?></h2>
			<?php endif ?>
			<?php if($this->hasData('mgs_panel_note')): ?>
				<div class="block-note"><?php echo $helper->decodeHtmlTag($this->getData('mgs_panel_note')) ?></div>
			<?php endif ?>
		</div>
	<?php endif ?>
	<?php if(count($portfolios)>0): ?>
		<?php
			$themeHelper = $this->helper('MGS\Mpanel\Helper\Data');
			$blockId = rand() . time();
			$useSlider = $this->getUseSlider();
			$showThumbnail = $this->getShowThumbnail();
			$showCategories = $this->getShowCategories();
			$showShowContent = $this->getShowContent();
			$perRow = $this->getPerrow();
			$col = $themeHelper->convertPerRowtoCol($perRow);
		?>
		<?php if($useSlider): ?>
			<?php 
				if($this->hasData('number_row') && $this->getNumberRow()>1){
					$columnCount = $perRow * $this->getNumberRow();
				}
			?>
			<div class="block widget mgs-portfolio row">
				<div class="owl-carousel portfolio-grid mgs-portfolio-carousel-<?php echo $blockId; ?>">
					<?php $i=0; $j=0; foreach ($portfolios as $portfolio): $i++; ?>
						<?php if($this->hasData('number_row') && $this->getNumberRow()>1): ?>
							<?php if ($j++ % $columnCount == 0): ?>
								<div class="item">
							<?php endif ?>
						<?php endif ?>
						<div<?php if($this->getNumberRow()>1): ?> class="<?php echo $themeHelper->getClearClass($perRow, $i) ?> <?php echo $themeHelper->convertColClass($col, 'col') ?>"<?php else: ?> class="item"<?php endif ?>>
							<div class="portfolio-item">
								<div class="portfolio-top-content">
									<?php if ($showThumbnail): ?>
										<a href="<?php echo $portfolio->getAddress(); ?>"><img src="<?php echo $this->getThumbnailSrc($portfolio)?>" alt="<?php echo $portfolio->getName() ?>" class="img-fluid"/></a>
									<?php endif ?>
								</div>
								<div class="portfolio-bottom-content">
									<h4><a href="<?php echo $portfolio->getAddress(); ?>"><?php echo $portfolio->getName() ?></a></h4>
									
									<?php if($showCategories): ?>
										<p class="category-link"><?php echo $this->getCategoriesLink($portfolio)?></p>
									<?php endif ?>
									
									<?php if ($showShowContent): ?>
										<?php $length = $this->getCharacterCount() ?>
										<div class="short-content">
											<?php if($length): ?>
												<?php echo $this->truncate($portfolio->getDescription(), $length); ?>
											<?php else: ?>
												<?php echo $portfolio->getDescription() ?>
											<?php endif ?>
										</div>
									<?php endif ?>
								</div>
							</div>
						</div>
						<?php if($this->hasData('number_row') && $this->getNumberRow()>1): ?>
							<?php if ($j % $columnCount == 0 || $j == count($portfolios)): ?>
								</div>
							<?php endif ?>
						<?php endif ?>
					<?php endforeach ?>
				</div>
			</div>
			<?php if($this->hasData('number_row') && $this->getNumberRow()>1):?>
				<?php $perRow = 1; ?>
			<?php endif ?>
			<script type="text/javascript">
				require([
					'jquery',
					'mgs/owlcarousel'
				], function ($) {
					var owl<?php echo $blockId ?> = $('.mgs-portfolio-carousel-<?php echo $blockId ?>').owlCarousel({
						items: <?php echo $perRow; ?>,
						autoplay: <?php echo $this->getAutoplay() ? 'true' : 'false' ?>,
						autoplayHoverPause: <?php echo $this->getStopAuto() ? 'true' : 'false' ?>,
						nav: <?php echo $this->getNavigation() ? 'true' : 'false' ?>,
						dots: <?php echo $this->getPagination() ? 'true' : 'false' ?>,
                        rtl: RTL,
						navText: ["<i class='fa fa-arrow-left'></i>","<i class='fa fa-arrow-right'></i>"],
						<?php if(!$this->hasData('number_row') || $this->getNumberRow()==1):?>
							responsive:{
								0:{
									items:1
								},
								480:{
									items:2
								},
								768:{
									items:<?php echo $perRow; ?>
								}
							}
						<?php endif ?>
					});
					<?php if($this->getPagination()): ?>
						owl<?php echo $blockId ?>.on('changed.owl.carousel', function (event) {
							if (event.item.count - event.page.size == event.item.index)
								$(event.target).find('.owl-dots div:last')
								  .addClass('active').siblings().removeClass('active');
						});
					<?php endif ?>
				});
			</script>
		<?php else: ?>
			
			<div class="block widget mgs-portfolio">
				<div class="block-content">
					<?php $col = $helper->convertPerRowtoCol($perRow); ?>
					<ul class="row <?php echo $helper->convertColClass($col, 'row') ?>">
						<?php $i=0; foreach ($portfolios as $portfolio): $i++; ?>
							<li class="item col-md-<?php echo $col ?> <?php if($i % $perRow == 1): ?>first-row-item<?php endif ?> <?php if($i % 2 == 1): ?>first-sm-item<?php endif ?> col-sm-6 col-12">
								<div class="portfolio-item">
									<div class="portfolio-top-content">
										<?php if ($showThumbnail): ?>
											<a href="<?php echo $portfolio->getAddress(); ?>"><img src="<?php echo $this->getThumbnailSrc($portfolio)?>" alt="<?php echo $portfolio->getName() ?>" class="img-fluid"/></a>
										<?php endif ?>
									</div>
									<div class="portfolio-bottom-content">
										<h4><a href="<?php echo $portfolio->getAddress(); ?>"><?php echo $portfolio->getName() ?></a></h4>
										
										<?php if($showCategories): ?>
											<p class="category-link"><?php echo $this->getCategoriesLink($portfolio)?></p>
										<?php endif ?>
										
										<?php if ($showShowContent): ?>
											<?php $length = $this->getCharacterCount() ?>
											<div class="short-content">
												<?php if($length): ?>
													<?php echo $this->truncate($portfolio->getDescription(), $length); ?>
												<?php else: ?>
													<?php echo $portfolio->getDescription() ?>
												<?php endif ?>
											</div>
										<?php endif ?>
									</div>
								</div>
							</li>
						<?php endforeach ?>
					</ul>
				</div>
			</div>
		<?php endif ?>

	<?php else: ?>
		<p class="note-msg"><?php echo $this->__('There are no portfolios matching the selection.') ?></p>
	<?php endif ?>
</div>