<?php
/**
 * Buzz_Agency Service Section Functions And Definations
 *
 * @package Buzz_Agency
 */
function buzz_agency_service_callback(){
	
	$service_option = get_theme_mod('buzz_agency_homepage_setting_service_option',0);
	$image_one = get_theme_mod('buzz_agency_servie_image_one','');
	$image_two = get_theme_mod('buzz_agency_servie_image_two','');

	if($service_option == '1'): 
?>
	<section class="service-section">
	    <div class="container">
	        <div class="service-content-wrapper">
	            <div class="service-content os-animation" data-os-animation="slideInRight">

	            	<?php $service_page  = get_theme_mod('buzz_agency_service_page'); ?>
	            	
	            	<?php   if( !empty( $service_page ) ): 

	            		$args = array (                                 
	            		'page_id'           => absint( $service_page ),
	            		'post_status'       => 'publish',
	            		'post_type'         => 'page',
	            		);

	            		$loop_service = new WP_Query($args);

	            		if ( $loop_service->have_posts() ) : ?>

			                <header class="entry-header heading">

		                		<?php while ($loop_service->have_posts()) : $loop_service->the_post();?>

				                    <h2 class="entry-title"><?php the_title(); ?></h2>
				                    <p><?php echo esc_html(wp_trim_words(get_the_content(),35,'...')); ?>
				                    </p>

		                		<?php endwhile; 
		                 		   wp_reset_postdata();?>     

			                </header>

			           <?php endif;
			           endif;
			           ?>  
     
		                <div class="owl-carousel owl-theme service-post-slider">
        				<?php
        				$service_section_cat = get_theme_mod('buzz_agency_service_section_cat'); 
        				$number = get_theme_mod('agency_service_num',5);
        				$readmore = get_theme_mod( 'agency_services_readmore',esc_html__('Discover More','buzz-agency') );
        				if( !empty( $service_section_cat) ) {
        					$loop = new WP_Query(
        						array(
        							'post_type' => 'post',    
        							'category_name' => esc_html($service_section_cat),
        							'posts_per_page' => absint( $number ),  
        						)
        					);
        				}else{
        					$loop = new WP_Query( array( 'post_type'=>'post','posts_per_page'=>absint( $number ), ) );
        				}   
        				if($loop->have_posts() ) {

        					while($loop->have_posts() ) {

        	            		$loop->the_post(); 
        	            		$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'buzz-agency-service-image', true );
    	            			$id = "";
								$post_id = absint( $id );
								$post = get_post( $post_id );
								if( ! $post instanceof WP_Post ) {
								return;
								} 
        	            		?>  
			                    <article class="post">
			                    	<?php if(!empty($image)){ ?> 
				                        <figure class="featuerd-image">
		                            	    <img src="<?php echo esc_url($image[0]);?>" />
				                        </figure>
			                        <?php } ?>	
			                        <div class="post-content">
			                            <header class="entry-header">
			                                <h3 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </h3>
			                            </header>
			                            <div class="entry-content">
			                                <p> <?php echo esc_html(wp_trim_words(get_the_content(),45,'&hellip;')); ?> </p>
			                            </div>
			                        </div>
			                    </article>
		                   	<?php 
       		               	}
       		               	wp_reset_postdata();
       		               }
       		               ?>   
		                </div>
						   
					<?php if(!empty($readmore)){ ?> 
	                	<a href="<?php the_permalink();?>" class="box-button">
	                		<?php echo esc_html( $readmore );?><span></span>
	                	</a>
	                <?php } ?>		

	            </div>
				<?php if( !empty( $image_one) ) { ?>

		            <figure class="featured-image os-animation" data-os-animation="slideInLeft"
		                data-os-animation-delay="0.8s">
		                <img src="<?php echo esc_url($image_one);?>" />
		            </figure>

				<?php } ?>

				<?php if( !empty( $image_two) ) { ?>

		            <figure class="service-bg-image os-animation" data-os-animation="slideInLeft"
		                data-os-animation-delay="0.3s">
		                 <img src="<?php echo esc_url($image_two);?>" />
		            </figure>

	            <?php } ?>
	        </div>

	    </div>
	</section>

	<?php endif;   ?>	
<?php
}
add_action('buzz_agency_service_callback_action','buzz_agency_service_callback');		