<?php
/**
 * Template part for displaying posts
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Glowify WP
 */

if (get_post_meta($post->ID, 'glowify_wp_blog_options', true)) {
    $single_blog_meta = get_post_meta($post->ID, 'glowify_wp_blog_options', true);
} else {
    $single_blog_meta = array();
}

if (array_key_exists('enable_featured_image', $single_blog_meta)) {
    $enable_featured_image = $single_blog_meta['enable_featured_image'];
} else {
    $enable_featured_image = true;
}

if (array_key_exists('featured_content', $single_blog_meta)) {
    $featured_content = $single_blog_meta['featured_content'];
} else {
    $featured_content = false;
}

if (array_key_exists('featured_image_upload', $single_blog_meta)) {
    $featured_image_upload = $single_blog_meta['featured_image_upload'];
} else {
    $featured_image_upload = '';
}

if ($featured_image_upload) {
    $image_logo_src = wp_get_attachment_image_src($featured_image_upload['id'], 'full', false);
}

$show_author_name = glowify_wp_option('single_show_author_name');
$show_post_date = glowify_wp_option('single_show_post_date');
$show_post_comment = glowify_wp_option('single_show_post_comment');

$enable_social_share = glowify_wp_option('enable_social_share') ?? true; 
$social_title  = glowify_wp_option('social_title') ?? '';         
$social_list = glowify_wp_option('social_list') ?? [];


?>

<!-- Single News -->
<div class="cs_blog_details">


    <div class="box-content-detail-blog p-0">
        
        <div class="cs_post_meta">
            <span>
                <i class="fa-solid fa-clock"></i> <?php echo get_the_date(); ?>                    
            </span>
            <span>
                <i class="fa-solid fa-circle-user"></i> <?php the_author(); ?>                    
            </span>
            <a href="<?php comments_link(); ?>">
                <i class="fa-solid fa-comment-dots"></i> <?php comments_number('0 Comments', '1 Comment', '% Comments'); ?>                   
            </a>
        </div>

        <h1><?php the_title(); ?></h1>

        <div class="box-image-header">
            <?php if (has_post_thumbnail() && $enable_featured_image == true && empty($image_logo_src)) : ?>
                <?php the_post_thumbnail('glowify-blog-single-thumb'); ?>
            <?php else : ?>
                <?php if (!empty($image_logo_src)) : ?>
                    <img src="<?php echo esc_url($image_logo_src[0]); ?>" alt="<?php esc_attr_e('Featured Image', 'glowify-wp'); ?>">
                <?php endif; ?>
            <?php endif; ?>
        </div>
        <div class="box-detail-info">
            <?php the_content(); ?>
        </div>
    </div>
    <?php wp_link_pages(array(
        'before' => '<div class="page-links">' . esc_html__('Pages:', 'glowify-wp'),
        'after'  => '</div>',
    )); ?>
</div>

<div class="cs_height_80 cs_height_lg_60"></div>

<div class="cs_post_share_wrap">
    <div class="cs_tagcloud">
        <a href="#" class="tag-cloud-link">Summer Skincare</a>
        <a href="#" class="tag-cloud-link">Sunscreen Tips</a>
        <a href="#" class="tag-cloud-link">Skin Type Solutions</a>
        <a href="#" class="tag-cloud-link">Skin Type Solutions</a>
        <a href="#" class="tag-cloud-link">Seasonal Beauty</a>
    </div>
    <?php if ($enable_social_share == true && !empty($social_list) ): ?>
    <div class="cs_social_share">
        <?php 
            // Loop through the social list
            foreach ($social_list as $social_item) {
                // Set default values for URL and target if not provided
                $icon = isset($social_item['icon']) && is_string($social_item['icon']) ? $social_item['icon'] : '';
                
                // Use the default values if 'link' is not set
                $link = isset($social_item['link']) ? $social_item['link'] : array(
                    'url' => '#', 
                    'target' => '_blank'
                );
                
                // Extract URL and target from the link array
                $url = isset($link['url']) && !empty($link['url']) ? $link['url'] : '#'; // Fallback to '#' if URL is empty
                $target = isset($link['target']) && $link['target'] === '_blank' ? '_blank' : '_self'; // Fallback to '_self' if target is not '_blank'

                // Render the social link if the icon exists
                if (!empty($icon)) {
                    echo '<a href="' . esc_url($url) . '" target="' . esc_attr($target) . '" rel="noopener noreferrer">';
                    echo '<i class="' . esc_attr($icon) . '"></i>';
                    echo '</a>';
                }
            }
        ?>
    </div>
    <?php endif; ?>
</div>

<div class="cs_height_80 cs_height_lg_60"></div>

<div class="cs_post_share">
    <div class="cs_categories_wrap">
        <div class="cs_categories">
            <?php
            $tags = get_the_tags();
            if ($tags) {
                foreach ($tags as $tag) {
                    echo '<a href="' . esc_url(get_tag_link($tag->term_id)) . '" class="cs_category">' . esc_html($tag->name) . '</a>';
                }
            }
            ?>
        </div>
    </div>
</div>
