<?php
/**
 * Event Section
 *
 * @package charitable_funds
 */

$wp_customize->add_section(
	'charitable_funds_events_section',
	array(
		'panel'    => 'charitable_funds_front_page_options',
		'title'    => esc_html__( 'Event Section', 'charitable-funds' ),
		'priority' => 10,
	)
);

// Event Section - Enable Section.
$wp_customize->add_setting(
	'charitable_funds_enable_event_section',
	array(
		'default'           => false,
		'sanitize_callback' => 'charitable_funds_sanitize_switch',
	)
);

$wp_customize->add_control(
	new Charitable_Funds_Toggle_Switch_Custom_Control(
		$wp_customize,
		'charitable_funds_enable_event_section',
		array(
			'label'    => esc_html__( 'Enable Event Section', 'charitable-funds' ),
			'section'  => 'charitable_funds_events_section',
			'settings' => 'charitable_funds_enable_event_section',
		)
	)
);

if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial(
		'charitable_funds_enable_event_section',
		array(
			'selector' => '#charitable_funds_service_section .section-link',
			'settings' => 'charitable_funds_enable_event_section',
		)
	);
}

// Event Section - Sub Heading.
$wp_customize->add_setting(
	'charitable_funds_trending_event_sub_heading',
	array(
		'default'           => '',
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'charitable_funds_trending_event_sub_heading',
	array(
		'label'           => esc_html__( 'Sub Heading', 'charitable-funds' ),
		'section'         => 'charitable_funds_events_section',
		'settings'        => 'charitable_funds_trending_event_sub_heading',
		'type'            => 'text',
		'active_callback' => 'charitable_funds_is_event_section_enabled',
	)
);

// Event Section - Heading.
$wp_customize->add_setting(
	'charitable_funds_trending_event_heading',
	array(
		'default'           => '',
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'charitable_funds_trending_event_heading',
	array(
		'label'           => esc_html__( 'Heading', 'charitable-funds' ),
		'section'         => 'charitable_funds_events_section',
		'settings'        => 'charitable_funds_trending_event_heading',
		'type'            => 'text',
		'active_callback' => 'charitable_funds_is_event_section_enabled',
	)
);

$wp_customize->add_setting('charitable_funds_banner_second_image', array(
    'default' => '',
    'sanitize_callback' => 'esc_url_raw',
));

$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'charitable_funds_banner_second_image', array(
    'label' => __('Event Image', 'charitable-funds'),
    'section' => 'charitable_funds_events_section',
	'active_callback' => 'charitable_funds_is_event_section_enabled',
)));

// Number of Tabs
$wp_customize->add_setting(
    'charitable_funds_event_number',
    array(
        'default'=> '',
        'sanitize_callback' => 'absint'
    )
);

$wp_customize->add_control(
    'charitable_funds_event_number',
    array(
        'label' => esc_html__('Number of Event Tabs','charitable-funds'),
        'description' => __('Add Count and Refresh Page','charitable-funds'),
        'section'=> 'charitable_funds_events_section',
        'type' => 'number',
        'input_attrs' => array(
            'step'  => 1,
            'min'   => 0,
            'max'   => 8,
        ),
        'active_callback' => 'charitable_funds_is_event_section_enabled',
    )
);

$charitable_funds_tab_count = get_theme_mod('charitable_funds_event_number');

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

    /*
    |---------------------------------------------------
    | Fetch Dates from The Events Calendar
    |---------------------------------------------------
    */
    $charitable_funds_events_query = new WP_Query(array(
        'post_type'      => 'tribe_events',
        'posts_per_page' => -1,
        'meta_key'       => '_EventStartDate',
        'orderby'        => 'meta_value',
        'order'          => 'ASC',
    ));

    $charitable_funds_date_choices = array();
    $charitable_funds_date_choices[''] = __('Select Event Date', 'charitable-funds');

    if ( $charitable_funds_events_query->have_posts() ) {
        while ( $charitable_funds_events_query->have_posts() ) {
            $charitable_funds_events_query->the_post();

            $charitable_funds_raw_date     = tribe_get_start_date(get_the_ID(), false, 'Y-m-d');
            $charitable_funds_display_date = tribe_get_start_date(get_the_ID(), false, 'F j, Y'); // April 13, 2025

            // Avoid duplicates
            $charitable_funds_date_choices[$charitable_funds_raw_date] = $charitable_funds_display_date;
        }
    }
    wp_reset_postdata();

    /*
    |---------------------------------------------------
    | Date Selector (NO TAB NAME FIELD)
    |---------------------------------------------------
    */
    $wp_customize->add_setting(
        'charitable_funds_event_date'.$charitable_funds_i,
        array(
            'default'           => '',
            'sanitize_callback' => 'sanitize_text_field',
        )
    );

    $wp_customize->add_control(
        'charitable_funds_event_date'.$charitable_funds_i,
        array(
            'type'    => 'select',
            'choices' => $charitable_funds_date_choices,
            /* translators: %s is the tab number */
            'label' => sprintf( __( 'Tab %s - Select Event Date', 'charitable-funds' ), $charitable_funds_i ),
            'section' => 'charitable_funds_events_section',
            'active_callback' => 'charitable_funds_is_event_section_enabled',
        )
    );
}

$wp_customize->add_setting( 
	'charitable_funds_events_settings_upgraded_features',
	array(
		'sanitize_callback' => 'sanitize_text_field'
	)
);

$wp_customize->add_control(
	'charitable_funds_events_settings_upgraded_features', 
	array(
		'type'=> 'hidden',
		'description' => "
			<div class='customizer-upgraded-features'>
				<i class='fas fa-info-circle'></i>
				<span class='pro-text'>
					Unlock Premium <strong>Features</strong> ?<br>
					<a target='_blank' href='". esc_url(CHARITABLE_FUNDS_PREMIUM_PAGE) ." '>Upgrade Pro</a>
				</div>
			</div>
		",
		'section' => 'charitable_funds_events_section'
	)
);