<?php
/**
 * Category Filter Template Part
 *
 * @package Peblog
 */

// Get customizer settings.
$show_empty_categories = get_theme_mod( 'peblog_show_empty_categories', false );
$selected_category     = get_theme_mod( 'peblog_selected_category', '' );
$posts_limit           = get_theme_mod( 'peblog_category_posts_limit', 12 );

$selected_slugs = array_filter( array_map( 'trim', explode( ',', $selected_category ) ) );

if ( ! empty( $selected_slugs ) ) {
	$selected_slugs = array_map( 'sanitize_title', $selected_slugs );
}

// Get categories based on settings.
$category_args = array();
if ( ! $show_empty_categories ) {
	$category_args['hide_empty'] = true;
} else {
	$category_args['hide_empty'] = false;
}

// If specific category is selected
if ( ! empty( $selected_slugs ) ) {
	$category_args['slug'] = $selected_slugs;
}

$categories = get_categories( $category_args );
?>

<!-- All Categories -->
<section id="filter-cateogries">
	<div class="filter-cat-container">
		<div class="category-title">
			<h2 class="heading"><?php echo esc_html( get_theme_mod( 'peblog_all_categories_title', __( 'Explore Categories', 'peblog' ) ) ); ?></h2>
		</div>
		<div class="categories">
			<div class="category">
				<div class="category-box active" data-filter="all">
					<div class="category-text"><?php esc_html_e( 'All', 'peblog' ); ?></div>
				</div>
			</div>
			
			<?php
			$category_icons = array(
				'design'           => 'design.svg',
				'blockchain'       => 'blockchain.svg',
				'software'         => 'software.svg',
				'machine-learning' => 'machine-learning.svg',
				'game'             => 'game.svg',
			);

			foreach ( $categories as $category ) {
				$custom_icon  = get_theme_mod( 'peblog_category_icon_' . $category->slug, '' );
				$default_icon = isset( $category_icons[ strtolower( $category->slug ) ] ) ? $category_icons[ strtolower( $category->slug ) ] : 'software.svg';
				$icon_url     = $custom_icon ? $custom_icon : esc_url( get_template_directory_uri() . '/assets/images/icon/' . $default_icon );
				?>
				<div class="category">
					<div class="category-box" data-filter="<?php echo esc_attr( $category->slug ); ?>">
						<div class="category-icon">
							<img src="<?php echo esc_url( $icon_url ); ?>" alt="<?php echo esc_attr( $category->name ); ?>" />
						</div>
						<div class="category-text"><?php echo esc_html( $category->name ); ?></div>
					</div>
				</div>
				<?php
			}
			?>
		</div>
	</div>

	<div class="filter-posts-container">
		<?php
		// Get posts for filtering based on customizer setting
		$posts_args = array( 'numberposts' => $posts_limit );

		// Filter by category if selected
		if ( ! empty( $selected_slugs ) ) {
			$posts_args['category_name'] = implode( ',', $selected_slugs );
		}

		$all_posts = get_posts( $posts_args );

		if ( empty( $all_posts ) ) :
			?>
			<div class="no-posts-message">
				<div class="no-posts-icon">
					<img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/icon/document.svg' ); ?>" alt="<?php esc_attr_e( 'No Posts', 'peblog' ); ?>" />
				</div>
				<h3><?php esc_html_e( 'No Posts Found', 'peblog' ); ?></h3>
				<p><?php esc_html_e( 'There are no posts to display in the category filter section. Please add some posts to see them here.', 'peblog' ); ?></p>
			</div>
		<?php else : ?>
			<?php
			foreach ( $all_posts as $filter_post ) {
				// Setup post data for template tags.
				setup_postdata( $filter_post );
				$post_categories = get_the_category( $filter_post->ID );
				$category_slugs  = array();
				foreach ( $post_categories as $post_category ) {
					$category_slugs[] = $post_category->slug;
				}
				$category_class = implode( ' ', $category_slugs );
				?>
			<div class="filter-post" data-category="<?php echo esc_attr( $category_class ); ?>">
				<a href="<?php echo esc_url( get_permalink( $filter_post->ID ) ); ?>">
					<?php if ( has_post_thumbnail( $filter_post->ID ) ) : ?>
						<?php echo get_the_post_thumbnail( $filter_post->ID, 'medium', array( 'class' => 'post-image' ) ); ?>
					<?php else : ?>
						<div class="post-placeholder">
							<div class="placeholder-icon">
								<img src="<?php echo esc_url( peblog_get_placeholder_icon( $filter_post->ID ) ); ?>" alt="<?php esc_attr_e( 'Post', 'peblog' ); ?>" />
							</div>
						</div>
					<?php endif; ?>
				</a>
				<div class="card-content">
					<ul>
						<li class="post-author-image">
							<a href="<?php echo esc_url( get_author_posts_url( $filter_post->post_author ) ); ?>">
								<?php echo get_avatar( $filter_post->post_author, 32 ); ?>
							</a>
						</li>
						<li class="post-author"><a href="<?php echo esc_url( get_author_posts_url( $filter_post->post_author ) ); ?>"><?php echo esc_html( get_the_author_meta( 'display_name', $filter_post->post_author ) ); ?></a></li>
						<li class="post-date"><span><?php echo esc_html( get_the_date( 'd.m.Y', $filter_post->ID ) ); ?></span></li>
					</ul>
					<h3 class="entry-title"><?php echo esc_html( $filter_post->post_title ); ?></h3>
					<div class="entry-expert">
						<p><?php echo esc_html( wp_trim_words( $filter_post->post_content, 15, '...' ) ); ?></p>
					</div>
					<div class="entry-meta">
						<div class="entry-tag">
							<?php if ( $post_categories && ! empty( $post_categories ) ) : ?>
								<?php
								// Show only first category.
								$category = $post_categories[0];
								// Dynamic color classes.
								$color_classes = array( 'tag-main', 'tag-warning', 'tag-success', 'tag-danger', 'tag-info' );
								$color_index   = $category->term_id % count( $color_classes );
								$color_class   = $color_classes[ $color_index ];
								?>
								<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>" class="<?php echo esc_attr( $color_class ); ?>"><?php echo esc_html( $category->name ); ?></a>
							<?php endif; ?>
						</div>
						<?php if ( function_exists( 'pvc_get_post_views' ) ) : ?>
							<div class="entry-rate">
								<a href="<?php echo esc_url( get_permalink( $filter_post->ID ) ); ?>">
									<img src="<?php echo esc_url( get_template_directory_uri() ); ?>/assets/images/icon/eye.svg" alt="<?php esc_attr_e( 'Views', 'peblog' ); ?>" />
									<?php echo esc_html( pvc_get_post_views( $filter_post->ID ) ); ?>
								</a>
							</div>
						<?php endif; ?>
					</div>
				</div>
			</div>
			<?php
			}
			// Reset post data after the loop.
			wp_reset_postdata();
			?>
		<?php endif; ?>
	</div>
</section>
<!-- All Categories end -->
