<?php
namespace Elementor;

class timersingle extends Widget_Base {
	
	public function get_name() {
		return 'timersingle';
	}
	
	public function get_title() {
		return 'تایمر صفحه محصول';
	}
	
 public function get_icon() {
		return 'eicon eicon-clock-o';
    }
    
    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-4',
			[
				'label' => __( 'رنگ متن تایمر', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .text32' => 'color: {{VALUE}} !important',
				],
			]
           );
		     $this->add_control(
'sepidcolor-4rgfe5t53',
			[
				'label' => __( 'رنگ پس زمینه شمارنده', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .right-two-p-sepid-new-elementor-3455445 .timer-wrapper .time1' => 'background: {{VALUE}} !important',
					'{{WRAPPER}} .right-two-p-sepid-new-elementor-3455445 .timer-wrapper .time1::after' => 'border-top-color: {{VALUE}} !important',
				],
			]
           );
		   	      $this->add_control(
'sepidcolor-5',
			[
				'label' => __( 'رنگ متن شمارنده', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .right-two-p-sepid-new-elementor-3455445 .timer-wrapper .time1' => 'color: {{VALUE}} !important',
				],
			]
           );
		   
	
		     $this->add_control(
'sepidcolor-4rgfe5t53422',
			[
				'label' => __( 'رنگ پس زمینه ثانیه شمار', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .right-two-p-sepid-new-elementor-3455445 .timer-wrapper.secgf .seconds.time1.mini-4-time' => 'background: {{VALUE}} !important',
					'{{WRAPPER}} .right-two-p-sepid-new-elementor-3455445 .timer-wrapper.secgf .seconds.time1.mini-4-time::after' => 'border-top-color: {{VALUE}} !important',
				],
			]
           );
		   	      $this->add_control(
'sepidcolor-555544',
			[
				'label' => __( 'رنگ متن ثانیه شمار', 'plugin-domain' ),
				'type' => Controls_Manager::COLOR,
			
				'selectors' => [
					'{{WRAPPER}} .right-two-p-sepid-new-elementor-3455445 .timer-wrapper.secgf .seconds.time1.mini-4-time' => 'color: {{VALUE}} !important',
				],
			]
           );
					 $this->add_responsive_control(
                        'sepidborder-2',
                        [
                            'label' => esc_html__( 'گردی شمارنده', 'woolentor' ),
                            'type' => Controls_Manager::DIMENSIONS,
                            'selectors' => [
                                '{{WRAPPER}} .right-two-p-sepid-new-elementor-3455445 .timer-wrapper .time1' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px !important;',
                            ],
                        ]
                    );
		$this->end_controls_section();
		
	}

