<?php
/**
 * @package     Joomla.Site
 * @subpackage  mod_articles_category
 *
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Layout\LayoutHelper;

require_once(__DIR__.'/../../helper.php');
$colOwl = $params->get('num_col', 4);
$moduleclass_sfx = $params->get('moduleclass_sfx','');

if(version_compare(JVERSION, '4', 'ge')) {
	$moduleclass_sfx = '';
}

?>
<div id="category-<?php echo $module->id; ?>" class="category-module<?php echo $moduleclass_sfx; ?> mod-owl">
	<div class="owl-carousel owl-theme">
		<?php if ($grouped) : ?>
			<?php foreach ($list as $group_name => $group) : ?>
			<li>
				<div class="mod-articles-category-group"><?php echo Text::_($group_name); ?></div>
				<ul>
					<?php foreach ($group as $item) : ?>
						<li>
							<?php if ($params->get('link_titles') == 1) : ?>
								<a class="mod-articles-category-title <?php echo $item->active; ?>" href="<?php echo $item->link; ?>">
									<?php echo $item->title; ?>
								</a>
							<?php else : ?>
								<?php echo $item->title; ?>
							<?php endif; ?>

							<?php if ($item->displayHits) : ?>
								<span class="mod-articles-category-hits">
									(<?php echo $item->displayHits; ?>)
								</span>
							<?php endif; ?>

							<?php if ($params->get('show_author')) : ?>
								<span class="mod-articles-category-writtenby">
									<?php echo $item->displayAuthorName; ?>
								</span>
							<?php endif; ?>

							<?php if ($item->displayCategoryTitle) : ?>
								<span class="mod-articles-category-category">
									(<?php echo $item->displayCategoryTitle; ?>)
								</span>
							<?php endif; ?>

							<?php if ($item->displayDate) : ?>
								<span class="mod-articles-category-date"><?php echo $item->displayDate; ?></span>
							<?php endif; ?>

							<?php if ($params->get('show_tags', 0) && $item->tags->itemTags) : ?>
								<div class="mod-articles-category-tags">
									<?php echo LayoutHelper::render('joomla.content.tags', $item->tags->itemTags); ?>
								</div>
							<?php endif; ?>

							<?php if ($params->get('show_introtext')) : ?>
								<p class="mod-articles-category-introtext">
									<?php echo $item->displayIntrotext; ?>
								</p>
							<?php endif; ?>

							<?php if ($params->get('show_readmore')) : ?>
								<p class="mod-articles-category-readmore">
									<a class="mod-articles-category-title <?php echo $item->active; ?>" href="<?php echo $item->link; ?>">
										<?php if ($item->params->get('access-view') == false) : ?>
											<?php echo Text::_('MOD_ARTICLES_CATEGORY_REGISTER_TO_READ_MORE'); ?>
										<?php elseif ($readmore = $item->alternative_readmore) : ?>
											<?php echo $readmore; ?>
											<?php echo HTMLHelper::_('string.truncate', $item->title, $params->get('readmore_limit')); ?>
												<?php if ($params->get('show_readmore_title', 0) != 0) : ?>
													<?php echo HTMLHelper::_('string.truncate', $item->title, $params->get('readmore_limit')); ?>
												<?php endif; ?>
										<?php elseif ($params->get('show_readmore_title', 0) == 0) : ?>
											<?php echo Text::sprintf('MOD_ARTICLES_CATEGORY_READ_MORE_TITLE'); ?>
										<?php else : ?>
											<?php echo Text::_('MOD_ARTICLES_CATEGORY_READ_MORE'); ?>
											<?php echo HTMLHelper::_('string.truncate', $item->title, $params->get('readmore_limit')); ?>
										<?php endif; ?>
									</a>
								</p>
							<?php endif; ?>
						</li>
					<?php endforeach; ?>
				</ul>
			</li>
			<?php endforeach; ?>
		<?php else : ?>
			<?php foreach ($list as $item) : ?>
				<?php 
					$customs = JATemplateHelper::getCustomFields2($item, 'article');
					$images = json_decode($item->images);
				?>
				<div class="item-inner">
					<div class="info-top">
						<div class="intro-image">
							<?php if($images->image_intro): ?>
								<?php echo LayoutHelper::render('joomla.content.intro_image', $item); ?>
							<?php else :?>
								<span>No Image</span>
							<?php endif ;?>

							<?php if(!empty($customs['rent-or-sale'])) :?>
								<span class="tag-label label-<?php echo $customs['rent-or-sale']->value ;?>"><?php echo Text::_('TPL_FOR').' '.$customs['rent-or-sale']->value ;?></span>
							<?php endif ;?>

							<span class="featured-label featured-label-<?php if($item->featured): echo 'yes'; else: echo 'no'; endif; ?>">
								<i class="icon ion-ios-heart"></i>
							</span>
						</div>

						<div class="info-detail">
							<?php if ($params->get('link_titles') == 1) : ?>
								<a class="mod-articles-category-title <?php echo $item->active; ?>" href="<?php echo $item->link; ?>"><?php echo $item->title; ?></a>
							<?php else : ?>
								<?php echo $item->title; ?>
							<?php endif; ?>

							<div class="meta">
								<?php if ($item->displayHits) : ?>
									<span class="mod-articles-category-hits">
										<?php echo $item->displayHits; ?>
									</span>
								<?php endif; ?>

								<?php if ($item->displayCategoryTitle) : ?>
									<span class="mod-articles-category-category">
										<?php echo $item->displayCategoryTitle; ?>
									</span>
								<?php endif; ?>

								<?php if ($item->displayDate) : ?>
									<span class="mod-articles-category-date">
										<?php echo $item->displayDate; ?>
									</span>
								<?php endif; ?>
							</div>

							<?php if(!empty($customs['address'])) :?>
								<div class="ct-field field-address">
									<span class="icon ion-ios-pin"></span>
									<?php echo $customs['address']->value ;?>		
								</div>
							<?php endif ;?>

							<?php if ($params->get('show_introtext')) : ?>
								<p class="mod-articles-category-introtext">
									<?php echo $item->displayIntrotext; ?>
								</p>
							<?php endif; ?>

							<?php if ($params->get('show_tags', 0) && $item->tags->itemTags) : ?>
								<div class="mod-articles-category-tags">
									<?php echo LayoutHelper::render('joomla.content.tags', $item->tags->itemTags); ?>
								</div>
							<?php endif; ?>

							<?php if ($params->get('show_readmore')) : ?>
								<p class="mod-articles-category-readmore">
									<a class="<?php echo $item->active; ?>" href="<?php echo $item->link; ?>">
										<?php if ($item->params->get('access-view') == false) : ?>
											<?php echo Text::_('MOD_ARTICLES_CATEGORY_REGISTER_TO_READ_MORE'); ?>
										<?php elseif ($readmore = $item->alternative_readmore) : ?>
											<?php echo $readmore; ?>
											<?php echo HTMLHelper::_('string.truncate', $item->title, $params->get('readmore_limit')); ?>
										<?php elseif ($params->get('show_readmore_title', 0) == 0) : ?>
											<?php echo Text::sprintf('MOD_ARTICLES_CATEGORY_READ_MORE_TITLE'); ?>
										<?php else : ?>
											<?php echo Text::_('MOD_ARTICLES_CATEGORY_READ_MORE'); ?>
											<?php echo HTMLHelper::_('string.truncate', $item->title, $params->get('readmore_limit')); ?>
										<?php endif; ?>
									</a>
								</p>
							<?php endif; ?>
						</div>
					</div>

					<div class="ct-field-footer">
						<?php if((!empty($customs['area'])) || !empty($customs['bathrooms']) || !empty($customs['bedrooms'])) :?>
						<div class="ct-field-row">
							<!-- Field Area -->
							<?php if(!empty($customs['area'])) :?>
							<div class="ct-field field-area">
								<div class="ct-field-label">
									<?php echo Text::_('TPL_AREA') ;?>
								</div>

								<div class="ct-field-value">
									<?php echo $customs['area']->value.' '.$customs['value-of-area']->value ;?>
								</div>
							</div>
							<?php endif ;?>
							<!-- // Field Area -->

							<!-- Field Bathrooms -->
							<?php if(!empty($customs['bathrooms'])) :?>
							<div class="ct-field field-bathrooms">
								<div class="ct-field-label">
									<?php echo Text::_('TPL_BATHROOMS') ;?>
								</div>

								<div class="ct-field-value">
									<?php echo $customs['bathrooms']->value ;?>
								</div>
							</div>
							<?php endif ;?>
							<!-- // Field Bathrooms -->

							<!-- Field Bedrooms -->
							<?php if(!empty($customs['bedrooms'])) :?>
							<div class="ct-field field-bedrooms">
								<div class="ct-field-label">
									<?php echo Text::_('TPL_BEDROOMS') ;?>
								</div>

								<div class="ct-field-value">
									<?php echo $customs['bedrooms']->value ;?>
								</div>
							</div>
							<?php endif ;?>
							<!-- // Field Bedrooms -->
						</div>
						<?php endif ;?>

						<div class="ct-field-row">
							<!-- Field Bedrooms -->
							<?php if(!empty($customs['price'])) :?>
							<div class="ct-field field-price">
								<div class="ct-field-value">
									<?php echo $customs['currency']->value.''.$customs['price']->value ;?> / 
								</div>
								<div class="value-type"><?php echo $customs['value-of-price']->value ;?></div>
							</div>
							<?php endif ;?>
							<!-- // Field Bedrooms -->

							<?php if ($params->get('show_author')) : ?>
								<div class="ct-field mod-articles-category-writtenby">
									<?php echo LayoutHelper::render('joomla.content.info_block.author_property', array('item' => $item, 'params' => $item->params)); ?> 
								</div>
							<?php endif; ?>
						</div>
					</div>
				</div>
			<?php endforeach; ?>
		<?php endif; ?>
	</div>
</div>


<script>
(function($){
  jQuery(document).ready(function($) {
    $("#category-<?php echo $module->id; ?> .owl-carousel").owlCarousel({
      addClassActive: true,
      items: <?php echo $colOwl ;?>,
      margin: 36,
      responsive : {
      	0 : {
      		items: 1,
      	},

      	768 : {
      		items: 2,
      	},

      	979 : {
      		items: 2,
      	},

      	1200 : {
      		items: 2,
      	},

      	1400 : {
      		items: 3,
      	},

      	1920 : {
      		items: <?php echo $colOwl ;?>,
      	}
      },
      loop: false,
      nav : false,
      dots: true,
      autoplay: false,
      lazyLoad: true
    });
  });
})(jQuery);
</script>