<?php
/*
 * Elementor Confin Testimonial Widget
 * Author & Copyright: themexshaper
*/

namespace Elementor;

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

class Confin_Testimonial extends Widget_Base{

	/**
	 * Retrieve the widget name.
	*/
	public function get_name(){
		return 'tmx-confin_testimonial';
	}

	/**
	 * Retrieve the widget title.
	*/
	public function get_title(){
		return esc_html__( 'Testimonial', 'confin-core' );
	}

	/**
	 * Retrieve the widget icon.
	*/
	public function get_icon() {
		return 'eicon-testimonial';
	}

	/**
	 * Retrieve the list of categories the widget belongs to.
	*/
	public function get_categories() {
		return ['themexshaper-category'];
	}

	/**
	 * Retrieve the list of scripts the Confin Testimonial widget depended on.
	 * Used to set scripts dependencies required to run the widget.
	*/
	public function get_script_depends() {
		return ['tmx-confin_testimonial'];
	}
	
	/**
	 * Register Confin Testimonial widget controls.
	 * Adds different input fields to allow the user to change and customize the widget settings.
	*/
	protected function register_controls(){
		
		$this->start_controls_section(
			'section_testimonial',
			[
				'label' => esc_html__( 'Testimonial Options', 'confin-core' ),
			]
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'testimonial_title',
			[
				'label' => esc_html__( 'Testimonial Title Text', 'confin-core' ),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__( 'Title Text', 'confin-core' ),
				'placeholder' => esc_html__( 'Type title text here', 'confin-core' ),
				'label_block' => true,
			]
		);
		$repeater->add_control(
			'testimonial_subtitle',
			[
				'label' => esc_html__( 'Testimonial Sub Title', 'confin-core' ),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__( 'Testimonial Sub Title', 'confin-core' ),
				'placeholder' => esc_html__( 'Type testimonial Sub title here', 'confin-core' ),
				'label_block' => true,
			]
		);
		$repeater->add_control(
			'testimonial_content',
			[
				'label' => esc_html__( 'Testimonial Content', 'confin-core' ),
				'type' => Controls_Manager::TEXTAREA,
				'default' => esc_html__( 'Testimonial Content', 'confin-core' ),
				'placeholder' => esc_html__( 'Type testimonial Content here', 'confin-core' ),
				'label_block' => true,
			]
		);
	  $repeater->add_control(
			'bg_image',
			[
				'label' => esc_html__( 'Testimonial Image', 'confin-core' ),
				'type' => Controls_Manager::MEDIA,
				'default' => [
					'url' => Utils::get_placeholder_image_src(),
				],
				
			]
		);
		$this->add_control(
			'testimonialItems_groups',
			[
				'label' => esc_html__( 'Testimonial Items', 'confin-core' ),
				'type' => Controls_Manager::REPEATER,
				'default' => [
					[
						'testimonial_title' => esc_html__( 'Testimonial', 'confin-core' ),
					],
					
				],
				'fields' =>  $repeater->get_controls(),
				'title_field' => '{{{ testimonial_title }}}',
			]
		);
		$this->end_controls_section();// end: Section
		
	
		// Title
		$this->start_controls_section(
			'section_title_style',
			[
				'label' => esc_html__( 'Title', 'confin-core' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'label' => esc_html__( 'Typography', 'confin-core' ),
				'name' => 'confin_title_typography',
				'selector' => '{{WRAPPER}} .testimonials-section .client-info h5',
			]
		);
		$this->add_control(
			'title_color',
			[
				'label' => esc_html__( 'Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .testimonials-section .client-info h5' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'title_padding',
			[
				'label' => __( 'Title Padding', 'confin-core' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .testimonials-section .client-info h5' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		$this->end_controls_section();// end: Section
	
		// SubTitle
		$this->start_controls_section(
			'section_sutitle_style',
			[
				'label' => esc_html__( 'SubTitle', 'confin-core' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'label' => esc_html__( 'Typography', 'confin-core' ),
				'name' => 'confin_subtitle_typography',
				'selector' => '{{WRAPPER}} .testimonials-section .client-info h5 + span',
			]
		);
		$this->add_control(
			'sutitle_color',
			[
				'label' => esc_html__( 'Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .testimonials-section .client-info h5 + span' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'sutitle_padding',
			[
				'label' => esc_html__( 'Title Padding', 'confin-core' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .testimonials-section .client-info h5 + span' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		$this->end_controls_section();// end: Section

		
		// Content
		$this->start_controls_section(
			'section_content_style',
			[
				'label' => esc_html__( 'Content', 'confin-core' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'label' => esc_html__( 'Typography', 'confin-core' ),
				'name' => 'ntrsv_content_typography',
				'selector' => '{{WRAPPER}} .testimonials-section .grid .quote p',
			]
		);
		$this->add_control(
			'content_color',
			[
				'label' => esc_html__( 'Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .testimonials-section .grid .quote p' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'content_bg_color',
			[
				'label' => esc_html__( 'Background Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .testimonials-section .grid .quote' => 'background-color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'content_padding',
			[
				'label' => esc_html__( 'Content Padding', 'confin-core' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .testimonials-section .grid .quote p' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		$this->end_controls_section();// end: Section

	
		// Dot Color
		$this->start_controls_section(
			'section_quote_style',
			[
				'label' => esc_html__( 'Dot', 'confin-core' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);
		
		$this->add_control(
			'dot_color',
			[
				'label' => esc_html__( 'Background Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .testimonials-section .owl-dots .owl-dot span' => 'background-color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'dot_br_color',
			[
				'label' => esc_html__( 'Border Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .testimonials-section .owl-dots .owl-dot' => 'border-color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'dot_active_br_color',
			[
				'label' => esc_html__( 'Active Border Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .testimonials-section .owl-dots .owl-dot.active' => 'border-color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'dot_active_bg_color',
			[
				'label' => esc_html__( 'Active Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .testimonials-section .owl-dots .owl-dot.active span' => 'background-color: {{VALUE}};',
				],
			]
		);
		$this->end_controls_section();// end: Section
		
	}

	/**
	 * Render Testimonial widget output on the frontend.
	 * Written in PHP and used to generate the final HTML.
	*/
	protected function render() {
		$settings = $this->get_settings_for_display();
		$testimonialItems_groups = !empty( $settings['testimonialItems_groups'] ) ? $settings['testimonialItems_groups'] : [];

		// Turn output buffer on
		ob_start(); ?>
		<div class="testimonials-section">
			<div class="container">
		    <div class="row">
		        <div class="col col-lg-8 col-lg-offset-2 col-sm-10 col-sm-offset-1">
		            <div class="testimonials-grids testimonials-slider">
		            		<?php 	// Group Param Output
											if( is_array( $testimonialItems_groups ) && !empty( $testimonialItems_groups ) ){
											foreach ( $testimonialItems_groups as $each_items ) { 

											$testimonial_title = !empty( $each_items['testimonial_title'] ) ? $each_items['testimonial_title'] : '';
											$testimonial_subtitle = !empty( $each_items['testimonial_subtitle'] ) ? $each_items['testimonial_subtitle'] : '';
											$testimonial_content = !empty( $each_items['testimonial_content'] ) ? $each_items['testimonial_content'] : '';

											$image_url = wp_get_attachment_url( $each_items['bg_image']['id'] );
											$image_alt = get_post_meta( $each_items['bg_image']['id'], '_wp_attachment_image_alt', true);

											?>
		                <div class="grid">
	                    <div class="quote">
	                    	<?php if( $testimonial_content ) { echo '<p>'.esc_html( $testimonial_content ).'</p>'; } ?>
	                    </div>
	                    <div class="client-info">
	                        <div class="client-pic">
	                          <?php 
		                        	if( $image_url ) { echo '<img src="'.esc_url( $image_url ).'" alt="'.esc_attr( $image_alt ).'">'; }
		                        ?>
	                        </div>
	                        <?php 
	                        	if( $testimonial_title ) { echo '<h5>'.esc_html( $testimonial_title ).'</h5>'; } 
	                        	if( $testimonial_subtitle ) { echo '<span>'.esc_html( $testimonial_subtitle ).'</span>'; }
	                        ?>
	                    </div>
		                </div>
		               <?php }
									} ?>
		            </div>
		        </div>
		    	</div>
		   </div>
		</div>
		<?php
			// Return outbut buffer
			echo ob_get_clean();	
		}
	/**
	 * Render Testimonial widget output in the editor.
	 * Written as a Backbone JavaScript template and used to generate the live preview.
	*/
	
	//protected function _content_template(){}
	
}
Plugin::instance()->widgets_manager->register( new Confin_Testimonial() );