<?php
/**
 * Latest Products Section options
 *
 * @package Theme Palace
 * @subpackage Enlightme Pro
 * @since Enlightme Pro 1.0.0
 */
if ( !class_exists('WooCommerce') ) {
    return;
}

// Add latest_products section
$wp_customize->add_section( 'enlightme_pro_latest_products_section', array(
    'title'             => esc_html__( 'Latest Products','enlightme-pro' ),
    'description'       => esc_html__( 'Note: To activate this section you need to install WooCommerce Plugin.', 'enlightme-pro' ),
    'panel'             => 'enlightme_pro_front_page_panel',
    'active_callback'   => function( $control ) {
        return ( 
            enlightme_pro_is_second_layout( $control )
            );
    },
) );

// latest_products content enable control and setting
$wp_customize->add_setting( 'enlightme_pro_theme_options[latest_products_section_enable]', array(
    'default'           =>  $options['latest_products_section_enable'],
    'sanitize_callback' => 'enlightme_pro_sanitize_switch_control',
) );

$wp_customize->add_control( new Enlightme_Pro_Switch_Control( $wp_customize, 'enlightme_pro_theme_options[latest_products_section_enable]', array(
    'label'             => esc_html__( 'Latest product Section Enable', 'enlightme-pro' ),
    'section'           => 'enlightme_pro_latest_products_section',
    'on_off_label'      => enlightme_pro_switch_options(),
) ) );

if ( isset( $wp_customize->selective_refresh ) ) {
    $wp_customize->selective_refresh->add_partial( 'enlightme_pro_theme_options[latest_products_section_enable]', array(
        'selector'            => '#latest-products .tooltiptext',
        'settings'            => 'enlightme_pro_theme_options[latest_products_section_enable]',
    ) );
}

// latest_products title setting and control
$wp_customize->add_setting( 'enlightme_pro_theme_options[latest_products_sub_title]', array(
    'sanitize_callback' => 'sanitize_text_field',
    'default'           => $options['latest_products_sub_title'],       
    'transport'         => 'postMessage',
) );

$wp_customize->add_control( 'enlightme_pro_theme_options[latest_products_sub_title]', array(
    'label'             => esc_html__( 'SUb Title', 'enlightme-pro' ),
    'section'           => 'enlightme_pro_latest_products_section',
    'active_callback'   => 'enlightme_pro_is_latest_products_section_enable',
    'type'              => 'text',
) );

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
    $wp_customize->selective_refresh->add_partial( 'enlightme_pro_theme_options[latest_products_sub_title]', array(
        'selector'            => '#latest-products p.section-subtitle',
        'settings'            => 'enlightme_pro_theme_options[latest_products_sub_title]',
        'container_inclusive' => false,
        'fallback_refresh'    => true,
        'render_callback'     => 'enlightme_pro_latest_products_sub_title_partial',
    ) );
}

// latest_products title setting and control
$wp_customize->add_setting( 'enlightme_pro_theme_options[latest_products_title]', array(
    'sanitize_callback' => 'sanitize_text_field',
    'default'           => $options['latest_products_title'],
    'transport'         => 'postMessage',
) );

$wp_customize->add_control( 'enlightme_pro_theme_options[latest_products_title]', array(
    'label'             => esc_html__( 'Title', 'enlightme-pro' ),
    'section'           => 'enlightme_pro_latest_products_section',
    'active_callback'   => 'enlightme_pro_is_latest_products_section_enable',
    'type'              => 'text',
) );
// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
    $wp_customize->selective_refresh->add_partial( 'enlightme_pro_theme_options[latest_products_title]', array(
        'selector'            => '#latest-products h2.section-title',
        'settings'            => 'enlightme_pro_theme_options[latest_products_title]',
        'container_inclusive' => false,
        'fallback_refresh'    => true,
        'render_callback'     => 'enlightme_pro_latest_products_title_partial',
    ) );
}

$wp_customize->add_setting( 'enlightme_pro_theme_options[latest_products_count]', array(
    'default'           => $options['latest_products_count'],
    'sanitize_callback' => 'enlightme_pro_sanitize_number_range',
    'validate_callback' => 'enlightme_pro_validate_latest_products_count',
) );

$wp_customize->add_control( 'enlightme_pro_theme_options[latest_products_count]', array(
    'label'             => esc_html__( 'Number of Products', 'enlightme-pro' ),
    'description'       => esc_html__( 'Note: Min 1 & Max 30. Please input the valid number and save. Then refresh the page to see the change.', 'enlightme-pro' ),
    'section'           => 'enlightme_pro_latest_products_section',
    'active_callback'   => 'enlightme_pro_is_latest_products_section_enable',
    'type'              => 'number',
    'input_attrs'       => array(
        'min'   => 1,
        'max'   => 30,
        'style' => 'width: 100px;'
        ),
) );

$wp_customize->add_setting( 'enlightme_pro_theme_options[latest_products_content_type]', array(
    'default'           => $options['latest_products_content_type'],
    'sanitize_callback' => 'enlightme_pro_sanitize_select',
) );

$wp_customize->add_control( 'enlightme_pro_theme_options[latest_products_content_type]', array(
    'label'             => esc_html__( 'Content Type', 'enlightme-pro' ),
    'section'           => 'enlightme_pro_latest_products_section',
    'type'              => 'select',
    'active_callback'   => 'enlightme_pro_is_latest_products_section_enable',
    'choices'           => array(
        'product'           => esc_html__( 'Product', 'enlightme-pro' ),
        'product-category'  => esc_html__( 'Product Category', 'enlightme-pro' ),
        ),
) );

for( $i = 1 ; $i <= $options['latest_products_count']; $i++ ){

    $wp_customize->add_setting( 'enlightme_pro_theme_options[latest_products_content_product_' . $i . ']', array(
        'sanitize_callback' => 'enlightme_pro_sanitize_page',
    ) );

    $wp_customize->add_control( new Enlightme_Pro_Dropdown_Chooser( $wp_customize, 'enlightme_pro_theme_options[latest_products_content_product_' . $i . ']', array(
        'label'             => sprintf( esc_html__( 'Select Product %d', 'enlightme-pro' ), $i ),
        'section'           => 'enlightme_pro_latest_products_section',
        'choices'           => enlightme_pro_product_choices(),
        'active_callback'   => 'enlightme_pro_is_latest_products_section_content_product_enable',
    ) ) );
}

$wp_customize->add_setting(  'enlightme_pro_theme_options[latest_products_content_product_category]', array(
        'sanitize_callback' => 'absint',
    ) ) ;

$wp_customize->add_control( new Enlightme_Pro_Dropdown_Taxonomies_Control( $wp_customize,'enlightme_pro_theme_options[latest_products_content_product_category]', array(
    'label'             => esc_html__( 'Select Product Categories', 'enlightme-pro' ),
    'description'       => esc_html__( 'Note: Press CTRL and select multiple category. Latest four posts will be shown from each selected category', 'enlightme-pro' ),
    'section'           => 'enlightme_pro_latest_products_section',
    'taxonomy'          => 'product_cat',
    'type'              => 'dropdown-taxonomies',
    'active_callback'   => 'enlightme_pro_is_latest_products_section_content_product_category_enable',
) ) );
