<?php
/**
 * The template for displaying Services
 *
 * @package Personal_Trainer
 */

if ( ! function_exists( 'personaltrainer_service_display' ) ) :
	/**
	* Add Featured content.
	*
	* @uses action hook personaltrainer_before_content.
	*
	* @since Personal Trainer 0.1
	*/
	function personaltrainer_service_display() {
		$output = '';

		// get data value from options
		$enable_content = get_theme_mod( 'personaltrainer_service_option', 'disabled' );

		if ( personaltrainer_check_section( $enable_content ) ) {
			$headline       = get_theme_mod( 'personaltrainer_service_headline', esc_html__( 'Services', 'personal-trainer' ) );
			$subheadline    = get_theme_mod( 'personaltrainer_service_subheadline' );

			$output = '
			<div class="services-content-wrapper ect-service section">
				<div class="wrapper">';
					if ( ! empty( $headline ) || ! empty( $subheadline ) ) {
						$output .= '<div class="section-heading-wrapper service-section-headline">';

						if ( ! empty( $headline ) ) {
							$output .= '<div class="section-title-wrapper"><h2 class="section-title">' . wp_kses_post( $headline ) . '</h2></div>';
						}

						if ( ! empty( $subheadline ) ) {
							$output .= '<div class="section-description">' . wp_kses_post( $subheadline ) . '</div>';
						}

						$output .= '</div><!-- .section-heading-wrapper -->';
					}
					$output .= '
					<div class="section-content-wrap">';

						$output .= personaltrainer_post_page_category_service();

						$output .= '
					</div> <!-- .section-content-wrap -->
				</div> <!-- .wrapper -->
			</div> <!-- .services-content-wrapper -->';
		}

		echo $output;
	}
endif;
add_action( 'personaltrainer_service', 'personaltrainer_service_display', 10 );

if ( ! function_exists( 'personaltrainer_post_page_category_service' ) ) :
	/**
	 * This function to display featured posts content
	 *
	 * @param $options: personaltrainer_theme_options from customizer
	 *
	 * @since Personal Trainer 0.1
	 */
	function personaltrainer_post_page_category_service() {
		global $post;

		$quantity   = get_theme_mod( 'personaltrainer_service_number', 3 );
		$no_of_post = 0; // for number of posts
		$post_list  = array();// list of valid post/page ids
		$type       = get_theme_mod( 'personaltrainer_service_type', 'category' );
		$output     = '';

		$args = array(
			'orderby'             => 'post__in',
			'ignore_sticky_posts' => 1 // ignore sticky posts
		);

		$args['post_type'] = 'ect-service';

		for ( $i = 1; $i <= $quantity; $i++ ) {
			$post_id = get_theme_mod( 'personaltrainer_service_cpt_' . $i );

			if ( $post_id ) {
				// Polylang Support.
				if ( class_exists( 'Polylang' ) ) {
					$post_id = pll_get_post( $post_id, pll_current_language() );
				}

				$post_list = array_merge( $post_list, array( $post_id ) );

				$no_of_post++;
			}
		}

		$args['post__in'] = $post_list;

		if ( 0 === $no_of_post ) {
			return;
		}

		$args['posts_per_page'] = $no_of_post;

		$loop = new WP_Query( $args );

		while ( $loop->have_posts() ) {
			$loop->the_post();

			$title_attribute = the_title_attribute( 'echo=0' );

			$i = absint( $loop->current_post + 1 );

			$post_style = ( $i % 2 ) ? 'content-left' : 'content-right';

			$output .= '
				<article id="service-post-' . esc_attr( $i ) . '" class="hentry ' . esc_attr( $type ) . '">
					<div class="post-wrap ' . $post_style . '">';
					$output .= '
						<div class="entry-container">';

					if ( get_theme_mod( 'personaltrainer_service_enable_title', 1 ) ) {
						$output .= the_title( '<header class="entry-header"><h2 class="entry-title">', '</h2></header><!-- .entry-header -->', false );
					}

					$content_show = get_theme_mod( 'personaltrainer_service_show', 'excerpt' );

					if ( 'excerpt' === $content_show ) {
						//Show Excerpt
						$more_tag_text = get_theme_mod( 'personaltrainer_excerpt_more_text',  esc_html__( 'Continue reading', 'personal-trainer' ) );

						$output .= '
							<div class="entry-content"><p>' . get_the_excerpt() . '</p></div><!-- .entry-content -->';
					} elseif ( 'full-content' === $content_show ) {
						//Show Content
						$content = apply_filters( 'the_content', get_the_content() );
						$content = str_replace( ' )]>', ' )]&gt;', $content );
						$output .= '<div class="entry-content">' .wp_kses_post( $content ) . '</div><!-- .entry-content -->';
					}

					// Default value if there is no first image
					$image = trailingslashit( esc_url ( get_template_directory_uri() ) ) . 'assets/images/no-thumb-760x760.jpg" >';

					if ( has_post_thumbnail() ) {
						$image = get_the_post_thumbnail_url( $post->ID, 'personaltrainer-service' );
					}
					else {
						// Get the first image in page, returns false if there is no image.
						$first_image = personaltrainer_get_first_image( $post->ID, 'personaltrainer-service', '', true );

						// Set value of image as first image if there is an image present in the page.
						if ( $first_image ) {
							$image = $first_image;
						}
					}

					$output .= '
						</div><!-- .entry-container -->';

					$output .= '
						<div class="post-thumbnail" style="background-image: url(' . esc_url( $image ) . '); ">
							' . the_title( '<a href="' . esc_url( get_permalink() ) . '" title="' . $title_attribute . '"><span class="screen-reader-text">', '</span></a>', false ) . '
						</div><!-- .post-thumbnail -->';

					$output .= '
					</div> <!-- .post-wrap -->
				</article> <!-- .article --><!-- .service-post-' . esc_attr( $i ) . ' -->';
			} //endwhile

		wp_reset_postdata();

		return $output;
	}
endif; // personaltrainer_post_page_category_service
