<?php
use Elementor\Widget_Base;
use Elementor\Controls_Manager;

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

class kavehwoocommerceprice extends \Elementor\Widget_Base
{

  public function get_name()
  {
    return 'kavehwoocommerceprice';
  }

  public function get_title()
  {
    return esc_html__('Single Product Price Element', 'kavehTheme');
  }

  public function get_icon()
  {
    return 'eicon-archive-posts';
  }

  public function get_categories()
  {
    return ['woocommercekavehelements'];
  }

  protected function register_controls()
  {
    $this->start_controls_section(
      'content1kavehwoocommerceprice',
      [
        'label' => esc_html__( 'Content', 'kavehTheme' ),
    
       'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
      ]
    );

    $this->add_control(
      'text_alignkavehwoocommerceprice',
      [
        'label' => esc_html__( 'Text Layout', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::CHOOSE,
        'default'   => 'text-start',
        'options' => [
          'text-end' => [
            'title' => esc_html__( 'Left Align', 'kavehTheme' ),
            'icon' => 'eicon-text-align-left',
          ],
          'text-center' => [
            'title' => esc_html__( 'Center Align', 'kavehTheme' ),
            'icon' => 'eicon-text-align-center',
          ],
          'text-start' => [
            'title' => esc_html__( 'Right Align', 'kavehTheme' ),
            'icon' => 'eicon-text-align-right',
          ],
        ],
        'toggle' => true,
      ]
    );
    $this->add_control(
      'titletagkavehwoocommerceprice',
      [
        'label' => esc_html__( 'Title Tag', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'H2',
        'options' => [
          'H1'  => esc_html__( 'H1', 'kavehTheme' ),
          'H2' => esc_html__( 'H2', 'kavehTheme' ),
          'H3' => esc_html__( 'H3', 'kavehTheme' ),
          'H4' => esc_html__( 'H4', 'kavehTheme' ),
          'H5' => esc_html__( 'H5', 'kavehTheme' ),
          'H6' => esc_html__( 'H6', 'kavehTheme' ),
          'p' => esc_html__( 'p', 'kavehTheme' ),
          'div' => esc_html__( 'div', 'kavehTheme' ),
          'span' => esc_html__( 'span', 'kavehTheme' ),
        ],
      ]
    );

    $this->end_controls_section();
    $this->start_controls_section(
      'style_kavehwoocommerceprice',
      [
        'label' => esc_html__( 'Style', 'kavehTheme' ),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
      ]
    );
    $this->add_control(
      'title1_color1_kavehwoocommerceprice',
      [
        'label' => esc_html__( 'Discount Title Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .price-add-to-cart .offer-product' => 'color: {{VALUE}} !important',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'title1_type_kavehwoocommerceprice',
        'label' => esc_html__( 'Discount Title Typography', 'kavehTheme' ),
        'selector' => '{{WRAPPER}} .price-add-to-cart .offer-product',
      ]
    );
    $this->add_control(
      'title2_color1_kavehwoocommerceprice',
      [
        'label' => esc_html__( 'Discounted Price Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} del .woocommerce-Price-amount bdi:first-child' => 'color: {{VALUE}} !important',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'title2_type_kavehwoocommerceprice',
        'label' => esc_html__( 'Discounted Price Typography', 'kavehTheme' ),
        'selector' => '{{WRAPPER}} del .woocommerce-Price-amount bdi:first-child',
      ]
    );
    $this->add_control(
      'line_color1_kavehwoocommerceprice',
      [
        'label' => esc_html__( 'Discount Line Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} del .woocommerce-Price-amount bdi:first-child' => 'text-decoration: line-through {{VALUE}} !important',
        ],
      ]
    );
    $this->add_control(
      'title3_color1_kavehwoocommerceprice',
      [
        'label' => esc_html__( 'Currency Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .woocommerce-Price-amount:only-child .woocommerce-Price-currencySymbol' => 'color: {{VALUE}}',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'title3_type_kavehwoocommerceprice',
        'label' => esc_html__( 'Currency Typography', 'kavehTheme' ),
        'selector' => '{{WRAPPER}} .woocommerce-Price-amount:only-child .woocommerce-Price-currencySymbol',
      ]
    );
    $this->end_controls_section();

  }



  protected function render()
  {
    $settings = $this->get_settings_for_display();
    $clsid = $this->get_id();
    if ( ! class_exists( 'WooCommerce' ) ) {
      return;
  }

  if (\Elementor\Plugin::$instance->editor->is_edit_mode()) {
    $args = array(
      'post_type' => 'product',
      'posts_per_page' => 1, // Latest product
      'orderby' => 'date',
      'order' => 'DESC',
  );
      $latest_product = wc_get_products($args);
        // Get product ID
        $product_id = $latest_product[0]; 
      global $product;
        if(!empty($product_id)){
        $product    = wc_get_product( $product_id );

        if ($product) {
          $regular_price = $product->get_regular_price(); // Regular price
          $sale_price    = $product->get_sale_price();    // Sale price
          $currency      = get_woocommerce_currency_symbol(); // Currency symbol
  

          // Generate dynamic HTML
          ?>
           <div class="price-add-to-cart">
                <?php if ( !$product->is_type( 'variable' ) ) : ?>
                    <?php if(!empty($sale_price) ): ?>
                    <div class="offer-product">
                        <span>         <?php
        $percentage = kavehWoo::showofferpercentage($product);
        echo $percentage . "%";
        ?> </span>
                        <?php esc_html_e('Discount', 'kavehTheme'); ?>
                    </div>
                    <?php endif; ?>
               <div class="price d-flex align-items-center mt-sm-0">
                <?php
                $price_v = $product->get_price_html(); 
                echo str_replace("&ndash;","<span class=\"dashvar\"> – </span>", $price_v);
                ?>
                </div>
              <?php elseif ( $product->is_type( 'variable' ) ) : 
              ?>

               <div class="price d-flex align-items-center mt-sm-0">
                <?php
                $price_v = $product->get_price_html(); 
                echo str_replace("&ndash;","<span class=\"dashvar\"> – </span>", $price_v);
                ?>
                </div>

              <?php
              endif; ?>
             
            </div>
          <?php
      }
          }else{
        esc_html__('No product found.', 'kavehTheme');  
      } 

  }else{
   global $product;


    $product_id = get_queried_object_id();
    if(!empty($product_id)){
    $product    = wc_get_product( $product_id ); ?>

<?php 
      if ($product) {
        $regular_price = $product->get_regular_price(); // Regular price
        $sale_price    = $product->get_sale_price();    // Sale price
        $currency      = get_woocommerce_currency_symbol(); // Currency symbol


        // Generate dynamic HTML
        ?>
            <div class="price-add-to-cart">
                <?php if ( !$product->is_type( 'variable' ) ) : ?>
                    <?php if(!empty($sale_price) ): ?>
                    <div class="offer-product">
                        <span>        <?php
                    $percentage = kavehWoo::showofferpercentage($product);
                    echo $percentage ;
                    ?> </span>
                  <?php esc_html_e('Discount', 'kavehTheme'); ?>
                    </div>
                    <?php endif; ?>
               <div class="price d-flex align-items-center mt-sm-0">
                <?php
                $price_v = $product->get_price_html(); 
                echo str_replace("&ndash;","<span class=\"dashvar\"> – </span>", $price_v);
                ?>
                </div>
              <?php elseif ( $product->is_type( 'variable' ) ) : 
              ?>

               <div class="price d-flex align-items-center mt-sm-0">
                <?php
                $price_v = $product->get_price_html(); 
                echo str_replace("&ndash;","<span class=\"dashvar\"> – </span>", $price_v);
                ?>
                </div>

              <?php
              endif; ?>
             
            </div>
        <?php 
    }

      }else{
    esc_html__('No product found.', 'kavehTheme');  
  } 


  }


  }

}


