<?php
/**
 * Athlete2 Theme
 * Blog recent posts widget
 *
 * @var $block Recent
 *
 * @category    Olegnax
 * @package     Olegnax_Athlete2
 * @copyright   Copyright (c) 2023 Olegnax (http://www.olegnax.com/)
 * @license     https://www.olegnax.com/license
 */

use Magefan\Blog\Block\Widget\Recent;

$at_settings = $this->helper('Olegnax\Athlete2\Helper\Helper')->getConfig('athlete2_settings/blog');
	$_styleClassic = $at_settings['blog_style_classic'];
	$_hideReadMore = $at_settings['blog_list_read_more'];
	$_hideCommentsLink = $at_settings['blog_list_comments_link'];
	$_hideAuthor = $at_settings['blog_list_author'];
	$_hideDate = $at_settings['blog_list_date'];
	$_hideCategories = $at_settings['blog_list_categories'];
	$_dateOnImage = $at_settings['blog_list_date_image'];
	$gWidth = $at_settings['blog_list_image_width'];
	$gHeight = $at_settings['blog_list_image_height'];
	$iWidth = $at_settings['blog_widget_image_width'];
	$iHeight = $at_settings['blog_widget_image_height'];
	$columns = 	$at_settings['widget_columns'];
	$imgAttrs='';
	if(!$columns) {
		$columns= 3;
	}
	$_postClasses = '';
	if($_dateOnImage){
		$_postClasses = ' date-above-image';
	}
	$classes = [];
	$classes[] = 'blog-list--image-top';
	$classes[] = 'columns-' . $columns;
	if($at_settings['blog_style_classic']){
		$classes[] = 'blog-style-classic ';
	} else{
		$classes[] = 'blog-style-modern ';
	}
	$classesOutput = implode(' ',	$classes);
?>

<?php
    $_postCollection = $block->getPostCollection();
?>
<?php if ($_postCollection->count()) { ?>
<div class="post-list-wrapper widget blog-widget-recent">
	<?php $blockTitle = $block->escapeHtml($block->getData('title'));
	if ($blockTitle){ ?>
		<h3 class="title"><?= $blockTitle ?></h3>
	<?php } ?>
    <div class="post-list clearfix <?= $classesOutput ?>">
        <?php foreach ($_postCollection as $_post) { ?>
            <?php
                $_postUrl = $block->escapeUrl($_post->getPostUrl());
                $_postName = $block->escapeHtml($_post->getTitle(), null, true);
				$_categoriesCount = $_post->getCategoriesCount();
				$dateMonth = $block->escapeHtmlAttr($_post->getPublishDate('F'));
				if(strlen($dateMonth) > 4){
					$dateMonth = substr($dateMonth, 0, 3);
				}
				if($iWidth && $iHeight) {
					$imgAttrs .=' width="' . $iWidth . '" height="' . $iHeight . '" ';
					$featuredImage = $_post->getFeaturedImage([$iWidth, $iHeight]);
				} elseif ($gWidth && $gHeight) {
					$imgAttrs .=' width="' . $gWidth . '" height="' . $gHeight . '" ';
					$featuredImage = $_post->getFeaturedImage([$gWidth, $gHeight]);
				} else {
					$featuredImage = $_post->getFeaturedImage();
				}
				$noImage = '';
				if(!$featuredImage){
					$noImage = ' no-image';
				}
            ?>
            <article class="post-holder post-holder-<?php echo (int)$_post->getId() ?><?= $_postClasses ?><?= $noImage ?>">
				<?php if ($featuredImage) { ?>
					<a class="post-image-wrapper" href="<?php echo $_postUrl ?>" title="<?php echo $_postName ?>">
						<?php if($_styleClassic) { ?>
							<span class="post-date">
								<span class="inner"><span class="day"><?php echo $_post->getPublishDate('d' )?></span><?php echo $dateMonth ?></span>
								<span class="hover"><span class="day"><?php echo $_post->getPublishDate('d') ?></span><?php echo $dateMonth ?></span>
							</span>
						<?php } ?>
						<?php if(!$_hideDate && !$_styleClassic && $_dateOnImage) { ?>
							<span class="item post-posed-date"><?php echo $_post->getPublishDate('F d, Y') ?></span>
						<?php } ?>
						<img src="<?php echo $block->escapeUrl($featuredImage) ?>" alt="<?php echo $_postName ?>" <?php echo $block->escapeHtmlAttr($imgAttrs) ?>/>
					</a>
				<?php } ?>
				<?php if ($_styleClassic) { ?>
					<?php if (!$featuredImage) { ?>
						<div class="post-date">
							<div class="inner"><span class="day"><?php echo $_post->getPublishDate('d' )?></span><?php echo $dateMonth ?></div>
							<div class="hover"><span class="day"><?php echo $_post->getPublishDate('d') ?></span><?php echo $dateMonth ?></div>
						</div>
					<?php } ?>
					<div class="post-content">
						<h2 class="post-title">
							<a class="post-item-link" href="<?php echo $_postUrl ?>">
								<?php echo $_postName; ?>
							</a>
						</h2>
						<div class="post-meta links-bg-hover">
							<?php if ( !$_hideCategories ) { ?>
								<?php if ( $_categoriesCount ) { ?>
									<div class="item post-categories">
										<?php $n = 0; foreach($_post->getParentCategories() as $ct) { $n++; ?>
											<a title="<?php echo $block->escapeHtml($ct->getTitle()) ?>" href="<?php echo $block->escapeUrl($ct->getCategoryUrl()) ?>">
												<?php echo $block->escapeHtml($ct->getTitle()) ?>
											</a>
										<?php } ?>
									</div>
								<?php } ?>
							<?php } ?>
						</div>
						<?php if ($content = $block->getShorContent($_post)) { ?>
							<div class="post-description clearfix">
								<?php echo $content ?>
							</div>
						<?php } ?>
						<div class="post-links">
							<?php if (!$_hideReadMore) { ?>
								<a class="item button post-read-more" href="<?php echo $_postUrl ?>" title="<?php echo $_postName ?>">
									<span><?php echo __('Read more') ?></span>
								</a>
							<?php } ?>
						</div>
					</div>
				<?php } else { ?>
					<div class="post-content">
						<?php if ( $_categoriesCount ) { ?>
							<div class="item post-categories">
								<?php $n = 0; foreach($_post->getParentCategories() as $ct) { $n++; ?>
									<a title="<?php echo $block->escapeHtml($ct->getTitle()) ?>" href="<?php echo $block->escapeUrl($ct->getCategoryUrl()) ?>">
										<?php echo $block->escapeHtml($ct->getTitle()) ?>
									</a>
								<?php } ?>
							</div>
						<?php } ?>
						<h2 class="post-title">
							<a class="post-item-link" href="<?php echo $_postUrl ?>">
								<?php echo $_postName; ?>
							</a>
						</h2>
						<div class="post-meta">
							<?php if(!$_hideDate ) { ?>
								<span class="item post-posed-date"><?php echo $_post->getPublishDate('F d, Y') ?></span>
							<?php } ?>
						</div>
						<?php if ($content = $block->getShorContent($_post)) { ?>
							<div class="post-description clearfix">
								<?php echo $content ?>
							</div>
						<?php } ?>
						<div class="post-links">
							<?php if (!$_hideReadMore) { ?>
								<a class="item post-read-more" href="<?php echo $_postUrl ?>" title="<?php echo $_postName ?>">
								   <?php echo __('Read more') ?>
								</a>
							<?php } ?>
						</div>
					</div>
				<?php } ?>
            </article>

        <?php } ?>
    </div>
</div>
<?php } ?>
