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

<aside id="secondary" class="widget-area sidebar">
    <?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->

<?php 
  if ( ! is_active_sidebar( 'sidebar-1' )) { ?>
<aside id="secondary" class="widget-area">
    <section id="Search" class="widget widget_block widget_search ">
        <h2 class="widget-title"><?php esc_html_e('Search', 'express-movers'); ?></h2>
        <?php get_search_form(); ?>
    </section>
    <section id="recent-posts" class="widget sidebar-card sidebar-card--recent" role="complementary">
        <h2 class="widget-title sidebar-card__title">
            <?php esc_html_e('Recent Posts', 'express-movers'); ?>
        </h2>
        <?php
        $express_movers_recent_posts = get_posts( array(
            'numberposts' => 3,
            'post_status' => 'publish',
        ) );
        ?>
        <ul class="sidebar-recent-posts-list">
            <?php foreach ( $express_movers_recent_posts as $express_movers_post ) : ?>
                <?php
                $express_movers_post_id = $express_movers_post->ID;
                $express_movers_thumb = get_the_post_thumbnail_url( $express_movers_post_id, 'thumbnail' );
                if ( empty( $express_movers_thumb ) ) {
                    $express_movers_thumb = get_template_directory_uri() . '/assets/images/no-images.jpg';
                }
                $express_movers_title = get_the_title( $express_movers_post_id );
                $express_movers_link  = get_permalink( $express_movers_post_id );
                $express_movers_date  = get_the_date( 'M j, Y', $express_movers_post_id );
                ?>
                <li class="sidebar-recent-post-item">
                    <a class="sidebar-recent-post-item__link" href="<?php echo esc_url( $express_movers_link ); ?>">
                        <img class="sidebar-recent-post-item__thumb"
                             src="<?php echo esc_url( $express_movers_thumb ); ?>"
                             alt="<?php echo esc_attr( $express_movers_title ); ?>" />
                        <div class="sidebar-recent-post-item__body">
                            <div class="sidebar-recent-post-item__title"><?php echo esc_html( $express_movers_title ); ?></div>
                            <div class="sidebar-recent-post-item__date">
                                <i class="fas fa-calendar-alt" aria-hidden="true"></i>
                                <?php echo esc_html( $express_movers_date ); ?>
                            </div>
                        </div>
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
    </section>

    <section id="archives" class="widget sidebar-card sidebar-card--archives">
        <h2 class="widget-title sidebar-card__title">
            <?php esc_html_e('Archives', 'express-movers'); ?>
        </h2>
        <ul class="sidebar-archives-list">
            <?php
            wp_get_archives( array(
                'type'            => 'monthly',
                'show_post_count' => true,
            ) );
            ?>
        </ul>
    </section>

    <section id="categories" class="widget sidebar-card sidebar-card--categories" role="complementary">
        <h2 class="widget-title sidebar-card__title">
            <?php esc_html_e('Categories', 'express-movers'); ?>
        </h2>
        <?php
        $express_movers_post_counts = wp_count_posts( 'post' );
        $express_movers_all_count = 0;
        if ( ! empty( $express_movers_post_counts ) && isset( $express_movers_post_counts->publish ) ) {
            $express_movers_all_count = (int) $express_movers_post_counts->publish;
        }

        $express_movers_categories = get_categories( array(
            'taxonomy'   => 'category',
            'hide_empty' => false,
            'orderby'    => 'name',
            'order'      => 'ASC',
        ) );
        ?>
        <ul class="sidebar-categories-list">
            <li class="sidebar-cat-item sidebar-cat-item--active">
                <a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php esc_html_e('All Posts', 'express-movers'); ?></a>
                <span class="sidebar-cat-count"><?php echo esc_html( $express_movers_all_count ); ?></span>
            </li>
            <?php foreach ( $express_movers_categories as $express_movers_cat ) : ?>
                <?php
                $express_movers_cat_count = (int) $express_movers_cat->count;
                if ( $express_movers_cat_count <= 0 ) {
                    continue;
                }
                ?>
                <li class="sidebar-cat-item">
                    <a href="<?php echo esc_url( get_category_link( $express_movers_cat->term_id ) ); ?>">
                        <?php echo esc_html( $express_movers_cat->name ); ?>
                    </a>
                    <span class="sidebar-cat-count"><?php echo esc_html( $express_movers_cat_count ); ?></span>
                </li>
            <?php endforeach; ?>
        </ul>
    </section>


</aside><!-- #secondary -->
<?php } ?>