<?php
namespace Elementor;

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

class Champions extends Widget_Base {

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

	public function get_title() {
		return esc_html__( 'Champions', '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' => __( 'Champions', 'pubzi-core' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

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

		$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' ),
			]
		);

		$this->add_control(
			'subtitle',
			[
				'label'       => __( 'Sub Title', 'pubzi-core' ),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => ['active' => true],
				'placeholder' => __( 'Enter your title', 'pubzi-core' ),
				'default'     => __( 'Default Sub Title', 'pubzi-core' ),
			]
		);

        $this->end_controls_section();

        // ========== Slider Loop Section ==========
        $this->start_controls_section(
            'slider_loop_content',
            [
                'label' => __( 'Slider Items', 'pubzi-core' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $slider_content_repeater = new Repeater();

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

        $slider_content_repeater->add_control(
            'video_url',
			[
				'name' => 'video_url',
				'label' => __( 'Video Url', 'pubzi-core' ),
				'type' => Controls_Manager::URL,
				'placeholder' => __( 'https://your-link.com', 'pubzi-core' ),
				'show_external' => true,
				'default' => [
					'url' => '',
					'is_external' => true,
					'nofollow' => true,
				],
			],
        );

		 $slider_content_repeater->add_control(
            'matches_champ_name',
			[
				'label'   => esc_html__('Champ name', 'pubzi-core'),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => '',
			]
        );

		$slider_content_repeater->add_control(
            'matches_title',
			[
				'label'   => esc_html__('Matches Title', 'pubzi-core'),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => '',
			]
        );

		$slider_content_repeater->add_control(
            'matches_title_url',
          	[
				'name' => 'matches_title_url',
				'label' => __( 'Title Url', 'pubzi-core' ),
				'type' => Controls_Manager::URL,
				'placeholder' => __( 'https://your-link.com', 'pubzi-core' ),
				'show_external' => true,
				'default' => [
					'url' => '',
					'is_external' => true,
					'nofollow' => true,
				],
			],
        );

        $repeater = new Repeater();

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

        $repeater->add_control(
			'slider_content_loop',
			[
				'label'   => __( 'Slider Content Repeater', 'pubzi-core' ),
				'type'    => Controls_Manager::REPEATER,
				'fields'  => $slider_content_repeater->get_controls(),
				'default' => [],
			]
		);

        $this->add_control(
            'tab_nav_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');

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

		?>

		<section class="champion-section section-padding fix bg-cover" style="background-image: url(<?php echo wp_get_attachment_url($settings['bg_image']['id']);?>);">
            <div class="container">
                <div class="section-title-area">
                    <div class="section-title style-3 mb-0">
                    <h2 class="t-uppercase wow fadeInUp" data-wow-delay=".3s">
					<?php if ( !empty($settings['image']['id']) ) : ?>   
						<img src="<?php echo esc_url(wp_get_attachment_url($settings['image']['id']));?>" alt=""/>
					<?php endif;?>  
						<?php echo wp_kses($settings['title'], $allowed_tags);?>
					</h2>
                    <p class="mt-3 ms-5 wow fadeInUp" data-wow-delay=".5s">
                       <?php echo wp_kses($settings['subtitle'], $allowed_tags);?>
                    </p>
                </div>
                 <div class="right-ber">
                    <ul class="nav" role="tablist">
						<?php foreach($tab_nav_loop as $key=>$item): ?>
                        <li class="nav-item style-2 wow fadeInUp" data-wow-delay=".4s">
                            <a href="#feature<?php echo esc_attr($key);?>" data-bs-toggle="tab" class="nav-link <?php if($key == 1) echo 'active';?>" aria-selected="<?php echo $key == 0 ? 'true' : 'false'; ?>" role="tab">
                                
							<?php echo wp_kses($item['section_title'], $allowed_tags);?>

                            </a>
                        </li>
						<?php endforeach; ?>
                    </ul>
                    <div class="array-btn">
                        <button class="array-prev">
                            <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none">
                            <g clip-path="url(#clip0_200_860)">
                                <path d="M-7.36055e-07 1.1603L-1.41494e-07 14.7623C-1.13526e-07 15.4021 0.520312 15.9224 1.16016 15.9224C1.8 15.9224 2.32031 15.4021 2.32031 14.7623L2.32031 3.96577L16.0172 17.6591C16.4707 18.1126 17.2055 18.1126 17.659 17.6591C18.1125 17.2056 18.1125 16.4708 17.659 16.0173L3.96211 2.32397L14.7586 2.32397C15.3984 2.32397 15.9187 1.80366 15.9187 1.16382C15.9187 0.523975 15.3984 0.00366144 14.7586 0.00366146L1.16016 0.00366206C0.520312 0.000146843 -7.64023e-07 0.520458 -7.36055e-07 1.1603Z" fill="#0B0E13"/>
                            </g>
                            <defs>
                                <clipPath id="clip0_200_860">
                                <rect width="18" height="18" fill="white" transform="translate(0 18.0002) rotate(-90)"/>
                                </clipPath>
                            </defs>
                            </svg>
                        </button>
                        <button class="array-next">
                            <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none">
                            <g clip-path="url(#clip0_200_853)">
                                <path d="M17.9941 16.8402L17.9941 3.23823C17.9941 2.59839 17.4738 2.07808 16.834 2.07808C16.1941 2.07808 15.6738 2.59839 15.6738 3.23823L15.6738 14.0347L1.97695 0.341357C1.52344 -0.112159 0.788671 -0.112159 0.335157 0.341357C-0.118359 0.794872 -0.118359 1.52964 0.335157 1.98315L14.032 15.6765L3.23555 15.6765C2.5957 15.6765 2.07539 16.1968 2.07539 16.8367C2.07539 17.4765 2.5957 17.9968 3.23555 17.9968L16.834 17.9968C17.4738 18.0003 17.9941 17.48 17.9941 16.8402Z" fill="#0B0E13"/>
                            </g>
                            <defs>
                                <clipPath id="clip02_200_853">
                                <rect width="18" height="18" fill="white" transform="translate(17.9941 0.000244141) rotate(90)"/>
                                </clipPath>
                            </defs>
                            </svg>
                        </button>
                  </div>
                 </div>
                </div>
            </div>
            <div class="tab-content mt-0 pt-0">

				<?php foreach($tab_nav_loop as $key=>$item): ?>
                <div id="feature<?php echo esc_attr($key);?>" class="tab-pane fade <?php if($key == 1) echo 'show active';?>" role="tabpanel">
                    <div class="swiper card-slider">
                        <div class="swiper-wrapper">

							<?php if (!empty($item['slider_content_loop'])) : 
							foreach ( $item['slider_content_loop'] as $platform ) : ?>
                            <div class="swiper-slide">
                                <div class="champion-card-item">
                                    <div class="champion-image">
                                        <?php if(!empty($platform['matches_team_image']['id'])): ?>
											<img src="<?php echo esc_url(wp_get_attachment_url($platform['matches_team_image']['id']));?>" alt="">
										<?php endif; ?>
                                        <a href="<?php echo esc_url($platform['video_url']['url']);?>" class="video-btn video-popup">
                                        <i class="fa-solid fa-play"></i>
                                        </a>
                                        <div class="champion-content">
                                            <span><?php echo wp_kses($platform['matches_champ_name'], $allowed_tags);?></span>
                                            <h4>
                                                <a href="<?php echo esc_url($platform['matches_title_url']['url']);?>"><?php echo wp_kses($platform['matches_title'], $allowed_tags);?></a>
                                            </h4>
                                        </div>
                                    </div>
                                </div>
                            </div>
							<?php endforeach; 
							endif; ?>

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

            </div>
        </section>



		<?php 
	}
}


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