<?php
/**
 * Template Part for displaying banner section in homepage
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package NDesign
 */

if ( get_theme_mod( 'blog_title' ) || get_theme_mod( 'blog_sub_title' ) || get_theme_mod( 'number_of_blog' ) ) : ?>
	<section class="blog-section grey-section page-section" id="blog">
		<div class="container">
			<div class="row">
				<div class="col-12">
					<div class="main-title-blk">
						<!-- Subtitle start -->
						<?php if ( get_theme_mod( 'blog_sub_title' ) ) { ?>
							<h4 class="main-subtitle text-center">
								<?php echo esc_html( get_theme_mod( 'blog_sub_title' ) ); ?>
							</h4>
						<?php } ?>
						<!-- Subtitle end -->
						<!-- Title Start -->
						<?php if ( get_theme_mod( 'blog_title' ) ) { ?>
							<h2 class="main-title text-center">
								<?php echo esc_html( get_theme_mod( 'blog_title' ) ); ?>
							</h2>
						<?php } ?>
					</div>
				</div>
			</div>
			<div class="row blog-wrap justify-content-lg-center">
				<?php
				$ndesign_args = array(
					'numberposts' => get_theme_mod( 'number_of_blog', true ),
					'orderby' => 'post_date',
					'order' => 'DESC',
					'post_type' => 'post',
					'post_status' => 'publish',
				);
				$ndesign_i = 0;
				$ndesign_recent_posts = wp_get_recent_posts( $ndesign_args );
				foreach ( $ndesign_recent_posts as $ndesign_key => $ndesign_val ) {
					$ndesign_i++;
					?>
					<div class="col-lg-4 col-md-6 mb-3">
						<!-- Blog image start -->
						<div class="blog-image">
							<img src="
							<?php
							if ( get_the_post_thumbnail_url( $ndesign_val['ID'] ) ) {
								echo esc_url(get_the_post_thumbnail_url( $ndesign_val['ID'] ));
							} else {
								echo esc_url( get_theme_mod( 'blog_dummy_image' ) );
							}
							?>
							" class="img-fluid">
						</div>
						<!-- Blog image end -->
						<!-- Blog detail start -->
						<div class="blog-detail">
							<!-- Blog date start -->
							<div class="blog-date">
								<?php
									setup_postdata( $ndesign_val );
									$ndesign_date = $ndesign_val['post_date'];
									$ndesign_author = get_the_author_meta( 'display_name', $ndesign_val['post_author'] );
									echo ( get_theme_mod( 'display_date', true ) ) ? date( 'M d, Y', strtotime( $ndesign_date ) ) : '';
									echo ( get_theme_mod( 'display_date', true ) && get_theme_mod( 'display_author', true ) ) ? ' / ' : '';
									echo ( get_theme_mod( 'display_author', true ) ) ? $ndesign_author : '';
								?>
							</div>
							<!-- Blog date end -->
							<!-- Blog Title Start -->
							<h2 class="blog-title">
								<?php echo get_the_title( $ndesign_val['ID'] ); ?>
							</h2>
							<!-- Blog Title end -->
							<!-- Blog Read More Button Start -->
							<?php if ( get_theme_mod( 'read_more_button', true ) ) { ?>
								<a href="<?php echo esc_url(get_the_permalink( $ndesign_val['ID'] )); ?>" class="btn btn-link">
									<?php echo __('Read More','ndesign'); ?><i class="fa fa-angle-right ml-2"></i>
								</a>
							<?php } ?>
							<!-- Blog Read More Button end -->
						</div>
						<!-- Blog detail end -->
					</div>
				<?php } ?>
			</div>
			<?php
			if ( 0 != $ndesign_i ) {
				?>
				<div class="row">
					<div class="col-12 text-center">
						<a href="<?php echo esc_url(get_permalink( get_option( 'page_for_posts' ) )); ?>" class="btn btn-primary" id="blogViewAll">
							<?php echo esc_html( get_theme_mod( 'blog_button_text' ) != '' ? get_theme_mod( 'blog_button_text' ) : 'View All' ); ?>
						</a>
					</div>
				</div>
			<?php } ?>
		</div>
	</section>
<?php endif ?>
<!-- Blog Section End -->
