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

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

class kavehwoocommerceaddtocart extends \Elementor\Widget_Base
{

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

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

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

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

  protected function register_controls()
  {
    $this->start_controls_section(
      'content1kavehwoocommerceaddtocart',
      [
        'label' => esc_html__( 'Content', 'kavehTheme' ),
    
       'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
      ]
    );
    $this->add_control(
      'title1kavehwoocommerceaddtocart',
      [
          'label' => esc_html__( 'Button Title', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::TEXT,
          'default' => esc_html__( 'Add to Cart', 'kavehTheme' ),
  
      ]
  );

    $this->add_control(
      'text_alignkavehwoocommerceaddtocart',
      [
        '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(
      'titletagkavehwoocommerceaddtocart',
      [
        '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_kavehwoocommerceaddtocart',
      [
        'label' => esc_html__( 'Style', 'kavehTheme' ),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'title1typekavehwoocommerceaddtocart',
        'label' => esc_html__( 'Title Typography', 'kavehTheme' ),
        'selector' => '{{WRAPPER}} .main-post-title',
      ]
    );
    $this->add_control(
      'title1color1kavehwoocommerceaddtocart',
      [
        'label' => esc_html__( 'Title Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .main-post-title' => 'color: {{VALUE}} !important',
        ],
      ]
    );
    $this->end_controls_section();

  }



  protected function render()
  {
      global $product;
      $product_id = get_the_ID();
      $product = wc_get_product($product_id); // Get product

      // Get widget settings
      $settings = $this->get_settings_for_display();
      $custom_button_text = $settings['title1kavehwoocommerceaddtocart'];
  
      // Check WooCommerce existence
      if (!class_exists('WooCommerce')) {
          return;
      }
  
      // Check Elementor editor mode
      if (\Elementor\Plugin::$instance->editor->is_edit_mode()) {
          // Query to get a sample product
          $args = array(
              'post_type' => 'product',
              'posts_per_page' => 1,
              'orderby' => 'date',
              'order' => 'DESC',
          );
  
          $product_query = new \WP_Query($args);
  
          if ($product_query->have_posts()) {
              $product_query->the_post(); // Set current product in loop
              $product = wc_get_product(get_the_ID()); // Get product
          } else {
              echo '<p>' . esc_html__('No product found.', 'kavehTheme') . '</p>';
              return;
          }
      }
  
      // Check product existence
      if (!$product) {
          echo '<p>' . esc_html__('No product found.', 'kavehTheme') . '</p>';
          return;
      }
  
      // Change add to cart button text
      add_filter('woocommerce_product_single_add_to_cart_text', function () use ($custom_button_text) {
          return $custom_button_text ? $custom_button_text : __('Add to Cart', 'kavehTheme');
      });


      if (\Elementor\Plugin::$instance->editor->is_edit_mode()):
        ?>
        <div class="woocommerce insingle_pr">
          <?php 
          // Display add to cart button
          woocommerce_template_single_add_to_cart();
          ?>
        </div>
        <?php
      else:
        ?>
        <div class="insingle_pr">
          <?php  
          woocommerce_template_single_add_to_cart();
          ?>
        </div>
        <?php

      endif;
  

  
      // Reset data
      if (\Elementor\Plugin::$instance->editor->is_edit_mode()) {
          wp_reset_postdata();
      }
  }
  

}


