<?php
/*
 * Elementor eCommerce Addon Team Widget
 * Author & Copyright: NicheAddon
*/

namespace Elementor;

if (!isset(get_option( 'naeca_uw_settings' )['naeca_team'])) { // enable & disable

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

class eCommerce_Elementor_Addon_Team extends Widget_Base{

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

	/**
	 * Retrieve the widget title.
	*/
	public function get_title(){
		return esc_html__( 'Team', 'ecommerce-addon' );
	}

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

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

	/**
	 * Register eCommerce Addon Team 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_team',
			[
				'label' => __( 'Team Options', 'ecommerce-addon' ),
			]
		);
		$this->add_control(
			'team_style',
			[
				'label' => esc_html__( 'Team Style', 'ecommerce-addon' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'one' => esc_html__( 'Style One', 'ecommerce-addon' ),
					'two' => esc_html__( 'Style Two', 'ecommerce-addon' ),
				],
				'default' => 'one',
				'description' => esc_html__( 'Select your team style.', 'ecommerce-addon' ),
			]
		);
		$this->add_control(
			'image',
			[
				'label' => esc_html__( 'Upload Image', 'ecommerce-addon' ),
				'type' => Controls_Manager::MEDIA,
				'frontend_available' => true,
				'default' => [
					'url' => Utils::get_placeholder_image_src(),
				],
				'description' => esc_html__( 'Set your icon image.', 'ecommerce-addon'),
			]
		);
		$this->add_control(
			'title',
			[
				'label' => esc_html__( 'Title Text', 'ecommerce-addon' ),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__( 'Cloud Hosting', 'ecommerce-addon' ),
				'placeholder' => esc_html__( 'Type title text here', 'ecommerce-addon' ),
				'label_block' => true,
				'separator' => 'before',
			]
		);
		$this->add_control(
			'title_link',
			[
				'label' => esc_html__( 'Title Link', 'ecommerce-addon' ),
				'type' => Controls_Manager::URL,
				'placeholder' => 'https://your-link.com',
				'default' => [
					'url' => '',
				],
				'label_block' => true,
				'separator' => 'before',
			]
		);
		$this->add_control(
			'position',
			[
				'label' => esc_html__( 'Position', 'ecommerce-addon' ),
				'default' => esc_html__( 'Cheif Operating Officer', 'ecommerce-addon' ),
				'placeholder' => esc_html__( 'Type your position here', 'ecommerce-addon' ),
				'type' => Controls_Manager::TEXTAREA,
				'label_block' => true,
				'separator' => 'before',
			]
		);	
		$repeater = new Repeater();
		$repeater->add_control(
			'social_icon',
			[
				'label' => esc_html__( 'Social Icon', 'ecommerce-addon' ),
				'type' => Controls_Manager::ICON,
				'options' => NAeCom_Controls_Helper_Output::get_include_icons(),
				'frontend_available' => true,
				'default' => 'fa fa-facebook-square',
			]
		);
		$repeater->add_control(
			'icon_link',
			[
				'label' => esc_html__( 'Icon Link', 'ecommerce-addon' ),
				'type' => Controls_Manager::URL,
				'placeholder' => 'https://your-link.com',
				'default' => [
					'url' => '',
				],
				'label_block' => true,
			]
		);
		$this->add_control(
			'listItems_groups',
			[
				'label' => esc_html__( 'Social Iocns', 'ecommerce-addon' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'title_field' => '{{{ social_icon }}}',
				'prevent_empty' => false,
			]
		);
		$this->end_controls_section();// end: Section

		// Section
			$this->start_controls_section(
				'section_box_style',
				[
					'label' => esc_html__( 'Section', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_control(
				'section_padding',
				[
					'label' => __( 'Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-team figure' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_control(
				'section_bg_color',
				[
					'label' => esc_html__( 'Background Color', 'ecommerce-addon' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .naeca-team figure' => 'background-color: {{VALUE}};',
					],
				]
			);
			$this->add_control(
				'section_border_radius',
				[
					'label' => __( 'Border Radius', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-team figure' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_group_control(
				Group_Control_Border::get_type(),
				[
					'name' => 'section_box_border',
					'label' => esc_html__( 'Border', 'ecommerce-addon' ),
					'selector' => '{{WRAPPER}} .naeca-team figure',
				]
			);
			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				[
					'name' => 'section_box_shadow',
					'label' => esc_html__( 'Box Shadow', 'ecommerce-addon' ),
					'selector' => '{{WRAPPER}} .naeca-team figure',
				]
			);
			$this->end_controls_section();// end: Section

		// Icon
			$this->start_controls_section(
				'section_icon_style',
				[
					'label' => esc_html__( 'Icon', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_control(
				'icon_color',
				[
					'label' => esc_html__( 'Icon Color', 'ecommerce-addon' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .naeca-team figure .naeca-icon i' => 'color: {{VALUE}};',
					],
				]
			);
			$this->add_control(
				'icon_size',
				[
					'label' => esc_html__( 'Icon Size', 'ecommerce-addon' ),
					'type' => Controls_Manager::SLIDER,
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 500,
							'step' => 1,
						],
					],
					'size_units' => [ 'px' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-team figure .naeca-icon i' => 'font-size: {{SIZE}}{{UNIT}};',
					],
				]
			);
			$this->add_control(
				'icon_bg_color',
				[
					'label' => esc_html__( 'Background Color', 'ecommerce-addon' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .naeca-team figure .naeca-icon' => 'background-color: {{VALUE}};',
					],
				]
			);
			$this->add_control(
				'icon_border_radius',
				[
					'label' => __( 'Border Radius', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-team figure .naeca-icon' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_group_control(
				Group_Control_Border::get_type(),
				[
					'name' => 'icon_box_border',
					'label' => esc_html__( 'Border', 'ecommerce-addon' ),
					'selector' => '{{WRAPPER}} .naeca-team figure .naeca-icon',
				]
			);
			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				[
					'name' => 'icon_box_shadow',
					'label' => esc_html__( 'Box Shadow', 'ecommerce-addon' ),
					'selector' => '{{WRAPPER}} .naeca-team figure .naeca-icon',
				]
			);
			$this->end_controls_section();// end: Section

		// Title
			$this->start_controls_section(
				'section_title_style',
				[
					'label' => esc_html__( 'Title', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_control(
				'title_padding',
				[
					'label' => __( 'Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-team figure h5' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_group_control(
				Group_Control_Typography::get_type(),
				[
					'label' => esc_html__( 'Typography', 'ecommerce-addon' ),
					'name' => 'sasstp_title_typography',
					'selector' => '{{WRAPPER}} .naeca-team figure h5',
				]
			);
			$this->start_controls_tabs( 'title_style' );
				$this->start_controls_tab(
					'title_normal',
					[
						'label' => esc_html__( 'Normal', 'ecommerce-addon' ),
					]
				);
				$this->add_control(
					'title_color',
					[
						'label' => esc_html__( 'Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-team figure h5, {{WRAPPER}} .naeca-team figure h5 a' => 'color: {{VALUE}};',
						],
					]
				);
				$this->end_controls_tab();  // end:Normal tab
				$this->start_controls_tab(
					'title_hover',
					[
						'label' => esc_html__( 'Hover', 'ecommerce-addon' ),
					]
				);
				$this->add_control(
					'title_hover_color',
					[
						'label' => esc_html__( 'Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-team figure h5 a:hover' => 'color: {{VALUE}};',
						],
					]
				);
				$this->end_controls_tab();  // end:Hover tab
			$this->end_controls_tabs(); // end tabs
			$this->end_controls_section();// end: Section

		// Position
			$this->start_controls_section(
				'section_position_style',
				[
					'label' => esc_html__( 'Position', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_control(
				'position_padding',
				[
					'label' => __( 'Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-team figure p' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_group_control(
				Group_Control_Typography::get_type(),
				[
					'label' => esc_html__( 'Typography', 'ecommerce-addon' ),
					'name' => 'position_typography',
					'selector' => '{{WRAPPER}} .naeca-team figure p',
				]
			);
			$this->add_control(
				'position_color',
				[
					'label' => esc_html__( 'Color', 'ecommerce-addon' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .naeca-team figure p' => 'color: {{VALUE}};',
					],
				]
			);
			$this->end_controls_section();// end: Section

		// Icon
			$this->start_controls_section(
				'section_sicon_style',
				[
					'label' => esc_html__( 'Icon', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_control(
				'social_padding',
				[
					'label' => __( 'Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-team-social' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_control(
				'sicon_border_radius',
				[
					'label' => __( 'Border Radius', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-team-social a' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->start_controls_tabs( 'sicon_style' );
				$this->start_controls_tab(
						'sicon_normal',
						[
							'label' => esc_html__( 'Normal', 'ecommerce-addon' ),
						]
					);
				$this->add_control(
					'sicon_color',
					[
						'label' => esc_html__( 'Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-team-social a' => 'color: {{VALUE}};',
						],
					]
				);
				$this->add_control(
					'sicon_bg',
					[
						'label' => esc_html__( 'Background Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-team-social a' => 'background-color: {{VALUE}};',
						],
					]
				);
				$this->add_group_control(
					Group_Control_Border::get_type(),
					[
						'name' => 'sicon_border',
						'label' => esc_html__( 'Border', 'ecommerce-addon' ),
						'selector' => '{{WRAPPER}} .naeca-team-social a',
					]
				);
				$this->end_controls_tab();  // end:Normal tab
				$this->start_controls_tab(
					'sicon_hover',
					[
						'label' => esc_html__( 'Hover', 'ecommerce-addon' ),
					]
				);
				$this->add_control(
					'sicon_hover_color',
					[
						'label' => esc_html__( 'Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-team-social a:hover' => 'color: {{VALUE}};',
						],
					]
				);
				$this->add_control(
					'sicon_bg_hov',
					[
						'label' => esc_html__( 'Background Hover Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-team-social a:hover' => 'background-color: {{VALUE}};',
						],
					]
				);
				$this->add_group_control(
					Group_Control_Border::get_type(),
					[
						'name' => 'sicon_hover_border',
						'label' => esc_html__( 'Border', 'ecommerce-addon' ),
						'selector' => '{{WRAPPER}} .naeca-team-social a:hover',
					]
				);
				$this->end_controls_tab();  // end:Hover tab
			$this->end_controls_tabs(); // end tabs

			$this->add_responsive_control(
				'sicon_size',
				[
					'label' => esc_html__( 'Size', 'ecommerce-addon' ),
					'type' => Controls_Manager::SLIDER,
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 1000,
							'step' => 1,
						],
						'%' => [
							'min' => 0,
							'max' => 100,
						],
					],
					'size_units' => [ 'px', '%' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-team-social a' => 'font-size:{{SIZE}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'sicon_width',
				[
					'label' => esc_html__( 'Width', 'ecommerce-addon' ),
					'type' => Controls_Manager::SLIDER,
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 1000,
							'step' => 1,
						],
						'%' => [
							'min' => 0,
							'max' => 100,
						],
					],
					'size_units' => [ 'px', '%' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-team-social a' => 'width:{{SIZE}}{{UNIT}};height:{{SIZE}}{{UNIT}};line-height:{{SIZE}}{{UNIT}};',
					],
				]
			);
			$this->add_control(
				'sicon_margin',
				[
					'label' => __( 'Margin', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-team-social a' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->end_controls_section();// end: Section

	}

	/**
	 * Render App Works widget output on the frontend.
	 * Written in PHP and used to generate the final HTML.
	*/
	protected function render() {
		$settings = $this->get_settings_for_display();
		$team_style = !empty( $settings['team_style'] ) ? $settings['team_style'] : '';
		$image = !empty( $settings['image']['id'] ) ? $settings['image']['id'] : '';
		$title = !empty( $settings['title'] ) ? $settings['title'] : '';
		$position = !empty( $settings['position'] ) ? $settings['position'] : '';
		$title_link = !empty( $settings['title_link']['url'] ) ? $settings['title_link']['url'] : '';
		$title_link_external = !empty( $settings['title_link']['is_external'] ) ? 'target="_blank"' : '';
		$title_link_nofollow = !empty( $settings['title_link']['nofollow'] ) ? 'rel="nofollow"' : '';
		$title_link_attr = !empty( $title_link ) ?  $title_link_external.' '.$title_link_nofollow : '';
		$listItems_groups = !empty( $settings['listItems_groups'] ) ? $settings['listItems_groups'] : '';

		$title_link = $title_link ? '<a href="'.esc_url($title_link).'" '.$title_link_attr.'>'. esc_html($title) .'</a>' : esc_html($title);
		$title = $title ? '<h5>'.$title_link.'</h5>' : '';
		$position = $position ? '<p>'.esc_html($position).'</p>' : '';

		// Image
		$image_url = wp_get_attachment_url( $image );
		$image = $image_url ? '<div class="naeca-image"><img src="'.esc_url($image_url).'" alt="'.esc_attr($title).'"></div>' : '';

		if ($team_style === 'two'){
		  $style_cls = ' team-style-two';
		} else {
		  $style_cls = ''; 
		}

		$output = '<div class="naeca-team'.esc_attr($style_cls).'">
								<figure>
		              '.$image.'
		              <figcaption>'.$title.$position.'</figcaption>
		              <div class="naeca-team-social">';
										// Group Param Output
										if ( is_array( $listItems_groups ) && !empty( $listItems_groups ) ){
										  foreach ( $listItems_groups as $each_list ) {
										  $icon_link = !empty( $each_list['icon_link'] ) ? $each_list['icon_link'] : '';
											$link_url = !empty( $icon_link['url'] ) ? esc_url($icon_link['url']) : '';
											$link_external = !empty( $icon_link['is_external'] ) ? 'target="_blank"' : '';
											$link_nofollow = !empty( $icon_link['nofollow'] ) ? 'rel="nofollow"' : '';
											$link_attr = !empty( $icon_link['url'] ) ?  $link_external.' '.$link_nofollow : '';
										  $social_icon = !empty( $each_list['social_icon'] ) ? $each_list['social_icon'] : '';
											$icon = $social_icon ? '<i class="'.esc_attr($social_icon).'" aria-hidden="true"></i>' : '';

										  $output .= '<a href="'.$link_url.'" '.$link_attr.'>'.$icon.'</a>';
										} }
      $output .= '<span><i class="fa fa-share-alt" aria-hidden="true"></i></span></div>
		            </figure>
              </div>';
		echo $output;

	}

}
Plugin::instance()->widgets_manager->register_widget_type( new eCommerce_Elementor_Addon_Team() );

} // enable & disable
