<?php
 /*
 * 
 * Custom parameter types 
 * for visual composer
 * 
 */ 

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/*
 * Text slides
 */
function text_slides_settings_field($settings, $value) {
	ob_start();
	?>
	<div class="text_slides_block">
		<?php $txtslides = json_decode(base64_decode($value), true); ?>

		<ul class="text_slides">
			<?php if ( !$txtslides ) { ?>
			<li>
				<input name="txt1" class="text_slide_field" type="text" value="" placeholder="<?php esc_attr_e('Enter your text for this slide.', 'dvl-uxshop-shortcodes');?>" />
				<i class="fa fa-sort reorder"></i>
				<a href="#" class="remove" title="<?php esc_attr_e('Remove Item', 'dvl-uxshop-shortcodes'); ?>"><i class="fa fa-times-circle"></i></a>
			</li>
			<?php } else { ?>
			<?php foreach ($txtslides as $k => $v) { ?>
			<li>
				<input name="<?php echo esc_attr( $v['name'] ); ?>" class="text_slide_field" type="text" value="<?php echo esc_attr( $v['value'] ); ?>" />
				<i class="fa fa-sort reorder"></i>
				<a href="#" class="remove" title="<?php esc_attr_e('Remove Item', 'dvl-uxshop-shortcodes'); ?>"><i class="fa fa-times-circle"></i></a>
			</li>
			<?php } } ?>
		</ul>
		
		<input type="button" class="new_txt_slide_button" value="<?php esc_attr_e('Add new text slide', 'dvl-uxshop-shortcodes') ?>" />
		
		<input name="<?php echo esc_attr( $settings['param_name'] ); ?>" style="display: none;" class="wpb_vc_param_value wpb-textinput <?php echo esc_attr( $settings['param_name'] ).' '.esc_attr( $settings['type'] ); ?>_field" type="text" value="<?php echo esc_attr( $value ); ?>" />
	</div>
	
	
	<?php
	$output = ob_get_clean();
	return $output;
}


/*
 * 	Woo product select
 */
function woo_product_settings_field($settings, $value) {
	$cnt = '';
	
	$products = shrk_vc_get_products();
	ob_start();
	?>
	<div class="woo_products_selector_block">
		<fieldset>
			<select <?php echo ($settings['multiple']) == true ? 'multiple="multiple"' : ''; ?> data-placeholder="<?php esc_attr_e('Select an item', 'dvl-uxshop-shortcodes'); ?>" name="shrk_vc_woo_products" data-param-name="<?php echo esc_attr( $settings['param_name'] ); ?>" rows="6">
				<?php echo ($settings['multiple']) == true ? '' : '<option></option>'; ?>
				<?php 
				foreach ($products as $product_id => $product_name) {
					echo '<option value="'.esc_attr( $product_id ).'" '. shrk_helper_print_selected($value, $product_id, $settings['multiple']) .'>'.$product_name.'</option>';
				}
				?>
			</select>
			<?php 
				$val_arr = explode(',', $value);
				if ( $settings['multiple'] == true && !empty($value) && !empty($val_arr) ) { 
					$cnt = count($val_arr) > 0 ? count($val_arr)." selected" : "";
				} 
				echo '<span class="multi-count">' . esc_attr( $cnt ) . '</span>';
			?>
			<input name="<?php echo esc_attr( $settings['param_name'] ); ?>" <?php echo WP_DEBUG!=true?'style="display: none;"':''; ?> class="wpb_vc_param_value wpb-textinput <?php echo esc_attr( $settings['param_name'] ).' '.esc_attr( $settings['type'] ); ?>_field" type="text" value="<?php echo esc_attr( $value ); ?>" />
		</fieldset>
	</div>
	<?php
	$output = ob_get_clean();
	return $output;
}


/*
 * 	Woo categories select
 */
