<?php
namespace XIMSAPLUGIN\Element;

use Elementor\Controls_Manager;
use Elementor\Controls_Stack;
use Elementor\Group_Control_Typography;
use Elementor\Scheme_Typography;
use Elementor\Scheme_Color;
use Elementor\Group_Control_Border;
use Elementor\Repeater;
use Elementor\Widget_Base;
use Elementor\Utils;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Plugin;

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

class dgi_team_widget extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * Retrieve Elementor widget name.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'xim-digital-team-';
	}

	/**
	 * Get widget title.
	 *
	 * Retrieve Elementor widget title.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return esc_html__( 'Digital Agency Team', 'ximsa' );
	}

	/**
	 * Get widget icon.
	 *
	 * Retrieve Elementor widget icon.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'gl-custom-icon';
	}

	/**
	 * Get widget categories.
	 *
	 * Retrieve the list of categories the Elementor widget belongs to.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'ximsa' ];
	}

	/**
	 * Register Elementor widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since 1.0.0
	 * @access protected
	 */
	 
	
	protected function register_controls() {
		$this->start_controls_section(
            'section_title_control',
            [
                'label' => __( 'Section Title Control', 'ximsa' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			[
				'name' => 'tm-bgbackground',
				'label' => esc_html__( 'Background', 'ximsa' ),
				'types' => [ 'classic' ],
				'exclude' => [ 'color' ],
				'selector' => '{{WRAPPER}} .xis-dia-team-section',
			]
		);
        $this->add_control(
			'shape',
			[
				'label' => esc_html__( 'Shape', 'ximsa' ),
				'type' => Controls_Manager::MEDIA,
                'label_block' => true,
			]
		);
        $this->add_control(
			'tm_subtitle',
			[
				'label' => esc_html__( 'Sub Title', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
			]
		);
        $this->add_control(
			'tm_title',
			[
				'label' => esc_html__( 'Title', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
                'label_block' => true,
			]
		);
        
        $this->end_controls_section();

		$this->start_controls_section(
            'team_options',
            [
                'label' => __( 'Team Options', 'ximsa' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'shadow_shape', [
                'label'       => esc_html__( 'Shadow Shape', 'ximsa' ),
                'type'        => Controls_Manager::MEDIA,
                'label_block' => true,
            ]
        );
        
        $repeater = new \Elementor\Repeater();
        $repeater->add_control(
            'team_id', [
                'label'       => esc_html__( 'Select Team', 'ximsa' ),
                'type'        => Controls_Manager::SELECT,
                'label_block' => true,
                'options'     => $this->select_param_posts(),
            ]
        );
        
        $this->add_control(
			'teamsitems',
			[
				'label' => esc_html__( 'Service List', 'ximsa' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
			]
		);
		$this->end_controls_section();
        
        $this->start_controls_section(
            'service_btm_option',
            [
                'label' => __( 'Service More Option', 'ximsa' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        
        $this->add_control(
			'sl_title',
			[
				'label' => esc_html__( 'Solution Text', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
                'label_block' => true,
			]
		);
        $this->add_control(
			'sl2_title',
			[
				'label' => esc_html__( 'Solution 2 Text', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
                'label_block' => true,
			]
		);
        $this->add_control(
			'sl_link_text',
			[
				'label' => esc_html__( 'Link Text', 'ximsa' ),
				'type' => Controls_Manager::TEXT,
			]
		);
        
        $this->add_control(
			'sl_link',
			[
				'label' => esc_html__( 'Link', 'ximsa' ),
				'type' => Controls_Manager::URL,
                'label_block' => true,
			]
		);
		$this->end_controls_section();
        $this->start_controls_section(
            'section_title_style',
            [
                'label' => __( 'Section Title Settings', 'ximsa' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'se__sub_title_style',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Sub Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );        
        $this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'sub_bgbackground',
				'label' => esc_html__( 'Background', 'ximsa' ),
				'types' => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .xis-dia-section-title span',
			]
		);
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'se_sth_typography',
                'label'          => esc_html__( 'Sub Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .xis-dia-section-title span',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'se_title_style',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'se_ht_color',
            [
                'label'     => esc_html__( 'Heading Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dia-section-title h2' => 'color: {{VALUE}} ',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'se_th_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '{{WRAPPER}} .xis-dia-section-title h2',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        
        $this->end_controls_section();
        $this->start_controls_section(
            'team_settings_style',
            [
                'label' => __( 'Team Settings', 'ximsa' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'team_title_stl',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Team Title Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'tm_ttl_color',
            [
                'label'     => esc_html__( 'Team Title Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dia-team-inner-box .xis-dia-inner-text-social .xis-dia-inner-text h3 a' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'tm_ttl_typography',
                'label'          => esc_html__( 'Team Title Typography', 'ximsa' ),
                'selector'       => '
                {{WRAPPER}} .xis-dia-team-inner-box .xis-dia-inner-text-social .xis-dia-inner-text h3
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'tm_deg_st',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Team Designation Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'tm_desig_color',
            [
                'label'     => esc_html__( 'Designation Color', 'ximsa' ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .xis-dia-team-inner-box .xis-dia-inner-text-social .xis-dia-inner-text span' => 'color: {{VALUE}} ',
                ],
            ]
        );
        
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'           => 'tm_desig_typography',
                'label'          => esc_html__( 'Typography', 'ximsa' ),
                'selector'       => '
                {{WRAPPER}} .xis-dia-team-inner-box .xis-dia-inner-text-social .xis-dia-inner-text span
                ',
                'fields_options' => [
                    'typography' => [
                        'default' => 'custom',
                    ],
                ],
            ]
        );
        $this->add_control(
            'social_bg_stl',
            [
                'type'      => Controls_Manager::HEADING,
                'label'     => esc_html__( 'Social Button Style', 'ximsa' ),
                'separator' => 'before',
            ]
        );
        $this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			[
				'name' => 'btn_bgbackground',
				'label' => esc_html__( 'Background', 'ximsa' ),
				'types' => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .xis-dia-team-inner-box .xis-dia-inner-text-social .xis-dia-inner-social:before',
			]
		);
             
        $this->end_controls_section();
	}
		


	protected function render() {
		
		
		$settings = $this->get_settings_for_display();?>

		<!-- Start of Team section
        ============================================= -->
        <section id="xis-dia-team" class="xis-dia-team-section position-relative">
            <span class="xis-dia-fnw-shape2 position-absolute"><img src="<?php echo esc_url($settings['shape']['url']);?>" alt=""></span>
            <div class="container">
                <div class="xis-dia-section-title xis-dia-headline text-center wow fadeInUp" data-wow-delay="100ms" data-wow-duration="1500ms">
                    <span><?php echo esc_html($settings['tm_subtitle']);?></span>
                    <h2><?php echo __($settings['tm_title']);?></h2>
                </div>
                <div class="xis-dia-team-content">
                    <div class="row justify-content-center">
                    <?php $shape = 0; foreach($settings['teamsitems'] as $index => $box): $shape++;?>
                    <?php  if( $box['team_id'] ) : ?>
                        <div class="col-lg-4 col-md-6 wow fadeInUp" data-wow-delay="200ms" data-wow-duration="1500ms">
                            <div class="xis-dia-team-inner-box position-relative">
                                <div class="xis-dia-inner-img">
                                    <img src="<?php echo esc_url(get_the_post_thumbnail_url($box['team_id']));?>" alt="">
                                </div>
                                <div class="xis-dia-inner-text-social xis-dia-headline position-absolute">
                                    <div class="xis-dia-inner-text">
                                        <h3><a href="<?php echo get_the_permalink($box['team_id']);?>"><?php echo get_the_title($box['team_id']);?></a></h3>
                                        <span><?php echo (get_post_meta( $box['team_id'], 'designation', true ));?></span>
                                    </div>
                                    <?php $icons = get_post_meta( $box['team_id'], 'social_profile', true );
                                        if ( ! empty( $icons ) ) :
                                    ?>
                                    <div class="xis-dia-inner-social">
                                    <?php foreach ( $icons as $h_icon ) :
                                $header_social_icons = json_decode( urldecode( ximsa_set( $h_icon, 'data' ) ) );
                                if ( ximsa_set( $header_social_icons, 'enable' ) == '' ) {
                                    continue;
                                }
                                                
                                $background = ximsa_set($header_social_icons, 'background');
                                $color = ximsa_set($header_social_icons, 'color');
                                                
                                if(!empty($background))
                                    $icon_background = "background-color:".$background.";";
                                else
                                    $icon_background = '';
                                                
                                if(!empty($color))
                                    $icon_color = "color:".$color.";";
                                else
                                    $icon_color = '';
                                                
                                $icon_class = explode( '-', ximsa_set( $header_social_icons, 'icon' ) ); ?>
                                    <a href="<?php echo ximsa_set( $header_social_icons, 'url' ); ?>" style="<?php echo esc_attr($icon_background); ?> <?php echo esc_attr($icon_color); ?>" target="_blank">
                                    <i class="fa <?php echo esc_attr( ximsa_set( $header_social_icons, 'icon' ) ); ?>"></i>
                                    </a>
                            	    <?php endforeach; ?>
                                    </div>
                                    <?php endif;?>
                                </div>
                            </div>
                        </div>
                        <?php endif; endforeach;?>
                    </div>
                </div>
            </div>
        </section>
    <!-- End of Team section
        ============================================= -->
    <?php     
      }
	
      protected function select_param_posts() {
        $args = wp_parse_args( [
            'post_type'   => 'team',
            'numberposts' => -1,
            'orderby'     => 'title',
            'order'       => 'ASC',
        ] );
    
        $query_query = get_posts( $args );
    
        $posts = [];
        if ( $query_query ) {
            foreach ( $query_query as $query ) {
                $posts[$query->ID] = $query->post_title;
            }
        }
    
        return $posts;
    }

}

