<?php
if ( ! class_exists( 'Draly_Dropshipping_Starter_Elementor_Verticalaccordion' ) ) {
	class Draly_Dropshipping_Starter_Elementor_Verticalaccordion extends Draly_Dropshipping_Starter_Elementor {
		public $name = 'verticalaccordion';
		public $title = 'Vertical Accordion';
		public $icon = 'eicon-slider-push';

		/**
		 * Register the 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() {
			//General Section
			$this->start_controls_section(
				'general_section',
				[
					'label' => esc_html__( 'General', 'draly-dropshipping-starter' ),
				]
			);
			$this->add_responsive_control(
				'height', [
					'label'       => esc_html__( 'Height (px)', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SLIDER,
					'size_units'  => [ 'px' ],
					'range'       => [
						'px' => [
							'min' => 100,
							'max' => 2000,
						],
					],
					'selectors'   => [
						'{{WRAPPER}} .verticalaccordion-item' => 'height: {{SIZE}}{{UNIT}};',
					],
					'label_block' => true
				]
			);
			$repeater = new \Elementor\Repeater();
			$repeater->add_group_control(
				\Elementor\Group_Control_Background::get_type(),
				[
					'name'     => 'background',
					'types'    => [ 'classic' ],
					'selector' => '{{WRAPPER}} {{CURRENT_ITEM}}',
				]
			);
			$repeater->add_control(
				'label', [
					'label'       => esc_html__( 'Label', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::TEXT,
					'placeholder' => esc_html__( 'Enter your label', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$repeater->add_control(
				'title', [
					'label'       => esc_html__( 'Title', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::TEXT,
					'placeholder' => esc_html__( 'Enter your title', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$repeater->add_control(
				'link_text', [
					'label'       => esc_html__( 'Link Text', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::TEXT,
					'placeholder' => esc_html__( 'Enter your link text', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$repeater->add_control(
				'link_url', [
					'label'         => esc_html__( 'Link URL', 'draly-dropshipping-starter' ),
					'type'          => \Elementor\Controls_Manager::URL,
					'placeholder'   => esc_html__( 'https://your-link.com', 'draly-dropshipping-starter' ),
					'show_external' => true,
					'default'       => [
						'url'         => '',
						'is_external' => true,
						'nofollow'    => false,
					],
					'label_block'   => true
				]
			);
			$this->add_control(
				'list',
				[
					'label'       => __( 'List', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::REPEATER,
					'fields'      => $repeater->get_controls(),
					'title_field' => '{{ title }}',
				]
			);
			$this->end_controls_section();
		}

		/**
		 * Render the widget output on the frontend.
		 *
		 * Written in PHP and used to generate the final HTML.
		 *
		 * @since 1.0.0
		 *
		 * @access protected
		 */
		protected function render() {
			$atts      = $this->get_settings_for_display();
			$css_class = array( 'draly-verticalaccordion' );
			if ( ! empty( $atts['list'] ) ) {
				?>
                <div class="<?php echo esc_attr( implode( ' ', $css_class ) ); ?>">
					<?php foreach ( $atts['list'] as $index => $list ) { ?>
                        <div class="elementor-repeater-item-<?php echo esc_attr( $list['_id'] ); ?> verticalaccordion-item">
                            <div class="verticalaccordion-info">
								<?php if ( $list['label'] ): ?>
                                    <div class="subtitle"><?php echo esc_html( $list['label'] ); ?></div>
								<?php endif; ?>
								<?php if ( $list['title'] ): ?>
                                    <h4 class="title"><?php echo esc_html( $list['title'] ); ?></h4>
								<?php endif; ?>
								<?php
								if ( $list['link_text'] && $list['link_url']['url'] ):
									$target = $list['link_url']['is_external'] ? '_blank' : '_self';
									?>
                                    <a class="verticalaccordion-button"
                                       href="<?php echo esc_url( $list['link_url']['url'] ); ?>"
                                       target="<?php echo esc_attr( $target ); ?>"><span><?php echo esc_html( $list['link_text'] ); ?></span>
                                        <svg width="17" height="13" viewBox="0 0 17 13" fill="none"
                                             xmlns="http://www.w3.org/2000/svg">
                                            <path d="M11.4722 12.2051C11.94 11.7513 12.408 11.2987 12.8763 10.8475L16.0339 7.7953C16.1136 7.71669 16.1962 7.63533 16.2803 7.55788C16.4333 7.41165 16.5912 7.2607 16.7447 7.10704L16.7579 7.09407C16.9096 6.94435 16.9965 6.74264 16.9999 6.53211C17.0033 6.32158 16.923 6.11896 16.7762 5.96762L11.779 0.943093L11.562 0.722934C11.409 0.563433 11.246 0.402811 11.0843 0.243356L11.0768 0.235966C10.9986 0.15902 10.9054 0.0984992 10.8027 0.057997C10.7 0.0174947 10.5898 -0.00216366 10.4788 0.000188737C10.3742 0.00115462 10.2706 0.023007 10.1743 0.064427C10.0781 0.105847 9.99113 0.165976 9.91873 0.241186C9.77046 0.391241 9.68615 0.591367 9.68342 0.79975C9.68068 1.00813 9.75973 1.20865 9.90404 1.35941L14.2362 5.71493L0.826591 5.65814C0.715475 5.65863 0.605366 5.68121 0.502852 5.72453C0.400337 5.76786 0.307524 5.83103 0.229966 5.91027C0.155393 5.98373 0.0965706 6.0712 0.0570638 6.16737C0.017557 6.26354 -0.00181132 6.36641 0.000133166 6.46975C0.00114047 6.6788 0.0846146 6.87841 0.232895 7.02635C0.381176 7.17429 0.582631 7.25894 0.794637 7.26241L14.2117 7.31956L11.9391 9.51719L10.48 10.9322C10.4023 11.0088 10.3221 11.0839 10.2428 11.1598C10.0866 11.3092 9.92472 11.4633 9.76763 11.6205L9.75567 11.6323C9.6769 11.7102 9.61466 11.8027 9.57263 11.9044C9.5306 12.0061 9.50963 12.1149 9.51099 12.2243C9.51075 12.3272 9.53169 12.429 9.57257 12.5233C9.61344 12.6177 9.67339 12.7026 9.74879 12.7731C9.89964 12.9184 10.1024 12.9998 10.3145 13C10.5265 13.0002 10.7312 12.9193 10.8854 12.7743L11.4722 12.2051Z"
                                                  fill="currentColor"/>
                                        </svg>
                                    </a>
								<?php endif; ?>
                            </div>
                        </div>
					<?php } ?>
                </div>
				<?php
			}
		}
	}
}