<?php

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

function shrk_sc_slabtext($atts) {
	$el_style = '';

	extract( shortcode_atts(array(
		"text_rows" => '',
		"max_width" => '',
		"style" => 'slab-style-1',
		"el_class" => '',
	), $atts) );  

	wp_enqueue_script( 'jquery-slabtext' );

	if ( trim($text_rows) != '' ) {
		$txt_rows = json_decode(base64_decode($text_rows), true);
	}

	if ( !empty($max_width) ) {
		$el_style = 'max-width:' . esc_attr( $max_width ) . ';';
	}

	$output = '';
	$output .= '<div class="txt-slab-wrapper ' . esc_attr( $el_class ) . ' ' . esc_attr( $style ) . '" style="' . esc_attr( $el_style ) . '">';
	$output .= '<div class="txt-slab">';

	if ( !empty( $txt_rows ) ) {
		foreach ($txt_rows as $key => $txt_row) {
			$output .= '<span class="slabtext">' . do_shortcode( $txt_row['value'] ) . '</span>';    
		}
	}

	$output .= '</div>';
	$output .= '</div>';

	return $output;

}

add_action( 'init', function () {
	add_shortcode('shrk_slabtext', 'shrk_sc_slabtext');
});

add_filter( 'shrk_filter_vc_mappings', function ( $shrk_vc_shortcodes ) {

	$shrk_vc_shortcodes["shrk_slabtext"] =  array(
		"name" => esc_html__("Slab text", 'dvl-uxshop-shortcodes'), 
		"base" => "shrk_slabtext",
		"category" => esc_html__('Theme Specific', 'dvl-uxshop-shortcodes'),
		"icon" => "icon-wpb-shrk",
		"show_settings_on_create" => true,
		"params" => array(
			array(
				"type" => "text_slides",
				"class" => "",
				"heading" => esc_html__("Add / remove text rows", 'dvl-uxshop-shortcodes'),
				"param_name" => "text_rows",
				"value" => ""
				),
			array(
				"type" => "textfield",
				"heading" => esc_html__("Container max width", 'dvl-uxshop-shortcodes'),
				"param_name" => "max_width",
				"description" => esc_html__("Enter a (CSS) value to override the container\'s max width. For example you can enter '500px' or '75%'.", 'dvl-uxshop-shortcodes')
				),
			array(
				"type" => "textfield",
				"heading" => esc_html__("Extra class name", 'dvl-uxshop-shortcodes'),
				"param_name" => "el_class",
				"description" => esc_html__("If you wish to style particular content element differently, then use this field to add a class name and then refer to it in your css file.", 'dvl-uxshop-shortcodes')
				),
			),
		'js_view' => 'shrkSlabTextView',
		'custom_markup' => '<h4 class="wpb_element_title"><i class="vc_element-icon icon-wpb-shrk"></i><span>'. esc_html__( "BigText: ", 'dvl-uxshop-shortcodes' ) .'</span><div class="text_rows" name="text_rows"></div></h4>',

	);

	return $shrk_vc_shortcodes;
} );

function shrk_sc_slabtext_scripts() {
	ob_start();
	?>(function ( $ ) {
		if ( typeof(vc) !== 'undefined' ) {
			window.shrkSlabTextView = vc.shortcode_view.extend( {
				changeShortcodeParams: function ( model ) {
					var params, txt, $res;
					params = model.get( 'params' );
					if ( _.isObject( params ) && _.isString( params.text_rows ) ) {
						txt  = json_decode(atob(params.text_rows));
						$res = this.$el.find( '> .wpb_element_wrapper [name="text_rows"]' );
						$res.html('');

						if ( txt ) {
							for (var i = 0; i < txt.length; i++) {
								$res.append( '<div>' + txt[i].value + '</div>' );
							}
						}
					}
				}
			});
		}
	})( window.jQuery );<?php 
	$script = ob_get_clean();

	wp_add_inline_script( 'vc-backend-min-js', $script );
}
add_action( 'admin_enqueue_scripts', 'shrk_sc_slabtext_scripts', 200 );

?>