<?php
function vegan_cafe_restaurant_blog_setting( $wp_customize ) {

$wp_customize->register_control_type( 'Vegan_Cafe_Restaurant_Control_Upgrade' );

$selective_refresh = isset( $wp_customize->selective_refresh ) ? 'postMessage' : 'refresh';
	$wp_customize->add_panel(
		'vegan_cafe_restaurant_frontpage_sections', array(
			'priority' => 1,
			'title' => esc_html__( 'Frontpage Sections', 'vegan-cafe-restaurant' ),
		)
	);
	
	/*=========================================
	Slider Section
	=========================================*/
	$wp_customize->add_section(
		'vegan_cafe_restaurant_slider_section', array(
			'title' => esc_html__( 'Slider Section', 'vegan-cafe-restaurant' ),
			'priority' => 3,
			'panel' => 'vegan_cafe_restaurant_frontpage_sections',
		)
	);

	// Slider Hide/ Show Setting // 
	$wp_customize->add_setting( 
		'vegan_cafe_restaurant_slider_setting' , 
			array(
			'default' => false,
			'sanitize_callback' => 'vegan_cafe_restaurant_sanitize_checkbox',
			'capability' => 'edit_theme_options',
			'priority' => 2,
		) 
	);
	$wp_customize->add_control(
	'vegan_cafe_restaurant_slider_setting', 
		array(
			'label'	      => esc_html__( 'Hide / Show Section', 'vegan-cafe-restaurant' ),
			'section'     => 'vegan_cafe_restaurant_slider_section',
			'settings'    => 'vegan_cafe_restaurant_slider_setting',
			'type'        => 'checkbox'
		) 
	);
	
	for ( $vegan_cafe_restaurant_count = 1; $vegan_cafe_restaurant_count <= 4; $vegan_cafe_restaurant_count++ ) {

		// Add color scheme setting and control.
		$wp_customize->add_setting( 'vegan_cafe_restaurant_slider_page' . $vegan_cafe_restaurant_count, array(
			'default'           => '',
			'sanitize_callback' => 'vegan_cafe_restaurant_sanitize_dropdown_pages'
		) );

		$wp_customize->add_control( 'vegan_cafe_restaurant_slider_page' . $vegan_cafe_restaurant_count, array(
			'label'    => __( 'Select Slide Image Page', 'vegan-cafe-restaurant' ),
			'section'  => 'vegan_cafe_restaurant_slider_section',
			'type'     => 'dropdown-pages'
		) );
	}

	// Slider Text
	$wp_customize->add_setting( 
    	'vegan_cafe_restaurant_slider_short_heading',
    	array(
			'capability'     	=> 'edit_theme_options',
			'sanitize_callback' => 'sanitize_text_field',
			'priority'      => 1,
		)
	);	

	$wp_customize->add_control( 
		'vegan_cafe_restaurant_slider_short_heading',
		array(
		    'label'   		=> __('Slider Top Text','vegan-cafe-restaurant'),
		    'section'		=> 'vegan_cafe_restaurant_slider_section',
			'type' 			=> 'text',
			'transport'         => $selective_refresh,
		)
	);

 	$wp_customize->add_setting('vegan_cafe_restaurant_banner_form_shortcode',array(
		'default'=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('vegan_cafe_restaurant_banner_form_shortcode',array(
		'label'	=> __('Add Contact Form Shortcode','vegan-cafe-restaurant'),
		'section'=> 'vegan_cafe_restaurant_slider_section',
		'type'=> 'text',
	));

	$wp_customize->add_setting( 'vegan_cafe_restaurant_upgrade_page_settings_11101',
	array(
		'sanitize_callback' => 'sanitize_text_field'
		)
	);
	$wp_customize->add_control( new Vegan_Cafe_Restaurant_Control_Upgrade(
		$wp_customize, 'vegan_cafe_restaurant_upgrade_page_settings_11101',
			array(
				'priority'      => 200,
				'section'       => 'vegan_cafe_restaurant_slider_section',
				'settings'      => 'vegan_cafe_restaurant_upgrade_page_settings_11101',
				'label'         => __( 'Vegan Cafe Restaurant Pro comes with additional features.', 'vegan-cafe-restaurant' ),
				'choices'       => array( __( '15+ Ready-Made Sections', 'vegan-cafe-restaurant' ), __( 'One-Click Demo Import', 'vegan-cafe-restaurant' ), __( 'WooCommerce Integrated', 'vegan-cafe-restaurant' ), __( 'Drag & Drop Section Reordering', 'vegan-cafe-restaurant' ),__( 'Advanced Typography Control', 'vegan-cafe-restaurant' ),__( 'Intuitive Customization Options', 'vegan-cafe-restaurant' ),__( '24/7 Support', 'vegan-cafe-restaurant' ), )
			)
		)
	); 

	// Add Explore Places Section
	$wp_customize->add_section('vegan_cafe_restaurant_projects', array(
	    'title'    => esc_html__('Explore Places Section', 'vegan-cafe-restaurant'),
	    'panel'    => 'vegan_cafe_restaurant_frontpage_sections',
	    'priority' => 3,
	));

	// Show / Hide Section
	$wp_customize->add_setting('vegan_cafe_restaurant_project_show_hide', array(
	    'default'           => true,
	    'sanitize_callback' => 'vegan_cafe_restaurant_sanitize_checkbox'
	));
	$wp_customize->add_control('vegan_cafe_restaurant_project_show_hide', array(
	    'type'    => 'checkbox',
	    'label'   => esc_html__('Show / Hide Section', 'vegan-cafe-restaurant'),
	    'section' => 'vegan_cafe_restaurant_projects',
	));

	// Section Heading
	$wp_customize->add_setting('vegan_cafe_restaurant_projects_main_heading', array(
	    'capability'        => 'edit_theme_options',
	    'sanitize_callback' => 'sanitize_text_field',
	    'priority'          => 1,
	));
	$wp_customize->add_control('vegan_cafe_restaurant_projects_main_heading', array(
	    'label'    => esc_html__('Add Section Heading', 'vegan-cafe-restaurant'),
	    'section'  => 'vegan_cafe_restaurant_projects',
	    'type'     => 'text',
	));

	// Number of Tabs
	$wp_customize->add_setting('vegan_cafe_restaurant_projects_number', array(
	    'default'           => '4',
	    'sanitize_callback' => 'absint',
	));
	$wp_customize->add_control('vegan_cafe_restaurant_projects_number', array(
	    'label'   => esc_html__('Number of Tabs to Show', 'vegan-cafe-restaurant'),
	    'section' => 'vegan_cafe_restaurant_projects',
	    'type'    => 'number',
	    'input_attrs' => array(
	        'step' => 1,
	        'min'  => 1
	    ),
	));

	// Dynamic Tabs Loop
	for ($vegan_cafe_restaurant_j = 1; $vegan_cafe_restaurant_j <= 4; $vegan_cafe_restaurant_j++) {
    // Add Tab Name
    $wp_customize->add_setting("vegan_cafe_restaurant_tab_name_{$vegan_cafe_restaurant_j}", array(
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control("vegan_cafe_restaurant_tab_name_{$vegan_cafe_restaurant_j}", array(
        /* translators: %s: Tab Count. */
		'label'   => sprintf(esc_html__('Add Tab %d Name', 'vegan-cafe-restaurant'), $vegan_cafe_restaurant_j),
        'section' => 'vegan_cafe_restaurant_projects',
        'type'    => 'text',
    ));

    // Get WooCommerce Product Categories
	$categories = get_terms(array(
	    'taxonomy'   => 'product_cat',
	    'hide_empty' => false,
	));

	// Initialize category list with a default option
	$vegan_cafe_restaurant_product_cats = array('' => 'Select');

	// Ensure $categories is a valid array of term objects
	if (!is_wp_error($categories) && !empty($categories) && is_array($categories)) {
	    foreach ($categories as $category) {
	        if (is_object($category) && isset($category->slug, $category->name)) {
	            $vegan_cafe_restaurant_product_cats[$category->slug] = $category->name;
	        }
	    }
	}

    // Select Product Category for Each Tab
    $wp_customize->add_setting("vegan_cafe_restaurant_projects_category_{$vegan_cafe_restaurant_j}", array(
        'default'           => 'product_cat1',
        'sanitize_callback' => 'sanitize_text_field',
    ));
    $wp_customize->add_control("vegan_cafe_restaurant_projects_category_{$vegan_cafe_restaurant_j}", array(
        'type'    => 'select',
        'choices' => $vegan_cafe_restaurant_product_cats,
        /* translators: %s: Tab Count. */
        'label'   => sprintf(esc_html__('Select Product Category for Tab %d', 'vegan-cafe-restaurant'), $vegan_cafe_restaurant_j),
        'section' => 'vegan_cafe_restaurant_projects',
    ));

    // Number of Products in Tab
    $wp_customize->add_setting("vegan_cafe_restaurant_products_to_show_{$vegan_cafe_restaurant_j}", array(
        'sanitize_callback' => 'absint',
        'default'           => 4,
    ));
    $wp_customize->add_control("vegan_cafe_restaurant_products_to_show_{$vegan_cafe_restaurant_j}", array(
        /* translators: %d: Tab Count. */
        'label'   => sprintf(esc_html__('Set Number of Products in Tab %d', 'vegan-cafe-restaurant'), $vegan_cafe_restaurant_j),
        'section' => 'vegan_cafe_restaurant_projects',
        'type'    => 'number',
        'input_attrs' => array(
            'step' => 1,
            'min'  => 1,
        ),
    ));

    // Loop through Products
    for ($vegan_cafe_restaurant_p = 1; $vegan_cafe_restaurant_p <= 4; $vegan_cafe_restaurant_p++) {
        // Number of People for Product
        $wp_customize->add_setting("vegan_cafe_restaurant_no_people_{$vegan_cafe_restaurant_j}_{$vegan_cafe_restaurant_p}", array(
            'sanitize_callback' => 'sanitize_text_field',
            'default'           => '56 - people',
        ));
        $wp_customize->add_control("vegan_cafe_restaurant_no_people_{$vegan_cafe_restaurant_j}_{$vegan_cafe_restaurant_p}", array(
			/* translators: %d: Product Count. */
			'label' => sprintf(esc_html__('Add Number of People for Product %1$d in Tab %2$d', 'vegan-cafe-restaurant'), $vegan_cafe_restaurant_p, $vegan_cafe_restaurant_j),
            'section' => 'vegan_cafe_restaurant_projects',
            'type'    => 'text',
            ),
        );

        // Location for Product
        $wp_customize->add_setting("vegan_cafe_restaurant_projects_location_{$vegan_cafe_restaurant_j}_{$vegan_cafe_restaurant_p}", array(
            'default'           => 'Hawaii',
            'sanitize_callback' => 'sanitize_text_field',
        ));
        $wp_customize->add_control("vegan_cafe_restaurant_projects_location_{$vegan_cafe_restaurant_j}_{$vegan_cafe_restaurant_p}", array(
			/* translators: %d: Product Count. */
			'label'   => sprintf(esc_html__('Location for Product %1$d in Tab %2$d', 'vegan-cafe-restaurant'), $vegan_cafe_restaurant_p, $vegan_cafe_restaurant_j),
            'section' => 'vegan_cafe_restaurant_projects',
            'type'    => 'text',
        ));
    }

	$wp_customize->add_setting( 'vegan_cafe_restaurant_upgrade_page_settings_1110',
	array(
		'sanitize_callback' => 'sanitize_text_field'
		)
	);
	$wp_customize->add_control( new Vegan_Cafe_Restaurant_Control_Upgrade(
		$wp_customize, 'vegan_cafe_restaurant_upgrade_page_settings_1110',
			array(
				'priority'      => 200,
				'section'       => 'vegan_cafe_restaurant_projects',
				'settings'      => 'vegan_cafe_restaurant_upgrade_page_settings_1110',
				'label'         => __( 'Vegan Cafe Restaurant Pro comes with additional features.', 'vegan-cafe-restaurant' ),
				'choices'       => array( __( '15+ Ready-Made Sections', 'vegan-cafe-restaurant' ), __( 'One-Click Demo Import', 'vegan-cafe-restaurant' ), __( 'WooCommerce Integrated', 'vegan-cafe-restaurant' ), __( 'Drag & Drop Section Reordering', 'vegan-cafe-restaurant' ),__( 'Advanced Typography Control', 'vegan-cafe-restaurant' ),__( 'Intuitive Customization Options', 'vegan-cafe-restaurant' ),__( '24/7 Support', 'vegan-cafe-restaurant' ), )
			)
		)
	); 
}

}

add_action( 'customize_register', 'vegan_cafe_restaurant_blog_setting' );

// service selective refresh
function vegan_cafe_restaurant_blog_section_partials( $wp_customize ){	
	// blog_title
	$wp_customize->selective_refresh->add_partial( 'blog_title', array(
		'selector'            => '.home-blog .title h6',
		'settings'            => 'blog_title',
		'render_callback'  => 'vegan_cafe_restaurant_blog_title_render_callback',
	
	) );
	
	// blog_subtitle
	$wp_customize->selective_refresh->add_partial( 'blog_subtitle', array(
		'selector'            => '.home-blog .title h2',
		'settings'            => 'blog_subtitle',
		'render_callback'  => 'vegan_cafe_restaurant_blog_subtitle_render_callback',
	
	) );
	
	// blog_description
	$wp_customize->selective_refresh->add_partial( 'blog_description', array(
		'selector'            => '.home-blog .title p',
		'settings'            => 'blog_description',
		'render_callback'  => 'vegan_cafe_restaurant_blog_description_render_callback',
	
	) );	
	}

add_action( 'customize_register', 'vegan_cafe_restaurant_blog_section_partials' );

// blog_title
function vegan_cafe_restaurant_blog_title_render_callback() {
	return get_theme_mod( 'blog_title' );
}

// blog_subtitle
function vegan_cafe_restaurant_blog_subtitle_render_callback() {
	return get_theme_mod( 'blog_subtitle' );
}

// service description
function vegan_cafe_restaurant_blog_description_render_callback() {
	return get_theme_mod( 'blog_description' );
}