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

        if ( true !== $hero_slider_enable ) {
            return false;
        }
        // Get slider section details
        $section_details = array();
        $section_details = apply_filters( 'ultravel_filter_hero_slider_section_details', $section_details );

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

        // Render slider section now.
        ultravel_render_hero_slider_section( $section_details );
    }
endif;

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

        // Content type.
        $hero_slider_content_type  = $options['hero_slider_content_type'];
        $hero_slider_count = 3;
        
        $content = array();
        switch ( $hero_slider_content_type ) {
        	
            case 'page':
                $page_ids = array();

                for ( $i = 1; $i <= $hero_slider_count; $i++ ) {
                    if ( ! empty( $options['hero_slider_content_page_' . $i] ) )
                        $page_ids[] = $options['hero_slider_content_page_' . $i];
                }
                
                $args = array(
                    'post_type'         => 'page',
                    'post__in'          => ( array ) $page_ids,
                    'posts_per_page'    => absint( $hero_slider_count ),
                    'orderby'           => 'post__in',
                    );                    
            break;

            case 'post':
                $post_ids = array();

                for ( $i = 1; $i <= $hero_slider_count; $i++ ) {
                    if ( ! empty( $options['hero_slider_content_post_' . $i] ) )
                        $post_ids[] = $options['hero_slider_content_post_' . $i];
                }
                $args = array(
                    'post_type'         => 'post',
                    'post__in'          => ( array ) $post_ids,
                    'posts_per_page'    => absint( $hero_slider_count ),
                    'orderby'           => 'post__in',
                    'ignore_sticky_posts'   => true,
                    );                    
            break;

            case 'category':
                $cat_id = ! empty( $options['hero_slider_content_category'] ) ? $options['hero_slider_content_category'] : '';
                $args = array(
                    'post_type'         => 'post',
                    'posts_per_page'    => absint( $hero_slider_count ),
                    'cat'               => absint( $cat_id ),
                    'ignore_sticky_posts'   => true,
                    );                    
            break;

            case 'trip':
                if ( ! class_exists( 'WP_Travel' ) )
                    return;

                $page_ids = array();

                for ( $i = 1; $i <= $hero_slider_count; $i++ ) {
                    if ( ! empty( $options['hero_slider_content_trip_' . $i] ) )
                        $page_ids[] = $options['hero_slider_content_trip_' . $i];
                }
                
                $args = array(
                    'post_type'         => 'itineraries',
                    'post__in'          => ( array ) $page_ids,
                    'posts_per_page'    => absint( $hero_slider_count ),
                    'orderby'           => 'post__in',
                    );                    
            break;

            default:
            break;
        }


            // Run The Loop.
            $query = new WP_Query( $args );
            if ( $query->have_posts() ) : 
                while ( $query->have_posts() ) : $query->the_post();
                    $page_post['id']        = get_the_id();
                    $page_post['title']     = get_the_title();
                    $page_post['url']       = get_the_permalink();
                    $page_post['excerpt']   = ultravel_trim_content( 15 );
                    $page_post['image']  	= has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'full' ) : '';

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


if ( ! function_exists( 'ultravel_render_hero_slider_section' ) ) :
  /**
   * Start slider section
   *
   * @return string slider content
   * @since Ultravel 1.0.0
   *
   */
   function ultravel_render_hero_slider_section( $content_details = array() ) {
        $options = ultravel_get_theme_options();

        if ( empty( $content_details ) ) {
            return;
        } ?>
        <div id="ultravel_hero_slider_section" class="slider-section">
            <div class="wrapper">
                <div class="hero-content-wrapper">
                    <div class="section-header">
                        <?php if ( $options['hero_slider_title'] ) : ?>
                            <h2 class="section-title"><?php echo esc_html( $options['hero_slider_title'] ); ?></h2>
                        <?php endif; ?>
                    </div><!-- .section-header -->

                    <div class="travel-search">
                        <?php echo get_search_form(); ?>
                    </div>
                </div>

                <div class="hero-image-wrapper">

                    <div class="hero-slider" data-slick='{"slidesToShow": 1, "slidesToScroll": 1, "infinite": true, "speed": 1000, "dots": false, "arrows":true, "autoplay": <?php echo $options['hero_slider_autoplay'] ? 'true' : 'false'; ?>, "draggable": true, "fade": false, "adaptiveHeight": false, "variableWidth": true }'>
                        <?php $i=1; foreach ( $content_details as $content ) : ?>
                            <article>
                                <div class="featured-image" style="background-image: url('<?php echo esc_url( $content['image'] ); ?>');">
                                    <div class="entry-container">
                                        <header class="entry-header">
                                            <h2 class="entry-title"><a href="<?php echo esc_url( $content['url'] ); ?>"><?php echo esc_html( $content['title'] ); ?></a></h2>
                                            <?php if ( !empty( $options['hero_slider_extra_text_'. $i] ) ) : ?>
                                                <p><?php echo esc_html( $options['hero_slider_extra_text_'. $i] ); ?></p>
                                            <?php endif; ?>
                                        </header>

                                        <div class="icon-container">
                                            <a href="#"><svg viewBox="0 0 31.49 31.49">
                                                <path d="M21.205,5.007c-0.429-0.444-1.143-0.444-1.587,0c-0.429,0.429-0.429,1.143,0,1.571l8.047,8.047H1.111
                                                C0.492,14.626,0,15.118,0,15.737c0,0.619,0.492,1.127,1.111,1.127h26.554l-8.047,8.032c-0.429,0.444-0.429,1.159,0,1.587
                                                c0.444,0.444,1.159,0.444,1.587,0l9.952-9.952c0.444-0.429,0.444-1.143,0-1.571L21.205,5.007z"></path>
                                            </svg>
                                        </a>
                                    </div>
                                </div>

                            </div><!-- .featured-image -->
                        </article>
                    <?php $i++; endforeach; ?>
                    </div><!-- .featured-slider -->
                </div>
            </div>
        </div>     
    <?php }
endif;