<?php
/**
 * Blog Section
 * 
 * @package Wellness_Coach_Lite
 */

$sec_title        = get_theme_mod( 'blog_section_title', __( 'From The Blog', 'wellness-coach-lite' ) );
$sec_sub_title    = get_theme_mod( 'blog_section_subtitle' );
$readmore         = get_theme_mod( 'blog_readmore', __( 'Read More', 'wellness-coach-lite' ) );
$blog             = get_option( 'page_for_posts' );
$blog_view_all    = get_theme_mod( 'blog_view_all', __( 'View All Posts', 'wellness-coach-lite' ) );
$ed_crop_blog     = get_theme_mod( 'ed_crop_blog', false );
$no_of_blog_posts = get_theme_mod( 'no_of_blog_posts', 3 );

$args = array(
    'post_type'           => 'post',
    'post_status'         => 'publish',
    'posts_per_page'      => $no_of_blog_posts,
    'ignore_sticky_posts' => true
);

$qry = new WP_Query( $args );

if( $sec_title || $sec_sub_title || $qry->have_posts() ){ ?>

<section id="blog_section" class="blog-section">
    <?php if( $sec_title || $sec_sub_title ){ ?>
        <header class="section-header">	
            <div class="container">
                <?php 
                    if( $sec_title ) echo '<h2 class="section-title">' . esc_html( $sec_title ) . '</h2>';
                    if( $sec_sub_title ) echo '<div class="section-desc">' . wp_kses_post( wpautop( $sec_sub_title ) ) . '</div>'; 
                ?>
            </div>
		</header>
    <?php } ?>
    
    <div class="blog-sec__content-wrapper">
        <div class="container">
            <div class="section-grid">
                <?php if( $qry->have_posts() ){ ?>
                    <div class="grid-item grid__main-content">
                        <div class="blog-sec__inner-wrapper">
                    		<?php while( $qry->have_posts() ){
                                $qry->the_post(); ?>
                                <article class="post horizontal">
                    				<figure class="post-thumbnail">
                                        <a href="<?php the_permalink(); ?>">
                                        <?php 
                                            if( has_post_thumbnail() ){
                                                if( $ed_crop_blog ){
                                                    the_post_thumbnail( 'full', array( 'itemprop' => 'image' ) );
                                                }else{
                                                    the_post_thumbnail( 'wellness-coach-lite-blog-section', array( 'itemprop' => 'image' ) );
                                                }
                                            }else{ 
                                                wellness_coach_lite_get_fallback_svg( 'wellness-coach-lite-blog-section' );//fallback
                                            }                            
                                        ?>                        
                                        </a>
                                    </figure>
                    				<div class="content-wrap">
                    					<header class="entry-header">
                                            <div class="entry-meta">
                                                <?php wellness_coach_lite_category(); ?>
                                            </div>
                                            <h3 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                            <div class="entry-meta">
                    						  <?php wellness_coach_lite_posted_on(); ?>
                                            </div>
                    					</header>
                    					<div class="entry-content">
                    						<?php the_excerpt(); ?>
                    					</div>     
                                        <div class="button-wrap">
                                            <a href="<?php the_permalink(); ?>" class="btn-link"><?php echo esc_html( $readmore ); ?></a>
                                        </div>   					
                    				</div>
                    			</article>			
                    			<?php 
                            } ?>
                        </div>
            		
                        <?php if( $blog && $blog_view_all ){ ?>
                            <div class="section__button-wrapper">
                    			<a href="<?php the_permalink( $blog ); ?>" class="wc-btn wc-btn-two"><?php echo esc_html( $blog_view_all ); ?></a>
                    		</div>
                        <?php } ?>
                    </div>
                <?php } wp_reset_postdata();

                if( is_active_sidebar( 'blog_section' ) ){ ?>
                    <div class="grid-item grid__side-content">
                        <div class="blog-sec__widget-area">
                            <?php dynamic_sidebar( 'blog_section' ); ?>
                        </div>
                    </div>
                <?php } ?>
            </div>        
        </div>        
    </div>        
</section>
<?php 
}