<?php



use Elementor\Widget_Base;
use Elementor\Controls_Manager;

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


class Productstabgrid2ndstkaveh extends Widget_Base
{

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

  public function get_title()
  {
    return esc_html__('Products Tab Grid Layout Two', 'kavehTheme');
  }

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

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

  protected function register_controls()
  {

    $this->start_controls_section(
      'contentproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Element Content', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
      ]
    );
    $repeater = new \Elementor\Repeater();
    $repeater->add_control(
      'tabtitle1productstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Tab Title', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::TEXT,
      ]
    );
    $repeater->add_control(
      'pproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Display Products', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'all',
        'options' => [
          'all' => esc_html__('All', 'kavehTheme'),
'cat' => esc_html__('Category', 'kavehTheme'),

        ],
      ]
    );
    $this->add_control(
      'kaveh_exclude_out_of_stock',
      [
        'label' => esc_html__('Hide Out of Stock Products', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'yes' => esc_html__('Yes', 'kavehTheme'),
'no' => esc_html__('No', 'kavehTheme'),
        'return_value' => 'yes',
        'default' => 'yes',
      ]
    );
    $this->add_control(
      'kaveh_only_sales',
      [
        'label' => esc_html__('Show Only Discounted Products', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'yes' => esc_html__('Yes', 'kavehTheme'),
'no' => esc_html__('No', 'kavehTheme'),
        'return_value' => 'yes',
        'default' => 'no',
      ]
    );
    $repeater->add_control(
      'psortproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Sort Products By', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'date',
        'options' => [
          'date' => esc_html__('Date', 'kavehTheme'),
'_price' => esc_html__('Price', 'kavehTheme'),
'_wc_average_rating' => esc_html__('Most Popular', 'kavehTheme'),
'total_sales' => esc_html__('Best Selling', 'kavehTheme'),

        ],
      ]
    );
    $repeater->add_control(
      'psortiproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Sort Type', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'desc',
        'options' => [
          'asc' => esc_html__('Ascending', 'kavehTheme'),
          'desc' => esc_html__('Descending', 'kavehTheme'),
        ],
      ]
    );
    $taboptiongrid = array();
    $orderby = 'name';
    $order = 'asc';
    $hide_empty = false;
    $cat_args = array(
      'orderby' => $orderby,
      'order' => $order,
      'hide_empty' => $hide_empty,
    );
    $product_categories = get_terms('product_cat', $cat_args);

    foreach ($product_categories as $key => $category) {
      $taboptiongrid[$category->term_id] = $category->name;
    }
    $repeater->add_control(
      'cat2productstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Select Category', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT2,
        'multiple' => true,
        'options' => $taboptiongrid,
        'condition' => [
          'pproductstabgrid2ndstkaveh' => 'cat',
        ],
      ]
    );
    $repeater->add_control(
      'productnumberproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Number of Products', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::NUMBER,
        'min' => 0,
        'max' => 100,
        'step' => 1,
        'default' => 8,
      ]
    );
    $this->add_control(
      'listproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Tabs', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::REPEATER,
        'fields' => $repeater->get_controls(),
        'default' => [
          [
            'tabtitle1productstabgrid2ndstkaveh' => esc_html__('Category', 'kavehTheme'),
          ],
        ],
      ]
    );

    $this->add_control(
      'styleproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Product Box Layout', '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' ),
          'four'  => esc_html__( 'Layout Four', 'kavehTheme' ),
          'five'  => esc_html__( 'Layout Five', 'kavehTheme' ),
          'six'  => esc_html__( 'Layout Six', 'kavehTheme' ),
          'seven'  => esc_html__( 'Layout Seven', 'kavehTheme' ),
          'eight'  => esc_html__( 'Layout Eight', 'kavehTheme' ),
          'nine'  => esc_html__( 'Layout Nine', 'kavehTheme' ),
          'ten'  => esc_html__( 'Layout Ten', 'kavehTheme' ),
          'eleven'  => esc_html__( 'Layout Eleven', 'kavehTheme' ),
          'twelve'  => esc_html__( 'Layout Twelve', 'kavehTheme' ),
          'thirteen'  => esc_html__( 'Layout Thirteen', 'kavehTheme' ),
          'fourteen'  => esc_html__( 'Layout Fourteen', 'kavehTheme' ),
          'fifteen'  => esc_html__( 'Layout Fifteen', 'kavehTheme' ),
          'sixteen'  => esc_html__( 'Layout Sixteen', 'kavehTheme' ),
          'seventeen'  => esc_html__( 'Layout Seventeen', 'kavehTheme' ),
          'eighteen'  => esc_html__( 'Layout Eighteen', 'kavehTheme' ),
          'nineteen'  => esc_html__( 'Layout Nineteen', 'kavehTheme' ),
          'twenty'  => esc_html__( 'Layout Twenty', 'kavehTheme' ),
          'twenty-one'  => esc_html__( 'Layout Twenty-One', 'kavehTheme' ),
          'twenty-two'  => esc_html__( 'Layout Twenty-Two', 'kavehTheme' ),
          'twenty-three'  => esc_html__( 'Layout Twenty-Three', 'kavehTheme' ),
          'twenty-four'  => esc_html__( 'Layout Twenty-Four', 'kavehTheme' ),
          'twenty-five'  => esc_html__( 'Layout Twenty-Five', 'kavehTheme' ),
          'twenty-six'  => esc_html__( 'Layout Twenty-Six', 'kavehTheme' ),
          'twenty-seven'  => esc_html__( 'Layout Twenty-Seven', 'kavehTheme' ),
          'twenty-eight'  => esc_html__( 'Layout Twenty-Eight', 'kavehTheme' ),
        ],
      ]
    );

    $this->end_controls_section();
    $this->start_controls_section(
      'style22productstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Style', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
      ]
    );
    $this->add_control(
      'buttontextcolorproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Text Color', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .top-seller-products-tabs li' => 'color: {{VALUE}}',

        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'buttonst3_productstabgrid2ndstkaveh',
        'label' => esc_html__('Text Typography', 'kavehTheme'),
        'selector' => '{{WRAPPER}} .top-seller-products-tabs li',
      ]
    );
    $this->add_control(
      'buttoncoloractivest3productstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Active Tab Background Color', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .top-seller-products-tabs li.active' => 'background-color: {{VALUE}}',
          '{{WRAPPER}}  .top-seller-products-tabs li:hover' => 'background-color: {{VALUE}}',

        ],
      ]
    );
    $this->add_control(
      'buttontextactivecolorproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Active Text Color', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .top-seller-products-tabs li.active' => 'color: {{VALUE}}',
          '{{WRAPPER}} .top-seller-products-tabs li:hover' => 'color: {{VALUE}}',


        ],
      ]
    );
    $this->add_control(
      'buttonpaddingproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Padding', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::DIMENSIONS,
        'size_units' => ['px'],
        'selectors' => [
          '{{WRAPPER}} .top-seller-products-tabs li' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
        ],
      ]
    );
    $this->add_control(
      'buttonradius1productstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Border Radius', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::DIMENSIONS,
        'size_units' => ['px'],
        'selectors' => [
          '{{WRAPPER}} .top-seller-products-tabs li' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
        ],
      ]
    );

    $this->add_control(
      'hr1productstabgrid2ndstkaveh',
      [
        'type' => \Elementor\Controls_Manager::DIVIDER,
      ]
    );
    $this->add_control(
      'bgnavproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Top Bar Background Color', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .top-seller-products-tabs' => 'background-color: {{VALUE}}',
        ],
      ]
    );
    $this->add_control(
      'bgnavaddingproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Top Bar Padding', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::DIMENSIONS,
        'size_units' => ['px'],
        'selectors' => [
          '{{WRAPPER}} .top-seller-products-tabs' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
        ],
      ]
    );
    $this->add_control(
      'bgnavradius1productstabgrid2ndstkaveh',
      [
        'label' => esc_html__('Top Bar Border Radius', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::DIMENSIONS,
        'size_units' => ['px'],
        'selectors' => [
          '{{WRAPPER}} .top-seller-products-tabs' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
        ],
      ]
    );
    $this->end_controls_section();
  }


  protected function render()
  {
    $settings = $this->get_settings_for_display();
    $clsid = $this->get_id();
    ?>
    <section class="top-seller-products">
      <div class="container">
        <!-- Start Tabs -->
        <ul class="top-seller-products-tabs nav nav-pills justify-content-center flex-nowrap">
          <?php
          $q = 1;
          if ($settings['listproductstabgrid2ndstkaveh']) {
            foreach ($settings['listproductstabgrid2ndstkaveh'] as $item) {
              ?>
              <li data-bs-toggle="tab" data-bs-target="#top-seller-products-<?php echo $q; ?>" class="<?php if (1 === $q) { ?>active <?php } else {
                 } ?>">
                <?php echo esc_html($item['tabtitle1productstabgrid2ndstkaveh']); ?>
              </li>
              <?php
              $q++;
            }
          }
          ?>
        </ul>
        <!-- End Tabs -->
        <!-- Start Tab Content -->
        <div class="tab-content">
          <?php
          $s = 1;
          if ($settings['listproductstabgrid2ndstkaveh']) {
            foreach ($settings['listproductstabgrid2ndstkaveh'] as $item) {
              ?>
              <div id="top-seller-products-<?php echo $s; ?>" class="tab-pane fade <?php if (1 == $s) { ?>active show<?php } else {
                 } ?>">
                <div class="row row-mobile">
                  <?php
                  if ('cat' === $item['pproductstabgrid2ndstkaveh']) {
                    $query_args = array(
                      'post_type' => array('product'),
'post_status' => array('publish'),
                      'posts_per_page' => $item['productnumberproductstabgrid2ndstkaveh'],
                      'orderby' => $item['psortproductstabgrid2ndstkaveh'] === 'date' ? 'date' : 'meta_value_num',
                      'order' => $item['psortiproductstabgrid2ndstkaveh'],
                      'tax_query' => array(
                          'relation' => 'AND',
                          array(
                            'taxonomy' => 'product_cat',
                            'field' => 'term_id',
                            'terms' => $item['cat2productstabgrid2ndstkaveh'],
                          ),
                      ),
                      'meta_query' => array(),  // Initialize meta_query to avoid undefined index error
                  );
                  
                  // Handle specific orderby values
                  switch ($item['psortproductstabgrid2ndstkaveh']) {
                      case '_price':
                          $query_args['meta_key'] = '_price';
                          $query_args['orderby'] = 'meta_value_num';
                          break;
                  
                      case '_wc_average_rating':
                          $query_args['meta_key'] = '_wc_average_rating';
                          $query_args['orderby'] = 'meta_value_num';
                          break;
                  
                      case 'total_sales':
                          $query_args['meta_key'] = 'total_sales';
                          $query_args['orderby'] = 'meta_value_num';
                          break;
                  }
                  
                  if ("yes" === $settings['kaveh_exclude_out_of_stock']) {
                      $query_args['tax_query'][] = array(
                          'taxonomy' => 'product_visibility',
                          'field' => 'name',
                          'terms' => array('outofstock'),
                          'operator' => 'NOT IN'
                      );
                  }
                  
                  if ("yes" === $settings['kaveh_only_sales']) {
                      $query_args['meta_query'][] = WC()->query->get_meta_query();  // Ensure meta_query is an array
                      $query_args['post__in'] = array_merge(array(0), wc_get_product_ids_on_sale());
                  }

                // Get the cached query results
                $cached_query = KavehCustomQuery::get_cached_query($query_args);
                if ($cached_query->have_posts()):
                  while ($cached_query->have_posts()):
                    $cached_query->the_post();
                    ?>
                        <div class="col-6 col-md-4 col-lg-3">
                          <?php $styleproduct21 = $settings['styleproductstabgrid2ndstkaveh']; ?>
                          <?php get_template_part("template-part/product-cart-$styleproduct21"); ?>
                        </div>
                        <?php
                  endwhile;
                  wp_reset_postdata();
                else:
                  echo 'No posts found.';
                endif;
                ?>
                    <?php
                    } else {
                      $query_args = array(
                        'post_type' => array('product'),
'post_status' => array('publish'),
                        'posts_per_page' => $item['productnumberproductstabgrid2ndstkaveh'],
                        'orderby' => $item['psortproductstabgrid2ndstkaveh'] === 'date' ? 'date' : 'meta_value_num',
                        'order' => $item['psortiproductstabgrid2ndstkaveh'],
                        'tax_query' => array(
                            'relation' => 'AND',
                        ),
                        'meta_query' => array(),  // Initialize meta_query to avoid undefined index error
                    );
                    
                    // Handle specific orderby values
                    switch ($item['psortproductstabgrid2ndstkaveh']) {
                        case '_price':
                            $query_args['meta_key'] = '_price';
                            $query_args['orderby'] = 'meta_value_num';
                            break;
                    
                        case '_wc_average_rating':
                            $query_args['meta_key'] = '_wc_average_rating';
                            $query_args['orderby'] = 'meta_value_num';
                            break;
                    
                        case 'total_sales':
                            $query_args['meta_key'] = 'total_sales';
                            $query_args['orderby'] = 'meta_value_num';
                            break;
                    }
                    
                    if ("yes" === $settings['kaveh_exclude_out_of_stock']) {
                        $query_args['tax_query'][] = array(
                            'taxonomy' => 'product_visibility',
                            'field' => 'name',
                            'terms' => array('outofstock'),
                            'operator' => 'NOT IN'
                        );
                    }
                    
                    if ("yes" === $settings['kaveh_only_sales']) {
                        $query_args['meta_query'][] = WC()->query->get_meta_query();  // Ensure meta_query is an array
                        $query_args['post__in'] = array_merge(array(0), wc_get_product_ids_on_sale());
                    }

                // Get the cached query results
                $cached_query = KavehCustomQuery::get_cached_query($query_args);
                if ($cached_query->have_posts()):
                  while ($cached_query->have_posts()):
                    $cached_query->the_post();
                    ?>
                        <div class="col-6 col-md-4 col-lg-3">
                          <?php $styleproduct21 = $settings['styleproductstabgrid2ndstkaveh']; ?>
                          <?php get_template_part("template-part/product-cart-$styleproduct21"); ?>
                        </div>
                        <?php
                  endwhile;
                  wp_reset_postdata();
                else:
                  echo 'No posts found.';
                endif;
                ?>
                    <?php
                  }
                  ?>
                </div>
              </div>
              <?php
              $s++;
            }
          }

          ?>
        </div>
        <!-- End Tab Content -->
      </div>
    </section>
  <?php
  }
}
