<?php
/**
 * The sidebar containing the main widget area
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package prime-construction
 */

?>

<aside id="secondary" class="widget-area sidebar">
	<?php
	$sidebar_text_case = get_theme_mod( 'sidebar_headingtext_case', 'Capitalize' );
	if ( 'Capitalize' === $sidebar_text_case ) {
		$sidebar_text_transform = 'capitalize';
	} elseif ( 'Uppercase' === $sidebar_text_case ) {
		$sidebar_text_transform = 'uppercase';
	} elseif ( 'Lowercase' === $sidebar_text_case ) {
		$sidebar_text_transform = 'lowercase';
	} else {
		$sidebar_text_transform = 'none';
	}

	if ( is_active_sidebar( 'sidebar-1' ) ) {
		dynamic_sidebar( 'sidebar-1' );
	} else {
		?>
		<section class="widget widget_search">
			<h2 class="widget-title mb-2" style="text-transform: <?php echo esc_attr( $sidebar_text_transform ); ?>; "><?php esc_html_e( 'Search Articles', 'prime-construction' ); ?></h2>
			<?php get_search_form(); ?>
		</section>

		<section class="widget widget_categories">
			<h2 class="widget-title" style="text-transform: <?php echo esc_attr( $sidebar_text_transform ); ?>; "><?php esc_html_e( 'Categories', 'prime-construction' ); ?></h2>
			<ul>
				<?php
				wp_list_categories(
					array(
						'orderby'    => 'name',
						'title_li'   => '',
						'show_count' => true,
					)
				);
				?>
			</ul>
		</section>

		<section class="widget widget_recent_entries">
			<h2 class="widget-title" style="text-transform: <?php echo esc_attr( $sidebar_text_transform ); ?>; "><span class="mr-3">
                <i class="fa fa-line-chart " aria-hidden="true"></i>
            </span><?php esc_html_e( 'Popular Posts', 'prime-construction' ); ?></h2>
			<ul class="popular-posts-list">
				<?php
				$prime_construction_recent_posts = get_posts(
					array(
						'numberposts' => 4,
						'post_status' => 'publish',
					)
				);

				foreach ( $prime_construction_recent_posts as $prime_construction_post ) :
					setup_postdata( $prime_construction_post );
					?>
					<li class="popular-post-item">
						<a href="<?php echo esc_url( get_permalink( $prime_construction_post->ID ) ); ?>">
							<span><?php echo esc_html( get_the_title( $prime_construction_post->ID ) ); ?></span>
						</a>
					</li>
                        	<?php $prime_construction_date = get_theme_mod('prime_construction_post_hide_date', false);
					if ($prime_construction_date === false) { ?>
						
                        <i class="fas fa-calendar-alt"></i><span class="entry-date">
                        <a href="<?php echo esc_url(get_day_link(get_post_time('Y'), get_post_time('m'), get_post_time('j'))); ?>"><?php echo esc_html(get_the_date()); ?>
                        <span class="screen-reader-text"><?php echo esc_html(get_the_date()); ?></span></a>
					<?php } ?>

				<?php endforeach; wp_reset_postdata(); ?>
			</ul>
		</section>

		<section class="widget widget_tag_cloud">
			<h2 class="widget-title" style="text-transform: <?php echo esc_attr( $sidebar_text_transform ); ?>; "><i class="fa-solid fa-tag"></i> <?php esc_html_e( 'Tags', 'prime-construction' ); ?></h2>
			<div class="tagcloud tags-list">
				<?php
				$tags = get_tags( array( 'number' => 12 ) );
				if ( $tags ) {
					foreach ( $tags as $tag ) {
						echo '<a href="' . esc_url( get_tag_link( $tag->term_id ) ) . '" class="tag-pill">' . esc_html( $tag->name ) . '</a>';
					}
				}
				?>
			</div>
		</section>

		<section class="widget widget_text contact-widget">
			<h2 class="widget-title" style="text-transform: <?php echo esc_attr( $sidebar_text_transform ); ?>; "><?php esc_html_e( 'Contact Us', 'prime-construction' ); ?></h2>
			<div class="contact-widget-content mt-3">
				<p><a href="mailto:info@example.com"><i class="fa-solid fa-envelope"></i>info@example.com</a></p>
				<p><a href="tel:+1234567890"><i class="fa-solid fa-phone"></i>+1 (234) 567-890</a></p>
				<p><i class="fa-solid fa-map-marker-alt"></i> <?php esc_html_e( '123 Construction Street, Building 456, City, Country', 'prime-construction' ); ?></p>
			</div>
		</section>
	<?php } ?>
</aside><!-- #secondary -->
