<?php
/*
 * Elementor eCommerce Addon Product Category Widget
 * Author & Copyright: NicheAddon
*/

namespace Elementor;

if (!isset(get_option( 'naeca_bw_settings' )['naeca_product_cat'])) { // enable & disable

if ( class_exists( 'woocommerce' ) ) {

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

class eCommerce_Elementor_Addon_Product_Category extends Widget_Base{

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

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

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

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

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

		$this->start_controls_section(
			'section_product_cat',
			[
				'label' => __( 'Product Category Item', 'ecommerce-addon' ),
			]
		);
		$this->add_control(
			'product_cat_style',
			[
				'label' => esc_html__( 'Product Category Style', 'ecommerce-addon' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'one' => esc_html__( 'Style One', 'ecommerce-addon' ),
					'two' => esc_html__( 'Style Two', 'ecommerce-addon' ),
				],
				'default' => 'one',
				'description' => esc_html__( 'Select your style.', 'ecommerce-addon' ),
			]
		);
		$this->add_control(
			'cat_limit',
			[
				'label' => esc_html__( 'Category Count', 'ecommerce-addon' ),
				'type' => Controls_Manager::NUMBER,
				'min' => 1,
				'max' => 100,
				'step' => 1,
				'default' => 3,
				'description' => esc_html__( 'Enter the number of items to show.', 'ecommerce-addon' ),
			]
		);
		$this->add_control(
			'cat_column',
			[
				'label' => __( 'Columns', 'ecommerce-addon' ),
				'type' => Controls_Manager::SELECT,
				'frontend_available' => true,
				'options' => [
					'col-1' => esc_html__( 'One Column', 'ecommerce-addon' ),
					'col-2' => esc_html__( 'Two Column', 'ecommerce-addon' ),
					'col-3' => esc_html__( 'Three Column', 'ecommerce-addon' ),
					'col-4' => esc_html__( 'Four Column', 'ecommerce-addon' ),
				],
				'default' => 'col-1',
				'description' => esc_html__( 'Select your column.', 'ecommerce-addon' ),
			]
		);
		$this->end_controls_section();// end: Section

		// Section
			$this->start_controls_section(
				'sectn_style',
				[
					'label' => esc_html__( 'Section', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_responsive_control(
				'section_padding',
				[
					'label' => __( 'Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .category-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'section_margin',
				[
					'label' => __( 'Margin', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .category-item' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'section_inner_padding',
				[
					'label' => __( 'Inner Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .category-body' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_group_control(
				Group_Control_Background::get_type(),
				[
					'name' => 'secn_bg_color',
					'label' => __( 'Background Color', 'ecommerce-addon' ),
					'types' => [ 'classic', 'gradient' ],
					'selector' => '{{WRAPPER}} .category-body',
				]
			);
			$this->add_group_control(
				Group_Control_Border::get_type(),
				[
					'name' => 'secn_border',
					'label' => esc_html__( 'Border', 'ecommerce-addon' ),
					'selector' => '{{WRAPPER}} .category-item',
				]
			);
			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				[
					'name' => 'secn_box_shadow',
					'label' => esc_html__( 'Section Box Shadow', 'ecommerce-addon' ),
					'selector' => '{{WRAPPER}} .category-item',
				]
			);
			$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_margin',
				[
					'label' => __( 'Title Spacing', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px' ],
					'selectors' => [
						'{{WRAPPER}} .category-body h5' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_group_control(
				Group_Control_Typography::get_type(),
				[
					'label' => esc_html__( 'Typography', 'ecommerce-addon' ),
					'name' => 'sastool_title_typography',
					'selector' => '{{WRAPPER}} .category-body h5',
				]
			);
			$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}} .category-body h5, {{WRAPPER}} .category-body h5 a' => 'color: {{VALUE}};',
						],
					]
				);
				$this->end_controls_tab();  // end:Normal tab
				$this->start_controls_tab(
					'title_hover',
					[
						'label' => esc_html__( 'Hover', 'ecommerce-addon' ),
					]
				);
				$this->add_control(
					'title_hover_color',
					[
						'label' => esc_html__( 'Color', 'ecommerce-addon' ),
						'type' => Controls_Manager::COLOR,
						'selectors' => [
							'{{WRAPPER}} .category-body h5 a:hover' => 'color: {{VALUE}};',
						],
					]
				);
				$this->end_controls_tab();  // end:Hover tab
			$this->end_controls_tabs(); // end tabs
			$this->end_controls_section();// end: Section

		// Content
			$this->start_controls_section(
				'section_content_style',
				[
					'label' => esc_html__( 'Content', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_responsive_control(
				'cont_padding',
				[
					'label' => __( 'Content Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .category-body p' => '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' => 'content_typography',
					'selector' => '{{WRAPPER}} .category-body p',
				]
			);
			$this->add_control(
				'content_color',
				[
					'label' => esc_html__( 'Color', 'ecommerce-addon' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .category-body p' => 'color: {{VALUE}};',
					],
				]
			);
			$this->end_controls_section();// end: Section

		// Count
			$this->start_controls_section(
				'section_count_style',
				[
					'label' => esc_html__( 'Count', 'ecommerce-addon' ),
					'tab' => Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_responsive_control(
				'count_margin',
				[
					'label' => __( 'Count Margin', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .categories-tag' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);
			$this->add_responsive_control(
				'count_padding',
				[
					'label' => __( 'Count Padding', 'ecommerce-addon' ),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .categories-tag' => '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' => 'count_typography',
					'selector' => '{{WRAPPER}} .categories-tag',
				]
			);
			$this->add_control(
				'count_color',
				[
					'label' => esc_html__( 'Color', 'ecommerce-addon' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .categories-tag' => 'color: {{VALUE}};',
					],
				]
			);
			$this->add_control(
				'count_bg_color',
				[
					'label' => esc_html__( 'Background Color', 'ecommerce-addon' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .categories-tag' => 'background: {{VALUE}};',
					],
				]
			);
			$this->end_controls_section();// end: Section

	}

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

		if ($product_cat_style === 'two') {
			$style_cls = ' categories-style-two';
		} else {
			$style_cls = '';
		}

		// Columns
		if($cat_column === 'col-2') {
      $col_class = 'nich-col-xl-6 nich-col-lg-6 nich-col-sm-6';
    } elseif($cat_column === 'col-3') {
      $col_class = 'nich-col-xl-4 nich-col-lg-4 nich-col-sm-6';
    } elseif($cat_column === 'col-4') {
      $col_class = 'nich-col-xl-3 nich-col-lg-4 nich-col-sm-6';
    } else {
      $col_class = 'nich-col-md-12';
    }

    if($cat_limit !== '') {
      $args['number'] = $cat_limit;
    }

		$pro_cats = get_terms('product_cat', $args);

    // Turn output buffer on
    ob_start(); ?>
    <div class="naeca-categories<?php echo $style_cls; ?>">
      <div class="nich-row">   
      <?php foreach ($pro_cats as $pro_cat) {
        $thumbnail_id = get_term_meta( $pro_cat->term_id, 'thumbnail_id', true );
        $cat_image    = wp_get_attachment_image_src( $thumbnail_id, 'fullsize', false, '' );
        $cat_image    = $cat_image[0];

        if($cat_image){
          $cat_image  = $cat_image;
        } else {
          $cat_image  = '';
        }

        $catUrl = get_term_link($pro_cat->term_id); ?>
      	<div class="<?php echo $col_class; ?>">
				<?php if ($product_cat_style === 'two') { ?>
					<div class="category-item">
            <?php if($cat_image){ ?><a href="<?php echo esc_url($catUrl); ?>"><img src="<?php echo esc_url($cat_image); ?>" alt="<?php echo esc_attr($pro_cat->name); ?>"></a><?php } ?>
            <div class="category-body">
              <?php if($pro_cat->name) { ?>
			          <h5><a href="<?php echo esc_url($catUrl); ?>"><?php echo esc_attr($pro_cat->name); ?></a></h5>
			        <?php } ?>
          		<span class="categories-tag"><?php echo esc_attr($pro_cat->count); ?> Products</span>
            </div>
          </div>
				<?php } else { ?>
      		<div class="category-item">
            <?php if($cat_image){ ?><a href="<?php echo esc_url($catUrl); ?>"><img src="<?php echo esc_url($cat_image); ?>" alt="<?php echo esc_attr($pro_cat->name); ?>"></a><?php } ?>
            <div class="category-body">
              <?php if($pro_cat->name) { ?>
			          <h5><a href="<?php echo esc_url($catUrl); ?>"><?php echo esc_attr($pro_cat->name); ?></a></h5>
			        <?php }
			        if($pro_cat->description) { ?>
			          <p><?php echo esc_attr($pro_cat->description); ?></p>
			        <?php } ?>
          		<span class="categories-tag"><?php echo esc_attr($pro_cat->count); ?> Products</span>
            </div>
          </div>
				<?php } ?>
	      </div>
      <?php } ?>
    	</div>
  	</div>
		<?php
		// Return outbut buffer
		echo ob_get_clean();

	}

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

}
} // enable & disable
