<?php
/**
 * Event section
 *
 * This is the template for the content of event section
 *
 * @package Theme Palace
 * @subpackage  Top Event
 * @since  Top Event 1.0.0
 */
if ( ! function_exists( 'top_event_add_event_section' ) ) :
    /**
    * Add event section
    *
    *@since  Top Event 1.0.0
    */
    function top_event_add_event_section() {
    	$options = top_event_get_theme_options();
        // Check if event is enabled on frontpage
        $event_enable = apply_filters( 'top_event_section_status', true, 'event_section_enable' );

        if ( true !== $event_enable ) {
            return false;
        }
        // Get event section details
        $section_details = array();
        $section_details = apply_filters( 'top_event_filter_event_section_details', $section_details );

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

        // Render event section now.
        top_event_render_event_section( $section_details );
}

endif;

if ( ! function_exists( 'top_event_get_event_section_details' ) ) :
    /**
    * event section details.
    *
    * @since  Top Event 1.0.0
    * @param array $input event section details.
    */
    function top_event_get_event_section_details( $input ) {
        $options = top_event_get_theme_options();

        // Content type.
        $event_content_type    = $options['event_content_type'];
        $event_count           = ! empty( $options['event_count'] ) ? $options['event_count'] : 4;
        
        $content = array();
        switch ( $event_content_type ) {
            
            case 'page':
                $page_ids = array();

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

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

            case 'event-category':
                $cat_id = ! empty( $options['event_content_event_category'] ) ? $options['event_content_event_category'] : '';
                $args = array(
                    'post_type'      => 'tp-event',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'tp-event-category',
                            'field'    => 'id',
                            'terms'    => $cat_id,
                        ),
                    ),
                    'posts_per_page'  => absint( $event_count ),
                    );                    
            break;

            default:
            break;
        }

        // Run The Loop.
        $query = new WP_Query( $args );
        if ( $query->have_posts() ) : 
            $i = 1;
            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['image']     = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'post-thumbnail' ) : get_template_directory_uri().'/assets/uploads/no-featured-image-600x450.jpg';

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

        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// event section content details.
add_filter( 'top_event_filter_event_section_details', 'top_event_get_event_section_details' );


if ( ! function_exists( 'top_event_render_event_section' ) ) :
  /**
   * Start event section
   *
   * @return string event content
   * @since  Top Event 1.0.0
   *
   */
   function top_event_render_event_section( $content_details = array() ) {
        $options = top_event_get_theme_options();
        $event_content_type    = $options['event_content_type'];

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

        <div id="events-section" class="relative page-section same-background">
            <div class="wrapper">
                <div class="section-header">
                    <h2 class="section-title"><?php echo esc_html($options['event_title']); ?></h2>
                    <p class="section-subtitle"><?php echo esc_html($options['event_sub_title']); ?></p>
                </div><!-- .section-header -->

                <div class="section-content">
                    <?php foreach ( $content_details as $content ) : 
                        $event_date = (class_exists('TP_Education')) ? get_tp_event_date($content['id']) : '';
                        $event_location = (class_exists('TP_Education')) ? get_tp_event_location($content['id']) : '';
                        $event_start_time = (class_exists('TP_Education')) ? get_tp_event_start_time($content['id']) : '';
                        $event_end_time = (class_exists('TP_Education')) ? get_tp_event_end_time($content['id']) : ''; ?>

                    <article>
                        <div class="event-item clear">
                        <?php if ( in_array( $event_content_type, array( 'event-category' ) ) && ! empty( $event_date ) ) : ?>
                            <span class="posted-on">
                                <?php echo esc_html( get_tp_event_date($content['id']) ); ?>
                            </span><!-- .posted-on -->
                        <?php else: top_event_posted_on($content['id']); endif; ?>
                            
                            <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 ( in_array( $event_content_type, array( 'event-category' ) ) && ! empty( $event_location ) ){
                                        echo '<p class="event-location">'. esc_html( $event_location ). '</p>';
                                    } ?>
                            </header>

                            <?php if ( in_array( $event_content_type, array( 'event-category' ) ) && !empty( $event_start_time ) && !empty( $event_end_time ) ) : ?>
                                <div class="venue-time">
                                    <span><?php echo sprintf( esc_html('%1$s - %2$s'), $event_start_time, $event_end_time ); ?></span>
                                </div><!-- .venue-time -->
                            <?php endif; ?>

                        </div><!-- .event-item -->
                    </article>

                    <?php endforeach; ?>
                </div><!-- .section-content -->
            </div><!-- .wrapper -->
        </div><!-- #events-section -->

<?php
    }    
endif;