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

        if ( true !== $activities_enable ) {
            return false;
        }
        // Get destination section details
        $section_details = array();
        $section_details = apply_filters( 'ultravel_filter_activities_section_details', $section_details );

        if ( empty( $section_details ) ) {
            return;
        }
        
        // Render destination section now.
        ultravel_render_activities_section( $section_details );
    }
endif;

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

        // Content type.
        $activities_content_type  = $options['activities_content_type'];
        $activities_count = 4;
        
        $content = array();
        switch ( $activities_content_type ) {
        	
            case 'category':
                for ( $i = 1; $i <= $activities_count; $i++ ) {

                    if ( ! empty( $options['activities_content_category_' . $i] ) ){
                        $page_post['id']      = $options['activities_content_category_' . $i];
                        $terms = get_term_by( 'id',  $page_post['id'], 'category' );

                        if ( ! empty( $terms ) && !is_wp_error($terms)) :
                            $page_post['title']   = $terms->name;
                            $page_post['url']     = get_category_link( $page_post['id'] );
                            $page_post['count']   = $terms->count;
                            $page_post['image'] = ! empty( $options['activities_background_image_' . $i] ) ? $options['activities_background_image_' . $i] : get_template_directory_uri() . '/assets/uploads/no-featured-image-600x450.jpg';
                        endif;
                    }
                    if ( ! empty(  $options['activities_content_category_' . $i] ) )
                        array_push( $content, $page_post );
                }                     
            break;

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

                for ( $i = 1; $i <= $activities_count; $i++ ) {
                    if ( ! empty( $options['activities_content_trip_types_' . $i] ) ){
                        $page_post['id']      = $options['activities_content_trip_types_' . $i];
                        $cat_image = get_term_meta( $page_post['id'], 'wp_travel_trip_type_image_id', true);
                        
                        $terms = get_term_by( 'id',  $page_post['id'], 'itinerary_types' );
                        
                        if ( ! empty( $terms ) && !is_wp_error($terms)) :
                            $page_post['title']   = $terms->name;
                            $page_post['url']     = get_term_link( $page_post['id'], 'itinerary_types' );
                            $page_post['count']   = $terms->count;
                            $page_post['image'] = ! empty( $cat_image ) ? wp_get_attachment_url( $cat_image ) : get_template_directory_uri() . '/assets/uploads/no-featured-image-600x450.jpg';
                        endif;
                    }
                    if ( ! empty(  $options['activities_content_trip_types_' . $i] ) )
                        array_push( $content, $page_post );
                }
                     
            break;

            default:
            break;
        }

            
        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// destination section content details.
add_filter( 'ultravel_filter_activities_section_details', 'ultravel_get_activities_section_details' );


if ( ! function_exists( 'ultravel_render_activities_section' ) ) :
  /**
   * Start destination section
   *
   * @return string destination content
   * @since Ultravel 1.0.0
   *
   */
   function ultravel_render_activities_section( $content_details = array() ) {
        $options = ultravel_get_theme_options();
        $activities_content_type  = $options['activities_content_type'];

        if ( empty( $content_details ) ) {
            return;
        } ?>

        <div id="ultravel_activities_section" class="page-section same-background">
            <?php if ( is_customize_preview()):
                    ultravel_section_tooltip( 'ultravel_activities_section' );
                endif; ?>
            <div class="wrapper">
                <div class="section-header">
                    <h2 class="section-title"><?php echo esc_html( $options['activities_title'] ); ?></h2>
                    <p class="section-subtitle"><?php echo esc_html( $options['activities_sub_title'] ); ?></p>
                </div><!-- .section-header -->

                <div class="activities-slider" data-slick='{"slidesToShow": 4, "slidesToScroll": 1, "infinite": false, "speed": 1000, "dots": false, "arrows":true, "autoplay": false, "draggable": true, "fade": false }'>

                    <?php foreach ( $content_details as $content ) : ?>
                        <article>
                            <div class="featured-image" style="background-image: url('<?php echo esc_url( $content['image'] ); ?>');">
                                <a href="<?php echo esc_url( $content['url'] ); ?>" class="post-thumbnail-link"></a>
                            </div><!-- .featured-image -->

                            <div class="entry-container">
                                <header class="entry-header">
                                    <h2 class="entry-title"><a href="<?php echo esc_url( $content['url'] ); ?>" tabindex="0"><?php echo esc_html( $content['title'] ); ?></a></h2>
                                    <?php if (  $options['activities_content_type'] !== 'category' && $options['activities_content_type'] !== 'product_cat' && $content['count'] > 0 ): ?>
                                            <p><?php echo $content['count'] . esc_html__( ' destinations', 'ultravel' ); ?></p>
                                        <?php elseif( $options['activities_content_type'] == 'product_cat' && $content['count'] > 0 ): ?>
                                            <p><?php echo $content['count'] . esc_html__( ' products', 'ultravel' ); ?></p>
                                        <?php else: ?>
                                        <?php if ( $content['count'] > 0 ): ?>
                                            <p><?php echo $content['count'] . esc_html__( ' posts', 'ultravel' ); ?></p>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                </header><!-- .entry-header -->
                            </div>
                        </article>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    <?php }
endif;