<?php

namespace MasterAddons\Addons;

// Elementor Classes
use MasterAddons\Inc\Classes\Base\Master_Widget;
use \Elementor\Utils;
use \Elementor\Controls_Manager;
use \Elementor\Repeater;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Background;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Group_Control_Text_Shadow;

use MasterAddons\Inc\Classes\Helper;
use MasterAddons\Inc\Admin\Config;

if (!defined('ABSPATH'))
	exit; // If this file is called directly, abort.

class Advanced_Accordion extends Master_Widget
{
	use \MasterAddons\Inc\Traits\Widget_Notice;
	use \MasterAddons\Inc\Traits\Widget_Assets_Trait;

	public function get_name()
	{
		return 'ma-advanced-accordion';
	}

	public function get_title()
	{
		return esc_html__('Advanced Accordion', 'master-addons');
	}

	public function get_icon()
	{
		return 'jltma-icon ' . Config::get_addon_icon('ma-accordion');
	}

	protected function register_controls()
	{

		/**
		 * Content Tab: Accordions
		 */
		$this->start_controls_section(
			'section_accordion_tabs',
			[
				'label' => esc_html__('Content', 'master-addons')
			]
		);

		$this->add_control(
			'accordion_type',
			[
				'label' => esc_html__('Accordion Type', 'master-addons'),
				'type' => Controls_Manager::SELECT,
				'default' => 'accordion',
				'label_block' => false,
				'options' => [
					'accordion' => esc_html__('Accordion', 'master-addons'),
					'toggle' => esc_html__('Toggle', 'master-addons'),
				],
				'frontend_available' => true,
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'accordion_tab_default_active',
			[
				'label' => esc_html__('Active as Default', 'master-addons'),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'no',
				'return_value' => 'yes',
			]
		);

		$repeater->add_control(
			'accordion_tab_icon_show',
			[
				'label' => esc_html__('Enable Custom Icon', 'master-addons'),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'no',
				'return_value' => 'yes',
			]
		);

		// Custom Icons Start
		$repeater->start_controls_tabs('accordion_tab_icon_custom');

		$repeater->start_controls_tab(
			'accordion_tab_icon_custom_expand',
			[
				'label' => __('Expand Icon', 'master-addons'),
				'condition' => [
					'accordion_tab_icon_show' => 'yes'
				],
			]
		);

		$repeater->add_control(
			'accordion_tab_title_icon',
			[
				'label' => esc_html__('Icon', 'master-addons'),
				'description' => esc_html__('Please choose an icon from the list.', 'master-addons'),
				'type' => Controls_Manager::ICONS,
				'fa4compatibility' => 'icon_title',
				'default' => [
					'value' => 'elementor-icons eicon-plus-square-o',
					'library' => 'solid',
				],
				'render_type' => 'template',
				'condition' => [
					'accordion_tab_icon_show' => 'yes'
				],
			]
		);

		$repeater->end_controls_tab();

		$repeater->start_controls_tab(
			'accordion_tab_icon_custom_collapse',
			[
				'label' => __('Collapse Icon', 'master-addons'),
				'condition' => [
					'accordion_tab_icon_show' => 'yes'
				],
			]
		);

		$repeater->add_control(
			'accordion_tab_title_icon_collapse',
			[
				'label' => esc_html__('Icon', 'master-addons'),
				'description' => esc_html__('Please choose an icon from the list.', 'master-addons'),
				'type' => Controls_Manager::ICONS,
				'fa4compatibility' => 'icon_collapse',
				'default' => [
					'value' => 'elementor-icons eicon-minus-square-o',
					'library' => 'solid',
				],
				'render_type' => 'template',
				'condition' => [
					'accordion_tab_icon_show' => 'yes'
				],
			]
		);
		$repeater->end_controls_tab();
		$repeater->end_controls_tabs();

		// Custom Icons End

		// Single Accordion Tab Styles - filtered through Free_Addons.php
		$single_tab_styling_controls = apply_filters('master_addons/addons/accordion/single_tab_styling', [
			[
				'control_id' => 'single_tab_title_bg_color_show_pro',
				'args' => [
					'label' => esc_html__('Enable Background Color (Pro)', 'master-addons'),
					'type' => Controls_Manager::CHOOSE,
					'options' => [
						'1' => [
							'title' => '',
							'icon' => 'eicon-lock',
						],
					],
					'default' => '1',
					'description' => Helper::upgrade_to_pro('Single Tab Styling available on'),
				],
			],
		]);

		foreach ($single_tab_styling_controls as $control) {
			$repeater->add_control($control['control_id'], $control['args']);
		}

		$repeater->add_control(
			'tab_title',
			[
				'label' => __('Title', 'master-addons'),
				'type' => Controls_Manager::TEXT,
				'default' => __('Accordion Title', 'master-addons'),
				'dynamic' => [
					'active' => true,
				],
			]
		);

		// Define default free options - Pro version overrides via filter
		$content_type_options = apply_filters('master_addons/addons/accordion/content_type', [
			'content'  => __('Content', 'master-addons'),
			'section'  => __('Saved Section (Pro)', 'master-addons'),
			'widget'   => __('Saved Widget (Pro)', 'master-addons'),
			'template' => __('Saved Page Template (Pro)', 'master-addons'),
		]);

		$repeater->add_control(
			'content_type',
			[
				'label'       => esc_html__('Content Type', 'master-addons'),
				'type'        => Controls_Manager::SELECT,
				'label_block' => false,
				'options'     => $content_type_options,
				'default'     => 'content',
				'description' => Helper::upgrade_to_pro('3+ more Content Types on'),
			]
		);

		$repeater->add_control(
			'accordion_content',
			[
				'label'     => esc_html__('Content', 'master-addons'),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => esc_html__('Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.', 'master-addons'),
				'dynamic'   => ['active' => true],
				'condition' => [
					'content_type' => 'content',
				],
			]
		);

		$repeater->add_control(
			'image',
			[
				'label'   => __('Image', 'master-addons'),
				'type'    => Controls_Manager::MEDIA,
				'dynamic' => [
					'active' => true,
				],
				'default' => [
					'url' => Utils::get_placeholder_image_src(),
				],
				'conditions' => [
					'terms' => [
						[
							'name'     => 'content_type',
							'operator' => '==',
							'value'    => 'image',
						],
					],
				],
			]
		);

		// Saved Widget options - Pro version overrides via filter
		$saved_widget_options = apply_filters('master_addons/addons/accordion/saved_widget', $this->get_page_template_options('widget'));

		$repeater->add_control(
			'saved_widget',
			[
				'label'   => __('Choose Widget', 'master-addons'),
				'type'    => Controls_Manager::SELECT,
				'options' => $saved_widget_options,
				'default' => '-1',
				'condition' => [
					'content_type' => 'widget',
				],
				'conditions' => [
					'terms' => [
						[
							'name'     => 'content_type',
							'operator' => '==',
							'value'    => 'widget',
						],
					],
				],
			]
		);

		// Saved Section options - Pro version overrides via filter
		$saved_section_options = apply_filters('master_addons/addons/accordion/saved_section', $this->get_page_template_options('section'));

		$repeater->add_control(
			'saved_section',
			[
				'label'   => __('Choose Section', 'master-addons'),
				'type'    => Controls_Manager::SELECT,
				'options' => $saved_section_options,
				'default' => '-1',
				'conditions' => [
					'terms' => [
						[
							'name'     => 'content_type',
							'operator' => '==',
							'value'    => 'section',
						],
					],
				],
			]
		);

		// Templates options - Pro version overrides via filter
		$templates_options = apply_filters('master_addons/addons/accordion/templates', $this->get_page_template_options('page'));

		$repeater->add_control(
			'templates',
			[
				'label'   => __('Choose Template', 'master-addons'),
				'type'    => Controls_Manager::SELECT,
				'options' => $templates_options,
				'default' => '-1',
				'conditions' => [
					'terms' => [
						[
							'name'     => 'content_type',
							'operator' => '==',
							'value'    => 'template',
						],
					],
				],
			]
		);

		$this->add_control(
			'tabs',
			[
				'type' => Controls_Manager::REPEATER,
				'default' => [
					['tab_title' => esc_html__('Accordion Tab Title 1', 'master-addons')],
					['tab_title' => esc_html__('Accordion Tab Title 2', 'master-addons')],
					['tab_title' => esc_html__('Accordion Tab Title 3', 'master-addons')],
				],
				'fields' => $repeater->get_controls(),
				'title_field' => '{{tab_title}}',
			]
		);

		$this->add_control(
			'toggle_icon_show',
			[
				'label' => esc_html__('Show Active/Inactive Icon?', 'master-addons'),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'yes',
				'separator' => 'before',
				'label_on' => __('Show', 'master-addons'),
				'label_off' => __('Hide', 'master-addons'),
				'return_value' => 'yes',
			]
		);

		$this->start_controls_tabs(
			'toggle_icons_style',
			[
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);

		$this->start_controls_tab(
			'toggle_icons_tab_expand',
			[
				'label' => __('Expand Icon', 'master-addons'),
			]
		);

		$this->add_control(
			'active_icon',
			[
				'label' => esc_html__('Expand Icon', 'master-addons'),
				'show_label' => false,
				'type' => Controls_Manager::ICONS,
				'fa4compatibility' => 'icon',
				'render_type' => 'template',
				'default' => [
					'value' => 'fas fa-plus',
					'library' => 'solid',
				],
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'toggle_icons_tab_collapse',
			[
				'label' => __('Collapse Icon', 'master-addons'),
			]
		);

		$this->add_control(
			'toggle_icon',
			[
				'label' => esc_html__('Collapse Icon', 'master-addons'),
				'type' => Controls_Manager::ICONS,
				'show_label' => false,
				'fa4compatibility' => 'icon',
				'render_type' => 'template',
				'default' => [
					'value' => 'fas fa-minus',
					'library' => 'solid',
				],
				'condition' => [
					'toggle_icon_show' => 'yes'
				]

			]
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_control(
			'toggle_speed',
			[
				'label' => esc_html__('Toggle Speed (ms)', 'master-addons'),
				'type' => Controls_Manager::NUMBER,
				'label_block' => false,
				'default' => 300,
				'frontend_available' => true,
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);

		$this->add_control(
			'title_html_tag',
			[
				'label' => __('Title HTML Tag', 'master-addons'),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'h1' => esc_html__('H1', 'master-addons'),
					'h2' => esc_html__('H2', 'master-addons'),
					'h3' => esc_html__('H3', 'master-addons'),
					'h4' => esc_html__('H4', 'master-addons'),
					'h5' => esc_html__('H5', 'master-addons'),
					'h6' => esc_html__('H6', 'master-addons'),
					'div' => esc_html__('div', 'master-addons'),
					'span' => esc_html__('span', 'master-addons'),
					'a' => esc_html__('a', 'master-addons'),
				],
				'default' => 'div',
			]
		);

		$this->end_controls_section();

		/**
		 * Style Tab: Items
		 */
		$this->start_controls_section(
			'section_accordion_items_style',
			[
				'label' => esc_html__('Items', 'master-addons'),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_responsive_control(
			'accordion_items_spacing',
			[
				'label' => __('Spacing', 'master-addons'),
				'type' => Controls_Manager::SLIDER,
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 200,
					],
				],
				'default' => [
					'size' => '',
				],
				'selectors' => [
					'{{WRAPPER}} .jltma-accordion-item' => 'margin-bottom: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'accordion_items_border',
				'label' => esc_html__('Border', 'master-addons'),
				'selector' => '{{WRAPPER}} .jltma-accordion-item',
				'fields_options' => [
					'border' => [
						'default' => 'solid',
					],
					'width' => [
						'default' => [
							'top' => '1',
							'right' => '1',
							'bottom' => '1',
							'left' => '1',
							'isLinked' => true,
						],
					],
					'color' => [
						'default' => '#eeeeee',
					],
				],
			]
		);

		$this->add_responsive_control(
			'accordion_items_border_radius',
			[
				'label' => esc_html__('Border Radius', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'selectors' => [
					'{{WRAPPER}} .jltma-accordion-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'accordion_items_box_shadow',
				'selector' => '{{WRAPPER}} .jltma-accordion-item',
			]
		);

		$this->end_controls_section();

		/**
		 * Style Tab: Title
		 */
		$this->start_controls_section(
			'section_title_style',
			[
				'label' => esc_html__('Title', 'master-addons'),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->start_controls_tabs('accordion_tabs_style');

		$this->start_controls_tab(
			'accordion_tab_normal',
			[
				'label' => __('Normal', 'master-addons'),
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'tab_title_bg_color',
				'label' => esc_html__('Background Color', 'master-addons'),
				'types' => ['classic', 'gradient'],
				'exclude' => ['image'],
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title'
			]
		);

		$this->add_control(
			'tab_title_text_color',
			[
				'label' => esc_html__('Text Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '#333333',
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'tab_title_typography',
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title',
			]
		);

		$this->add_control(
			'title_alignment',
			[
				'label' => __('Title Align', 'master-addons'),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					'left' => [
						'title' => __('Left', 'master-addons'),
						'icon' => 'eicon-text-align-left',
					],
					'center' => [
						'title' => __('Center', 'master-addons'),
						'icon' => 'eicon-text-align-center',
					],
					'right' => [
						'title' => __('Right', 'master-addons'),
						'icon' => 'eicon-text-align-right',
					],
				],
				'default' => 'left',
				'selectors' => [
					// '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title' => 'text-align: {{VALUE}} !important;',
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-title-text' => 'text-align: {{VALUE}};',
				]
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'tab_title_border',
				'label' => esc_html__('Border', 'master-addons'),
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title',
			]
		);

		$this->add_responsive_control(
			'jltma_accordion_border_radius',
			array(
				'label' => esc_html__('Border Radius', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => array('px', '%'),
				'selectors' => array(
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'accordion_tab_hover',
			[
				'label' => __('Hover', 'master-addons'),
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'tab_title_bg_color_hover',
				'label' => esc_html__('Background Color', 'master-addons'),
				'types' => ['classic', 'gradient'],
				'exclude' => ['image'],
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title:hover'
			]
		);

		$this->add_control(
			'tab_title_text_color_hover',
			[
				'label' => esc_html__('Text Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title:hover,
						{{WRAPPER}} .jltma-accordion-item.ma-multicolor-accordion .jltma-accordion-tab-title:hover' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'tab_title_border_color_hover',
			[
				'label' => esc_html__('Border Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title:hover' => 'border-color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'tab_title_border_hover',
				'label' => esc_html__('Border', 'master-addons'),
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title:hover',
			]
		);

		$this->add_responsive_control(
			'jltma_accordion_border_radius_hover',
			array(
				'label' => esc_html__('Border Radius', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => array('px', '%'),
				'selectors' => array(
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'accordion_tab_active',
			[
				'label' => __('Active', 'master-addons'),
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'tab_title_bg_color_active',
				'label' => esc_html__('Background Color', 'master-addons'),
				'types' => ['classic', 'gradient'],
				'exclude' => ['image'],
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-item .jltma-accordion-tab-title.active'
			]
		);

		$this->add_control(
			'tab_title_text_color_active',
			[
				'label' => esc_html__('Text Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title.active' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'tab_title_border_color_active',
			[
				'label' => esc_html__('Border Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title.active' => 'border-color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'tab_title_border_active',
				'label' => esc_html__('Border', 'master-addons'),
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title.active',
			]
		);

		$this->add_responsive_control(
			'jltma_accordion_border_radius_active',
			array(
				'label' => esc_html__('Border Radius', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => array('px', '%'),
				'selectors' => array(
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title.active' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name' => 'jltma_accordion_box_shadow',
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title',
			)
		);

		$this->add_responsive_control(
			'jltma_accordion_box_padding',
			[
				'label' => esc_html__('Padding', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();

		/**
		 * Style Tab: Content
		 */
		$this->start_controls_section(
			'section_content_style',
			[
				'label' => esc_html__('Content', 'master-addons'),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'tab_content_bg_color',
			[
				'label' => esc_html__('Background Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '',
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-item .jltma-accordion-tab-content' => 'background: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'tab_content_text_color',
			[
				'label' => esc_html__('Text Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '#333',
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-item .jltma-accordion-tab-content,
						{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-item .jltma-accordion-tab-content p' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'tab_content_typography',
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-item .jltma-accordion-tab-content',
			]
		);

		$this->add_responsive_control(
			'tab_content_padding',
			[
				'label' => esc_html__('Padding', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-item .jltma-accordion-tab-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			array(
				'name' => 'tab_content_padding_text_shadow',
				'label' => __('Text Shadow', 'master-addons'),
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-item .jltma-accordion-tab-content',
			)
		);

		$this->end_controls_section();

		/**
		 * Style Tab
		 */
		$this->start_controls_section(
			'section_toggle_icon_style',
			[
				'label' => esc_html__('Toggle Icons', 'master-addons'),
				'tab' => Controls_Manager::TAB_STYLE,
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);

		$this->add_control(
			'toggle_icon_position',
			array(
				'label' => __('Icon Position', 'master-addons'),
				'type' => Controls_Manager::CHOOSE,
				'options' => array(
					'left' => array(
						'title' => __('Left', 'master-addons'),
						'icon' => 'eicon-h-align-left',
					),
					'none' => array(
						'title' => __('None', 'master-addons'),
						'icon' => 'eicon-ban',
					),
					'right' => array(
						'title' => __('Right', 'master-addons'),
						'icon' => 'eicon-h-align-right',
					)
				),
				'default' => 'right',
				'separator' => 'after',
				'toggle' => false,
				'render_type' => 'template',
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			)
		);

		$this->add_responsive_control(
			'toggle_icon_size',
			[
				'label' => __('Icon Size', 'master-addons'),
				'type' => Controls_Manager::SLIDER,
				'default' => [
					'size' => 16,
					'unit' => 'px',
				],
				'size_units' => ['px'],
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 100,
						'step' => 1,
					]
				],
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title .jltma-accordion-toggle-icon' => 'font-size: {{SIZE}}{{UNIT}};',
				],
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);

		$this->add_responsive_control(
			'tab_icon_spacing',
			[
				'label' => __('Icon Spacing', 'master-addons'),
				'type' => Controls_Manager::SLIDER,
				'size_units' => ['px'],
				'default' => [
					'size' => 10,
					'unit' => 'px',
				],
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 100,
						'step' => 1,
					]
				],
				'selectors' => [
					'{{WRAPPER}} .ma-accordion-icon-align-left .jltma-accordion-tab-title .jltma-accordion-toggle-icon' => 'margin-left: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .ma-accordion-icon-align-right .jltma-accordion-tab-title .jltma-accordion-toggle-icon' => 'margin-right: {{SIZE}}{{UNIT}};',
				],
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);

		$this->add_responsive_control(
			'toggle_icon_padding',
			[
				'label' => esc_html__('Icon Padding', 'master-addons'),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => ['px', 'em', '%'],
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title .jltma-accordion-toggle-icon' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);

		$this->add_control(
			'toggle_icons_color_heading',
			[
				'label' => __('Icon Colors', 'master-addons'),
				'type' => Controls_Manager::HEADING,
				'separator' => 'before',
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);
		$this->start_controls_tabs(
			'toggle_icons_color_tabs',
			[
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);

		$this->start_controls_tab(
			'toggle_icons_color_normal',
			[
				'label' => __('Normal', 'master-addons'),
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'toggle_icon_bg',
				'label' => esc_html__('Background Color', 'master-addons'),
				'types' => ['classic', 'gradient'],
				'exclude' => ['image'],
				'condition' => [
					'toggle_icon_show' => 'yes'
				],
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title .jltma-accordion-toggle-icon'
			]
		);

		$this->add_control(
			'toggle_icon_color',
			[
				'label' => esc_html__('Icon Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'default' => '#444',
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title .jltma-accordion-toggle-icon' => 'color: {{VALUE}};',
				],
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'toggle_icons_color_hover',
			[
				'label' => __('Hover', 'master-addons'),
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'toggle_icon_bg_color_hover',
				'label' => esc_html__('Background Color', 'master-addons'),
				'types' => ['classic', 'gradient'],
				'exclude' => ['image'],
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-item:hover .jltma-accordion-tab-title .jltma-accordion-toggle-icon'
			]
		);

		$this->add_control(
			'toggle_icon_color_hover',
			[
				'label' => esc_html__('Icon Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-item:hover .jltma-accordion-tab-title .jltma-accordion-toggle-icon' => 'color: {{VALUE}};',
				],
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'toggle_icons_color_active',
			[
				'label' => __('Active', 'master-addons'),
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'toggle_icon_bg_color_active',
				'label' => esc_html__('Background Color', 'master-addons'),
				'types' => ['classic', 'gradient'],
				'exclude' => ['image'],
				'selector' => '{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title.active .jltma-accordion-toggle-icon'
			]
		);

		$this->add_control(
			'toggle_icon_color_active',
			[
				'label' => esc_html__('Icon Color', 'master-addons'),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .jltma-advanced-accordion .jltma-accordion-tab-title.active .jltma-accordion-toggle-icon' => 'color: {{VALUE}};'
				],
				'condition' => [
					'toggle_icon_show' => 'yes'
				]
			]
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();

		// Help Docs section (links from config.php)
		$this->jltma_help_docs('ma-accordion');

		$this->upgrade_to_pro_message();
	}

	protected function render()
	{

		$settings = $this->get_settings_for_display();

		// Whitelist allowed HTML tags to prevent XSS
		$allowed_tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'p'];
		$title_html_tag = in_array($settings['title_html_tag'], $allowed_tags, true)
			? $settings['title_html_tag']
			: 'h3';

		$id_int = substr($this->get_id_int(), 0, 3);

		$this->add_render_attribute('jltma-accordion', [
			'class' => 'jltma-advanced-accordion',
			'id' => 'jltma-advanced-accordion-' . esc_attr($this->get_id()),
			'data-accordion-id' => esc_attr($this->get_id())
		]);

		$toggle_icon_position = '';
		if (!empty($settings['toggle_icon_position'])) {
			$toggle_icon_position = $settings['toggle_icon_position'];
		}

		$this->add_render_attribute([
			'jltma-accordion-wrap' => [
				'class' => [
					'ma-accordion-wrap',
					'ma-accordion-icon-align-' . esc_attr($toggle_icon_position),
					'id' => 'ma-accordion-' . esc_attr($this->get_id())
				]
			]
		]);
		?>

		<div <?php echo $this->get_render_attribute_string('jltma-accordion'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor render attribute string is safe ?> 		<?php echo 'data-accordion-id="' . esc_attr($this->get_id()) . '"'; ?>>
			<div <?php echo $this->get_render_attribute_string('jltma-accordion-wrap'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor render attribute string is safe ?>">

				<?php
				foreach ($settings['tabs'] as $index => $tab) {

					$tab_count = $index + 1;
					$tab_title_setting_key = $this->get_repeater_setting_key('tab_title', 'tabs', $index);
					$tab_content_setting_key = $this->get_repeater_setting_key('accordion_content', 'tabs', $index);

					$tab_title_class = ['jltma-accordion-tab-title jltma-accordion-header'];
					$tab_content_class = ['jltma-accordion-tab-content'];

					if ($tab['accordion_tab_default_active'] == 'yes') {
						$tab_title_class[] = 'active-default';
						$tab_content_class[] = 'active-default';
					}

					$this->add_render_attribute($tab_title_setting_key, [
						'id' => 'jltma-accordion-tab-title-' . esc_attr($id_int) . esc_attr($tab_count),
						'class' => $tab_title_class,
						'tabindex' => esc_attr($id_int) . esc_attr($tab_count),
						'data-tab' => esc_attr($tab_count),
						'role' => 'tab',
						'aria-controls' => 'jltma-accordion-tab-content-' . esc_attr($id_int) . esc_attr($tab_count),
					]);

					$this->add_render_attribute($tab_content_setting_key, [
						'id' => 'jltma-accordion-tab-content-' . esc_attr($id_int) . esc_attr($tab_count),
						'class' => $tab_content_class,
						'data-tab' => $tab_count,
						'role' => 'tabpanel',
						'aria-labelledby' => 'jltma-accordion-tab-title-' . esc_attr($id_int) . esc_attr($tab_count),
					]);

					// Single tab styling - only apply if Pro control exists
					if (isset($tab['single_tab_title_bg_color_show']) && $tab['single_tab_title_bg_color_show'] == 'yes') {
						$single_item_class = 'jltma-multicolor-accordion';
					}
					?>

					<div
						class="jltma-accordion-item elementor-repeater-item-<?php echo esc_attr($tab['_id']); ?> <?php echo isset($single_item_class) ? esc_attr($single_item_class) : ''; ?>">
						<<?php echo esc_html($title_html_tag); ?> <?php echo $this->get_render_attribute_string($tab_title_setting_key); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor render attribute string is safe

											// Single tab styling - only apply if Pro control exists
											if (isset($tab['single_tab_title_bg_color_show']) && $tab['single_tab_title_bg_color_show'] == 'yes') { ?>
									style="background-color:<?php echo esc_attr($tab['single_tab_title_bg_color']); ?>;
									color:<?php echo esc_attr($tab['single_title_text_color']); ?>" <?php } ?>>
							<div class="jltma-accordion-title-icon">

								<?php
								if ($settings['toggle_icon_show'] === 'yes' && !isset($settings['toggle_icon_position'])) {
									$settings['toggle_icon_position'] = 'right';
								}
								if ($settings['toggle_icon_show'] === 'yes' && ($settings['toggle_icon_position'] == "left")) {
									if ($tab['accordion_tab_icon_show'] === 'yes') { ?>
										<span class="jltma-accordion-toggle-icon jltma-accordion-tab-icon">
										<?php
											$migrated = isset($tab['__fa4_migrated']['accordion_tab_title_icon_collapse']);
											$is_new = empty($tab['icon_collapse']) && \Elementor\Icons_Manager::is_migration_allowed();
											if ($is_new || $migrated) {
												$this->render_icon($tab['accordion_tab_title_icon_collapse'], ['aria-hidden' => 'true', 'class' => 'jltma-el-accordion-icon-closed']);
											} else { ?>
												<i class="jltma-el-accordion-icon-closed <?php echo esc_attr($tab['icon_collapse']); ?>"
													aria-hidden="true"></i>
											<?php }

											$active_migrated = isset($tab['__fa4_migrated']['accordion_tab_title_icon']);
											$active_is_new = empty($tab['icon_title']) && \Elementor\Icons_Manager::is_migration_allowed();
											if ($active_is_new || $active_migrated) {
												$this->render_icon($tab['accordion_tab_title_icon'], ['aria-hidden' => 'true', 'class' => 'jltma-el-accordion-icon-opened']);
											} else { ?>
												<i class="jltma-el-accordion-icon-opened <?php echo esc_attr($tab['icon_title']); ?>" aria-hidden="true"></i>
										<?php } ?>

										</span>
									<?php } else { ?>
										<span class="jltma-accordion-toggle-icon">
										<?php
											if (!empty($settings['toggle_icon']['value'])) {
												$this->render_icon($settings['toggle_icon'], ['aria-hidden' => 'true', 'class' => 'jltma-el-accordion-icon-closed']);
											}

											if (!empty($settings['active_icon']['value'])) {
												$this->render_icon($settings['active_icon'], ['aria-hidden' => 'true', 'class' => 'jltma-el-accordion-icon-opened']);
											}
										?>
										</span>
									<?php }
								} ?>

								<div class="jltma-accordion-title-text">
									<?php echo $this->parse_text_editor(Helper::wp_kses_custom($tab['tab_title'])); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- content passed through wp_kses_custom sanitiser ?>
								</div>
							</div>

							<?php if ($settings['toggle_icon_show'] === 'yes' && ($settings['toggle_icon_position'] == "right")) {
								if ($tab['accordion_tab_icon_show'] === 'yes') { ?>
									<span class="jltma-accordion-toggle-icon jltma-accordion-tab-icon">
										<?php
										$migrated = isset($tab['__fa4_migrated']['accordion_tab_title_icon_collapse']);
										$is_new = empty($tab['icon_collapse']) && \Elementor\Icons_Manager::is_migration_allowed();
										if ($is_new || $migrated) {
											$this->render_icon($tab['accordion_tab_title_icon_collapse'], ['aria-hidden' => 'true', 'class' => 'jltma-el-accordion-icon-closed']);
										} else { ?>
											<i class="jltma-el-accordion-icon-closed <?php echo esc_attr($tab['icon_collapse']); ?>"
												aria-hidden="true"></i>
										<?php }

										$active_migrated = isset($tab['__fa4_migrated']['accordion_tab_title_icon']);
										$active_is_new = empty($tab['icon_title']) && \Elementor\Icons_Manager::is_migration_allowed();
										if ($active_is_new || $active_migrated) {
											$this->render_icon($tab['accordion_tab_title_icon'], ['aria-hidden' => 'true', 'class' => 'jltma-el-accordion-icon-opened']);
										} else { ?>
											<i class="jltma-el-accordion-icon-opened <?php echo esc_attr($tab['icon_title']); ?>" aria-hidden="true"></i>
										<?php } ?>
									</span>
								<?php } else { ?>
									<span class="jltma-accordion-toggle-icon">
										<?php
										if (!empty($settings['toggle_icon']['value'])) {
											$this->render_icon($settings['toggle_icon'], ['aria-hidden' => 'true', 'class' => 'jltma-el-accordion-icon-closed']);
										}

										if (!empty($settings['active_icon']['value'])) {
											$this->render_icon($settings['active_icon'], ['aria-hidden' => 'true', 'class' => 'jltma-el-accordion-icon-opened']);
										}
										?>
									</span>
								<?php }
							} ?>

						</<?php echo esc_html($title_html_tag); ?>>

						<div <?php echo $this->get_render_attribute_string($tab_content_setting_key); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor render attribute string is safe ?>>
							<?php
							// Content type rendering - Pro content types filtered through Free_Addons.php
							$pro_content_types = apply_filters('master_addons/addons/accordion/content_types', ['content']);

							if ($tab['content_type'] == 'content') {
								echo do_shortcode($tab['accordion_content']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- shortcode output is intentional HTML
							} else if (in_array('section', $pro_content_types) && $tab['content_type'] == 'section' && !empty($tab['saved_section'])) {
								echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display($tab['saved_section']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor builder content is safe
							} else if (in_array('template', $pro_content_types) && $tab['content_type'] == 'template' && !empty($tab['templates'])) {
								echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display($tab['templates']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor builder content is safe
							} else if (in_array('widget', $pro_content_types) && $tab['content_type'] == 'widget' && !empty($tab['saved_widget'])) {
								echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display($tab['saved_widget']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor builder content is safe
							}
							?>
						</div>

					</div>
				<?php } ?>
			</div>
		</div>
		<?php
	}

	public function get_page_template_options($type = '')
	{

		$page_templates = Helper::jltma_get_page_templates($type);

		$options[-1] = __('Select', 'master-addons');

		if (count($page_templates)) {
			foreach ($page_templates as $id => $name) {
				$options[$id] = $name;
			}
		} else {
			$options['no_template'] = __('No saved templates found!', 'master-addons');
		}

		return $options;
	}
}
