<?php
namespace Elementor;

/**
 * Elementor Widget
 * @package Pubzi
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class Team_Grid extends Widget_Base {

    public function get_name() {
        return 'pubzi-team-grid-widget';
    }

    public function get_title() {
        return esc_html__( 'Team Grid', 'pubzi-core' );
    }

    public function get_icon() {
        return 'eicon-flash';
    }

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

    protected function register_controls() {

        $this->start_controls_section(
            'content_section',
            [
                'label' => __( 'Team Styles', 'pubzi-core' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'style',
            [
                'label'   => esc_html__( 'Style', 'pubzi-core' ),
                'type'    => Controls_Manager::SELECT,
                'default' => 'style1',
                'options' => [
                    'style1' => esc_html__( 'Style 01', 'pubzi-core' ),
                    'style2' => esc_html__( 'Style 02', 'pubzi-core' ),
                    'style3' => esc_html__( 'Style 03', 'pubzi-core' ),
                    'style4' => esc_html__( 'Style 04', 'pubzi-core' ),
                ],
            ]
        );

		$this->add_control(
			'image',
				[
				  'label' => __( 'Image', 'pubzi-core' ),
				  'type' => Controls_Manager::MEDIA,
				  'default' => ['url' => Utils::get_placeholder_image_src(),],
				  'condition'	=> ['style' => ['style2']],
				]
		);	

		$this->add_control(
			'subtitle',
			[
				'label'       => __( 'Sub Title', 'pubzi-core' ),
				'type'        => Controls_Manager::TEXTAREA,
				'default' => esc_html__('Get specialist cargo services worldwide', 'pubzi-core'),
				'dynamic'     => [
					'active' => true,
				],
				'condition'	=> ['style' => ['style2', 'style4']],
			]
		);
		
		$this->add_control(
			'title',
			[
				'label'       => __( 'Title', 'pubzi-core' ),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your title', 'pubzi-core' ),
				'default'     => __( 'Default Title', 'pubzi-core' ),
				'condition'	=> ['style' => ['style2', 'style4']],
			]
		);	

        $this->end_controls_section();

        $this->start_controls_section(
            'team_list_items',
            [
                'label' => __( 'Team List Items', 'pubzi-core' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        // Nested repeater for socials
        $socials_repeater = new Repeater();

        $socials_repeater->add_control(
            'socials_icon',
            [
                'label'   => __( 'Socials Icon', 'pubzi-core' ),
                'type'    => Controls_Manager::ICONS,
                'default' => [
                    'value'   => 'fab fa-facebook-f',
                    'library' => 'fa-brands',
                ],
            ]
        );

        $socials_repeater->add_control(
            'socials_link',
            [
                'label'         => __( 'Socials Link', 'pubzi-core' ),
                'type'          => Controls_Manager::URL,
                'show_external' => true,
                'default'       => [
                    'url'         => '#',
                    'is_external' => true,
                    'nofollow'    => true,
                ],
            ]
        );

        // Main repeater for team items
        $repeater = new Repeater();

        $repeater->add_control(
            'section_title',
            [
                'label'   => __( 'Title', 'pubzi-core' ),
                'type'    => Controls_Manager::TEXT,
                'default' => __( 'John Doe', 'pubzi-core' ),
            ]
        );

        $repeater->add_control(
            'section_subtitle',
            [
                'label'   => __( 'Sub Title', 'pubzi-core' ),
                'type'    => Controls_Manager::TEXT,
                'default' => __( 'Designer', 'pubzi-core' ),
            ]
        );

        $repeater->add_control(
            'section_image',
            [
                'label'   => __( 'Team Photo', 'pubzi-core' ),
                'type'    => Controls_Manager::MEDIA,
                'default' => [ 'url' => Utils::get_placeholder_image_src() ],
            ]
        );

        $repeater->add_control(
            'section_image2',
            [
                'label'   => __( 'Shape Image', 'pubzi-core' ),
                'type'    => Controls_Manager::MEDIA,
                'default' => [ 'url' => Utils::get_placeholder_image_src() ],
            ]
        );

        $repeater->add_control(
            'section_button_link',
            [
                'label'         => __( 'Profile Link', 'pubzi-core' ),
                'type'          => Controls_Manager::URL,
                'show_external' => true,
                'default'       => [
                    'url'         => '#',
                    'is_external' => true,
                    'nofollow'    => true,
                ],
            ]
        );

       $repeater->add_control(
		'socials_loop',
			[
				'label'   => __( 'Repeater Socials', 'pubzi-core' ),
				'type'    => Controls_Manager::REPEATER,
				'fields'  => $socials_repeater->get_controls(),
				'default' => [
					[
						'socials_icon' => [ 'value' => 'fab fa-facebook-f', 'library' => 'fa-brands' ],
						'socials_link' => [ 'url' => '#', 'is_external' => true, 'nofollow' => true ],
					],
					[
						'socials_icon' => [ 'value' => 'fab fa-linkedin-in', 'library' => 'fa-brands' ],
						'socials_link' => [ 'url' => '#', 'is_external' => true, 'nofollow' => true ],
					],
					[
						'socials_icon' => [ 'value' => 'fab fa-instagram', 'library' => 'fa-brands' ],
						'socials_link' => [ 'url' => '#', 'is_external' => true, 'nofollow' => true ],
					],
					[
						'socials_icon' => [ 'value' => 'fab fa-behance', 'library' => 'fa-brands' ],
						'socials_link' => [ 'url' => '#', 'is_external' => true, 'nofollow' => true ],
					],
				],
			]
		);


        $this->add_control(
            'team_loop',
            [
                'type'        => Controls_Manager::REPEATER,
                'fields'      => $repeater->get_controls(),
                'title_field' => '{{{ section_title }}}',
                'default'     => [],
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $allowed_tags = wp_kses_allowed_html( 'post' );

        $team_loop = ! empty( $settings['team_loop'] ) && is_array( $settings['team_loop'] ) ? $settings['team_loop'] : [];

        ?>

        <?php if ( 'style1' === $settings['style'] ) : ?>

            <div class="team-section">
                <div class="swiper team-slider">
                    <div class="swiper-wrapper">
                        <?php foreach ( $team_loop as $item ) : ?>
                            <div class="swiper-slide">
                                <div class="team-box-items">
                                    <div class="content">
                                        <h3>
                                            <a href="<?php echo esc_url( ! empty( $item['section_button_link']['url'] ) ? $item['section_button_link']['url'] : '#' ); ?>">
                                                <?php echo wp_kses( $item['section_title'], $allowed_tags ); ?>
                                            </a>
                                        </h3>
                                        <p><?php echo wp_kses( $item['section_subtitle'], $allowed_tags ); ?></p>
                                    </div>

                                    <div class="thumb">
                                        <?php if ( ! empty( $item['section_image']['id'] ) ) :
                                            $img_src = wp_get_attachment_url( $item['section_image']['id'] );
                                            if ( $img_src ) : ?>
                                                <img src="<?php echo esc_url( $img_src ); ?>" alt="<?php echo esc_attr( ! empty( $item['section_title'] ) ? $item['section_title'] : 'team' ); ?>">
                                            <?php endif;
                                        endif; ?>

                                        <div class="social-icon d-flex align-items-center">

											<?php foreach ( $item['socials_loop'] as $platform ) : ?>
												<a href="<?php echo esc_url( $platform['socials_link']['url'] ); ?>">
													<?php if ( ! empty( $platform['socials_icon']['value'] ) ) : ?>
														<i class="<?php echo esc_attr( $platform['socials_icon']['value'] ); ?>"></i>
													<?php endif; ?>
												</a>
											<?php endforeach; ?>

                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>

       	<?php  elseif ( 'style2' === $settings['style'] ) : ?>		

		  <section class="team-section-2 fix section-padding pt-0">
            <div class="team-shape">
               <?php  if ( !empty(esc_url($settings['image']['id']) )) : ?>   
					<img src="<?php echo wp_get_attachment_url($settings['image']['id']);?>" alt=""/>
				<?php endif;?>
            </div>
            <div class="container">
                <div class="section-title-2 text-center">
                    <h6 class="wow fadeInUp">
						<?php echo $settings['subtitle'];?>
					</h6>
                    <h2 class="wow fadeInUp" data-wow-delay=".3s">
                     	<?php echo $settings['title'];?>
                    </h2>
                </div>
				<div class="swiper team-slider-2">
					<div class="swiper-wrapper">
						<?php foreach ( $team_loop as $item ) : ?>
						<div class="swiper-slide">
							<div class="team-single-style-2">
								<div class="team-bg">
									<?php if(!empty(wp_get_attachment_url($item['section_image2']['id']))): ?>
										<img src="<?php echo wp_get_attachment_url($item['section_image2']['id']);?>" alt="">
									<?php endif;?>
								</div>
									<div class="social-icon align-items-center">
										<?php foreach ( $item['socials_loop'] as $platform ) : ?>
											<a href="<?php echo esc_url( $platform['socials_link']['url'] ); ?>">
												<?php if ( ! empty( $platform['socials_icon']['value'] ) ) : ?>
													<i class="<?php echo esc_attr( $platform['socials_icon']['value'] ); ?>"></i>
												<?php endif; ?>
											</a>
										<?php endforeach; ?>
									</div>
								<div class="thumb">
									<?php if ( ! empty( $item['section_image']['id'] ) ) :
										$img_src = wp_get_attachment_url( $item['section_image']['id'] );
										if ( $img_src ) : ?>
											<img src="<?php echo esc_url( $img_src ); ?>" alt="<?php echo esc_attr( ! empty( $item['section_title'] ) ? $item['section_title'] : 'team' ); ?>">
										<?php endif;
									endif; ?>
									<div class="team-content">
										<h3>
											<a href="<?php echo esc_url( ! empty( $item['section_button_link']['url'] ) ? $item['section_button_link']['url'] : '#' ); ?>">
												<?php echo wp_kses( $item['section_title'], $allowed_tags ); ?>
											</a>
										</h3>
										<p><?php echo wp_kses( $item['section_subtitle'], $allowed_tags ); ?></p>
									</div>
								</div>
							</div>
						</div>
						<?php endforeach; ?>
					</div>
				</div>
            </div>
            <div class="array-button d-grid align-items-center">
                <button class="array-prev">
                    <svg xmlns="http://www.w3.org/2000/svg" width="19" height="18" viewBox="0 0 19 18" fill="none">
                        <g clip-path="url(#clip0_46_137)">
                            <path d="M0.499999 1.16006L0.5 14.762C0.5 15.4019 1.02031 15.9222 1.66016 15.9222C2.3 15.9222 2.82031 15.4019 2.82031 14.762L2.82031 3.96553L16.5172 17.6589C16.9707 18.1124 17.7055 18.1124 18.159 17.6589C18.6125 17.2054 18.6125 16.4706 18.159 16.0171L4.46211 2.32373L15.2586 2.32373C15.8984 2.32373 16.4187 1.80342 16.4187 1.16357C16.4187 0.523731 15.8984 0.0034173 15.2586 0.00341732L1.66016 0.00341792C1.02031 -9.72975e-05 0.499999 0.520214 0.499999 1.16006Z" fill="#0B0E13"/>
                        </g>
                        <defs>
                            <clipPath id="clip0_46_137">
                            <rect width="18" height="18" fill="white" transform="translate(0.5 18) rotate(-90)"/>
                            </clipPath>
                        </defs>
                    </svg>
                </button>
                <button class="array-next">
                    <svg xmlns="http://www.w3.org/2000/svg" width="19" height="18" viewBox="0 0 19 18" fill="none">
                    <g clip-path="url(#clip0_46_130)">
                        <path d="M18.5 16.8399L18.5 3.23799C18.5 2.59814 17.9797 2.07783 17.3398 2.07783C16.7 2.07783 16.1797 2.59814 16.1797 3.23799L16.1797 14.0345L2.48281 0.341113C2.0293 -0.112403 1.29453 -0.112403 0.841017 0.341112C0.387501 0.794628 0.387501 1.52939 0.841017 1.98291L14.5379 15.6763L3.74141 15.6763C3.10156 15.6763 2.58125 16.1966 2.58125 16.8364C2.58125 17.4763 3.10156 17.9966 3.74141 17.9966L17.3398 17.9966C17.9797 18.0001 18.5 17.4798 18.5 16.8399Z" fill="#0B0E13"/>
                    </g>
                    <defs>
                        <clipPath id="clip0_46_130">
                        <rect width="18" height="18" fill="white" transform="translate(18.5) rotate(90)"/>
                        </clipPath>
                    </defs>
                    </svg>
                </button>
            </div>
        </section>

        <?php  elseif ( 'style3' === $settings['style'] ) : ?>	

            <div class="gt-team-wrapper gt-style-2">
                <div class="row g-4">

                    <?php foreach ( $team_loop as $item ) : ?>
                    <div class="col-xl-4 col-lg-6 col-md-6">
                        <div class="team-single-style-2 mt-0">
                            <div class="team-bg">
                              	<?php if(!empty(wp_get_attachment_url($item['section_image2']['id']))): ?>
                                    <img src="<?php echo wp_get_attachment_url($item['section_image2']['id']);?>" alt="">
                                <?php endif;?>
                            </div>
                             <div class="social-icon align-items-center">
                                <?php foreach ( $item['socials_loop'] as $platform ) : ?>
                                    <a href="<?php echo esc_url( $platform['socials_link']['url'] ); ?>">
                                        <?php if ( ! empty( $platform['socials_icon']['value'] ) ) : ?>
                                            <i class="<?php echo esc_attr( $platform['socials_icon']['value'] ); ?>"></i>
                                        <?php endif; ?>
                                    </a>
                                <?php endforeach; ?>
                                </div>
                            <div class="thumb">
                                <?php if ( ! empty( $item['section_image']['id'] ) ) :
                                    $img_src = wp_get_attachment_url( $item['section_image']['id'] );
                                    if ( $img_src ) : ?>
                                        <img src="<?php echo esc_url( $img_src ); ?>" alt="<?php echo esc_attr( ! empty( $item['section_title'] ) ? $item['section_title'] : 'team' ); ?>">
                                    <?php endif;
                                endif; ?>
                                <div class="team-content">
                                  	<h3>
                                        <a href="<?php echo esc_url( ! empty( $item['section_button_link']['url'] ) ? $item['section_button_link']['url'] : '#' ); ?>">
                                            <?php echo wp_kses( $item['section_title'], $allowed_tags ); ?>
                                        </a>
                                    </h3>
                                    <p><?php echo wp_kses( $item['section_subtitle'], $allowed_tags ); ?></p>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>

                </div>
            </div>

        <?php  elseif ( 'style4' === $settings['style'] ) : ?>	

        <section class="gt-team-section-4 fix section-padding section-padding">
            <div class="container">
                <?php if($settings['title']): ?>
                <div class="section-title-2 text-center">
                    <h6 class="wow fadeInUp"><?php echo $settings['subtitle'];?></h6>
                    <h2 class="wow fadeInUp" data-wow-delay=".3s">
                        <?php echo $settings['title'];?>
                    </h2>
                </div>
                <?php endif; ?>
           </div>
            <div class="gt-team-wrapper-4">
                <?php foreach ( $team_loop as $item ) : ?>
               <div class="gt-team-area">
                    <div class="container">
                        <div class="gt-team-box-items-2 bor-top">
                            <div class="gt-team-content">
                                <span><?php echo wp_kses( $item['section_subtitle'], $allowed_tags ); ?></span>
                                <h3>
                                    <a href="<?php echo esc_url( ! empty( $item['section_button_link']['url'] ) ? $item['section_button_link']['url'] : '#' ); ?>">
                                        <?php echo wp_kses( $item['section_title'], $allowed_tags ); ?>
                                    </a>
                                </h3>
                            </div>
                            <div class="gt-social-icon d-flex align-items-center">
                              <?php foreach ( $item['socials_loop'] as $platform ) : ?>
                                <a href="<?php echo esc_url( $platform['socials_link']['url'] ); ?>">
                                    <?php if ( ! empty( $platform['socials_icon']['value'] ) ) : ?>
                                        <i class="<?php echo esc_attr( $platform['socials_icon']['value'] ); ?>"></i>
                                    <?php endif; ?>
                                </a>
                            <?php endforeach; ?>
                            </div>
                            <div class="gt-thumb">
                                <?php if ( ! empty( $item['section_image']['id'] ) ) :
                                    $img_src = wp_get_attachment_url( $item['section_image']['id'] );
                                    if ( $img_src ) : ?>
                                        <img src="<?php echo esc_url( $img_src ); ?>" alt="<?php echo esc_attr( ! empty( $item['section_title'] ) ? $item['section_title'] : 'team' ); ?>">
                                    <?php endif;
                                endif; ?>
                            </div>
                        </div>
                    </div>
               </div>
                <?php endforeach; ?>
            </div>
        </section>

		<?php endif ;?>	

        <?php
    }
}

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

