<?php
if ( ! get_theme_mod( 'charitable_funds_enable_event_section', false ) ) {
    return;
}
$charitable_funds_tab_count = get_theme_mod('charitable_funds_event_number');
$charitable_funds_banner_second_image = get_theme_mod( 'charitable_funds_banner_second_image');
$charitable_funds_default_image_url = get_template_directory_uri() . '/resource/img/event.png';
$charitable_funds_trending_event_sub_heading = get_theme_mod( 'charitable_funds_trending_event_sub_heading' );
$charitable_funds_trending_event_heading = get_theme_mod( 'charitable_funds_trending_event_heading' );
?>
<section id="charitable_funds_event_section" class="event-section">
    <div class="asterthemes-wrapper">
        <?php if ( ! empty( $charitable_funds_trending_event_sub_heading ) ) : ?>
            <h4 class="event-subheading">
                <?php echo esc_html( $charitable_funds_trending_event_sub_heading ); ?>
            </h4>
        <?php endif; ?>
        <?php if ( ! empty( $charitable_funds_trending_event_heading ) ) : ?>
            <h3 class="event-heading">
                <?php echo esc_html( $charitable_funds_trending_event_heading ); ?>
            </h3>
        <?php endif; ?>

        <div class="wow zoomIn" data-wow-duration="1.5s">
            <div class="tab">
                <?php 
                $charitable_funds_day_suffix = array(1 => '1st Day', 2 => '2nd Day', 3 => '3rd Day');

                for ($charitable_funds_i = 1; $charitable_funds_i <= $charitable_funds_tab_count; $charitable_funds_i++) :

                    $charitable_funds_selected_date = get_theme_mod('charitable_funds_event_date'.$charitable_funds_i);
                    if (empty($charitable_funds_selected_date)) continue;

                    // Date formatting
                    $charitable_funds_title = date("F j, Y", strtotime($charitable_funds_selected_date));
                    $charitable_funds_slug  = strtolower(str_replace(array(',', ' '), '-', $charitable_funds_title));

                    // Day label
                    $charitable_funds_label = isset($charitable_funds_day_suffix[$charitable_funds_i]) ? $charitable_funds_day_suffix[$charitable_funds_i] : $charitable_funds_i . "th Day";
                ?>
                    <button class="tablinks event-tab-btn" onclick="charitable_funds_event_tab(event, '<?php echo esc_attr($charitable_funds_slug); ?>')">
                        <span class="event-day-label"><?php echo esc_html($charitable_funds_label); ?></span>
                        <span class="event-date"><?php echo esc_html($charitable_funds_title); ?></span>
                    </button>
                <?php endfor; ?>
            </div>
            <div class="charity-contents">
                <div class="charity-image">
                    <?php
                    if ( ! empty( $charitable_funds_banner_second_image ) ) { ?>
                        <img src="<?php echo esc_url( $charitable_funds_banner_second_image ); ?>"/>
                    <?php } else { ?>
                        <img src="<?php echo esc_url( $charitable_funds_default_image_url ); ?>"/>
                    <?php } ?>
                </div>
                <div class="charity-tab-contents">
                    <?php for ( $charitable_funds_i = 1; $charitable_funds_i <= $charitable_funds_tab_count; $charitable_funds_i++ ) :
                    $charitable_funds_selected_date = get_theme_mod('charitable_funds_event_date'.$charitable_funds_i);
                    if (empty($charitable_funds_selected_date)) continue;

                        $charitable_funds_title = date("F j, Y", strtotime($charitable_funds_selected_date));
                        $charitable_funds_slug  = strtolower(str_replace(array(',', ' '), '-', $charitable_funds_title));
                        $charitable_funds_start_day = $charitable_funds_selected_date . ' 00:00:00';
                        $charitable_funds_end_day   = $charitable_funds_selected_date . ' 23:59:59';

                        $charitable_funds_events = new WP_Query(array(
                            'post_type'      => 'tribe_events',
                            'posts_per_page' => -1,
                            'meta_query'     => array(
                                array(
                                    'key'     => '_EventStartDate',
                                    'value'   => array($charitable_funds_start_day, $charitable_funds_end_day),
                                    'compare' => 'BETWEEN',
                                    'type'    => 'DATETIME'
                                ),
                            ),
                            'orderby' => 'event_date',
                            'order'   => 'ASC'
                        ));
                    ?>
                    <div id="<?php echo esc_attr($charitable_funds_slug); ?>" class="tabcontent">
                        <div class="event-section-wrapper">
                            <?php if ( $charitable_funds_events->have_posts() ) : ?>
                                <?php while ( $charitable_funds_events->have_posts() ) : $charitable_funds_events->the_post(); ?>
                                    <?php $charitable_funds_event_time = tribe_get_start_time(get_the_ID(), 'g:i A'); ?>
                                    <div class="event-box">
                                        <div class="event-card">
                                            <div class="event-thumb">
                                                <a href="<?php the_permalink(); ?>">
                                                    <?php 
                                                    if ( has_post_thumbnail() ) :
                                                        the_post_thumbnail('medium');
                                                    else :
                                                        echo '<img src="'. esc_url(get_template_directory_uri().'/resource/img/default.png') .'" />';
                                                    endif;
                                                    ?>
                                                </a>
                                            </div>
                                            <div class="event-content">
                                                <h4 class="event-title"><?php the_title(); ?></h4>
                                                <p class="event-excerpt"><?php echo wp_trim_words(get_the_excerpt(), 20); ?></p>
                                            </div>
                                            <div class="event-num">
                                                <div class="event-time">
                                                    <span><?php echo esc_html($charitable_funds_event_time); ?></span>
                                                </div>
                                                <div class="event-btn tutor-event-pricing">
                                                    <a href="<?php the_permalink(); ?>">
                                                        <?php esc_html_e('Event Details', 'charitable-funds'); ?>
                                                    </a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php endwhile; wp_reset_postdata(); ?>
                            <?php else : ?>
                                <p><?php esc_html_e('No events found for this date.', 'charitable-funds'); ?></p>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endfor; ?>
                </div>
            </div>
        </div>
    </div>
</section>
