<?php
/**
 * Widget: Testimonail Widget
 *
 * Widget show the posts from selected categories in carousel layouts.
 *
 * @package MatrixAddons
 * @subpackage trainify
 * @since 1.0.0
 */

if( !class_exists( 'Trainify_Testimonial_Section_Widget' ) ):

	class Trainify_Testimonial_Section_Widget extends TrainifyFramework_Master_Widget{

		/**
		 * Register widget with WordPress.
		 */
		public function __construct(){

			$widget_ops = array(
				'classname'   => 'trainify_testimonial_section_widget tff-full-widget',
				'description' => esc_html__( 'Displays testimonial from repeater data.', 'trainify' ),
				'customize_selective_refresh' => true,
			);
			parent::__construct( 'trainify_testimonial_section_widget', esc_html__( 'TF - Testimonial Section', 'trainify' ), $widget_ops );

		}

		/**
		 * Helper function that holds widget fields
		 * Array is used in update and form functions
		 */
		public function widget_fields( $instance = array() ) {

			$fields = array(
				'trainify_framework_widget_tab'       => array(
					'trainify_framework_widget_field_name'     => 'trainify_framework_widget_tab',
					'trainify_framework_widget_field_title'    => esc_html__( 'General', 'trainify' ),
					'trainify_framework_widget_field_default'  => 'general',
					'trainify_framework_widget_field_type'     => 'tabgroup',
					'trainify_framework_widget_field_options'  => array(
						'general'=>array(
							'trainify_framework_widget_field_title'=>esc_html__('General', 'trainify'),
							'trainify_framework_widget_field_options'=> array(
								'title' => array(
									'trainify_framework_widget_field_name'        => 'title',
									'trainify_framework_widget_field_title'       => esc_html__( 'Block title', 'trainify' ),
									'trainify_framework_widget_field_default'  => esc_html__( 'Client Testimonials', 'trainify' ),
									'trainify_framework_widget_field_description' => esc_html__( 'Enter your block title. (Optional - Leave blank to hide title.)', 'trainify' ),
									'trainify_framework_widget_field_type'  => 'text'
								),
								'title_target'    => array(
									'trainify_framework_widget_field_name'     => 'title_target',
									'trainify_framework_widget_field_wraper'   => 'title-target',
									'trainify_framework_widget_field_title'    => esc_html__( 'Title Target', 'trainify' ),
									'trainify_framework_widget_field_default'  => '',
									'trainify_framework_widget_field_type'     => 'select',
									'trainify_framework_widget_field_options'  => trainify_framework_link_target(),
									'trainify_framework_widget_field_relation' => array(
										'exist' => array(
											'show_fields'   => array(
												'title-link', 
											),
										),
										'empty' => array(
											'hide_fields'   => array(
												'title-link', 
											),
										),
									),
								),
								'title_link'    => array(
									'trainify_framework_widget_field_name'     => 'title_link',
									'trainify_framework_widget_field_wraper'   => 'title-link',
									'trainify_framework_widget_field_title'    => esc_html__( 'Title link', 'trainify' ),
									'trainify_framework_widget_field_default'  => '',
									'trainify_framework_widget_field_type'     => 'text',
								),
								'background_image'    => array(
									'trainify_framework_widget_field_name'     => 'background_image',
									'trainify_framework_widget_field_wraper'   => 'background-section',
									'trainify_framework_widget_field_title'    => esc_html__( 'Background Image', 'trainify' ),
									'trainify_framework_widget_field_default'  => '',
									'trainify_framework_widget_field_type'     => 'upload',
								),
								'background_overlay' => array(
									'trainify_framework_widget_field_name'     => 'background_overlay',
									'trainify_framework_widget_field_title'    => esc_html__( 'Background Overlay', 'trainify' ),
									'trainify_framework_widget_field_default'  => '',
									'trainify_framework_widget_field_type'     => 'color',
								),
								'overlay_opacity' => array(
									'trainify_framework_widget_field_name'     => 'overlay_opacity',
									'trainify_framework_widget_field_title'    => esc_html__( 'Overlay Opacity', 'trainify' ),
									'trainify_framework_widget_field_default'  => '0.3',
									'trainify_framework_widget_field_type'     => 'number',
									'trainify_framework_widget_field_attrs'   => array(
										'min'=> 0,
										'max'=> 1,
										'step' => 0.1,
									),
								),
								'testimonial_list'         => array(
									'trainify_framework_widget_field_name'     => 'testimonial_list',
									'trainify_framework_widget_field_title'    => esc_html__( 'Testimonial List', 'trainify' ),
									'trainify_framework_widget_field_type'     => 'repeater',
									'trainify_framework_widget_description'    => esc_html__('To add client testimonial click to add testimonial.', 'trainify'),
									'tff_repeater_row_title'    => esc_html__('Testimonial Details', 'trainify'),
									'tff_repeater_addnew_label' => esc_html__('Add Testimonial', 'trainify'),
									'trainify_framework_widget_field_wraper'   => 'testimonial-list-wrapper',
									'trainify_framework_widget_field_default'  => array(
										array(
											'client_name'		=> esc_html__( 'Name One', 'trainify' ),
											'client_position'	=> esc_html__( 'Position One', 'trainify' ),
											'client_words'		=> esc_html__( 'Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.', 'trainify' ),
											'client_image'		=> '',
										),
										array(
											'client_name'		=> esc_html__( 'Name Two', 'trainify' ),
											'client_position'	=> esc_html__( 'Position Two', 'trainify' ),
											'client_words'		=> esc_html__( 'Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.', 'trainify' ),
											'client_image'		=> '',
										),
										array(
											'client_name'		=> esc_html__( 'Nmae Three', 'trainify' ),
											'client_position'	=> esc_html__( 'Position Three', 'trainify' ),
											'client_words'		=> esc_html__( 'Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.', 'trainify' ),
											'client_image'		=> '',
										),
										array(
											'client_name'		=> esc_html__( 'Name Four', 'trainify' ),
											'client_position'	=> esc_html__( 'Position Four', 'trainify' ),
											'client_words'		=> esc_html__( 'Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.', 'trainify' ),
											'client_image'		=> '',
										),
									),
									'trainify_framework_widget_field_options'  => array(
										'client_name' => array(
											'trainify_framework_widget_field_name'     => 'client_name',
											'trainify_framework_widget_field_title'    => esc_html__( 'Client Name', 'trainify' ),
											'trainify_framework_widget_field_default'  => '',
											'trainify_framework_widget_field_type'     => 'text',
										),
										'client_position' => array(
											'trainify_framework_widget_field_name'     => 'client_position',
											'trainify_framework_widget_field_title'    => esc_html__( 'Client Position', 'trainify' ),
											'trainify_framework_widget_field_default'  => '',
											'trainify_framework_widget_field_type'     => 'text',
										),
										
										'client_words' => array(
											'trainify_framework_widget_field_name'     => 'client_words',
											'trainify_framework_widget_field_title'    => esc_html__( 'Client Words', 'trainify' ),
											'trainify_framework_widget_field_default'  => '',
											'trainify_framework_widgets_row'		   => 7,
											'trainify_framework_widget_field_type'     => 'textarea',
										),
										'client_image' => array(
											'trainify_framework_widget_field_name'     => 'client_image',
											'trainify_framework_widget_field_title'    => esc_html__( 'Client Image', 'trainify' ),
											'trainify_framework_widget_field_default'  => '',
											'trainify_framework_widget_field_type'     => 'upload',
										),
									),
								),
							)
						),
						'layout'=>array(
							'trainify_framework_widget_field_title'=>esc_html__('Layout', 'trainify'),
							'trainify_framework_widget_field_options'=> array(
								'no_of_column' => array(
									'trainify_framework_widget_field_name'          => 'no_of_column',
									'trainify_framework_widget_field_title'         => esc_html__( 'No of column', 'trainify' ),
									'trainify_framework_widget_field_default'  => 2,
									'trainify_framework_widget_field_type'   => 'select',
									'trainify_framework_widget_field_options' => array(
										'1' => esc_html__('Column One', 'trainify'),
										'2' => esc_html__('Column Two', 'trainify'),
										'3' => esc_html__('Column Three', 'trainify'),
									),
								),
								'testimonial_layout' => array(
									'trainify_framework_widget_field_name'          => 'testimonial_layout',
									'trainify_framework_widget_field_title'         => esc_html__( 'Testimonial Layout', 'trainify' ),
									'trainify_framework_widget_field_type'   => 'select',
									'trainify_framework_widget_field_default'  => 'layout2',
									'trainify_framework_widget_field_options' => array(
										'layout1' => esc_html__('Layout One', 'trainify'),
										'layout2' => esc_html__('Layout Two', 'trainify'),
									),
								),
							)
						),
					)
				)
			);

			$widget_fields_key = 'fields_'.$this->id_base;
			$widgets_fields = apply_filters( $widget_fields_key, $fields );
			return $widgets_fields;

		}

		/**
		 * Front-end display of widget.
		 *
		 * @see WP_Widget::widget()
		 *
		 * @param array $args Widget arguments.
		 * @param array $instance Saved values from database.
		 */
		public function widget( $args, $instance ) {
		
			if ( empty( $instance ) ) {
				return;
			}

			$before_title = (isset($args['before_title'])) ? $args['before_title'] : '';
			$after_title = (isset($args['after_title'])) ? $args['after_title'] : '';
			$before_widget = (isset($args['before_widget'])) ? $args['before_widget'] : '';
			$after_widget = (isset($args['after_widget'])) ? $args['after_widget'] : '';

			/*
			 * General Tab
			 */
			$title  = isset( $instance['title'] ) ? esc_html( $instance['title'] ) : '';
			$title  = apply_filters( 'widget_title', $title, $instance, $this->id_base );
			$title_target      = isset( $instance['title_target'] ) ? esc_html( $instance['title_target'] ) : '';
			$title_link  = isset( $instance['title_link'] ) ? esc_html( $instance['title_link'] ) : '';
			$total_posts_show  = isset( $instance['total_posts_show'] ) ? absint($instance['total_posts_show']) : 6;
			$background_image  = isset( $instance['background_image'] ) ? esc_url($instance['background_image']) : '';
			$background_overlay  = isset( $instance['background_overlay'] ) ? esc_url($instance['background_overlay']) : '';
			$overlay_opacity  = isset( $instance['overlay_opacity'] ) ? esc_attr($instance['overlay_opacity']) : '';			
			$testimonial_list  = isset( $instance['testimonial_list'] ) ? $instance['testimonial_list'] : array();

            /*
			 * Layout Tab
			 */
            $testimonial_layout  = isset( $instance['testimonial_layout'] ) ? $instance['testimonial_layout'] : 'layout2';
            $no_of_column  = isset( $instance['no_of_column'] ) ? absint($instance['no_of_column']) : 2;
            trainify_framework_before_widget($args);
            ?>
            <div class="trainify-testimonial-wrapper" style="background-image:url('<?php echo esc_url($background_image); ?>');">
            	<span class="testimonial-overlay" style="background-color: <?php echo esc_attr($background_overlay); ?>; opacity: <?php esc_attr($overlay_opacity); ?>;">&nbsp;</span>
            	<div class="tff-container">
            		<?php
            		$title_args = array(
            			'title' => $title,
            			'title_target'	=> $title_target,
            			'title_link'	=> $title_link,
            			'before_title'	=> $before_title,
            			'after_title'	=> $after_title
            		);
            		do_action('trainify_framework_widget_title', $title_args);

            		$testimonial_class = 'column'.$no_of_column.' '.$testimonial_layout;

            		$testimonial_list = apply_filters( 'trainify_widget_testimonials_list', $testimonial_list, $instance, $args, $this );

            		$this->testimonials_list_template( $testimonial_list, $testimonial_class, $instance, $args );
            		
            		?>
            	</div>
            </div>
            <?php
            trainify_framework_after_widget($args);

        }

        public function testimonials_list_template($testimonial_list, $testimonial_class, $instance, $args ){
        	if($testimonial_list):
        		?>
        		<ul class="trainify-testimonial-list <?php echo esc_attr( $testimonial_class ); ?>">
        			<?php 
        			foreach($testimonial_list as $testimonial_detail){
        				$client_name = (isset($testimonial_detail['client_name'])) ? $testimonial_detail['client_name'] : '';
        				$client_position = (isset($testimonial_detail['client_position'])) ? $testimonial_detail['client_position'] : '';
        				$client_words = (isset($testimonial_detail['client_words'])) ? $testimonial_detail['client_words'] : '';
        				$client_image = (isset($testimonial_detail['client_image'])) ? $testimonial_detail['client_image'] : '';
        				?>
        				<li>
        					<div class="testimonial-container">
        						<div class="client-words">
        							<p><?php echo esc_textarea($client_words); ?></p>
        						</div>
        						<div class="client-logo-details-wrap">
        							<figure class="client-logo">
        								<img src="<?php echo esc_url($client_image); ?>" title="" alt=""/>
        							</figure>
        							<div class="client-details">
        								<span class="client-name"><?php echo esc_html($client_name); ?></span>
        								<span class="client-position"><?php echo esc_html($client_position); ?></span>
        							</div>
        						</div>
        					</div>
        				</li>
        			<?php } ?>
        		</ul>
        		<?php
        	endif;
        }

    }

endif;