<?php
namespace Elementor;


class ThemeDraft_Project_Slider_Three_Widget extends Widget_Base {

	public function get_name() {

		return 'themedraft_project_slider_three';
	}

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

	public function get_icon() {

		return 'eicon-slider-push';
	}

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


	protected function register_controls() {

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

		$repeater = new Repeater();

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

		$repeater->add_control(
			'title',
			[
				'label'       => __( 'Title', 'themedraft-core' ),
				'type'        => Controls_Manager::TEXTAREA,
				'rows'        => 5,
				'default'     => __( 'Bridge Construction', 'themedraft-core' ),
			]
		);

		$repeater->add_control(
			'subtitle',
			[
				'label'       => __('Subtitle', 'themedraft-core'),
				'type'        => Controls_Manager::TEXT,
				'default'     => __('Building Construction', 'themedraft-core'),
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'details_url',
			[
				'label'         => __( 'Details URL', 'themedraft-core' ),
				'type'          => Controls_Manager::URL,
				'placeholder'   => __( 'https://your-link.com', 'themedraft-core' ),
				'show_external' => true,
				'default'       => [
					'url'         => '#',
					'is_external' => false,
					'nofollow'    => false,
				],
			]
		);

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

		$repeater->add_control(
			'p3_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(
			'projects',
			[
				'label'       => __('Projects List', 'themedraft-core'),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => [
					[
						'subtitle'        => __('Building Construction', 'themedraft-core'),
						'title' => __('Bridge Management.', 'themedraft-core'),
					],
				],
				'title_field' => '{{{ title }}}',
			]
		);

		$this->add_responsive_control(
			'project_height',
			[
				'label' => __( 'Item Height', 'themedraft-core' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => ['px'],
				'range' => [
					'px' => [
						'min' => 200,
						'max' => 1000,
					],
				],
				'devices' => [ 'desktop', 'tablet', 'mobile' ],

				'selectors' => [
					'{{WRAPPER}} .td-project-slider-three-item .td-project-image' => 'height: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();

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

		$this->add_control(
			'enable_slider',
			[
				'label'     => esc_html__( 'Enable Slider', 'themedraft-core' ),
				'type'      => Controls_Manager::SWITCHER,
				'label_on'  => esc_html__( 'Yes', 'themedraft-core' ),
				'label_off' => esc_html__( 'No', 'themedraft-core' ),
				'default'   => 'yes',
			]
		);

		$this->add_control(
			'slide_row',
			[
				'label'       => __( 'Slide Row', 'themedraft-core' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => __( 2, 'themedraft-core' ),
				'condition' => [
					'enable_slider' => 'yes',
				],
			]
		);

		$this->add_control(
			'autoplay',
			[
				'label'       => __( 'Autoplay', 'themedraft-core' ),
				'type'        => Controls_Manager::SWITCHER,
				'show_label'  => true,
				'label_block' => false,
				'default'     => 'yes',
				'condition' => [
					'enable_slider' => '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' => [
					'enable_slider' => 'yes',
					'autoplay' => 'yes',
				],
			]
		);

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

		$this->add_control(
			'dots',
			[
				'label'       => __( 'Navigation Dots', 'themedraft-core' ),
				'type'        => Controls_Manager::SWITCHER,
				'show_label'  => true,
				'label_block' => false,
				'default'     => 'no',
				'condition' => [
					'enable_slider' => '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'     => 3,
				'condition' => [
					'enable_slider' => 'yes',
				],
			]
		);

		$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,
				'condition' => [
					'enable_slider' => 'yes',
				],
			]
		);

		$this->add_control(
			'desktop_col',
			[
				'label'   => __( 'Column On Desktop', 'themedraft-core' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'col-lg-4',
				'options' => [
					'col-lg-12' => __( '1 Column', 'themedraft-core' ),
					'col-lg-6'  => __( '2 Column', 'themedraft-core' ),
					'col-lg-4'  => __( '3 Column', 'themedraft-core' ),
					'col-lg-3'  => __( '4 Column', 'themedraft-core' ),
				],
				'condition' => [
					'enable_slider!' => 'yes',
				],
			]
		);

		$this->add_control(
			'tab_col',
			[
				'label'   => __( 'Column On Tablet', 'themedraft-core' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'col-md-6',
				'options' => [
					'col-md-12' => __( '1 Column', 'themedraft-core' ),
					'col-md-6'  => __( '2 Column', 'themedraft-core' ),
					'col-md-4'  => __( '3 Column', 'themedraft-core' ),
					'col-md-3'  => __( '4 Column', 'themedraft-core' ),
				],
				'condition' => [
					'enable_slider!' => 'yes',
				],
			]
		);

		$this->end_controls_section();

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

		$this->add_control(
			'overlay_color',
			[
				'label'       => esc_html__('Overlay Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-project-slider-three-content-wrapper' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'text_color',
			[
				'label'       => esc_html__('Text Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-project-slider-three-content .td-title,{{WRAPPER}} .td-project-slider-three-content .td-subtitle' => 'color: {{VALUE}};',
					'{{WRAPPER}} .td-project-slider-three-item .td-details-link a' => 'background-color: {{VALUE}};',
					'{{WRAPPER}} .td-project-slider-three-content' => 'border-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'link_color',
			[
				'label'       => esc_html__('Link Icon Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-project-slider-three-item .td-details-link a' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'link_hover_color',
			[
				'label'       => esc_html__('Link Icon Hover Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-project-slider-three-item .td-details-link a:hover' => 'color: {{VALUE}};',
				],
			]
		);

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

		$this->add_control(
			'arrow_hover_bg',
			[
				'label'       => esc_html__('Slide Arrow Hover Background', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .slick-arrow:hover' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'dot_color',
			[
				'label'       => esc_html__('Slide Dot Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .slick-dots button' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'dot_hover_color',
			[
				'label'       => esc_html__('Slide Dot Hover Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .slick-dots button:hover,{{WRAPPER}} .slick-dots .slick-active button' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

	}

	//Render
	protected function render() {
		$settings = $this->get_settings_for_display();
		$slider_id = rand(100, 10000);
		if ($settings['enable_slider'] == 'yes'){
			$item_col = 'col-12';
			if(is_rtl()){
				$slide_dir = 'dir="ltr"';
			}else{
				$slide_dir = '';
			}
		}else{
			$item_col = $settings['desktop_col'] . ' '. $settings['tab_col'];
		}
		?>
        <div class="td-project-slider-three-wrapper">
            <div class="container">
                <div <?php if($settings['enable_slider'] == 'yes' && is_rtl()){echo $slide_dir;} ?> id="project-slider-three-<?php echo $slider_id;?>" class="row">

					<?php if($settings['projects']){
						foreach ($settings['projects'] as $project){
							$target   = $project['details_url']['is_external'] ? ' target="_blank"' : '';
							$nofollow = $project['details_url']['nofollow'] ? ' rel="nofollow"' : '';

							if($project['entrance_animation']){
								$td_animation = 'wow'.' ' . $project['entrance_animation'];
								$td_animation_duration = $project['p3_animation_duration'];
							}else{
								$td_animation ='';
								$td_animation_duration ='';
							}
							?>

                            <div class="<?php echo $item_col;?> td-project-slider-column">
                                <div class="td-project-slider-three-item <?php echo $td_animation;?>" <?php echo $td_animation_duration;?>>
                                    <div class="td-project-image td-cover-bg" style="background-image: url(<?php echo $project['image']['url'];?>)"></div>

                                    <div class="td-project-slider-three-content-wrapper">
                                        <div class="td-project-slider-three-content">
                                            <a href="<?php echo $project['details_url']['url'];?>" class="td-title-link" <?php echo $target . $nofollow?>>
                                                <h4 class="td-title"><?php echo $project['title'];?></h4>
                                            </a>
                                            <p class="td-subtitle"><?php echo $project['subtitle'];?></p>
                                        </div>

                                        <div class="td-details-link">
                                            <a href="<?php echo $project['details_url']['url'];?>" <?php echo $target . $nofollow?>><i class="fas fa-link"></i></a>
                                        </div>
                                    </div>
                                </div>
                            </div>
							<?php

						}
					}?>
                </div>
            </div>
        </div>

		<?php if ($settings['enable_slider'] == 'yes') : ?>
            <script>
                (function ($) {
                    "use strict";
                    $(document).ready(function () {
                        $("#project-slider-three-<?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: <?php echo json_encode( $settings['dots'] == 'yes' ? true : 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: true,
                            pauseOnHover: false,
                            centerMode: false,
                            rows: <?php echo $settings['slide_row'];?>,
                            responsive: [
                                {
                                    breakpoint: 992,
                                    settings: {
                                        slidesToShow: <?php echo json_encode( $settings['tab_count'] )?>, //768-991
                                    }
                                },
                                {
                                    breakpoint: 768,
                                    settings: {
                                        slidesToShow: 1, // 0 -767
                                    }
                                }
                            ]
                        });
                    });
                })(jQuery);
            </script>
		<?php endif; ?>

		<?php
	}
}

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