protected function render() {
      $settings   = $this->get_settings_for_display();
        global $product;
        $product = wc_get_product()


				?>
  <?php if (is_product()) { ?>

           <?php 
		   global $post;
								if(  $product->is_on_sale() && $product->is_type('variable')){
					

	

							
		 
    if( $product->is_type('variable') ){
        $default_attributes = $product->get_default_attributes();
        foreach($product->get_available_variations() as $variation_values ){
            foreach($variation_values['attributes'] as $key => $attribute_value ){
                $attribute_name = str_replace( 'attribute_', '', $key );
                $default_value = $product->get_variation_default_attribute($attribute_name);
                if( $default_value == $attribute_value ){
                    $is_default_variation = true;
                } else {
                    $is_default_variation = false;
                    break; // Stop this loop to start next main lopp
                }
            }
            if( $is_default_variation ){
                $variation_id = $variation_values['variation_id'];
                break; // Stop the main loop
		
            }
        }

global $product;

if ($product->is_on_sale() && $product->is_type('variable') ) {
    $default_variation_id = 0;
    $default_variation = $product->get_available_variations()[0];
    if (is_a($default_variation, 'WC_Product_Variation')) {
        $variation_id = $default_variation->get_variation_id_by_sku($default_variation->get_sku());
    }
	
if ($product->is_on_sale() && $product->is_type('variable')) {
    global $product;

    $variation_id = null;

    if ($product->is_type('variable')) {
        $default_attributes = $product->get_default_attributes();
        foreach ($product->get_available_variations() as
 
$variation_values) {
            foreach ($variation_values['attributes'] as
 
$key => $attribute_value) {
                $attribute_name = str_replace('attribute_', '', $key);
                $default_value = $product->get_variation_default_attribute($attribute_name);

                if ($default_value == $attribute_value) {
                    $variation_id = $variation_values['variation_id'];
                    break 2; // Exit both loops
                }
            }
        }
    }

    if ($variation_id) {
        $sale_end_date = get_post_meta($variation_id, '_sale_price_dates_to', true);
    } else {
        // Variation is not selected, so don't display anything
    }
}
    if (empty($sale_end_date) || $sale_end_date < current_time('timestamp')) {
								        // چیزی نمایش داده نمیشود
    } else {
        // The discount is still active
 

        // Now we get the default variation data
        if( $is_default_variation ){
            // Raw output of available "default" variation details data
		 $meta_date = get_post_meta( $variation_id, '_sale_price_dates_to', true );
					if( empty( $meta_date ) ){
																//	echo esc_html_e( 'زمان بندی پیشنهاد ویژه تعیین نشده است!', 'studiare' );
															}
          	else{
					$date  = date( 'm/d/Y 23:59', $meta_date );//echo $date;
			
								
				
	
		
            echo '<div class="right-two-p-sepid-new-elementor-3455445">



<div id="fmgf" class="timer">

   <div class="timer-wrapper">
      <div class="days time1"></div><span class="text32">روز</span>
   </div>

   <div class="timer-wrapper">
      <div class="hours time1"></div> <span class="text32">ساعت</span>
   </div>

   <div class="timer-wrapper">
      <div class="minutes time1"></div> <span class="text32">دقیقه</span>
   </div>

   <div class="timer-wrapper secgf">
      <div class="seconds time1 mini-4-time"></div> <span class="text32">ثانیه</span>
   </div>

</div><script type="text/javascript">
  $("#fmgf")
  .countdown("'; print_r($date); echo '", function(event) {
   $(".days").html(event.strftime("%D"));
   $(".hours").html(event.strftime("%H"));
   $(".minutes").html(event.strftime("%M"));
   $(".seconds").html(event.strftime("%S"));
});
</script></div>
';
}
          			

   


								}
								else {
								        // چیزی نمایش داده نمیشود
	
									
								}
			}
					   }
}			
									}
								
								else {
									
							



  $sales_price_to = get_post_meta($post->ID, '_sale_price_dates_to', true);
															if( empty( $meta_date ) ){
																//	echo esc_html_e( 'زمان بندی پیشنهاد ویژه تعیین نشده است!', '' );
															}
					else{
					// $date  = date_i18n( 'm/d/Y', $meta_date );
					}
					
					
					
 $sale_end_date = get_post_meta($product->get_id(), '_sale_price_dates_to', true);
  if ($sale_end_date < current_time('timestamp')) {
    // تخفیف به پایان رسیده است، بنابراین تایمر را نمایش ندهید
  } else {
	  global $post;


					$date  = date( 'm/d/Y 23:59', $sales_price_to );//echo $date;



									echo '
									
									
									
				
									
				<div class="right-two-p-sepid-new-elementor-3455445">



<div id="fmgf" class="timer">

   <div class="timer-wrapper">
      <div class="days time1"></div><span class="text32">روز</span>
   </div>

   <div class="timer-wrapper">
      <div class="hours time1"></div> <span class="text32">ساعت</span>
   </div>

   <div class="timer-wrapper">
      <div class="minutes time1"></div> <span class="text32">دقیقه</span>
   </div>

   <div class="timer-wrapper secgf">
      <div class="seconds time1 mini-4-time"></div> <span class="text32">ثانیه</span>
   </div>

</div><script type="text/javascript">
  $("#fmgf")
  .countdown("'; print_r($date); echo '", function(event) {
   $(".days").html(event.strftime("%D"));
   $(".hours").html(event.strftime("%H"));
   $(".minutes").html(event.strftime("%M"));
   $(".seconds").html(event.strftime("%S"));
});
</script></div>
									
									
';
								}}
								

  
?>

  <?php } ?>
		<?php


	}


	
	protected function _content_template() {

    }
	
	
}