<?php

/* @var Tangibledesign\MyHome\Widgets\BlogPost\BlogPostMetaWidget $mhCurrentWidget */
global $mhCurrentWidget;

$mhBlogPost = $mhCurrentWidget->getBlogPost();
if (!$mhBlogPost) {
    return;
}

$mhSettings = $mhCurrentWidget->get_settings_for_display();
$mhShowAuthor = !empty($mhSettings['show_author']);
$mhShowAvatar = !empty($mhSettings['show_avatar']);
$mhShowDate = !empty($mhSettings['show_date']);
$mhShowComments = !empty($mhSettings['show_comments']);
?>
<div class="myhome-blog-meta-widget">
    <?php if ($mhShowAuthor) : 
        $mhUser = $mhBlogPost->getUser();
        if ($mhUser) : 
            $mhUserName = $mhUser->getDisplayName();
            $mhInitials = '';
            if ($mhShowAvatar) {
                $mhNameParts = explode(' ', trim($mhUserName));
                if (count($mhNameParts) >= 2) {
                    $mhInitials = strtoupper(substr($mhNameParts[0], 0, 1) . substr($mhNameParts[1], 0, 1));
                } else {
                    $mhInitials = strtoupper(substr($mhUserName, 0, 2));
                }
            }
            ?>
            <div class="myhome-blog-meta-widget__item myhome-blog-meta-widget__author">
                <?php if ($mhShowAvatar) : ?>
                    <div class="myhome-blog-meta-widget__avatar">
                        <?php echo esc_html($mhInitials); ?>
                    </div>
                <?php endif; ?>
                <?php echo esc_html($mhUserName); ?>
            </div>
        <?php endif; ?>
    <?php endif; ?>

    <?php if ($mhShowDate) : ?>
        <div class="myhome-blog-meta-widget__item myhome-blog-meta-widget__date">
            <div class="myhome-blog-meta-widget__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($mhBlogPost->getPublishDate()); ?>
        </div>
    <?php endif; ?>

    <?php if ($mhShowComments) : ?>
        <div class="myhome-blog-meta-widget__item myhome-blog-meta-widget__comments">
            <div class="myhome-blog-meta-widget__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-message-square">
                    <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path>
                </svg>
            </div>
            <?php echo esc_html($mhBlogPost->getCommentsText()); ?>
        </div>
    <?php endif; ?>
</div>
