<?php 
/**
 * Template part for displaying Featured Classes Section
 *
 * @package Esports Blog
 */

$esports_blog_classes       = get_theme_mod( 'esports_blog_classes_setting', false );
$esports_blog_service_title = get_theme_mod( 'esports_blog_service_title' );
$esports_blog_service_text  = get_theme_mod( 'esports_blog_service_text' );

if ( $esports_blog_classes ) : ?>
    <div class="our-classes">
        <div class="container">
            <div class="service">
                <div class="side-border">
                    <?php if ( ! empty( $esports_blog_service_title ) ) : ?>
                        <h3 class="mb-0"><?php echo esc_html( $esports_blog_service_title ); ?></h3>
                    <?php endif; ?>

                    <?php if ( ! empty( $esports_blog_service_text ) ) : ?>
                        <p class="title-text"><?php echo esc_html( $esports_blog_service_text ); ?></p>
                    <?php endif; ?>
                </div>

                <div class="owl-carousel house-category-carousel">
                    <?php
                    $esports_blog_categories = get_categories( array(
                        'orderby'    => 'name',
                        'order'      => 'ASC',
                        'hide_empty' => 1,
                        'number'     => 12,
                    ) );

                    if ( ! empty( $esports_blog_categories ) ) {
                        foreach ( $esports_blog_categories as $category ) {
                            $cat_query = new WP_Query( array(
                                'post_type'      => 'post',
                                'posts_per_page' => 3,
                                'cat'            => intval( $category->term_id ),
                                'post_status'    => 'publish',
                            ) );

                            if ( $cat_query->have_posts() ) :
                                ?>
                                <div class="item category-block">
                                    <div class="category-inner">
                                        <div class="category-posts">
                                            <?php
                                            $esports_blog_index = 0;
                                            while ( $cat_query->have_posts() ) : $cat_query->the_post();
                                                if ( $esports_blog_index === 0 ) :
                                                    ?>
                                                    <article class="post post-large">
                                                        <a class="post-link wow zoomIn delay-1000" data-wow-duration="2s" href="<?php echo esc_url( get_permalink() ); ?>">
                                                            <?php
                                                            if ( has_post_thumbnail() ) {
                                                                the_post_thumbnail( 'large', array(
                                                                    'alt' => esc_attr( get_the_title() ),
                                                                ) );
                                                            } else {
                                                                ?>
                                                                <img src="<?php echo esc_url( get_stylesheet_directory_uri() . '/images/default-header.png' ); ?>" alt="<?php echo esc_attr( get_the_title() ); ?>">
                                                                <?php
                                                            }
                                                            ?>
                                                        </a>
                                                    </article>
                                                    <?php
                                                else :
                                                    ?>
                                                    <article class="post post-small">
                                                        <a class="post-link wow zoomIn delay-2000" data-wow-duration="2s" href="<?php echo esc_url( get_permalink() ); ?>">
                                                            <?php
                                                            if ( has_post_thumbnail() ) {
                                                                the_post_thumbnail( 'medium', array(
                                                                    'alt' => esc_attr( get_the_title() ),
                                                                ) );
                                                            } else {
                                                                ?>
                                                                <img src="<?php echo esc_url( get_stylesheet_directory_uri() . '/images/default-header.png' ); ?>" alt="<?php echo esc_attr( get_the_title() ); ?>">
                                                                <?php
                                                            }
                                                            ?>
                                                        </a>
                                                    </article>
                                                    <?php
                                                endif;
                                                $esports_blog_index++;
                                            endwhile;
                                            ?>
                                        </div><!-- .category-posts -->
                                    </div><!-- .category-inner -->
                                    <div class="category-header">
                                        <div class="category-text">
                                            <h4 class="category-name mb-0">
                                                <a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
                                                    <?php echo esc_html( $category->name ); ?>
                                                </a>
                                            </h4>
                                            <?php
                                            if ( ! empty( $category->description ) ) {
                                                $desc_excerpt = wp_trim_words( $category->description, 8, '...' );
                                                echo '<p class="category-description">' . esc_html( $desc_excerpt ) . '</p>';
                                            }
                                            ?>
                                        </div><!-- .category-text -->

                                        <div class="category-icon">
                                            <a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>"><span>&#10140;</span></a>
                                        </div><!-- .category-icon -->
                                    </div><!-- .category-header -->
                                </div><!-- .item -->
                                <?php
                                wp_reset_postdata();
                            endif;
                        }
                    } else {
                        echo '<p>' . esc_html__( 'No categories found.', 'esports-blog' ) . '</p>';
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>
<?php endif; ?>
