<?php
/*
 * Elementor eCommerce Addon Pricing Tab Widget
 * Author & Copyright: NicheAddon
*/

namespace Elementor;

if (!isset(get_option( 'naeca_uw_settings' )['naeca_pricing_tab'])) { // enable & disable

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

class eCommerce_Elementor_Addon_PricingTab extends Widget_Base{

	/**
	 * Retrieve the widget name.
	*/
	public function get_name(){
		return 'naecommerce_basic_pricing_tab';
	}

	/**
	 * Retrieve the widget title.
	*/
	public function get_title(){
		return esc_html__( 'Tab / Pricing Tab', 'ecommerce-addon' );
	}

	/**
	 * Retrieve the widget icon.
	*/
	public function get_icon() {
		return 'eicon-tabs';
	}

	/**
	 * Retrieve the list of categories the widget belongs to.
	*/
	public function get_categories() {
		return ['naecommerce-basic-category'];
	}

	/**
	 * Register eCommerce Addon Pricing Tab widget controls.
	 * Adds different input fields to allow the user to change and customize the widget settings.
	*/
	protected function register_controls(){

		$templates = get_posts( 'post_type="elementor_library"&numberposts=-1' );
	  $elementor_templates = array();
	  if ( $templates ) {
	    foreach ( $templates as $template ) {
	      $elementor_templates[ $template->ID ] = $template->post_title;
	    }
	  } else {
	    $elementor_templates[ __( 'No templates found', 'ecommerce-addon' ) ] = 0;
	  }

		$this->start_controls_section(
			'section_active',
			[
				'label' => __( 'Pricing Tab Options', 'ecommerce-addon' ),
			]
		);
		$this->add_control(
			'active',
			[
				'label' => __( 'Active Pricing Tab Number', 'ecommerce-addon' ),
				'type' => Controls_Manager::TEXT,
				'default' => 1,
			]
		);
		$this->add_control(
			'tab_style',
			[
				'label' => esc_html__( 'Tab Style', 'ecommerce-addon' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'one' => esc_html__( 'Style One (Normal)', 'ecommerce-addon' ),
					'two' => esc_html__( 'Style Two (Pricing Tab)', 'ecommerce-addon' ),
				],
				'default' => 'one',
				'description' => esc_html__( 'Select your tab style.', 'ecommerce-addon' ),
			]
		);
		$this->add_responsive_control(
			'content_alignment',
			[
				'label' => esc_html__( 'Tab Alignment', 'ecommerce-addon' ),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					'left' => [
						'title' => esc_html__( 'Left', 'ecommerce-addon' ),
						'icon' => 'fa fa-align-left',
					],
					'center' => [
						'title' => esc_html__( 'Center', 'ecommerce-addon' ),
						'icon' => 'fa fa-align-center',
					],
					'right' => [
						'title' => esc_html__( 'Right', 'ecommerce-addon' ),
						'icon' => 'fa fa-align-right',
					],
				],
				'default' => 'center',
				'selectors' => [
					'{{WRAPPER}} ul.naeca-tab-links' => 'text-align: {{VALUE}};',
				],
			]
		);
		$this->end_controls_section();// end: Section

		$this->start_controls_section(
			'section_tab_content',
			[
				'label' => __( 'Pricing Tab Group', 'ecommerce-addon' ),
			]
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'tab_id',
			[
				'label' => esc_html__( 'Pricing Tab ID', 'ecommerce-addon' ),
				'type' => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'Type tab id here', 'ecommerce-addon' ),
				'label_block' => true,
			]
		);
		$repeater->add_control(
			'opt_heading',
			[
				'label' => __( 'Title', 'ecommerce-addon' ),
				'type' => Controls_Manager::HEADING,
				'separator' => 'after',
			]
		);
		$repeater->add_control(
			'tab_title',
			[
				'label' => esc_html__( 'Pricing Tab Title', 'ecommerce-addon' ),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__( 'Monthly', 'ecommerce-addon' ),
				'placeholder' => esc_html__( 'Type tab title here', 'ecommerce-addon' ),
				'label_block' => true,
			]
		);
		$repeater->add_control(
			'opt_cont',
			[
				'label' => __( 'Content', 'ecommerce-addon' ),
				'type' => Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);
		$repeater->add_control(
			'tab_content',
			[
				'label' => __( 'Choose Templates', 'ecommerce-addon' ),
				'type' => Controls_Manager::SELECT2,
				'default' => [],
				'options' => $elementor_templates,
				'multiple' => false,
			]
		);
		$this->add_control(
			'tabItems_groups',
			[
				'label' => esc_html__( 'Schedule Group', 'ecommerce-addon' ),
				'type' => Controls_Manager::REPEATER,
				'default' => [
					[
						'tab_title' => esc_html__( 'Monthly', 'ecommerce-addon' ),
					],

				],
				'fields' => $repeater->get_controls(),
				'title_field' => '{{{ tab_title }}}',
			]
		);
		$this->end_controls_section();// end: Section

		// Pricing Tab
			$this->start_controls_section(
				'sectn_style',
				[
					'label' => esc_html__( 'Pricing Tab', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_responsive_control(
				'info_padding',
				[
					'label' => __( 'Pricing Tab Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px' ],
					'selectors' => [
						'{{WRAPPER}} ul.naeca-tab-links' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'info_margin',
				[
					'label' => __( 'Pricing Tab Margin', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px' ],
					'selectors' => [
						'{{WRAPPER}} ul.naeca-tab-links' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_group_control(
				Group_Control_Border::get_type(),
				[
					'name' => 'tab_border',
					'label' => esc_html__( 'Pricing Tab Border', 'ecommerce-addon' ),
					'selector' => '{{WRAPPER}} ul.naeca-tab-links',
				]
			);
			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				[
					'name' => 'section_box_shadow',
					'label' => esc_html__( 'Box Shadow', 'ecommerce-addon' ),
					'selector' => '{{WRAPPER}} ul.naeca-tab-links',
				]
			);
			$this->end_controls_section();// end: Section

		// Title
			$this->start_controls_section(
				'section_title_style',
				[
					'label' => esc_html__( 'Title', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_responsive_control(
				'title_padding',
				[
					'label' => __( 'Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px' ],
					'selectors' => [
						'{{WRAPPER}} .naeca-tab-links a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_group_control(
				Group_Control_Typography::get_type(),
				[
					'label' => esc_html__( 'Typography', 'ecommerce-addon' ),
					'name' => 'sasstp_title_typography',
					'selector' => '{{WRAPPER}} .naeca-tab-links a',
				]
			);
			$this->start_controls_tabs( 'title_style' );
				$this->start_controls_tab(
					'title_normal',
					[
						'label' => esc_html__( 'Normal', 'ecommerce-addon' ),
					]
				);
				$this->add_control(
					'title_color',
					[
						'label' => esc_html__( 'Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-tab-links a' => 'color: {{VALUE}};',
						],
					]
				);
				$this->add_control(
					'title_bg_color',
					[
						'label' => esc_html__( 'Background Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-tab-links li a' => 'background-color: {{VALUE}};',
						],
					]
				);
				$this->end_controls_tab();  // end:Normal tab
				$this->start_controls_tab(
					'title_hover',
					[
						'label' => esc_html__( 'Active', 'ecommerce-addon' ),
					]
				);
				$this->add_control(
					'title_hover_color',
					[
						'label' => esc_html__( 'Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-tab-links li.active a' => 'color: {{VALUE}};',
						],
					]
				);
				$this->add_control(
					'title_bg_hover_color',
					[
						'label' => esc_html__( 'Background Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .naeca-tab-links li.active a' => 'background-color: {{VALUE}};',
						],
					]
				);
				$this->end_controls_tab();  // end:Hover tab
			$this->end_controls_tabs(); // end tabs
			$this->end_controls_section();// end: Section

	}

	/**
	 * Render Pricing Tab widget output on the frontend.
	 * Written in PHP and used to generate the final HTML.
	*/
	protected function render() {
		// Pricing Tab query
		$settings = $this->get_settings_for_display();
		$tab_style = !empty( $settings['tab_style'] ) ? $settings['tab_style'] : '';
		$active = !empty( $settings['active'] ) ? $settings['active'] : '';
		$tabItems_groups = !empty( $settings['tabItems_groups'] ) ? $settings['tabItems_groups'] : '';

		if ($tab_style === 'two'){
		  $style_cls = ' tab-style-two';
		} else {
		  $style_cls = '';
		}

		$output = '<div class="naeca-tab-wrap'.esc_attr($style_cls).'">';
		if ( !empty( $tabItems_groups ) && is_array( $tabItems_groups ) ){
			$output .= '<ul class="naeca-tab-links">';
      	$key = 1;
      	foreach ( $tabItems_groups as $each_tab ) {
					$title = !empty( $each_tab['tab_title'] ) ? $each_tab['tab_title'] : '';
					$tab_id = !empty( $each_tab['tab_id'] ) ? $each_tab['tab_id'] : '';

					$active_class = ( $key == $active ) ? ' class="active"' : '';
					$id = $tab_id ? sanitize_title($tab_id) : sanitize_title($title);

					$output .= '<li'.$active_class.'><a href="#naeca-'.$key.$id.'">'.esc_html($title).'</a></li>';
					$key++;
				}
      $output .= '</ul><div class="naeca-tab-content">';
      	$key = 1;
      	foreach ( $tabItems_groups as $each_tab ) {
      		$templates = !empty( $each_tab['tab_content'] ) ? $each_tab['tab_content'] : '';
					$active_cls = ( $key == $active ) ? ' active' : '';

					$title = !empty( $each_tab['tab_title'] ) ? $each_tab['tab_title'] : '';
					$tab_id = !empty( $each_tab['tab_id'] ) ? $each_tab['tab_id'] : '';
					$id = $tab_id ? sanitize_title($tab_id) : sanitize_title($title);

					$output .= '<div class="naeca-tab'.$active_cls.'" id="naeca-'.$key.$id.'">'.do_shortcode('[naecommerce_elementor_template id="'.$templates.'"]').'</div>';
					$key++;
			  }
			$output .= '</div>';
		}
		$output .= '</div>';
		echo $output;

	}

}
Plugin::instance()->widgets_manager->register_widget_type( new eCommerce_Elementor_Addon_PricingTab() );

} // enable & disable
