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

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

class kavehwoocommerceattrexe extends \Elementor\Widget_Base
{

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

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

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

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

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

    $this->add_control(
      'stylekavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Box Style', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'one',
        'options' => [
        'one'  => esc_html__( 'Layout One', 'kavehTheme' ),
        'two'  => esc_html__( 'Layout Two', 'kavehTheme' ),
        'three'  => esc_html__( 'Layout Three', 'kavehTheme' ),
        ],
      ]
      );

    $this->add_control(
      'text_alignkavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Text Alignment', '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,
        'condition' => [
          'stylekavehwoocommerceattrexe!' => 'two', // Condition for disabling in the second layout
      ],
      ]
    );
    $this->add_control(
      'titletagkavehwoocommerceattrexe',
      [
        '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->add_control(
      'maxattributestodisplay_kavehwoocommerceattrexe',
      [
          'label' => __('Number of Attributes', 'kavehTheme'),
          'type' => \Elementor\Controls_Manager::NUMBER,
          'default' => 6,
      ]
  );

  $this->add_control(
    'columns_count_kavehwoocommerceattrexe',
    [
        'label' => __('Number of Columns', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => '2',
        'options' => [
            '1' => __('1 Column', 'kavehTheme'),
            '2' => __('2 Columns', 'kavehTheme'),
            '3' => __('3 Columns', 'kavehTheme'),
            '4' => __('4 Columns', 'kavehTheme'),
        ],
        'condition' => [
          'stylekavehwoocommerceattrexe' => 'three',
      ],
    ]
);


    $this->end_controls_section();
    $this->start_controls_section(
      'style_kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Title Style', 'kavehTheme' ),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
        'condition' => [
          'stylekavehwoocommerceattrexe' => 'one',
        ],
      ]
    );
    $this->add_control(
      'title1color1kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Title Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .detail-product .detail .options ul li' => 'color: {{VALUE}} !important',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'title1typekavehwoocommerceattrexe',
        'label' => esc_html__( 'Title Typography', 'kavehTheme' ),
        'selector' => '{{WRAPPER}} .detail-product .detail .options ul li',
      ]
    );

    $this->end_controls_section();
    $this->start_controls_section(
      'style2_kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Title Style', 'kavehTheme' ),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
        'condition' => [
          'stylekavehwoocommerceattrexe' => 'two',
        ],
      ]
    );
    $this->add_control(
      'title2color1kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Title Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .detail-product-six__info ul.list__detail li' => 'color: {{VALUE}} !important',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'title2typekavehwoocommerceattrexe',
        'label' => esc_html__( 'Title Typography', 'kavehTheme' ),
        'selector' => '{{WRAPPER}} .detail-product-six__info ul.list__detail li',
      ]
    );
    $this->add_control(
      'bg1color1kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Features Background Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
      ]
    );
    $this->add_control(
      'paddingbox1attr_kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Features Inner Spacing', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::DIMENSIONS,
        'size_units' => [ 'px' ],
        'selectors' => [
          '{{WRAPPER}} .detail-product-six__info' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
        ],
      ]
    );
    $this->add_control(
      'borderradius1attr_kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Features Background Rounded Corner', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::DIMENSIONS,
        'size_units' => [ 'px' ],
        'selectors' => [
          '{{WRAPPER}} .detail-product-six__info::before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
        ],
      ]
    );
    $this->add_control(
      'bg2color1kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Rings Background Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .detail-product-six__info ul.list__detail li::before' => 'border: 1px solid {{VALUE}} !important',
        ],
      ]
    );
		$this->add_control(
			'widthfor_kavehwoocommerceattrexe',
			[
			  'label' => esc_html__( 'Rings Width', 'kavehTheme' ),
			  'type' => \Elementor\Controls_Manager::SLIDER,
				'unit' => 'px',
			  'range' => [
				'px' => [
				  'min' => 1,
				  'max' => 20,
				],
			  ],
			  'selectors' => [
				'{{WRAPPER}} .detail-product-six__info ul.list__detail li::before' => 'width: {{SIZE}}{{UNIT}};',
			  ],
			]
		  );
      $this->add_control(
        'heightfor_kavehwoocommerceattrexe',
        [
          'label' => esc_html__( 'Rings Height', 'kavehTheme' ),
          'type' => \Elementor\Controls_Manager::SLIDER,
          'unit' => 'px',
          'range' => [
          'px' => [
            'min' => 1,
            'max' => 20,
          ],
          ],
          'selectors' => [
          '{{WRAPPER}} .detail-product-six__info ul.list__detail li::before' => 'height: {{SIZE}}{{UNIT}};',
          ],
        ]
        );
    $this->add_control(
      'borderradius2attr_kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Rings Background Rounded Corner', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::DIMENSIONS,
        'size_units' => [ 'px' ],
        'selectors' => [
          '{{WRAPPER}} .detail-product-six__info ul.list__detail li::before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
        ],
      ]
    );

    $this->end_controls_section();
    $this->start_controls_section(
      'style3_kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Title Style', 'kavehTheme' ),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
        'condition' => [
          'stylekavehwoocommerceattrexe' => 'three',
        ],
      ]
    );

    $this->add_control(
      'title3color1kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Top Title Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .detail-product-five__options ul li > div .title' => 'color: {{VALUE}} !important',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'title3typekavehwoocommerceattrexe',
        'label' => esc_html__( 'Top Title Typography', 'kavehTheme' ),
        'selector' => '{{WRAPPER}} .detail-product-five__options ul li > div .title',
      ]
    );
    $this->add_control(
      'title4color1kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Main Title Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .detail-product-five__options ul li > div .sub-title' => 'color: {{VALUE}} !important',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'title4typekavehwoocommerceattrexe',
        'label' => esc_html__( 'Main Title Typography', 'kavehTheme' ),
        'selector' => '{{WRAPPER}} .detail-product-five__options ul li > div .sub-title',
      ]
    );
    $this->add_control(
      'bg2color2kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Boxes Background Color', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .detail-product-five__options ul li > div' => 'background-color: {{VALUE}} !important',
        ],
      ]
    );
    $this->add_control(
      'paddingbox3box_kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Features Inner Spacing', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::DIMENSIONS,
        'size_units' => [ 'px' ],
        'selectors' => [
          '{{WRAPPER}} .detail-product-five__options ul li > div' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
        ],
      ]
    );
    $this->add_control(
      'borderradius3box_kavehwoocommerceattrexe',
      [
        'label' => esc_html__( 'Features Background Rounded Corner', 'kavehTheme' ),
        'type' => \Elementor\Controls_Manager::DIMENSIONS,
        'size_units' => [ 'px' ],
        'selectors' => [
          '{{WRAPPER}} .detail-product-five__options ul li > div' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Box_Shadow::get_type(),
      [
        'name' => 'box_shadow_kavehwoocommerceattrexe',
        'label' => esc_html__( 'Button Shadow', 'kavehTheme' ),
        'selector' => '{{WRAPPER}} .detail-product-five__options ul li > div',
      ]
    );
    $this->end_controls_section();


  }
  



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

  $max_attributes_to_display = $settings['maxattributestodisplay_kavehwoocommerceattrexe']; // Desired number of product features
  $text_align = $settings['text_alignkavehwoocommerceattrexe'];



  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]; 
      
        if(!empty($product_id)){
        $product    = wc_get_product( $product_id );
        $i = 0;
              $product_attributes = $product->get_attributes();
              if(!empty($product_attributes)){
                if($settings['stylekavehwoocommerceattrexe']==='one'){ ?>
        <div class="detail-product <?php echo esc_attr($text_align); ?>">
          <div class="detail">
            <div class="options">
              <ul class="d-flex align-items-center flex-wrap">
                <?php
                        // Loop through each attribute
                        foreach ($product_attributes as $attribute){
                        $attribute_slug = $attribute->get_name();
                        if ($i < $max_attributes_to_display) { // Use dynamic variable
                ?>

                    <li> <?php echo wc_attribute_label( $attribute_slug ) ?> : <?php echo $product->get_attribute( $attribute_slug ) ?> </li>
                  <?php 
                    $i++;
                       }
                     }      
                  ?>
              </ul>
            </div>
          </div>
        </div>
            <?php    
                }elseif($settings['stylekavehwoocommerceattrexe']==='two'){?>
                  <div class="detail-product-six__info <?php echo esc_attr($text_align); ?>">
                      <ul class="list__detail d-flex flex-column">
                      <?php
                      // Loop through each attribute
                      foreach ($product_attributes as $attribute){
                      $attribute_slug = $attribute->get_name();
                      if ($i < $max_attributes_to_display) { // Use dynamic variable
              ?>
              <li class="d-flex align-items-start"> <?php echo wc_attribute_label( $attribute_slug ) ?> : <?php echo $product->get_attribute( $attribute_slug ) ?> </li>
          <?php 
          $i++;
                  }
                }      
                ?>
                     </ul>
               </div>
        <?php
}elseif($settings['stylekavehwoocommerceattrexe'] === 'three'){ ?>
  <div class="detail-product-five__options <?php echo esc_attr($text_align); ?>">
      <ul class="d-flex flex-lg-wrap columns-<?php echo esc_attr($settings['columns_count_kavehwoocommerceattrexe']); ?>">
      <?php
      // Loop through each attribute
      foreach ($product_attributes as $attribute) {
          $attribute_slug = $attribute->get_name();
          if ($i < $max_attributes_to_display) { // Use dynamic variable
              ?>
              <li class="w-100">
                  <div>
                      <div class="title"><?php echo wc_attribute_label($attribute_slug); ?></div>
                      <div class="sub-title"><?php echo $product->get_attribute($attribute_slug); ?></div>
                  </div>
              </li>
              <?php
              $i++;
          }
      }
      ?>
      </ul>
  </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 );

    $i = 0;
          $product_attributes = $product->get_attributes();
          if(!empty($product_attributes)){
            if($settings['stylekavehwoocommerceattrexe']==='one'){
              ?>
      <div class="detail-product <?php echo esc_attr($text_align); ?>">
        <div class="detail">
          <div class="options">
            <ul class="d-flex align-items-center flex-wrap">
            <?php
                    // Loop through each attribute
                    foreach ($product_attributes as $attribute){
                    $attribute_slug = $attribute->get_name();
                    if ($i < $max_attributes_to_display) { // Use dynamic variable
            ?>

            <li> <?php echo wc_attribute_label( $attribute_slug ) ?> : <?php echo $product->get_attribute( $attribute_slug ) ?> </li>
            <?php 
            $i++;
                }
              }      
              ?>
            </ul>
          </div>
        </div>
      </div>
          <?php    
              }elseif($settings['stylekavehwoocommerceattrexe']==='two'){?>
                <div class="detail-product-six__info <?php echo esc_attr($text_align); ?>">
                    <ul class="list__detail d-flex flex-column">
                    <?php
                    // Loop through each attribute
                    foreach ($product_attributes as $attribute){
                    $attribute_slug = $attribute->get_name();
                    if ($i < $max_attributes_to_display) { // Use dynamic variable
            ?>
            <li class="d-flex align-items-start"> <?php echo wc_attribute_label( $attribute_slug ) ?> : <?php echo $product->get_attribute( $attribute_slug ) ?> </li>
        <?php 
        $i++;
                }
              }      
              ?>
                   </ul>
             </div>
      <?php
}elseif($settings['stylekavehwoocommerceattrexe'] === 'three'){ ?>
<div class="detail-product-five__options <?php echo esc_attr($text_align); ?>">
    <ul class="d-flex flex-lg-wrap columns-<?php echo esc_attr($settings['columns_count_kavehwoocommerceattrexe']); ?>">
    <?php
    // Loop through each attribute
    foreach ($product_attributes as $attribute) {
        $attribute_slug = $attribute->get_name();
        if ($i < $max_attributes_to_display) { // Use dynamic variable
            ?>
            <li class="w-100">
                <div>
                    <div class="title"><?php echo wc_attribute_label($attribute_slug); ?></div>
                    <div class="sub-title"><?php echo $product->get_attribute($attribute_slug); ?></div>
                </div>
            </li>
            <?php
            $i++;
        }
    }
    ?>
    </ul>
</div>

      <?php
              }
          }

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


  }
?>
  <style>
    .detail-product .detail .options{
    padding-right: 0;
    }
    .product .detail {
    padding: 0;
    }
    .product {
    background-color: unset;
    -webkit-box-shadow: unset;
    -moz-box-shadow: 0 10px 50px rgba(0, 0, 0, .1);
    box-shadow: unset;
    }
    .detail-product-six__info::before {
    content: "";
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 174px;
    border-radius: 50px;
    background-image: linear-gradient(269.35deg, <?php echo esc_html($settings['bg1color1kavehwoocommerceattrexe']); ?> 5.12%, rgba(248, 249, 251, 0) 49.03%);
    z-index: -1;
    }
  </style>
<?php

  }

}


