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

        if ( true !== $promotion_enable ) {
            return false;
        }

        // Get Promotion section details
        $section_details = array();
        $section_details = apply_filters( 'top_event_filter_promotion_section_details', $section_details );
        if ( empty( $section_details ) ) {
            return;
        }

        // Render Promotion section now.
        top_event_render_promotion_section( $section_details[0] );
    }
endif;

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

        $promotion_content_type  = $options['promotion_content_type']; // Promotion content type
        
        $content = array();
        switch ( $promotion_content_type ) {
        	
            case 'page':
                $page_id = ! empty( $options['promotion_content_page'] ) ? $options['promotion_content_page'] : '';
                $args = array(
                    'post_type'             => 'page',
                    'posts_per_page'        => 1,
                    'p'                     => $page_id,
                    'ignore_sticky_posts'   => true,
                    );                    
            break;

            case 'event':
                $page_id = ! empty( $options['promotion_content_event'] ) ? $options['promotion_content_event'] : '';
                
                $args = array(
                    'post_type'         => 'tp-event',
                    'p'                     => $page_id,
                    'posts_per_page'        => 1,
                    'ignore_sticky_posts'   => true,
                    );                    
            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']   = top_event_trim_content( absint($options['promotion_excerpt_length']) );
                $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;

// Promotion section content details.
add_filter( 'top_event_filter_promotion_section_details', 'top_event_get_promotion_section_details' );


if ( ! function_exists( 'top_event_render_promotion_section' ) ) :
  /**
   * Start Promotion section
   *
   * @return string Promotion content
   * @since  Top Event 1.0.0
   *
   */
   function top_event_render_promotion_section( $content_details = array() ) {
        $options    = top_event_get_theme_options();
        $event_date = (class_exists('TP_Education')) ? get_tp_event_date($content_details['id']) : '';
        $promotion_content_type  = $options['promotion_content_type']; // Promotion content type

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

        <div id="promotion-section" class="relative page-section" style="background-image: url('<?php echo esc_url( $content_details['image'] ); ?>');">
            <div class="overlay"></div>

                <div class="wrapper">
                    <header class="entry-header">
                        <h2 class="entry-title"><?php echo esc_html($content_details['title']); ?></h2>
                    </header>

                    <div class="entry-content">
                        <p><?php echo wp_kses_post($content_details['excerpt']); ?></p>
                    </div><!-- .entry-content -->
                    
                    <?php if($promotion_content_type == 'event') : ?>

                        <div id="countdown" data-date="<?php echo (!empty($event_date)) ? $event_date : date(DATE_RFC2822); ?>">
                            <ul>
                                <li><span id="days"></span><?php _e('Days', 'top-event'); ?></li>
                                <li><span id="hours"></span><?php _e('Hours', 'top-event'); ?></li>
                                <li><span id="minutes"></span><?php _e('Minutes', 'top-event'); ?></li>
                                <li><span id="seconds"></span><?php _e('Seconds', 'top-event'); ?></li>
                            </ul>
                        </div><!-- #countdown -->
                        
                    <?php endif; ?>
                </div><!-- .wrapper -->
            </div><!-- #promotion-section -->
       
    <?php }
endif;