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

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

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

$wp_customize->add_control(
	new Construktly_Toggle_Switch_Custom_Control(
		$wp_customize,
		'construktly_enable_associate_section',
		array(
			'label'    => esc_html__( 'Enable Associate Section', 'construktly' ),
			'section'  => 'construktly_associate_section',
			'settings' => 'construktly_enable_associate_section',
		)
	)
);

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

// Associate Section - Number of Logos.
$wp_customize->add_setting(
	'construktly_associate_count',
	array(
		'default'           => 5,
		'sanitize_callback' => 'construktly_sanitize_number_range',
	)
);

$wp_customize->add_control(
	'construktly_associate_count',
	array(
		'label'           => esc_html__( 'Number of Logos', 'construktly' ),
		'section'         => 'construktly_associate_section',
		'settings'        => 'construktly_associate_count',
		'type'            => 'number',
		'input_attrs'     => array(
			'min' => 1,
			'max' => 20,
		),
		'active_callback' => 'construktly_is_associate_section_enabled',
	)
);

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

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

	// AssociateSection - Logo.
	$wp_customize->add_setting(
		'construktly_associate_logo_' . $i,
		array(
			'sanitize_callback' => 'construktly_sanitize_image',
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Image_Control(
			$wp_customize,
			'construktly_associate_logo_' . $i,
			array(
				/* translators: %d: count. */
				'label'           => sprintf( esc_html__( 'Logo %d', 'construktly' ), $i ),
				'section'         => 'construktly_associate_section',
				'settings'        => 'construktly_associate_logo_' . $i,
				'active_callback' => 'construktly_is_associate_section_enabled',
			)
		)
	);

	// AssociateSection - Logo URL.
	$wp_customize->add_setting(
		'construktly_associate_logo_url_' . $i,
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);

	$wp_customize->add_control(
		'construktly_associate_logo_url_' . $i,
		array(
			/* translators: %d: count. */
			'label'           => sprintf( esc_html__( 'Logo URL %d', 'construktly' ), $i ),
			'section'         => 'construktly_associate_section',
			'settings'        => 'construktly_associate_logo_url_' . $i,
			'type'            => 'url',
			'active_callback' => 'construktly_is_associate_section_enabled',
		)
	);

}
