<?php get_header(); ?>
	<div class="content inner archive">
			<div id="page" class="page-content l-background">
				<section class="l-hero-archive pt-lg-4">
					<div class="container">
						<a class="c-arrow-prev" href="<?php echo esc_url( home_url( '/blog-press' ) ); ?>"><i class="icon icon-left-open-big"></i>Back</a>
						<h1 class="c-title"><?php single_cat_title(); ?></h1>
						<p class="c-description"><?php  $description = category_description(); $description = strip_tags($description); echo $description; ?></p>
					</div>
				</section>
				<section class="l-featured-archive">
					<div class="container">
						<div class="row">
							<div class="col-md-6 col-lg-7">
							<?php
								$cur_cat = get_cat_ID( single_cat_title("",false) ); //get the cat id
								$category = &get_category($cur_cat);
								$category = $category->slug; //get cat slug

								$args = array(
									'post_type'      => 'post',
									'posts_per_page' => 1,
									'post_status' => 'publish',
									'category_name' => $category
								
								);
								$loop = new WP_Query( $args );


								if ( $loop->have_posts() ):
									?>
								<?php while ( $loop->have_posts() ) : $loop->the_post();?>
										<?php
											$post_id = get_the_ID();
											$imagefeatured = get_the_post_thumbnail_url($post_id,'featured-archive');
											$format = get_post_format( $post_id );
											if(!$format):
												$format = "article";
											endif;
										?>
								<div class="c-featured-news">
									<a href="<?php the_permalink(); ?>">
										<img src="<?php echo $imagefeatured; ?>" alt="<?php echo the_title(); ?>">
										<div class="c-card-news__type"><i class="icon icon-<?php echo $format; ?>"></i></div>
									</a>
								</div>
								<article class="c-card-category">
									<p class="c-card-category__meta"><?php echo get_the_date('M'); ?> <?php echo get_the_date('d'); ?>, <?php echo get_the_date('Y'); ?> · <?php echo prefix_estimated_reading_time( get_the_content() ); ?> min read</p>
									<a href="<?php the_permalink(); ?>">
										<h3 class="c-card-category__title"><?php echo the_title(); ?></h3>
									</a>
								</article>
								<?php
									endwhile; wp_reset_query();
									endif;
								?>

							</div>
							<div class="d-md-none d-lg-block col-lg-1">

							</div>
							<div class="col-md-6 col-lg-4">

							<?php

								$args = array(
									'post_type'      => 'post',
									'posts_per_page' => 1,
									'offset' => 1,
									'post_status' => 'publish',
									'category_name' => $category
								
								);
								$loop = new WP_Query( $args );


								if ( $loop->have_posts() ):
									?>
								<?php while ( $loop->have_posts() ) : $loop->the_post();?>
										<?php
											$post_id = get_the_ID();
											$imagefeatured = get_the_post_thumbnail_url($post_id,'related-archive');
											$format = get_post_format( $post_id );
											if(!$format):
												$format = "article";
											endif;
										?>
								<div class="c-featured-news">
									<a href="<?php the_permalink(); ?>">
										<img src="<?php echo $imagefeatured; ?>" alt="<?php echo the_title(); ?>">
										<div class="c-card-news__type"><i class="icon icon-<?php echo $format; ?>"></i></div>
									</a>
								</div>
								<article class="c-card-category">
									<p class="c-card-category__meta"><?php echo get_the_date('M'); ?> <?php echo get_the_date('d'); ?>, <?php echo get_the_date('Y'); ?> · <?php echo prefix_estimated_reading_time( get_the_content() ); ?> min read</p>
									<a href="<?php the_permalink(); ?>">
										<h3 class="c-card-category__title"><?php echo the_title(); ?></h3>
									</a>
								</article>
								<?php
									endwhile; wp_reset_query();
									endif;
								?>
							</div>
						</div>
					</div>
				</section>
				<section class="l-featured-author">
					<div class="container">
						<div class="row align-center">
							<div class="col-md-5">
							<?php

								$args = array(
									'post_type'      => 'post',
									'posts_per_page' => 1,
									'offset' => 2,
									'post_status' => 'publish',
									'category_name' => $category
								
								);
								$loop = new WP_Query( $args );


								if ( $loop->have_posts() ):
									?>
								<?php while ( $loop->have_posts() ) : $loop->the_post();?>
									<?php
										$author = get_field('author_news');
									?>
									<article class="c-card-featured">
										<p class="c-card-featured__meta"><?php echo get_the_date('M'); ?> <?php echo get_the_date('d'); ?>, <?php echo get_the_date('Y'); ?> · <?php echo prefix_estimated_reading_time( get_the_content() ); ?> min read</p>
										<a href="<?php the_permalink(); ?>">
											<h3 class="c-card-featured__title feature-title"><?php echo the_title(); ?></h3>
										</a>
										<div class="c-card-author">
											<p><?php echo $author["user_avatar"]; ?><?php echo $author["user_firstname"]; ?></p>
										</div>
									</article>
								<?php
									endwhile; wp_reset_query();
									endif;
								?>
							</div>
							<div class="col-md-7">
							<?php
								$args = array(
									'post_type'      => 'post',
									'posts_per_page' => 1,
									'offset' => 2,
									'post_status' => 'publish',
									'category_name' => $category

								);
								$loop = new WP_Query( $args );


								if ( $loop->have_posts() ):
									?>
								<?php while ( $loop->have_posts() ) : $loop->the_post();?>
										<?php
											$post_id = get_the_ID();
											$imagefeatured = get_the_post_thumbnail_url($post_id,'featured-archive');
											$format = get_post_format( $post_id );
											if(!$format):
												$format = "article";
											endif;
										?>
								<div class="c-featured-news">
									<a href="<?php the_permalink(); ?>">
										<img src="<?php echo $imagefeatured; ?>" alt="<?php echo the_title(); ?>">
										<div class="c-card-news__type"><i class="icon icon-<?php echo $format; ?>"></i></div>
									</a>
								</div>
							</div>
							<?php
								endwhile; wp_reset_query();
								endif;
							?>
						</div>
					</div>
				</section>
				<section class="l-archive-list">
					<div class="container">
						<div class="row my-posts">
							<?php
								$args = array(
									'post_type'      => 'post',
									'posts_per_page' => 6,
									'paged' => 1,
									'offset' => 3,
									'post_status' => 'publish',
									'category_name' => $category

								);
								$loop = new WP_Query( $args );
								$totalPost = $loop->found_posts;

								if ( $loop->have_posts() ):
									?>
								<?php while ( $loop->have_posts() ) : $loop->the_post();?>
									<?php
										$post_id = get_the_ID();
										$imagefeatured = get_the_post_thumbnail_url($post_id,'list-archive');
										$format = get_post_format( $post_id );
										if(!$format):
											$format = "article";
										endif;
									?>
									<div class="col-md-6 col-lg-4">
										<div class="c-featured-news">
											<a href="<?php the_permalink(); ?>">
												<img src="<?php echo $imagefeatured; ?>" alt="<?php echo the_title(); ?>">
												<div class="c-card-news__type"><i class="icon icon-<?php echo $format; ?>"></i></div>
											</a>
										</div>
										<article class="c-card-category">
											<p class="c-card-category__meta"><?php echo get_the_date('M'); ?> <?php echo get_the_date('d'); ?>, <?php echo get_the_date('Y'); ?> · <?php echo prefix_estimated_reading_time( get_the_content() ); ?> min read</p>
											<a href="<?php the_permalink(); ?>">
												<h3 class="c-card-category__title"><?php echo the_title(); ?></h3>
											</a>
										</article>
									</div>
							<?php
								endwhile; wp_reset_query();
								endif;
							?>
						</div>
							<?php if($totalPost > 9): ?>
								<div class="c-btn-viem-all">
									<a class="loadmore" href="#">Load More</a>
								</div>
							<?php endif; ?>
							<script type="text/javascript">
								var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
								var page = 2;
								var category = "<?php echo $category; ?>";
								jQuery(function($) {
									$('body').on('click', '.loadmore', function(e) {
										e.preventDefault();
										$('.loadmore').html('Loading...');
										var data = {
											'action': 'load_posts_by_ajax',
											'page': page,
											'category': category,
											'security': '<?php echo wp_create_nonce("load_more_posts"); ?>'
										};
										
										$.post(ajaxurl, data, function(response) {
											$('.loadmore').html('Load more');
											if(response != '') {
												$('.my-posts').append(response);
												page++;
											} else {
												$('.loadmore').hide();
											}
										});
									});
								});
								</script>
					</div>
				</section>
			</div><!-- .page-content -->
	</div><!-- .content.inner.blog -->

<?php get_footer(); ?>
