<?php
namespace Elementor;

class emtiazproduct extends Widget_Base {
	
	public function get_name() {
		return 'emtiazproduct';
	}
	
	public function get_title() {
		return 'نمایش امتیاز محصول';
	}
	
 public function get_icon() {
		return 'eicon eicon-product-related';
    }
    
    public function get_categories() {
        return [ 'farnamproduct' ];
    }

	
	protected function register_controls() {





	
		
		
  $this->start_controls_section(
            'farnam_product_style_2',
            [
                'label' => esc_html__( 'تنظیمات استایل و ظاهر', 'woolentor' ),
            ]
        );

	      $this->add_control(
'sepidcolor-3',
			[
				'label' => __( 'رنگ پس زمینه باکس اصلی', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .emtiaz--402-dey-2 .emtiaz-2' => 'background: {{VALUE}} !important',
				],
			]
           );
		    $this->add_control(
'sepidcolor-2',
			[
				'label' => __( 'رنگ عنوان', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .emtiaz--402-dey-2 .emtiaz-title' => 'color: {{VALUE}} !important',
				],
			]
           );
		    $this->add_control(
'sepidcolor-245d1',
			[
				'label' => __( 'رنگ کادر باکس امتیاز ها', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .emtiaz--402-dey-2 .emtiaz-4 .emtiaz-5' => 'border-color: {{VALUE}} !important',
				],
			]
           );
		   
  $this->add_control(
'sepidcolor-5fr1',
			[
				'label' => __( 'رنگ پس زمینه باکس امتیاز ها', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .emtiaz-5' => 'background: {{VALUE}} !important',
				],
			]
           );

		
		  $this->add_control(
'sepidcolor-4',
			[
				'label' => __( 'رنگ متن نام امتیاز', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .emtiaz-6 .number' => 'color: {{VALUE}} !important',
				],
			]
           );
		   	  $this->add_control(
'sepidcolor-4hg',
			[
				'label' => __( 'رنگ متن تعداد امتیاز', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .emtiaz-6 .shomaresh' => 'color: {{VALUE}} !important',
				],
			]
           );
		   	      $this->add_control(
'sepidcolor-5',
			[
				'label' => __( 'رنگ پس زمینه نوار درصد', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .emtiaz--402-dey-2 .emtiaz-4 .emtiaz-5 .bars' => 'background: {{VALUE}} !important',
				],
			]
           );
		   
	
		     $this->add_control(
'sepidcolor-6',
			[
				'label' => __( 'رنگ پس زمینه نوار فعال درصد', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .emtiaz--402-dey-2 .emtiaz-4 .emtiaz-5 .bars .barsf' => 'background: {{VALUE}} !important',
				],
			]
           );
		   
	
		    
		  
		  
		   
					 $this->add_responsive_control(
                        'sepidborder-2',
                        [
                            'label' => esc_html__( 'گردی باکس اصلی', 'woolentor' ),
                            'type' => Controls_Manager::DIMENSIONS,
                            'selectors' => [
                                '{{WRAPPER}} .emtiaz--402-dey-2 .emtiaz-2' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px !important;',
                            ],
                        ]
                    );
					
					$this->add_responsive_control(
                        'sepidborder-3',
                        [
                            'label' => esc_html__( 'گردی باکس امتیاز', 'woolentor' ),
                            'type' => Controls_Manager::DIMENSIONS,
                            'selectors' => [
                                '{{WRAPPER}} .lidemt' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px !important;',
                            ],
                        ]
                    );
					
					$this->add_responsive_control(
                        'sepidborder-4',
                        [
                            'label' => esc_html__( 'گردی باکس امتیاز ها', 'woolentor' ),
                            'type' => Controls_Manager::DIMENSIONS,
                            'selectors' => [
                                '{{WRAPPER}} .emtiaz--402-dey-2 .emtiaz-4 .emtiaz-5' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px !important;',
                            ],
                        ]
                    );
				
					
					
		$this->end_controls_section();
		
	}
	protected function render() {
		$settings = $this->get_settings_for_display();
	if ( ! post_type_supports( 'product', 'comments' ) ) {
			return;
		}
	?>
	           <?php if (is_product()) { ?>

<?php
if ( 'no' === get_option( 'woocommerce_enable_review_rating' ) ) {
	return;
}

global
 
$product;

if ( 'no' === get_option( 'woocommerce_enable_review_rating' ) ) {
	return;
}
$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average   = $product->get_average_rating();

if ( $rating_count > 0 ) :

	$average_int = intval($average);

	if ( $average_int < 3 ) {
		$class_value = '#FBA700';
	} else {
		$class_value = '#2DCEA2';
	}

	// کد شما اینجاست

endif; ?>            <div class="emtiaz--402-dey-2">

            <div class="emtiaz-2">
                            <!-- Averate Rating -->
                            <div class="emtiaz-3">
						
							                            <div class="emtiaz-33">
	<div class="emtiaz-title">
							متوسط امتیاز ها
							</div>
							                                   		<?php echo wc_get_rating_html( $average, $rating_count ); ?>

                                    <div class="emtiaz-number">	<div class="lidemt" style="background: <?php echo $class_value; ?>"><?php echo esc_attr($average); ?></div>
</div>
                            

                            </div></div>
							
							
                           <?php
                 // WP_Comment_Query arguments
$args = array (
	'status'		=> 'approve',
	'type'			=> 'review',
	'post_id'		=> get_the_id(),
);

// Get the comments
$comments = get_comments($args);

// Initialize the variables
$rate1 = $rate2 = $rate3 = $rate4 = $rate5 = 0;

// The Comment Loop

if ( $comments ) {
	foreach ( $comments
 
as
 
$comment ) {
		$rate = get_comment_meta($comment->comment_ID, 'rating', true);
		switch($rate) {
			case
 
1:
				$rate1++;
				break;
			case
 
2:
				$rate2++;
				break;
			case
 
3:
				$rate3++;
				break;
			case
 
4:
				$rate4++;
				break;
			case
 
5:
				$rate5++;
				break;
		} // switch
	}
}

// Get the rates
$rates = array('5'=>$rate5,'4'=>$rate4,'3'=>$rate3,'2'=>$rate2,'1'=>$rate1);
                            ?>
                            <div class="emtiaz-4">
						
                                <div class="emtiaz-5">
	                                <?php foreach($rates as $key => $rate): ?>
		                              
<?php
										$fill_value = 0;
										$rating_count = get_comments_number(get_the_id());
										if($rating_count > 0) {
											$fill_value = min(round($rate*100/$rating_count, 2), 100);
										}
										?>

<div class="emtiaz-6">
										                                            <span class="number"><?php echo esc_attr($key.' '.__('امتیاز', 'studiare')); ?></span>

												<div class="bars">
														<div class="barsf" style="width:<?php echo esc_attr($fill_value); ?>%"></div>
												</div>
												<span class="shomaresh"><?php echo esc_attr($rate); ?></span>
										</div>
	                                <?php endforeach; ?>
                                </div>
                            </div>
                        </div>
                        </div>

		   <?php }

else {
	echo '
	
	<div class="emtiaz--402-dey-2">

            <div class="emtiaz-2">
                            <!-- Averate Rating -->
                            <div class="emtiaz-3">
						
							                            <div class="emtiaz-33">
	<div class="emtiaz-title">
							متوسط امتیاز ها
							</div>
							                                   		<div class="star-rating" role="img" aria-label="نمره 2.00 از 5"><span style="width:40%">امتیاز <strong class="rating">2.00</strong> از 5 امتیاز <span class="rating">1</span> مشتری</span></div>
                                    <div class="emtiaz-number">	<div class="lidemt" style="background: #FBA700">2.00</div>
</div>
                            

                            </div></div>
							
							
                                                       <div class="emtiaz-4">
						
                                <div class="emtiaz-5">
	                                		                              

<div class="emtiaz-6">
										                                            <span class="number">5 امتیاز</span>

												<div class="bars">
														<div class="barsf" style="width:0%"></div>
												</div>
												<span class="shomaresh">0</span>
										</div>
	                                		                              

<div class="emtiaz-6">
										                                            <span class="number">4 امتیاز</span>

												<div class="bars">
														<div class="barsf" style="width:0%"></div>
												</div>
												<span class="shomaresh">0</span>
										</div>
	                                		                              

<div class="emtiaz-6">
										                                            <span class="number">3 امتیاز</span>

												<div class="bars">
														<div class="barsf" style="width:0%"></div>
												</div>
												<span class="shomaresh">0</span>
										</div>
	                                		                              

<div class="emtiaz-6">
										                                            <span class="number">2 امتیاز</span>

												<div class="bars">
														<div class="barsf" style="width:100%"></div>
												</div>
												<span class="shomaresh">1</span>
										</div>
	                                		                              

<div class="emtiaz-6">
										                                            <span class="number">1 امتیاز</span>

												<div class="bars">
														<div class="barsf" style="width:0%"></div>
												</div>
												<span class="shomaresh">0</span>
										</div>
	                                                                </div>
                            </div>
                        </div>
                        </div>
	
	';
}

		   ?>




		<?php


	}


	
	protected function _content_template() {

    }
	
	
}