<?php
namespace Elementor;

class ThemeDraft_Testimonial_ThreeWidget extends Widget_Base {

	public function get_name() {

		return 'themedraft_testimonial_three';
	}

	public function get_title() {
		return esc_html__( 'Testimonial Three', 'themedraft-core' );
	}

	public function get_icon() {

		return 'eicon-testimonial';
	}

	public function get_categories() {
		return [ 'themedraft_elements' ];
	}


	protected function register_controls() {

		//Content tab start
		$this->start_controls_section(
			'testimonial_three_settings',
			[
				'label' => esc_html__( 'Testimonials', 'themedraft-core' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'image',
			[
				'label'       => __( 'Person Image', 'themedraft-core' ),
				'type'        => Controls_Manager::MEDIA,
				'label_block' => true,
				'default'     => [
					'url' => Utils::get_placeholder_image_src(),
				],
			]
		);

		$repeater->add_control(
			'name',
			[
				'label'       => __('Person Name', 'themedraft-core'),
				'type'        => Controls_Manager::TEXT,
				'default'     => __('Md Nadim Khan', 'themedraft-core'),
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'designation',
			[
				'label'       => __('Person Designation', 'themedraft-core'),
				'type'        => Controls_Manager::TEXT,
				'default'     => __('Managing Director', 'themedraft-core'),
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'text',
			[
				'label'   => __('Testimonial Text', 'themedraft-core'),
				'type'    => Controls_Manager::WYSIWYG,
				'default' => __('<p>“Nostrud exercitation ullamco laboris nisi aliquip commodo consequat duis aute irure dolor in the repr henderit in voluptate velit esse cillumm and dolore fugiat nulla".</p>', 'themedraft-core'),
			]
		);

		$repeater->add_control(
			'rating',
			[
				'label' => esc_html__( 'Rating', 'themedraft-core' ),
				'type' => Controls_Manager::SELECT,
				'show_label' => true,
				'label_block' => false,

				'options' => [
					'1'  => esc_html__( '1 Star', 'themedraft-core' ),
					'2'  => esc_html__( '2 Star', 'themedraft-core' ),
					'3'  => esc_html__( '3 Star', 'themedraft-core' ),
					'4'  => esc_html__( '4 Star', 'themedraft-core' ),
					'5'  => esc_html__( '5 Star', 'themedraft-core' ),
				],

				'default' => '5',
			]
		);

		$repeater->add_control(
			'entrance_animation',
			[
				'label' => __( 'Entrance Animation', 'themedraft-core' ),
				'type' => Controls_Manager::ANIMATION,
				'prefix_class' => 'animated ',
				'separator' => 'before ',
			]
		);

		$repeater->add_control(
			't3_animation_duration',
			[
				'label'     => __( 'Animation Duration', 'themedraft-core' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'data-wow-duration="1.25s"',
				'options'   => [
					'data-wow-duration="2s"'    => __( 'Slow', 'themedraft-core' ),
					'data-wow-duration="1.25s"' => __( 'Normal', 'themedraft-core' ),
					'data-wow-duration=".75s"'  => __( 'Fast', 'themedraft-core' ),
				],
				'condition' => [
					'entrance_animation!' => '',
				]
			]
		);

		$this->add_control(
			'testimonials',
			[
				'label'       => __('Testimonials List', 'themedraft-core'),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => [
					[
						'name'        => __('Md Nadim Khan', 'themedraft-core'),
						'designation'        => __('Managing Director', 'themedraft-core'),
						'text' => __('“Nostrud exercitation ullamco laboris nisi aliquip commodo consequat duis aute irure dolor in the repr henderit in voluptate velit esse cillumm and dolore fugiat nulla".', 'themedraft-core'),
					],
				],
				'title_field' => '{{{name }}}',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'slider_options',
			[
				'label' => esc_html__( 'Slider Options', 'themedraft-core' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'autoplay',
			[
				'label'       => __( 'Autoplay', 'themedraft-core' ),
				'type'        => Controls_Manager::SWITCHER,
				'show_label'  => true,
				'label_block' => false,
				'default'     => 'yes',
			]
		);

		$this->add_control(
			'autoplay_interval',
			[
				'label'       => __( 'Autoplay Interval', 'themedraft-core' ),
				'type'        => Controls_Manager::SELECT,
				'show_label'  => true,
				'label_block' => false,
				'options'     => [
					'2000'  => __( '2 seconds', 'themedraft-core' ),
					'3000'  => __( '3 seconds', 'themedraft-core' ),
					'4000'  => __( '4 seconds', 'themedraft-core' ),
					'5000'  => __( '5 seconds', 'themedraft-core' ),
					'6000'  => __( '6 seconds', 'themedraft-core' ),
					'7000'  => __( '7 seconds', 'themedraft-core' ),
					'8000'  => __( '8 seconds', 'themedraft-core' ),
					'9000'  => __( '9 seconds', 'themedraft-core' ),
					'10000' => __( '10 seconds', 'themedraft-core' ),
				],
				'default'     => '4000',
				'condition' => [
					'autoplay' => 'yes',
				],
			]
		);

		$this->add_control(
			'infinity_loop',
			[
				'label'       => __( 'Loop', 'themedraft-core' ),
				'type'        => Controls_Manager::SWITCHER,
				'show_label'  => true,
				'label_block' => false,
				'default'     => 'yes',
			]
		);

		$this->add_control(
			'nav_arrow',
			[
				'label'       => __( 'Navigation Arrow', 'themedraft-core' ),
				'type'        => Controls_Manager::SWITCHER,
				'show_label'  => true,
				'label_block' => false,
				'default'     => 'yes',
			]
		);

		$this->add_control(
			'desktop_count',
			[
				'label'       => __( 'Column On Desktop', 'themedraft-core' ),
				'type'        => Controls_Manager::SELECT,
				'show_label'  => true,
				'label_block' => false,
				'options'     => [
					1 => __( '1 Column', 'themedraft-core' ),
					2 => __( '2 Column', 'themedraft-core' ),
					3 => __( '3 Column', 'themedraft-core' ),
					4 => __( '4 Column', 'themedraft-core' ),
				],
				'default'     => 2,
			]
		);

		$this->add_control(
			'tab_count',
			[
				'label'       => __( 'Column On Tablet', 'themedraft-core' ),
				'type'        => Controls_Manager::SELECT,
				'show_label'  => true,
				'label_block' => false,
				'options'     => [
					1 => __( '1 Column', 'themedraft-core' ),
					2 => __( '2 Column', 'themedraft-core' ),
					3 => __( '3 Column', 'themedraft-core' ),
					4 => __( '4 Column', 'themedraft-core' ),
				],
				'default'     => 2,
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'colors_options',
			[
				'label' => esc_html__( 'Colors', 'themedraft-core' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'bg_color',
			[
				'label'       => esc_html__('Background Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-testimonial-three-content,{{WRAPPER}} .td-testimonial-three-content:before' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'text_color',
			[
				'label'       => esc_html__('Text Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-testimonial-three-content .td-testimonial-text,{{WRAPPER}} .td-testimonial-three-content .td-designation' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'name_color',
			[
				'label'       => esc_html__('Name Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-testimonial-three-content .td-name' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'star_color',
			[
				'label'       => esc_html__('Star Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-testimonial-three-content .td-author-rating' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'arrow_bg',
			[
				'label'       => esc_html__('Arrow Background Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-testimonial-three-wrapper .slick-arrow' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'arrow_color',
			[
				'label'       => esc_html__('Arrow Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-testimonial-three-wrapper .slick-arrow:hover' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

	}

	//Render
	protected function render() {
		$settings = $this->get_settings_for_display();
		$slider_id = rand(10, 10000);
		if(is_rtl()){
			$slide_dir = 'dir="ltr"';
		}else{
			$slide_dir = '';
		}
		?>
        <div class="td-testimonial-three-wrapper">
            <div class="container">
                <div <?php echo $slide_dir;?> id="td-testimonial-three-slider-<?php echo $slider_id;?>" class="row">
					<?php if ( $settings['testimonials'] ) {
						foreach ( $settings['testimonials'] as $testimonial ) {
							if($testimonial['entrance_animation']){
								$td_animation = 'wow'.' ' . $testimonial['entrance_animation'];
								$td_animation_duration = $testimonial['t3_animation_duration'];
							}else{
								$td_animation ='';
								$td_animation_duration ='';
							}
							?>
                            <div class="col-12">
                                <div class="td-testimonial-three-item-wrapper <?php echo $td_animation;?>" <?php echo $td_animation_duration;?>>
                                    <div class="td-image td-cover-bg" style="background-image: url(<?php echo esc_url($testimonial['image']['url']);?>)"></div>

                                    <div class="td-testimonial-three-content">
                                        <div class="td-testimonial-text">
											<?php echo $testimonial['text'];?>
                                        </div>

                                        <div class="td-testimonial-author">
                                            <div class="td-author-info">
                                                <h4 class="td-name"><?php echo $testimonial['name'];?></h4>
                                                <p class="td-designation"><?php echo $testimonial['designation'];?></p>
                                            </div>

                                            <div class="td-author-rating">
                                                <ul class="td-list-style td-list-inline">
													<?php
													$star = $testimonial['rating'];

													for($x = 1; $x <= $star; $x++){ ?>
                                                        <li>
                                                            <i class="fas fa-star"></i>
                                                        </li>
													<?php }
													?>
                                                </ul>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
							<?php
						}
					} ?>
                </div>
            </div>
        </div>

        <script>
            (function ($) {
                "use strict";
                $(document).ready(function () {
                    $("#td-testimonial-three-slider-<?php echo $slider_id;?>").slick({
                        slidesToShow: <?php echo json_encode( $settings['desktop_count'] )?>,
                        autoplay: <?php echo json_encode( $settings['autoplay'] == 'yes' ? true : false ); ?>,
                        autoplaySpeed: <?php echo json_encode( $settings['autoplay_interval'] )?>, //interval
                        speed: 1500, // slide speed
                        dots: false,
                        arrows: <?php echo json_encode( $settings['nav_arrow'] == 'yes' ? true : false ); ?>,
                        prevArrow: '<i class="slick-arrow slick-prev fas fa-arrow-left"></i>',
                        nextArrow: '<i class="slick-arrow slick-next fas fa-arrow-right"></i>',
                        infinite: <?php echo json_encode( $settings['infinity_loop'] == 'yes' ? true : false ); ?>,
                        pauseOnHover: false,
                        centerMode: false,
                        responsive: [

                            {
                                breakpoint: 1025,
                                settings: {
                                    slidesToShow: <?php echo json_encode( $settings['tab_count'] )?>, //768-991
                                }
                            },

                            {
                                breakpoint: 992,
                                settings: {
                                    slidesToShow: <?php echo json_encode( $settings['tab_count'] )?>, //768-991
                                    arrows: false
                                }
                            },
                            {
                                breakpoint: 768,
                                settings: {
                                    slidesToShow: 1, // 0 -767
                                    arrows: false
                                }
                            }
                        ]
                    });
                });
            })(jQuery);
        </script>
		<?php

	}
}

Plugin::instance()->widgets_manager->register( new ThemeDraft_Testimonial_ThreeWidget );