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

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

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

$wp_customize->add_control(
	new Construktly_Toggle_Switch_Custom_Control(
		$wp_customize,
		'construktly_enable_testimonial_section',
		array(
			'label'    => esc_html__( 'Enable Testimonial Section', 'construktly' ),
			'section'  => 'construktly_testimonial_section',
			'settings' => 'construktly_enable_testimonial_section',
		)
	)
);

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

// Testimonial Section - Section Title.
$wp_customize->add_setting(
	'construktly_testimonial_section_title',
	array(
		'default'           => __( 'Clients Testimonials', 'construktly' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'construktly_testimonial_section_title',
	array(
		'label'           => esc_html__( 'Section Title', 'construktly' ),
		'section'         => 'construktly_testimonial_section',
		'settings'        => 'construktly_testimonial_section_title',
		'type'            => 'text',
		'active_callback' => 'construktly_is_testimonial_section_enabled',
	)
);

// Testimonial Section - Section Text.
$wp_customize->add_setting(
	'construktly_testimonial_section_text',
	array(
		'default'           => '',
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'construktly_testimonial_section_text',
	array(
		'label'           => esc_html__( 'Section Text', 'construktly' ),
		'section'         => 'construktly_testimonial_section',
		'settings'        => 'construktly_testimonial_section_text',
		'type'            => 'text',
		'active_callback' => 'construktly_is_testimonial_section_enabled',
	)
);

// Testimonial Section - Content Type.
$wp_customize->add_setting(
	'construktly_testimonial_content_type',
	array(
		'default'           => 'page',
		'sanitize_callback' => 'construktly_sanitize_select',
	)
);

$wp_customize->add_control(
	'construktly_testimonial_content_type',
	array(
		'label'           => esc_html__( 'Select Content Type', 'construktly' ),
		'section'         => 'construktly_testimonial_section',
		'settings'        => 'construktly_testimonial_content_type',
		'type'            => 'select',
		'active_callback' => 'construktly_is_testimonial_section_enabled',
		'choices'         => array(
			'page' => esc_html__( 'Page', 'construktly' ),
			'post' => esc_html__( 'Post', 'construktly' ),
		),
	)
);

// Testimonial Section - Number of Posts.
$wp_customize->add_setting(
	'construktly_testimonial_count',
	array(
		'default'           => 3,
		'sanitize_callback' => 'construktly_sanitize_number_range',
	)
);

$wp_customize->add_control(
	'construktly_testimonial_count',
	array(
		'label'           => esc_html__( 'Number of Items to Show', 'construktly' ),
		'description'     => esc_html__( 'Note: Min 1 & Max 20. Please input the valid number and save. Then refresh the page to see the change.', 'construktly' ),
		'section'         => 'construktly_testimonial_section',
		'settings'        => 'construktly_testimonial_count',
		'type'            => 'number',
		'input_attrs'     => array(
			'min' => 1,
			'max' => 12,
		),
		'active_callback' => 'construktly_is_testimonial_section_enabled',
	)
);

// List out selected number of fields.
$testimonial_count = get_theme_mod( 'construktly_testimonial_count', 3 );
for ( $i = 1; $i <= $testimonial_count; $i++ ) {
	// Testimonial Section - Select Post.
	$wp_customize->add_setting(
		'construktly_testimonial_content_post_' . $i,
		array(
			'sanitize_callback' => 'absint',
		)
	);

	$wp_customize->add_control(
		'construktly_testimonial_content_post_' . $i,
		array(
			/* translators: %d: count. */
			'label'           => sprintf( esc_html__( 'Select Post %d', 'construktly' ), $i ),
			'section'         => 'construktly_testimonial_section',
			'settings'        => 'construktly_testimonial_content_post_' . $i,
			'active_callback' => 'construktly_is_testimonial_section_and_content_type_post_enabled',
			'type'            => 'select',
			'choices'         => construktly_get_post_choices(),
		)
	);

	// Testimonial Section - Select Page.
	$wp_customize->add_setting(
		'construktly_testimonial_content_page_' . $i,
		array(
			'sanitize_callback' => 'absint',
		)
	);

	$wp_customize->add_control(
		'construktly_testimonial_content_page_' . $i,
		array(
			/* translators: %d: count. */
			'label'           => sprintf( esc_html__( 'Select Page %d', 'construktly' ), $i ),
			'section'         => 'construktly_testimonial_section',
			'settings'        => 'construktly_testimonial_content_page_' . $i,
			'active_callback' => 'construktly_is_testimonial_section_and_content_type_page_enabled',
			'type'            => 'select',
			'choices'         => construktly_get_page_choices(),
		)
	);

	// Testimonial Section - Designation.
	$wp_customize->add_setting(
		'construktly_testimonial_designation_' . $i,
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'construktly_testimonial_designation_' . $i,
		array(
			/* translators: %d: count. */
			'label'           => sprintf( esc_html__( 'Designation %d', 'construktly' ), $i ),
			'section'         => 'construktly_testimonial_section',
			'settings'        => 'construktly_testimonial_designation_' . $i,
			'active_callback' => 'construktly_is_testimonial_section_enabled',
		)
	);

}
