<?php
/**
 * Product Section
 *
 * @package handmade_craft
 */

$wp_customize->add_section(
	'handmade_craft_products_section',
	array(
		'panel'    => 'handmade_craft_front_page_options',
		'title'    => esc_html__( 'Product Section', 'handmade-craft' ),
		'priority' => 10,
	)
);

// Product Section - Enable Section.
$wp_customize->add_setting(
	'handmade_craft_enable_product_section',
	array(
		'default'           => false,
		'sanitize_callback' => 'handmade_craft_sanitize_switch',
	)
);

$wp_customize->add_control(
	new Handmade_Craft_Toggle_Switch_Custom_Control(
		$wp_customize,
		'handmade_craft_enable_product_section',
		array(
			'label'    => esc_html__( 'Enable Product Section', 'handmade-craft' ),
			'section'  => 'handmade_craft_products_section',
			'settings' => 'handmade_craft_enable_product_section',
		)
	)
);

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

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

$wp_customize->add_control(
	'handmade_craft_trending_product_heading',
	array(
		'label'           => esc_html__( 'Heading', 'handmade-craft' ),
		'section'         => 'handmade_craft_products_section',
		'settings'        => 'handmade_craft_trending_product_heading',
		'type'            => 'text',
		'active_callback' => 'handmade_craft_is_product_section_enabled',
	)
);

if(class_exists('woocommerce')){

$handmade_craft_args = array(
	'type'                     => 'product',
	'child_of'                 => 0,
	'parent'                   => '',
	'orderby'                  => 'term_group',
	'order'                    => 'ASC',
	'hide_empty'               => false,
	'hierarchical'             => 1,
	'number'                   => '',
	'taxonomy'                 => 'product_cat',
	'pad_counts'               => false
);
$handmade_craft_categories = get_categories($handmade_craft_args);
$handmade_craft_cat_posts = array();
$handmade_craft_m = 0;
$handmade_craft_cat_posts[]='Select';
foreach($handmade_craft_categories as $handmade_craft_category){
	if($handmade_craft_m==0){
		$handmade_craft_default = $handmade_craft_category->slug;
		$handmade_craft_m++;
	}
	$handmade_craft_cat_posts[$handmade_craft_category->slug] = $handmade_craft_category->name;
}

$wp_customize->add_setting('handmade_craft_trending_product_category',array(
	'default'	=> 'uncategorized',
	'sanitize_callback' => 'handmade_craft_sanitize_select',
));
$wp_customize->add_control('handmade_craft_trending_product_category',array(
	'type'    => 'select',
	'choices' => $handmade_craft_cat_posts,
	'label' => __('Select category to display products ','handmade-craft'),
	'section' => 'handmade_craft_products_section',
	'active_callback' => 'handmade_craft_is_product_section_enabled',
));
}

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

$wp_customize->add_control(
	'handmade_craft_product_view_button_text',
	array(
		'label'           => esc_html__( 'Button Text', 'handmade-craft' ),
		'section'         => 'handmade_craft_products_section',
		'settings'        => 'handmade_craft_product_view_button_text',
		'type'            => 'text',
		'active_callback' => 'handmade_craft_is_product_section_enabled',
	)
);

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

$wp_customize->add_control(
	'handmade_craft_product_view_button_url',
	array(
		'label'           => esc_html__( 'Button Link', 'handmade-craft' ),
		'section'         => 'handmade_craft_products_section',
		'settings'        => 'handmade_craft_product_view_button_url',
		'type'            => 'text',
		'active_callback' => 'handmade_craft_is_product_section_enabled',
	)
);