<?php
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Repeater;
Use Elementor\Core\Schemes\Typography;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly
/**
 *
 * Course Tab Content widget.
 *
 * @since 1.0.0
 */
class Widgetkit_Pro_LearnDash_Course_Tab_Content extends Widget_Base {

	public function get_name() {
		return 'widgetkit-pro-learndash-course-tab-content';
	}

	public function get_title() {
		return __( 'Learndash Course Tab Content', 'widgetkit-pro' );
	}

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

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

	public function get_style_depends() {
        return [
            'widgetkit_pro_default',
        ];
    }

	protected function register_controls() {

		$this->start_controls_section(
			'general_section',
			[
				'label' => __( 'General', 'widgetkit-pro' ),
				'tab' => Controls_Manager::TAB_CONTENT,
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'tab_title', [
				'label' => __( 'Title', 'widgetkit-pro' ),
				'type' => Controls_Manager::TEXT,
				'default' => __( 'Tab Title' , 'widgetkit-pro' ),
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'tab_icon', [
				'label' => __( 'Icon', 'widgetkit-pro' ),
				'type' => Controls_Manager::ICONS,
				'default' => [
					'value' => 'fas fa-star',
					'library' => 'solid',
				],
				'label_block' => true,
			]
		);

		$repeater->add_control(
			'tab_content_source',
			[
				'label' => __( 'Tab Content Soure', 'widgetkit-pro' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => 'editor',
				'options' => [
					'editor'  => __( 'Editor', 'widgetkit-pro' ),
					'ld-template' => __( 'Learndash Template', 'widgetkit-pro' ),
				],
				'separator' => 'before',
			]
		);

		$repeater->add_control(
			'tab_content_learndash_template',
			[
				'label' => __( 'Learndash Template', 'widgetkit-pro' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'options' => $this->template_shortcode_source(),
				'condition'	=> [
					'tab_content_source' => 'ld-template'
				]
			]
		);

		$repeater->add_control(
			'tab_content_editor', [
				'label' => __( 'Content', 'widgetkit-pro' ),
				'type' => Controls_Manager::WYSIWYG,
				'default' => __( 'Tab Content' , 'widgetkit-pro' ),
				'label_block' => true,
				'condition'	=> [
					'tab_content_source' => 'editor'
				]
			]
		);

		$this->add_control(
			'list',
			[
				'label' => __( 'Content', 'widgetkit-pro' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[
						'tab_title' => __( 'Tab Title', 'widgetkit-pro' ),
					],
				],
				'title_field' => '{{{ tab_title }}}',
			]
		);

		$this->end_controls_section();


		// Title Style
		$this->start_controls_section(
			'title_style_section',
			[
				'label' => __( 'Title', 'widgetkit-pro' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
            'title_section',
            [
            	'label' => __( 'Title ', 'widgetkit-pro' ),
            	'type' => Controls_Manager::HEADING
            ]
        );

		$this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'label' => __( 'Typography', 'widgetkit-pro' ),
                'name' => 'tab_title_typography',
				'selector' => '{{WRAPPER}} .learndash-wrapper .ld-tabs .ld-tabs-navigation .ld-tab',
            ]
        );

		// title color tabs
		$this->start_controls_tabs('course_title_color_tabs');
			$this->start_controls_tab(
				'tab_title_style_normal',
				[
					'label' => __( 'Normal', 'widgetkit-pro' ),
				]
			);
				$this->add_control(
					'tab_title_color',
					[
						'label' => __( 'Color', 'widgetkit-pro' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .learndash-wrapper .ld-tabs .ld-tabs-navigation .ld-tab' => 'color: {{VALUE}} !important;'
						]
					]
				);
			$this->end_controls_tab();
			
			//active style
            $this->start_controls_tab(
                'tab_title_style_active',
                [
                    'label' => __( 'Active', 'widgetkit-pro' ),
                ]
			);
			

				$this->add_control(
					'tab_title_active_color',
					[
						'label' => __( 'Active Color', 'widgetkit-pro' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .learndash-wrapper .ld-tabs .ld-tabs-navigation .ld-tab.ld-active' => 'color: {{VALUE}} !important;',
							'{{WRAPPER}} .learndash-wrapper .ld-tabs .ld-tabs-navigation .ld-tab.ld-active:after' => 'background-color: {{VALUE}} !important;'
						]
					]
				);

			$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->add_responsive_control(
			'tab_title_padding',
			[
				'label' => esc_html__( 'Padding', 'widgetkit-pro' ),
				'type'  => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [
					'{{WRAPPER}} .learndash-wrapper .ld-tabs .ld-tabs-navigation .ld-tab' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;',
				]
			]
		);

		$this->add_responsive_control(
			'tab_title_margin',
			[
				'label' => esc_html__( 'Margin', 'widgetkit-pro' ),
				'type'  => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [
					'{{WRAPPER}} .learndash-wrapper .ld-tabs .ld-tabs-navigation .ld-tab' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;',
				]
			]
		);


		$this->end_controls_section();

	}

	public function template_shortcode_source() {
		$query = new WP_Query( array(
			'post_type' => 'wkfe-ld-template',
			'posts_per_page'    => '-1'
		) );
		$arr = [];
		while( $query->have_posts() ) {
			$query->the_post();
			$arr[get_the_ID()] = get_the_title();
		}
		wp_reset_postdata();

		return $arr;
	}

	protected function render() {
		include( plugin_dir_path( __FILE__ ) . '../layout/learndash-course-tab-content.php');
	}

}