<?php
/**
 * Related posts based on categories and tags.
 * 
 */
$deep_cleaning_post_page_title =  get_theme_mod( 'deep_cleaning_post_page_title', 1 );
$deep_cleaning_post_page_meta =  get_theme_mod( 'deep_cleaning_post_page_meta', 1 );
$deep_cleaning_post_page_thumb =  get_theme_mod( 'deep_cleaning_post_page_thumb', 1 );
$deep_cleaning_post_page_content =  get_theme_mod( 'deep_cleaning_post_page_content', 1 );

$deep_cleaning_post_args = array(
    'posts_per_page'    => absint( get_theme_mod( 'deep_cleaning_related_posts_count', '3' ) ),
    'orderby'           => 'rand',
    'post__not_in'      => array( get_the_ID() ),
);

$deep_cleaning_tax_terms = wp_get_post_terms( get_the_ID(), 'category' );
$deep_cleaning_terms_ids = array();
foreach( $deep_cleaning_tax_terms as $tax_term ) {
	$deep_cleaning_terms_ids[] = $tax_term->term_id;
}

$deep_cleaning_post_args['category__in'] = $deep_cleaning_terms_ids; 

if(get_theme_mod('deep_cleaning_related_post',true)==1){

    $deep_cleaning_related_posts = new WP_Query( $deep_cleaning_post_args );

    if ( $deep_cleaning_related_posts->have_posts() ) : ?>
        <div class="related-post mt-5">
            <h3 class="mb-4"><?php echo esc_html(get_theme_mod('deep_cleaning_related_post_title','Related Post'));?></h3>
            <div class="row">
                <?php while ( $deep_cleaning_related_posts->have_posts() ) : $deep_cleaning_related_posts->the_post(); ?>
                    <div class="col-lg-4 col-md-6">
                        <article id="post-<?php the_ID(); ?>" <?php post_class('article-box'); ?>>
                            <?php if ($deep_cleaning_post_page_thumb == 1 ) {?>
                                <?php if(has_post_thumbnail()){
                                    the_post_thumbnail();
                                    } else{?>
                                <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/img/teambg.png" alt="" />
                                <?php } ?>
                            <?php }?>
                            <?php if ($deep_cleaning_post_page_meta == 1 ) {?>
                                <div class="meta-info-box my-2">
                                    <span class="entry-author"><?php esc_html_e('BY','deep-cleaning'); ?> <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' )) ); ?>"><?php the_author(); ?></a></span>
                                    <span class="ms-2"><?php echo esc_html(get_the_date()); ?></span>
                                </div>
                            <?php }?>
                            <div class="post-summery">
                                <?php if ($deep_cleaning_post_page_title == 1 ) {?>
                                    <?php the_title('<h3 class="entry-title pb-3"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h3>');?>
                                <?php }?>
                                <?php if ($deep_cleaning_post_page_content == 1 ) {?>
                                    <p><?php echo wp_trim_words( get_the_content(), esc_attr(get_theme_mod('deep_cleaning_post_page_excerpt_length', 30)) ); ?><?php echo esc_html(get_theme_mod('deep_cleaning_post_page_excerpt_suffix','[...]')); ?></p>
                                <?php }?>
                                <a href="<?php the_permalink(); ?>" class="btn-text"><?php esc_html_e('Read More','deep-cleaning'); ?></a>
                            </div>
                        </article>
                    </div>
                <?php endwhile; ?>
            </div>
        </div>
    <?php endif;
    wp_reset_postdata();
}