<?php if ( ! defined( 'ABSPATH' ) ) {
	return;
}
/*
	Name: Vertical Tabs
	Description: Create and display a Vertical Tabs element
	Class: TH_VerticalTabs
	Category: content
	Level: 3
	Multiple: true
	Keywords: side, menu
*/

/**
 * Class TH_VerticalTabs
 *
 * Create and display a Vertical Tabs element
 *
 * @package  Kallyas
 * @category Page Builder
 * @author   Team Hogash
 * @since    4.0.0
 */
class TH_VerticalTabs extends ZnElements {
	private static $instance_nr = 0;

	public static function getName() {
		return __( 'Vertical Tabs', 'zn_framework' );
	}

	/**
	 * This method is used to display the output of the element.
	 * @return void
	 */
	public function element() {

		// Increment instance number
		self::$instance_nr++;
		$cached_instance_nr = self::$instance_nr;

		$options = $this->data['options'];

		$elm_classes   = array();
		$elm_classes[] = $uid = $this->data['uid'];
		$elm_classes[] = zn_get_element_classes( $options );

		$attributes = zn_get_element_attributes( $options );

		$color_scheme  = '' == $this->opt( 'element_scheme', '' ) ? zget_option( 'zn_main_style', 'color_options', false, 'light' ) : $this->opt( 'element_scheme', '' );
		$elm_classes[] = 'verttabs--' . $color_scheme;
		$elm_classes[] = 'element-scheme--' . $color_scheme;

		$elm_classes[] = $vt_style = $this->opt( 'vtabs_style', 'kl-style-2' );
		$elm_classes[] = 'vr-tabs-' . $vt_style;

		$single_tabs   = $this->opt( 'single_vertical_tab', array() );
		$tabsListCount = count( $single_tabs );
		$showTabOnHover = ( $this->opt('hover_tabs', '') === 'yes' ) ? true : false;

		if ( empty( $single_tabs ) ) {
			return;
		}

		// Begin render
		echo '<div class="vertical_tabs vr-tabs ' . implode( ' ', $elm_classes ) . ' clearfix" ' . $attributes . ' data-is-hover-active-enabled="' . $showTabOnHover . '">';
		echo '<div class="tabbable vr-tabs-tbb">';

		echo '<ul class=" vr-tabs-nav fixclear">';

		$content = '';

		for ($i = 0; $i < $tabsListCount; $i++ ) {
			$cls  = '';
			$icon = '';

			if ( 0 == $i ) {
				$cls = 'active';
			}

			$uniq_name = $uid . '_' . $cached_instance_nr . '_' . $i;

			// ICON CHECK
			if ( ! empty( $single_tabs[ $i ]['vts_tab_icon'] ) ) {
				$iconHolder = $single_tabs[ $i ]['vts_tab_icon'];
				$icon       = ! empty( $iconHolder['family'] ) ? '<span class="vr-tabs-nav-icon " ' . zn_generate_icon( $single_tabs[ $i ]['vts_tab_icon'] ) . '></span>' : '';
			}

			// Tab Handle
			echo '<li class="vr-tabs-nav-item ' . ( 'kl-style-1' == $vt_style ? 'text-custom-parent-act' : '' ) . ' ' . $cls . '">';
			echo '<a class="vr-tabs-nav-link text-custom-active text-custom-hover " href="#tabs_v2-pane' . $uniq_name . '" data-toggle="tab">';
			echo $icon;
			echo $single_tabs[$i]['vts_tab_title'];
			echo '</a>';

			echo '</li>';
		}

		echo '</ul>';

		echo '<div class="tab-content vr-tabs-content">';

		// foreach ( $single_tabs as $tab )
		for ( $i = 0; $i < $tabsListCount; $i++ ) {
			$cls       = $content = '';
			$uniq_name = $uid . '_' . $cached_instance_nr . '_' . $i;
			if ( 0 === $i ) {
				$cls = 'active';
			}

			// Convert the old content to PB elements
			if ( empty( $this->data['content'][ $i ] ) && ( ! empty( $single_tabs[ $i ]['vts_tab_c_title'] ) || ! empty( $single_tabs[ $i ]['vts_tab_c_content'] ) ) ) {
				$textbox                   = ZNB()->frontend->addModuleToLayout( 'TH_TextBox', array( 'stb_title' => $single_tabs[ $i ]['vts_tab_c_title'], 'stb_content' => $single_tabs[ $i ]['vts_tab_c_content'], 'stb_title_heading' => 'h4' ) );
				$column                    = ZNB()->frontend->addModuleToLayout( 'ZnColumn', array(), array( $textbox ), 'col-sm-12' );
				$this->data['content'][ $i ] = array( $column );
			}

			/* if the hover tabs is disabled add class fade in */
			$fade_cls = ( $this->opt('hover_tabs', '') === 'yes' ) ? '' : ' fade in ';
			echo ZNB()->utility->getElementContainer(array(
				'cssClasses' => 'tab-pane vr-tabs-tabpane ' . $fade_cls . $cls . ' row ' . $this->opt( 'gutter_size', '' ),
				'attributes' => array(
					'id' => 'tabs_v2-pane' . $uniq_name,
				),
			));

			if ( empty( $this->data['content'][$i] ) ) {
				$column                    = ZNB()->frontend->addModuleToLayout( 'ZnColumn', array(), array(), 'col-sm-12' );
				$this->data['content'][$i] = array( $column );
			}

			if ( ! empty( $this->data['content'][ $i ] ) ) {
				ZNB()->frontend->renderElements( $this->data['content'][ $i ] );
			}

			echo '</div>';
		}

		echo '</div>';

		echo '</div>';
		echo '</div>';
	}

