<?php

// Prevent Direct Access
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$show_author = get_theme_mod('blog_list_show_author', true);
$show_date = get_theme_mod('blog_list_show_date', true);
$show_categories = get_theme_mod('blog_list_show_categories', true);
$show_tags = get_theme_mod('blog_list_show_tags', true);

$background = get_theme_mod('blog_top_background');
get_header();
?>

<section id="page_header" class="page-header padding_top" <?php if($background){ echo 'style="background-image:url('.wp_get_attachment_url($background).');"'; } ?>>
	<div class="container header_content padding_top">
		<div class="row">
			<div class="col-sm-12">
			
				<?php
				$blog_link = get_permalink( get_option('page_for_posts') );
				$blog_tag = '<a href="'.$blog_link.'">'.esc_html__('Blog', 'maxo-fa').'</a> / ';

				if( is_category() ){
					$title = sprintf( esc_html__('"%s" Category Archive', 'maxo-fa'), single_cat_title('', false) );
				}
				else if( is_tag() ){
					$title = sprintf( esc_html__('"%s" Tag Archive', 'maxo-fa'), single_tag_title('', false) );
				}
				else if( is_author() ){
					$title = sprintf( esc_html__('"%s" Author Archive', 'maxo-fa'), get_the_author_meta( 'display_name', $wp_query->query_vars['author'] ) );
				}
				else if( is_day() ){
					$title = sprintf( esc_html__('"%s" Day Archive', 'maxo-fa'), get_the_date('j F Y') );
				}
				else if( is_month() ){
					$title = sprintf( esc_html__('"%s" Month Archive', 'maxo-fa'), get_the_date('F Y') );
				}
				else if( is_year() ){
					$title = sprintf( esc_html__('"%s" Year Archive', 'maxo-fa'), get_the_date('Y') );
				}
				else if( is_search() ){
					$title = sprintf( esc_html__('Search Results For "%s"', 'maxo-fa'), get_search_query() );
				}
				else if( is_archive() ){
					$title = esc_html__('Archive', 'maxo-fa');
				}
				else{
					$blog_tag = '';
					$title = esc_html__('Blog', 'maxo-fa');
				}

				echo '<h1 class="whitecolor bottom25">'.$blog_tag.$title.'</h1>';
				?>
				
			</div>
		</div>
	</div>
</section>

<section id="ourblog" class=" padding_bottom padding_top_half">
	<div class="container">
		<div class="row">

			<?php
			if( is_active_sidebar('sidebar') ){
				$content_class = 'col-md-8 col-sm-8';
				$img_size = 'blog';
			}
			else{
				$content_class = 'col-xs-12';
				$img_size = 'blog_wide';
			}
			?>
		
			<div class="<?php echo $content_class ?>">
				
				<?php if( have_posts() ){ ?>
				<div class="row">
				
					<?php while( have_posts() ){ the_post(); ?>

					<div class="col-sm-12 padding_top_half blog_item">
					
						<?php if( has_post_thumbnail() ){ ?>
						<div class="image">
							<a href="<?php the_permalink() ?>">
								<?php the_post_thumbnail($img_size) ?>
							</a>
						</div>
						<?php } ?>
						
						<div class="row">

							<?php
							$side_visible = false;
							$content_class = 'col-xs-12';

							if( $show_author || $show_date || ($show_categories && has_category()) || ($show_tags && has_tag()) ){
								$side_visible = true;
								$content_class = 'col-md-8 col-sm-7';
							}
							?>
						
							<div class="<?php echo $content_class ?>">
								<h3 class="darkcolor top30 bottom20"><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h3>
								<?php the_excerpt() ?>
							</div>
							
							<?php if( $side_visible ){ ?>
							<div class="col-md-4 col-sm-5">
								<div class="blog_author borderleft top30">

									<?php if( $show_author ){ ?>
									<span class="photo_container"><?php echo get_avatar( get_the_author_meta('ID'), 36 ); ?></span>
									<div class="authorname"><a href="<?php echo get_author_posts_url( get_the_author_meta('ID') ) ?>"><?php the_author() ?></a></div>
									<?php } ?>
								
									<?php if( $show_date ){ ?>
									<p class="metas"> <i class="fa fa-calendar"></i> <span><?php the_time('j F Y') ?></span> </p>
									<?php } ?>

									<?php if( $show_categories && has_category() ){ ?>
									<p class="metas"> <i class="fa fa-folder-open"></i> <span><?php the_category(' '.esc_html_x(',', 'separator', 'maxo-fa').' ') ?></span> </p>
									<?php } ?>
									
									<?php if( $show_tags && has_tag() ){ ?>
									<p class="metas"> <i class="fa fa-tags"></i> <span><?php the_tags('', ' '.esc_html_x(',', 'separator', 'maxo-fa').' ', '') ?></span> </p>
									<?php } ?>
									
								</div>
							</div>
							<?php } ?>
							
						</div>
						
					</div>

					<?php } ?>
					
				</div>
				<?php
				}
				else{
					if( is_search() ){
						esc_html_e('No Results Found', 'maxo-fa');
					}
					else{
						esc_html_e("There's Nothing to Show", 'maxo-fa');
					}
				}
				?>
				
				<?php
				$pages = paginate_links(array(
					'type' => 'array',
					'next_text' => '<i class="fa fa-angle-'.(is_rtl() ? 'left' : 'right').'"></i>',
					'prev_text' => '<i class="fa fa-angle-'.(is_rtl() ? 'right' : 'left').'"></i>'
				));

				if( $pages ){
				?>
				<ul class="pager padding_top_half">
					<?php
					foreach($pages as $page){
						echo '<li>'.$page.'</li>';
					}
					?>
				</ul>
				<?php } ?>
				
			</div>
			
			<?php get_sidebar(); ?>
			
		</div>
	</div>
</section>

<?php get_footer(); ?>