function woo_categories_settings_field($settings, $value) {
	$cnt = '';
	
	$product_cats = shrk_vc_get_product_categories();
	ob_start();
	?>
	<div class="woo_categories_selector_block">
		<fieldset>
			<select <?php echo ($settings['multiple']) == true ? 'multiple="multiple"' : ''; ?> data-placeholder="<?php esc_attr_e('Select an item', 'dvl-uxshop-shortcodes'); ?>" name="shrk_vc_woo_categories" data-param-name="<?php echo esc_attr( $settings['param_name'] ); ?>" rows="6">
				<?php echo ($settings['multiple']) == true ? '' : '<option></option>'; ?>
				<?php 
				foreach ($product_cats as $cat) {
					if ( $settings['output'] == 'slug' ) {
						$opt_val = $cat->slug;
					} else {
						$opt_val = $cat->term_id;
					}
					echo '<option value="'.esc_attr( $opt_val ).'" '. shrk_helper_print_selected($value, $opt_val, $settings['multiple']) .'>'.$cat->name.'</option>';
				}
				?>
			</select>
			<?php 
				$val_arr = explode(',', $value);
				if ( $settings['multiple'] == true && !empty($value) && !empty($val_arr) ) { 
					$cnt = count($val_arr) > 0 ? count($val_arr)." selected" : "";
				} 
				echo '<span class="multi-count">' . esc_attr( $cnt ) . '</span>';
			?>
			<input name="<?php echo esc_attr( $settings['param_name'] ); ?>" <?php echo WP_DEBUG!=true?'style="display: none;"':''; ?> class="wpb_vc_param_value wpb-textinput <?php echo esc_attr( $settings['param_name'] ).' '.esc_attr( $settings['type'] ); ?>_field" type="text" value="<?php echo esc_attr( $value ); ?>" />
		</fieldset>
	</div>
	<?php
	$output = ob_get_clean();
	return $output;
}

/**
 * Default theme's font family selelector
 */
function shrk_vc_field_font_family( $settings, $value ) {
	$fonts = $theme_fonts = array();

	if ( class_exists( 'ReduxTypekit' ) ) {
		$tk = new ReduxTypekit;
		$tk->get_theme_options();

		if ( !empty( $tk->kit_fonts_full ) ) {
			$fonts = $tk->kit_fonts_full;
		}
	}
	$theme_fonts = apply_filters( 'shrk_vc_theme_fonts', $fonts );

	ob_start();
	?>
	<div class=shrk_font_family_block">
		<fieldset>
			<select data-placeholder="<?php esc_attr_e('Select an item', 'dvl-uxshop-shortcodes'); ?>" name="<?php echo esc_attr( $settings['param_name'] ); ?>" data-param-name="<?php echo esc_attr( $settings['param_name'] ); ?>" class="wpb_vc_param_value wpb-textinput <?php echo esc_attr( $settings['param_name'] ).' '.esc_attr( $settings['type'] ); ?>_field">
				<?php echo '<option value="default">' . esc_html__('Default', 'dvl-uxshop-shortcodes') . '</option>'; ?>
				<?php 
				foreach ($theme_fonts as $slug => $theme_font) {
					if ( !empty( $theme_font['variations'] ) ) {
						foreach ($theme_font['variations'] as $variation) {
							$val = $slug . '|' . implode(':', $variation );
							echo '<option value="'.esc_attr( $val ).'" '. shrk_helper_print_selected($value, $val) .'>' . $theme_font['name'] . ' [' . implode(' ', $variation ) . ']</option>';
						}
					} else {
						echo '<option value="'.esc_attr( $slug ).'" '. shrk_helper_print_selected($value, $slug) .'>' . $theme_font['name'] . '</option>';
					}
				}
				?>
			</select>
		</fieldset>
	</div>
	<?php
	$output = ob_get_clean();
	return $output;
}

function shrk_vc_addShortcodeParams() {
	if ( function_exists('vc_add_shortcode_param') ) {
		vc_add_shortcode_param( 'woo_products', 'woo_product_settings_field', plugin_dir_url( __FILE__ ) . 'woo_select.js' );
		vc_add_shortcode_param( 'text_slides', 'text_slides_settings_field', plugin_dir_url( __FILE__ ) . 'text_slides.js' );
		vc_add_shortcode_param( 'woo_categories', 'woo_categories_settings_field', plugin_dir_url( __FILE__ ) . 'woo_select.js' );
		vc_add_shortcode_param( 'shrk_font_family', 'shrk_vc_field_font_family' );
	}
}
add_action( 'vc_load_default_params', 'shrk_vc_addShortcodeParams' );


function shrk_helper_print_selected($value, $current, $ismulti = false) {
	$selected = 'selected="selected"';
	
	if ( !$ismulti ) {
		if ( $value == $current ) {
			return $selected;
		}
	} else {
		$value_arr = explode(',', $value);
		if ( in_array($current, $value_arr) ) {
			return $selected;
		}
	}
	return '';
}

?>