	/**
	 * This method is used to retrieve the configurable options of the element.
	 * @return array The list of options that compose the element and then passed as the argument for the render() function
	 */
	public function options() {
		$extra_options = array(
			'name'           => __( 'Tabs', 'zn_framework' ),
			'description'    => __( 'Here you can add your desired tabs.', 'zn_framework' ),
			'id'             => 'single_vertical_tab',
			'std'            => '',
			'type'           => 'group',
			'add_text'       => __( 'Tab', 'zn_framework' ),
			'remove_text'    => __( 'Tab', 'zn_framework' ),
			'group_sortable' => true,
			'element_title'  => 'vts_tab_title',
			'subelements'    => array(
				array(
					'name'        => __( 'Tab Title', 'zn_framework' ),
					'description' => __( 'Please enter the desired title that will appear as tab.', 'zn_framework' ),
					'id'          => 'vts_tab_title',
					'std'         => '',
					'type'        => 'text',
				),
				array(
					'name'        => __( 'Tab icon', 'zn_framework' ),
					'description' => __( 'Select your desired icon that will appear on the left side of the tab title.', 'zn_framework' ),
					'id'          => 'vts_tab_icon',
					'std'         => '',
					'type'        => 'icon_list',
					'class'       => 'zn_full',
				),
			),
		);
		$uid = $this->data['uid'];

		$options = array(
			'has_tabs'  => true,
			'general'   => array(
				'title'   => 'General options',
				'options' => array(
					array(
						'name'        => __( 'Select style', 'zn_framework' ),
						'description' => __( 'Select the desired style for this element', 'zn_framework' ),
						'id'          => 'vtabs_style',
						'type'        => 'select',
						'std'         => 'kl-style-2',
						'options'     => array(
							'kl-style-1' => __( 'Style 1', 'zn_framework' ),
							'kl-style-2' => __( 'Style 2', 'zn_framework' ),
						),
					),
					array(
						'id'             => 'element_scheme',
						'name'           => 'Element Color Scheme',
						'description'    => 'Select the color scheme of this element',
						'type'           => 'select',
						'std'            => '',
						'options'        => array(
							''      => 'Inherit from Kallyas options > Color Options [Requires refresh]',
							'light' => 'Light (default)',
							'dark'  => 'Dark',
						),
						'live'        => array(
							'multiple' => array(
								array(
									'type'         => 'class',
									'css_class'    => '.' . $uid,
									'val_prepend'  => 'verttabs--',
								),
								array(
									'type'         => 'class',
									'css_class'    => '.' . $uid,
									'val_prepend'  => 'element-scheme--',
								),
							),
						),
					),
					array (
						'id'            => 'hover_tabs',
						'name'          => 'Show panels on tabs hover?',
						'description'   => 'If this option is enabled, the panels will show when you hover on the element tabs',
						"std"         => "no",
						'type'        => 'zn_radio',
						'options'        => array(
							'yes' => __( "Yes", 'zn_framework' ),
							'no' => __( "No", 'zn_framework' ),
						),
						'class'        => 'zn_radio--yesno',
					),
					$extra_options,
				),
			),

			'help' => znpb_get_helptab( array(
				'video'   => sprintf( '%s', esc_url( 'https://my.hogash.com/video_category/kallyas-wordpress-theme/#dSJi2pegFow' ) ),
				'docs'    => sprintf( '%s', esc_url( 'https://my.hogash.com/documentation/vertical-tabs/' ) ),
				'copy'    => $uid,
				'general' => true,
			)),

		);
		return $options;
	}

}
