<?php
namespace CSSIgniter\Ignition\Elementor\Widget;

class Product_Tabs extends \Elementor\Widget_Base {
	public function get_style_depends() {
		wp_register_style( 'ignition-elementor-product-tabs', plugin_dir_url( __DIR__ ) . 'assets/css/product-tabs.min.css', array( 'slick' ), IGNITION_ELEMENTOR_WIDGETS_VERSION );

		return array(
			'ignition-elementor-product-tabs',
		);

	}

	public function get_script_depends() {
		wp_register_script( 'ignition-elementor-product-tabs', plugin_dir_url( __DIR__ ) . 'assets/js/product-tabs.min.js', array( 'slick' ), IGNITION_ELEMENTOR_WIDGETS_VERSION, true );

		return array(
			'ignition-elementor-product-tabs',
		);

	}

	public function get_name() {
		return 'ignition-product-tabs';
	}

	public function get_title() {
		return __( 'Product Tabs', 'ignition-elementor-widgets' );
	}

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

	public function get_categories() {
		return array( 'ignition-elementor-widgets' );

	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_tabs',
			array(
				'label' => __( 'Tabs', 'ignition-elementor-widgets' ),
			)
		);

		$repeater = new \Elementor\Repeater();

		$repeater->add_control(
			'tab_title',
			array(
				'label'       => esc_html__( 'Tab title', 'ignition-elementor-widgets' ),
				'placeholder' => esc_html__( 'Tab title...', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'label_block' => true,
			)
		);

		$product_categories = get_terms(
			array(
				'taxonomy' => 'product_cat',
			)
		);

		$repeater->add_control(
			'product_category',
			array(
				'label'   => esc_html__( 'Product category', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => '',
				'options' => wp_list_pluck( $product_categories, 'name', 'term_id' ),
			)
		);

		$repeater->add_control(
			'handpicked_products',
			array(
				'label'       => esc_html__( 'Select products', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::SELECT2,
				'label_block' => true,
				'multiple'    => true,
				'options'     => \CSSIgniter\Ignition\Elementor\Widgets_Helper::get_all_post_type_items( 'product' ),
			)
		);

		$this->add_control(
			'tabs',
			array(
				'label'       => esc_html__( 'Tabs', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => '{{{ tab_title }}}',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_layout',
			array(
				'label' => __( 'Layout', 'ignition-elementor-widgets' ),
			)
		);

		$this->add_control(
			'columns',
			array(
				'label'   => esc_html__( 'Number of columns', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 2,
						'max'  => 4,
						'step' => 1,
					),
				),
				'default' => array(
					'unit' => 'px',
					'size' => 3,
				),
			)
		);

		$this->add_control(
			'number_products',
			array(
				'label'   => esc_html__( 'Number of products', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 1,
						'max'  => 12,
						'step' => 1,
					),
				),
				'default' => array(
					'unit' => 'px',
					'size' => 3,
				),
			)
		);

		$this->add_control(
			'tab_button_alignment',
			array(
				'label'   => esc_html__( 'Tab button alignment', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::CHOOSE,
				'options' => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default' => 'left',
			)
		);

		$this->add_control(
			'layout',
			array(
				'label'   => esc_html__( 'Layout', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'grid',
				'options' => array(
					'grid'   => esc_html__( 'Grid', 'ignition-elementor-widgets' ),
					'slider' => esc_html__( 'Slider', 'ignition-elementor-widgets' ),
				),
			)
		);

		$this->add_control(
			'show_category',
			array(
				'label'        => esc_html__( 'Show category', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->add_control(
			'show_price',
			array(
				'label'        => esc_html__( 'Show price', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_rating',
			array(
				'label'        => esc_html__( 'Show rating', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_stock',
			array(
				'label'        => esc_html__( 'Show stock', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->add_control(
			'show_add_to_cart',
			array(
				'label'        => esc_html__( 'Show add to cart button', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'tab_button_styles',
			array(
				'label' => esc_html__( 'Tab button colors', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			)
		);

		$this->start_controls_tabs(
			'tab_button_colors'
		);

		$this->start_controls_tab(
			'tab_button_normal_colors',
			array(
				'label' => esc_html__( 'Normal', 'ignition-elementor-widgets' ),
			)
		);

		$this->add_control(
			'text_color',
			array(
				'label'     => esc_html__( 'Text color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ignition-elementor-widgets-product-tab' => 'color: {{VALUE}}',
				),
			)
		);

		$this->add_control(
			'background_color',
			array(
				'label'     => esc_html__( 'Background color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ignition-elementor-widgets-product-tab' => 'background-color: {{VALUE}}',
				),
			)
		);

		$this->add_control(
			'border_color',
			array(
				'label'     => esc_html__( 'Border color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ignition-elementor-widgets-product-tab' => 'border-color: {{VALUE}}',
				),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'tab_button_active_colors',
			array(
				'label' => esc_html__( 'Active', 'ignition-elementor-widgets' ),
			)
		);

		$this->add_control(
			'active_text_color',
			array(
				'label'     => esc_html__( 'Text color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ignition-elementor-widgets-product-tab.active' => 'color: {{VALUE}}',
				),
			)
		);

		$this->add_control(
			'active_background_color',
			array(
				'label'     => esc_html__( 'Background color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ignition-elementor-widgets-product-tab.active' => 'background-color: {{VALUE}}',
				),
			)
		);

		$this->add_control(
			'active_border_color',
			array(
				'label'     => esc_html__( 'Border color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ignition-elementor-widgets-product-tab.active' => 'border-color: {{VALUE}}',
				),
			)
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();
	}

	protected function render() {
		$settings     = $this->get_settings();
		$tabs         = $settings['tabs'];
		$columns      = $settings['columns']['size'];
		$num_products = $settings['number_products']['size'];

		$container_classes = array(
			'ignition-elementor-widgets-product-tabs__content',
			'row',
			'row-items',
		);

		$container_classes[] = "row-columns-{$columns}";
		$classes[]           = ignition_get_columns_classes( $columns );

		if ( function_exists( 'ignition_customizer_defaults' ) ) {
			$mobile_classes = get_theme_mod( 'woocommerce_shop_mobile_columns', ignition_customizer_defaults( 'woocommerce_shop_mobile_columns' ) );

			if ( 2 === (int) $mobile_classes && 1 !== (int) $columns ) {
				$classes[0] = str_replace( 'col-12', 'col-6', $classes[0] );
			}
		}

		$item_template_vars = array(
			'columns'     => $columns,
			'show-cat'    => $settings['show_category'],
			'show-rating' => $settings['show_rating'],
			'show-price'  => $settings['show_price'],
			'show-stock'  => $settings['show_stock'],
			'show-button' => $settings['show_add_to_cart'],
		);
		?>
		<div class="ignition-elementor-widgets-product-tabs ignition-elementor-widgets-product-tabs-<?php echo esc_attr( $settings['tab_button_alignment'] ); ?> ignition-elementor-widgets-product-tabs-<?php echo esc_attr( $settings['layout'] ); ?>">
			<?php if ( ! empty( $tabs ) ) : ?>
			<ul class="ignition-elementor-widgets-product-tabs__nav">
				<?php
				$is_active = true;

				foreach ( $tabs as $tab ) {
					$cat_title = isset( $tab['product_category'] ) && ! empty( $tab['product_category'] ) ? get_term( $tab['product_category'] )->name : false;
					$tab_title = isset( $tab['tab_title'] ) && ! empty( $tab['tab_title'] ) ? $tab['tab_title'] : $cat_title;

					if ( $tab_title ) :
						?>
						<li class="ignition-elementor-widgets-product-tab <?php echo esc_attr( $is_active ? 'active' : '' ); ?>"><?php echo esc_html( $tab_title ); ?></li>
						<?php
						$is_active = false;
					endif;
				}
				?>
			</ul>
			<?php endif; ?>

			<div class="ignition-elementor-widgets-product-tabs__content-wrapper">
				<?php if ( empty( $tabs ) ) : ?>
					<?php $container_classes[] = 'active'; ?>
					<div class="<?php echo esc_attr( implode( ' ', $container_classes ) ); ?>">
						<?php
						$classes[] = 'placeholder-wrapper';

						for ( $j = 0; $j < $num_products; $j++ ) {
							?>
							<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
								<div class="entry-item entry-item-product product type-product has-post-thumbnail first placeholder-product">
									<a href="#" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">
										<div class="entry-item-thumb">
											<img src="<?php echo esc_url( wc_placeholder_img_src() ); ?>">
										</div>
									</a>
									<div class="entry-item-content">
										<a href="#" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">
											<h2 class="woocommerce-loop-product__title"><?php esc_html_e( 'Product title', 'ignition' ); ?></h2>
										</a>
										<span class="price"><span class="woocommerce-Price-amount amount"><?php esc_html_e( 'Price', 'ignition' ); ?></span></span>

										<a href="#" class="button wp-element-button product_type_simple add_to_cart_button ajax_add_to_cart" rel="nofollow"><?php esc_html_e( 'Add to cart', 'ignition' ); ?></a>
									</div>
								</div>
							</div>
						<?php } ?>
					</div>
					<?php
				else :
					$is_active = true;
					foreach ( $tabs as $tab ) {
						$product_data = array();

						if ( ! empty( $tab['handpicked_products'] ) ) {
							$product_data = $tab['handpicked_products'];
						} elseif ( isset( $tab['product_category'] ) ) {
							$product_data = $tab['product_category'];
						}

						if ( $is_active ) {
							array_push( $container_classes, 'active' );
						} elseif ( in_array( 'active', $container_classes, true ) ) {
							$key = array_search( 'active', $container_classes, true );
							unset( $container_classes[ $key ] );
						}

						$args = array(
							'post_type'      => 'product',
							'posts_per_page' => $num_products,
						);

						$tax_query = array(
							array(
								'taxonomy' => 'product_visibility',
								'field'    => 'name',
								'terms'    => 'exclude-from-catalog',
								'operator' => 'NOT IN',
							),
						);

						if ( ! empty( $tab['handpicked_products'] ) ) {
							$handpicked_query = array(
								'post__in' => $product_data,
							);

							$args = array_merge( $args, $handpicked_query );

						} elseif ( ! empty( $tab['product_category'] ) ) {
							$tax_query = array_merge(
								array(
									'relation' => 'AND',
									array(
										'taxonomy' => 'product_cat',
										'terms'    => $product_data,
										'field'    => 'id',
									),
								),
								$tax_query,
							);
						}

						$args['tax_query'] = $tax_query;

						$loop = new \WP_Query( $args );

						if ( ( ! empty( $tab['handpicked_products'] ) || ! empty( $tab['product_category'] ) ) && $loop->have_posts() ) :

							$item_template_vars['selected-term'] = isset( $tab['product_category'] ) && empty( $tab['handpicked_products'] ) ? get_term( $product_data ) : false;
							?>
							<div class="<?php echo esc_attr( implode( ' ', $container_classes ) ); ?>" data-columns="<?php echo (int) $columns; ?>" data-products="<?php echo (int) $num_products; ?>">
							<?php

							while ( $loop->have_posts() ) :
								$loop->the_post();

								?>
									<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
										<?php
										\CSSIgniter\Ignition\Elementor\Widgets_Helper::get_template_part( 'product-tabs/item', get_post_type(), $item_template_vars );
										?>
									</div>
										<?php
								endwhile;
							?>
							</div>
							<?php
							wp_reset_postdata();
						else :
							?>
							<div class="<?php echo esc_attr( implode( ' ', $container_classes ) ); ?> ignition-elementor-widgets-product-tabs__no-products">
								<?php esc_html_e( 'No products found. Please select another product category or create a custom selection.', 'ignition-elementor-widgets' ); ?>
							</div>
							<?php
						endif;
						$is_active = false;
					}
				endif;
				?>
			</div>
		</div>
		<?php
	}
}
