<?php

use Tangibledesign\MyHome\Widgets\General\PostsSidebarWidget;

/* @var PostsSidebarWidget $mhCurrentWidget */
global $mhCurrentWidget;

$mhPosts = $mhCurrentWidget->getPosts();
if ($mhPosts->isEmpty()) {
    return;
}
?>
<div class="myhome-sidebar-widget">
    <h3 class="myhome-sidebar-widget__label">
        <?php echo esc_html($mhCurrentWidget->getLabel()); ?>
    </h3>

    <div class="myhome-sidebar-widget__content">
        <div class="myhome-sidebar-posts">
            <?php
            global $post;
            foreach ($mhPosts as $mhPost) :
                $post = $mhPost->getPost();
                setup_postdata($post); ?>
                <div class="myhome-sidebar-posts__item">
                    <a
                            class="myhome-sidebar-posts__image"
                            href="<?php echo esc_url(get_the_permalink()); ?>"
                            title="<?php the_title_attribute(); ?>"
                    >
                        <img
                                class="lazyload"
                                src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAQAAAA3fa6RAAAADklEQVR42mNkAANGCAUAACMAA2w/AMgAAAAASUVORK5CYII="
                                data-src="<?php echo esc_url(apply_filters('myhome/wp/widgets/posts/image', get_the_post_thumbnail_url(null, 'myhome_360_240'), get_the_ID())); ?>"
                                alt="<?php the_title_attribute(); ?>"
                                width="120"
                                height="80"
                        >
                    </a>

                    <div class="myhome-sidebar-posts__content">
                        <a
                                class="myhome-sidebar-posts__label"
                                href="<?php echo esc_url(get_the_permalink()); ?>"
                                title="<?php the_title_attribute(); ?>"
                        >
                            <?php the_title(); ?>
                        </a>

                        <?php if (!$mhCurrentWidget->hidePublishDate()) : ?>
                            <div class="myhome-sidebar-posts__date">
                                <div class="myhome-sidebar-posts__icon">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-calendar">
                                        <rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect>
                                        <line x1="16" y1="2" x2="16" y2="6"></line>
                                        <line x1="8" y1="2" x2="8" y2="6"></line>
                                        <line x1="3" y1="10" x2="21" y2="10"></line>
                                    </svg>
                                </div>

                                <?php echo esc_html(get_the_date()); ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            <?php
            endforeach;
            wp_reset_postdata();
            ?>
        </div>
    </div>
</div>
