<?php
namespace Elementor;

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

class Price_List extends Widget_Base {

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

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

        $this->end_controls_section();

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

        $slider_content_repeater = new Repeater();



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

		 $slider_content_repeater->add_control(
            'package_name',
			[
				'label'   => esc_html__('Package name', 'pubzi-core'),
				'type'    => Controls_Manager::TEXT,
				'default' => '',
			]
        );

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

		$slider_content_repeater->add_control(
            'package_price',
			[
				'label'   => esc_html__('Package Price', 'pubzi-core'),
				'type'    => Controls_Manager::TEXT,
				'default' => '',
			]
        );

		$slider_content_repeater->add_control(
            'package_des',
            [
				'label'   => esc_html__('Package Description', 'pubzi-core'),
				'type'    => Controls_Manager::TEXT,
				'default' => '',
			]
        );

		$slider_content_repeater->add_control(
            'price_icon_image',
			[
				'label'   => __( 'Price List Icon', 'pubzi-core' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => ['url' => Utils::get_placeholder_image_src()],
			]
        );

		$slider_content_repeater->add_control(
            'price_des',
            [
				'label'   => esc_html__('Price List Description 1', 'pubzi-core'),
				'type'    => Controls_Manager::TEXT,
				'default' => '',
			]
        );

		$slider_content_repeater->add_control(
            'price_des2',
            [
				'label'   => esc_html__('Price List Description 2', 'pubzi-core'),
				'type'    => Controls_Manager::TEXT,
				'default' => '',
			]
        );

		$slider_content_repeater->add_control(
            'price_des3',
            [
				'label'   => esc_html__('Price List Description 3', 'pubzi-core'),
				'type'    => Controls_Manager::TEXT,
				'default' => '',
			]
        );

		$slider_content_repeater->add_control(
            'price_des4',
            [
				'label'   => esc_html__('Price List Description 4', 'pubzi-core'),
				'type'    => Controls_Manager::TEXT,
				'default' => '',
			]
        );

		$slider_content_repeater->add_control(
            'price_des5',
            [
				'label'   => esc_html__('Price List Description 5', 'pubzi-core'),
				'type'    => Controls_Manager::TEXT,
				'default' => '',
			]
        );

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

		$slider_content_repeater->add_control(
            'btn_shape2',
			[
				'label'   => __( 'Button Shape 2', 'pubzi-core' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => ['url' => Utils::get_placeholder_image_src()],
			]
        );

		$slider_content_repeater->add_control(
            'price_button_link',
			[
				'label' => __( 'Button 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(
            'section_col_number',
			[
				'name' => 'section_col_number',
				'label' => __( 'Select Column', 'pubzi-core' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'col-xl-12' => __( 'Col XL 12', 'pubzi-core' ),
					'col-xl-9' => __( 'Col XL 9', 'pubzi-core' ),
					'col-xl-8' => __( 'Col XL 8', 'pubzi-core' ),
					'col-xl-7' => __( 'Col XL 7', 'pubzi-core' ),
					'col-xl-6' => __( 'Col XL 6', 'pubzi-core' ),
					'col-xl-5' => __( 'Col XL 5', 'pubzi-core' ),
					'col-xl-4' => __( 'Col XL 4', 'pubzi-core' ),
					'col-xl-3' => __( 'Col XL 3', 'pubzi-core' ),
					],
					'default' => 'col-xl-6',
			],
        );

        $repeater = new Repeater();

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

        $repeater->add_control(
			'slider_content_loop',
			[
				'label'   => __( 'Price 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'] : [];

		$count = 0;
		?>

		 <div class="pricing__tab-content m-0 p-0">
			<div class="tab-content m-0 p-0" id="nav-tabContent">

				<?php foreach($tab_nav_loop as $key=>$item): ?>
				<div class="tab-pane fade <?php if($key == 0) echo 'show active';?>" id="pt-1<?php echo esc_attr($key);?>" role="tabpanel" aria-labelledby="pt-1-tab<?php echo esc_attr($key);?>">
					<div class="row g-4">

						<?php if (!empty($item['slider_content_loop'])) : 
						foreach ( $item['slider_content_loop'] as $platform ) : 
						$count++;
						?>
						<div class="<?php echo esc_attr($platform['section_col_number']); ?>">
							<div class="gt-pricing-box-items-2" style="background-image: url('<?php echo esc_url(wp_get_attachment_url($platform['price_bg_image']['id']));?>');">
								<?php if ( $count % 2 == 1 ) : ?>
								<span class="gt-price-tag"><?php echo esc_html($platform['package_name']);?></span>
								<?php else : ?>
								<span class="gt-price-tag gt-bg-white"><?php echo esc_html($platform['package_name']);?></span>
								<?php endif; ?>
								<div class="gt-pricing-header">
									<div class="gt-icon">
										<?php if(!empty($platform['price_image']['id'])): ?>
											<img src="<?php echo esc_url(wp_get_attachment_url($platform['price_image']['id']));?>" alt="">
										<?php endif; ?>
									</div>
									<h2><?php echo esc_html($platform['package_price']);?></h2>
									<p>
										<?php echo esc_html($platform['package_des']);?>
									</p>
								</div>
								<ul class="gt-pricing-list">
									<?php if($platform['price_des']): ?>
									<li>
											<?php if(!empty($platform['price_icon_image']['id'])): ?>
											<img src="<?php echo esc_url(wp_get_attachment_url($platform['price_icon_image']['id']));?>" alt="">
										<?php endif; ?>
										<?php echo esc_html($platform['price_des']);?>
									</li>
									<?php endif; ?>
									<?php if($platform['price_des2']): ?>
									<li>
											<?php if(!empty($platform['price_icon_image']['id'])): ?>
											<img src="<?php echo esc_url(wp_get_attachment_url($platform['price_icon_image']['id']));?>" alt="">
										<?php endif; ?>
										<?php echo esc_html($platform['price_des2']);?>
									</li>
									<?php endif; ?>
									<?php if($platform['price_des3']): ?>
									<li>
											<?php if(!empty($platform['price_icon_image']['id'])): ?>
											<img src="<?php echo esc_url(wp_get_attachment_url($platform['price_icon_image']['id']));?>" alt="">
										<?php endif; ?>
										<?php echo esc_html($platform['price_des3']);?>
									</li>
									<?php endif; ?>
									<?php if($platform['price_des4']): ?>
									<li>
											<?php if(!empty($platform['price_icon_image']['id'])): ?>
											<img src="<?php echo esc_url(wp_get_attachment_url($platform['price_icon_image']['id']));?>" alt="">
										<?php endif; ?>
										<?php echo esc_html($platform['price_des4']);?>
									</li>
									<?php endif; ?>
									<?php if($platform['price_des5']): ?>
									<li>
										<?php if(!empty($platform['price_icon_image']['id'])): ?>
											<img src="<?php echo esc_url(wp_get_attachment_url($platform['price_icon_image']['id']));?>" alt="">
										<?php endif; ?>
										<?php echo esc_html($platform['price_des5']);?>
									</li>
									<?php endif; ?>
								</ul>
								<div class="gt-pricing-btn">
									<div class="gt-bg-shape">
										<?php if(!empty($platform['btn_shape']['id'])): ?>
											<img src="<?php echo esc_url(wp_get_attachment_url($platform['btn_shape']['id']));?>" alt="">
										<?php endif; ?>
									</div>
									<div class="gt-bg-shape-hover">
										<?php if(!empty($platform['btn_shape2']['id'])): ?>
											<img src="<?php echo esc_url(wp_get_attachment_url($platform['btn_shape2']['id']));?>" alt="">
										<?php endif; ?>
									</div>
									<a href="<?php echo esc_url($platform['price_button_link']['url']);?>" class="gt-pricing-text">
										choose plan <i class="fa-solid fa-arrow-right"></i>
									</a>
								</div>
							</div>
						</div>
						<?php endforeach; 
						endif; ?>

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

			</div>
		</div>





		<?php 
	}
}


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