<?php
/**
 * About section
 *
 * This is the template for the content of about section
 *
 * @package Theme Palace
 * @subpackage Ultravel
 * @since Ultravel 1.0.0
 */
if ( ! function_exists( 'ultravel_add_about_section' ) ) :
    /**
    * Add about section
    *
    *@since Ultravel 1.0.0
    */
    function ultravel_add_about_section() {
    	$options = ultravel_get_theme_options();
        // Check if about is enabled on frontpage
        $about_enable = apply_filters( 'ultravel_section_status', true, 'about_section_enable' );

        if ( true !== $about_enable ) {
            return false;
        }
        // Get about section details
        $section_details = array();
        $section_details = apply_filters( 'ultravel_filter_about_section_details', $section_details );

        $section_details_1 = array();
        $section_details_1 = apply_filters( 'ultravel_filter_service_section_details', $section_details_1 );

        if ( empty( $section_details ) ) {
            return;
        }

        // Render about section now.
        ultravel_render_about_section( $section_details, $section_details_1 );
    }
endif;

if ( ! function_exists( 'ultravel_get_about_section_details' ) ) :
    /**
    * about section details.
    *
    * @since Ultravel 1.0.0
    * @param array $input about section details.
    */
    function ultravel_get_about_section_details( $input ) {
        $options = ultravel_get_theme_options();

        $content = array();
        $page_id = ! empty( $options['about_content_page'] ) ? $options['about_content_page'] : '';
        $args = array(
            'post_type'         => 'page',
            'page_id'           => $page_id,
            'posts_per_page'    => 1,
        );                    


        // Run The Loop.
        $query = new WP_Query( $args );
        if ( $query->have_posts() ) : 
            while ( $query->have_posts() ) : $query->the_post();
                $page_post['title']     = get_the_title();
                $page_post['excerpt']   = ultravel_trim_content( 35 );
                $page_post['url']       = get_the_permalink();
                $page_post['image']     = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'medium_large' ) : get_template_directory_uri().'./assets/uploads/no-featured-image-590x650.jpg';

                // Push to the main array.
                array_push( $content, $page_post );
            endwhile;
        endif;
        wp_reset_postdata();
            
        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// about section content details.
add_filter( 'ultravel_filter_about_section_details', 'ultravel_get_about_section_details' );

if ( ! function_exists( 'ultravel_get_service_section_details' ) ) :
    /**
    * services section details.
    *
    * @since Ultravel 1.0.0
    * @param array $input services section details.
    */
    function ultravel_get_service_section_details( $input ) {
        $options = ultravel_get_theme_options();

        $service_count  = 3;
        $content  = array();
        $page_ids = array();

        for ( $i = 1; $i <= $service_count; $i++ ) {
            if ( ! empty( $options['service_content_page_' . $i] ) )
                $page_ids[] = $options['service_content_page_' . $i];
        }

        $args = array(
            'post_type'         => 'page',
            'post__in'          => ( array ) $page_ids,
            'posts_per_page'    => $service_count,
            'orderby'           => 'post__in',
        );                    


            // Run The Loop.
            $query = new WP_Query( $args );
            if ( $query->have_posts() ) : 
                while ( $query->have_posts() ) : $query->the_post();
                    $page_post['title']     = get_the_title();
                    $page_post['url']       = get_the_permalink();
                    $page_post['excerpt']   = ultravel_trim_content( $options['service_excerpt_length'] );
                    $page_post['image']     = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'full' ) : get_template_directory_uri() . '/assets/uploads/no-featured-image-600x450.jpg';

                    // Push to the main array.
                    array_push( $content, $page_post );
                endwhile;
            endif;
            wp_reset_postdata();

            
        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// services section content details.
add_filter( 'ultravel_filter_service_section_details', 'ultravel_get_service_section_details' );


if ( ! function_exists( 'ultravel_render_about_section' ) ) :
  /**
   * Start about section
   *
   * @return string about content
   * @since Ultravel 1.0.0
   *
   */
   function ultravel_render_about_section( $content_details = array(),  $content_details_1 = array()) {
        $options = ultravel_get_theme_options();
        
        if ( empty( $content_details ) ) {
            return;
        }

        $counter_featured_image = !empty( $options['counter_bg_image'] ) ? $options['counter_bg_image'] : '';

        ?>
            <div id="ultravel_about_section" class="page-section same-background">
                <div class="wrapper">
                    <?php foreach ( $content_details as $content ) : ?>
                        <div class="section-header-wrapper clear">
                            <div class="section-header clear">
                                <p class="section-subtitle"><?php echo esc_html( $options['about_sub_title'] ); ?></p>
                                <h2 class="section-title"><?php echo esc_html( $content['title'] ); ?></h2>
                                <?php if ( !empty( $content['url'] ) ) : ?>
                                    <div class="read-more">
                                        <a href="<?php echo esc_url( $content['url'] ); ?>" class="btn"><?php echo esc_html( $options['about_btn_title'] ); ?></a>
                                    </div>
                                <?php endif; ?>
                            </div><!-- .section-header -->

                            <div class="section-content">
                                <p><?php echo wp_kses_post( $content['excerpt'] ); ?></p>
                            </div><!-- .section-content -->
                        </div><!-- .section-header-wrapper -->
                    <?php endforeach; ?>

                    <div class="about-wrapper">
                        <article class="has-post-thumbnail">
                            <div class="featured-image" style="background-image:url('<?php echo esc_url( $counter_featured_image ); ?>');">
                                <div class="counter-wrapper clear">
                                    <?php for( $i=1; $i <= 2; $i++ ): ?>
                                        <?php if ( !empty( $options['about_counter_count_'.$i] ) ): ?>
                                            <div class="counter-item">
                                                <h3 class="counter-value"><?php echo esc_html( $options['about_counter_count_'.$i] ); ?></h3>
                                                <span class="counter-title"><?php echo esc_html( $options['about_counter_label_'.$i] ); ?></span>
                                            </div><!-- .counter-item -->
                                        <?php endif; ?>
                                    <?php endfor; ?>
                                </div>
                            </div>

                            <div class="entry-container">
                                <?php foreach ( $content_details_1 as $i => $content ) : ?>
                                    <div class="services-item">
                                        <div class="icon-container">
                                            <a href="<?php echo esc_url( $content['url'] ); ?>">
                                                <i class="fa <?php echo ! empty( $options['service_content_icon_' . ($i+1)] ) ? esc_attr( $options['service_content_icon_' . ($i+1)] ) : 'fa-cogs'; ?>"></i>
                                            </a>
                                        </div>

                                        <header class="entry-header">
                                            <h2 class="entry-title"><a href="<?php echo esc_url( $content['url']); ?>"><?php echo esc_html( $content['title']); ?></a></h2>
                                            <p><?php echo wp_kses_post( $content['excerpt'] ); ?></p>
                                        </header>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        </article>
                        <?php foreach ( $content_details as $content ) :
                            if ( !empty( $content['url'] ) ) : ?>
                                <div class="read-more">
                                    <a href="<?php echo esc_url( $content['url'] ); ?>" class="btn"><?php echo esc_html( $options['about_btn_title'] ); ?></a>
                                </div>
                            <?php endif;
                        endforeach; ?>
                    </div>
                </div>
            </div>
    <?php
    }
endif;

