<?php
/**
 * Counter Section
 *
 * @package Construktly
 */

$wp_customize->add_section(
	'construktly_counter_section',
	array(
		'panel' => 'construktly_front_page_options',
		'title' => esc_html__( 'Counter Section', 'construktly' ),
	)
);

// Counter Section - Enable Section.
$wp_customize->add_setting(
	'construktly_enable_counter_section',
	array(
		'default'           => false,
		'sanitize_callback' => 'construktly_sanitize_switch',
	)
);

$wp_customize->add_control(
	new Construktly_Toggle_Switch_Custom_Control(
		$wp_customize,
		'construktly_enable_counter_section',
		array(
			'label'    => esc_html__( 'Enable Counter Section', 'construktly' ),
			'section'  => 'construktly_counter_section',
			'settings' => 'construktly_enable_counter_section',
		)
	)
);

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

// Counter Section - Background Color.
$default_color = construktly_get_default_color();
$wp_customize->add_setting(
	'construktly_counter_background_color',
	array(
		'default'           => $default_color['primary'],
		'sanitize_callback' => 'sanitize_hex_color',
	)
);

$wp_customize->add_control(
	new WP_Customize_Color_Control(
		$wp_customize,
		'construktly_counter_background_color',
		array(
			'label'           => __( 'Background Color', 'construktly' ),
			'section'         => 'construktly_counter_section',
			'settings'        => 'construktly_counter_background_color',
			'active_callback' => 'construktly_is_counter_section_enabled',
		)
	)
);

// Counter Section - Number of Counters.
$wp_customize->add_setting(
	'construktly_counter_count',
	array(
		'default'           => 4,
		'sanitize_callback' => 'absint',
	)
);

$wp_customize->add_control(
	'construktly_counter_count',
	array(
		'label'           => esc_html__( 'Number of Counters', 'construktly' ),
		'description'     => esc_html__( 'Note: Min 1 & Max 4. Please input the valid number and save. Then refresh the page to see the change.', 'construktly' ),
		'section'         => 'construktly_counter_section',
		'settings'        => 'construktly_counter_count',
		'type'            => 'number',
		'input_attrs'     => array(
			'min' => 1,
			'max' => 4,
		),
		'active_callback' => 'construktly_is_counter_section_enabled',
	)
);

// List out selected number of fields.
$counter_count = get_theme_mod( 'construktly_counter_count', 4 );

for ( $i = 1; $i <= $counter_count; $i++ ) {

	// Counter Section - Counter Icon.
	$wp_customize->add_setting(
		'construktly_counter_icon_' . $i,
		array(
			'sanitize_callback' => 'construktly_sanitize_image',
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Image_Control(
			$wp_customize,
			'construktly_counter_icon_' . $i,
			array(
				'label'           => sprintf( esc_html__( 'Icon %d', 'construktly' ), $i ),
				'section'         => 'construktly_counter_section',
				'settings'        => 'construktly_counter_icon_' . $i,
				'active_callback' => 'construktly_is_counter_section_enabled',
			)
		)
	);

	// Counter Section - Counter Label.
	$wp_customize->add_setting(
		'construktly_counter_label_' . $i,
		array(
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'construktly_counter_label_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Label %d', 'construktly' ), $i ),
			'section'         => 'construktly_counter_section',
			'settings'        => 'construktly_counter_label_' . $i,
			'type'            => 'text',
			'active_callback' => 'construktly_is_counter_section_enabled',
		)
	);

	// Counter Section - Counter Value.
	$wp_customize->add_setting(
		'construktly_counter_value_' . $i,
		array(
			'default'           => '',
			'sanitize_callback' => 'absint',
		)
	);

	$wp_customize->add_control(
		'construktly_counter_value_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Value %d', 'construktly' ), $i ),
			'section'         => 'construktly_counter_section',
			'settings'        => 'construktly_counter_value_' . $i,
			'type'            => 'number',
			'input_attrs'     => array( 'min' => 1 ),
			'active_callback' => 'construktly_is_counter_section_enabled',
		)
	);

	// Counter Section - Counter Suffix.
	$wp_customize->add_setting(
		'construktly_counter_value_suffix_' . $i,
		array(
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'construktly_counter_value_suffix_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Value Suffix %d', 'construktly' ), $i ),
			'section'         => 'construktly_counter_section',
			'settings'        => 'construktly_counter_value_suffix_' . $i,
			'type'            => 'text',
			'active_callback' => 'construktly_is_counter_section_enabled',
		)
	);

}
