<?php

namespace TRANZIXPLUGIN\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\Plugin;

/**
 * Elementor button widget.
 * Elementor widget that displays a button with the ability to control every
 * aspect of the button design.
 *
 * @since 1.0.0
 */
class H1_Team extends Widget_Base {

	/**
	 * Get widget name.
	 * Retrieve button widget name.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'tranzix_h1_team';
	}

	/**
	 * Get widget title.
	 * Retrieve button widget title.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return string Widget title.
	 */
	public function get_title() {
		return esc_html__( 'H1 Team', 'tranzix' );
	}

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

	/**
	 * Get widget categories.
	 * Retrieve the list of categories the button widget belongs to.
	 * Used to determine where to display the widget in the editor.
	 *
	 * @since  2.0.0
	 * @access public
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'tranzix' ];
	}
	
	/**
	 * Register button 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(
			'h1_team',
			[
				'label' => esc_html__( 'H1 Team', 'tranzix' ),
			]
		);
		$this->add_control(
			'bgimg',
			[
				'label' => esc_html__('Background image', 'rashid'),
				'type' => Controls_Manager::MEDIA,
				'default' => ['url' => Utils::get_placeholder_image_src(),],
			]
		);
		$this->add_control(
			'title',
			[
				'label'       => __( 'Title', 'rashid' ),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your title', 'rashid' ),
			]
		);
		$this->add_control(
			'text',
			[
				'label'       => __( 'Description Text', 'rashid' ),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your Description', 'rashid' ),
			]
		);
		$this->add_control(
			'bttn',
			[
				'label'       => __( 'Button', 'rashid' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your Button Title', 'rashid' ),
			]
		);	
		$this->add_control(
			'btnlink',
			[
			  'label' => __( 'Button Url', 'rashid' ),
			  'type' => Controls_Manager::URL,
			  'placeholder' => __( 'https://your-link.com', 'rashid' ),
			  'show_external' => true,
			  'default' => [
				'url' => '',
				'is_external' => true,
				'nofollow' => true,
			  ],
			
		   ]
		);
		$this->add_control(
			'column',
			[
				'label'   => esc_html__( 'Column', 'rashid' ),
				'type'    => Controls_Manager::SELECT,
				'default' => '3',
				'options' => array(
					'12'   => esc_html__( 'One Column', 'rashid' ),
					'6'   => esc_html__( 'Two Column', 'rashid' ),
					'4'   => esc_html__( 'Three Column', 'rashid' ),
					'3'   => esc_html__( 'Four Column', 'rashid' ),
					'2'   => esc_html__( 'Six Column', 'rashid' ),
				),
			]
		);
		$this->add_control(
			'sec_class',
			[
				'label'       => __( 'Section Class', 'rashid' ),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter Section Class', 'rashid' ),
			]
		);
		$this->add_control(
			'text_limit',
			[
				'label'   => esc_html__( 'Text Limit', 'tranzix' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 3,
				'min'     => 1,
				'max'     => 100,
				'step'    => 1,
			]
		);
		$this->add_control(
			'query_number',
			[
				'label'   => esc_html__( 'Number of post', 'tranzix' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 3,
				'min'     => 1,
				'max'     => 100,
				'step'    => 1,
			]
		);
		$this->add_control(
			'query_orderby',
			[
				'label'   => esc_html__( 'Order By', 'tranzix' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'date',
				'options' => array(
					'date'       => esc_html__( 'Date', 'tranzix' ),
					'title'      => esc_html__( 'Title', 'tranzix' ),
					'menu_order' => esc_html__( 'Menu Order', 'tranzix' ),
					'rand'       => esc_html__( 'Random', 'tranzix' ),
				),
			]
		);
		$this->add_control(
			'query_order',
			[
				'label'   => esc_html__( 'Order', 'tranzix' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'DESC',
				'options' => array(
					'DESc' => esc_html__( 'DESC', 'tranzix' ),
					'ASC'  => esc_html__( 'ASC', 'tranzix' ),
				),
			]
		);
		$this->add_control(
			'query_exclude',
			[
				'label'       => esc_html__( 'Exclude', 'tranzix' ),
				'type'        => Controls_Manager::TEXT,
				'description' => esc_html__( 'Exclude posts, pages, etc. by ID with comma separated.', 'tranzix' ),
			]
		);
		$this->add_control(
            'query_category', 
				[
				  'type' => Controls_Manager::SELECT,
				  'label' => esc_html__('Category', 'tranzix'),
				  'options' => get_team_categories()
				]
		);
		$this->add_control(
			'show_pagination',
			[
				'label' => __( 'Enable/Disable Pagination', 'tranzix' ),
				'type'     => Controls_Manager::SWITCHER,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enable/Disable Pagination', 'tranzix' ),
			]
		);
		
		
		$this->end_controls_section();

		
	}

	/**
	 * Render button widget output on the frontend.
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since  1.0.0
	 * @access protected
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();
        
        $paged = tranzix_set($_POST, 'paged') ? esc_attr($_POST['paged']) : 1;

		$this->add_render_attribute( 'wrapper', 'class', 'templatepath-tranzix' );
		$args = array(
			'post_type'      => 'tranzix_team',
			'posts_per_page' => tranzix_set( $settings, 'query_number' ),
			'orderby'        => tranzix_set( $settings, 'query_orderby' ),
			'order'          => tranzix_set( $settings, 'query_order' ),
			'paged'         => $paged
		);
		if ( tranzix_set( $settings, 'query_exclude' ) ) {
			$settings['query_exclude'] = explode( ',', $settings['query_exclude'] );
			$args['post__not_in']      = tranzix_set( $settings, 'query_exclude' );
		}
		if( tranzix_set( $settings, 'query_category' ) ) $args['team_cat'] = tranzix_set( $settings, 'query_category' );
		$query = new \WP_Query( $args );

		if ( $query->have_posts() ) 
		{
		?>
  
		<section class=" team-section-three <?php echo esc_attr($settings['sec_class']);?>">
			
			<?php  if ( esc_url($settings['bgimg']['id']) ) : ?>
			<div class="map-layer" style="background-image:url(<?php echo wp_get_attachment_url($settings['bgimg']['id']);?>)">
			<?php else :?>	
			<div class="noimage">
			<?php endif;?>
			</div>
			
			<div class="auto-container">
				<div class="row clearfix">
					<!-- Title Column -->
					<div class="title-column col-lg-5 col-md-12 col-sm-12">
						<div class="inner-column wow fadeInLeft" data-wow-delay="0ms" data-wow-duration="1500ms">
							
							
							<?php if($settings['title']): ?>
							<div class="sec-title">
								<h2><?php echo $settings['title'];?></h2>
								<div class="separator"></div>
							</div>
							<div class="text">
								<p><?php echo $settings['text'];?></p>
							</div> 
							
							<?php endif; ?>
							
							<?php if($settings['bttn']): ?>
							<a href="<?php echo esc_url($settings['btnlink']['url']);?>" class="theme-btn btn-style-four"><?php echo $settings['bttn'];?></a> 
							<?php endif; ?>
							
							</div>
					</div>
					<!-- Blocks Column -->
					<div class="blocks-column col-lg-7 col-md-12 col-sm-12">
						<div class="inner-column">
							<div class="row clearfix">
							
								<?php while ( $query->have_posts() ) : $query->the_post(); ?>
							
								<div class="team-block-three col-lg-<?php echo esc_attr($settings['column'], true );?> col-md-6 col-sm-12">
									<div class="inner-box wow fadeInUp" data-wow-delay="0ms" data-wow-duration="1500ms">
										<div class="image">
										
										
										<?php the_post_thumbnail(); ?> 
										
										
										
										</div>
										<div class="lower-content">
											<div class="content">
												<h3><?php the_title(); ?></h3>
												<div class="designation"><?php echo (get_post_meta( get_the_id(), 'designation', true ));?></div>
												
												
												<?php	
												$icons = get_post_meta( get_the_id(), 'social_profile', true );
													if ( ! empty( $icons ) ) :?>
												<div class="social-box">
												
													<?php
													foreach ( $icons as $h_icon ) :
													$header_social_icons = json_decode( urldecode( tranzix_set( $h_icon, 'data' ) ) );
													if ( tranzix_set( $header_social_icons, 'enable' ) == '' ) {
													continue;
													}
													$icon_class = explode( '-', tranzix_set( $header_social_icons, 'icon' ) );?>
												
													<a href="<?php echo tranzix_set( $header_social_icons, 'url' ); ?>" class="fab icon <?php echo esc_attr( tranzix_set( $header_social_icons, 'icon' ) ); ?>"></"></a>
													
													<?php endforeach; ?>
													
													
												</div>
												<?php endif; ?>
												
												
											</div>
										</div>
									</div>
								</div>
								
								<?php endwhile; ?>
								
							</div>
						</div>
					</div>
				</div>
			</div>
		</section>
		
  
        <?php }
		wp_reset_postdata();
	}

}