<?php
/**
 * Blog sidebar posts list template
 *
 * @var $block AbstractList
 */

use Magefan\Blog\Block\Post\PostList\AbstractList;
use Magefan\Blog\Helper\Image;

?>

<?php
    $_postCollection = $block->getPostCollection();
?>
<?php
	$athleteHelper = $this->helper('Olegnax\Athlete2\Helper\Helper')->getConfig('athlete2_settings/blog');
	$showImage = $athleteHelper['blog_sidebar_recent_image'];
	if($showImage){
		$width = (int)($athleteHelper['blog_sidebar_recent_image_width'] ?: 300);
		$height = (int)($athleteHelper['blog_sidebar_recent_image_height'] ?: 200);
	}
    $imageHelper = $this->helper( Image::class);
?>
<?php if ($_postCollection->count()) { ?>
<div class="widget block block-<?= $block->escapeHtml(str_replace('_', '-', $block->getWidgetKey())) ?> block-list-posts" >
    <div class="block-title">
        <strong><?= $block->escapeHtml(__($block->getBlockTitle() ?: 'Recent Posts')) ?></strong>
    </div>
    <div class="block-content">
        <?php foreach ($_postCollection as $_post) { ?>
            <?php
                $_postUrl = $block->escapeUrl($_post->getPostUrl());
                $_postName = $block->escapeHtml($_post->getTitle());
				$featuredImage = $_post->getFeaturedListImage() ?: $_post->getFeaturedImage();
            ?>
            <div class="item clearfix <?php if ($showImage) { echo 'with-image'; } ?>">
                <?php if ($featuredImage && $showImage) { ?>
                    <div class="post-image">
                        <?php
                        $featuredImgAlt = $_post->getData('featured_list_img_alt') ?: $_post->getData('featured_img_alt');
                        if (!$featuredImgAlt) {
                            $featuredImgAlt =  $_postName;
                        }
                        ?>
                        <a class="post-item-link" title="<?= $_postName ?>" href="<?= $_postUrl ?>">
                            <img data-width-amp="<?= $width ?>" data-height-amp="<?= $height ?>" layout="responsive" src="<?= $imageHelper->init($_post->getFeaturedListImg() ?: $_post->getFeaturedImg())->resize($width, $height); ?>"
                                 alt="<?= $block->escapeHtml($featuredImgAlt) ?>" />
                        </a>
                    </div>
                <?php } ?>
                <?php if ($featuredImage && $_post->isPublishDateEnabled()) { ?>
                    <div class="post-item-date">
                        <span class="value"><?= $block->escapeHtml($_post->getPublishDate()) ?></span>
                    </div>
                <?php } ?>
                <a class="post-item-link" title="<?= $_postName ?>" href="<?= $_postUrl ?>">
                    <?= $_postName ?>
                </a>
            </div>
        <?php } ?>
    </div>
</div>
<?php } ?>
