<?php



use Elementor\Widget_Base;
use Elementor\Controls_Manager;

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


class Productsvertistkaveh extends Widget_Base
{

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

  public function get_title()
  {
    return esc_html__('Vertical Products', 'kavehTheme');
  }

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

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

  protected function register_controls()
  {

    $this->start_controls_section(
      'contentproductsvertistkaveh',
      [
        'label' => esc_html__('Title Settings', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
      ]
    );

    $this->add_control(
      'titleproductsvertistkaveh',
      [
        'label' => esc_html__('Title', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => esc_html__('Best Selling Products', 'kavehTheme'),

      ]
    );
    $this->add_control(
      'tagheadingproductsvertistkaveh',
      [
        '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(
      'content2productsvertistkaveh',
      [
        'label' => esc_html__('Product Settings', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
      ]
    );

    $this->add_control(
      'pproductsvertistkaveh',
      [
        '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(
      'psortproductsvertistkaveh',
      [
        '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__('Sales Amount', 'kavehTheme'),

        ],
      ]
    );
    $this->add_control(
      'psortiproductsvertistkaveh',
      [
        'label' => esc_html__('Sort Type', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'desc',
        'options' => [
          'asc' => esc_html__('Ascending', 'kavehTheme'),
          'desc' => esc_html__('Descending', 'kavehTheme'),
        ],
      ]
    );
    $options2 = 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) {
      $options2[$category->term_id] = $category->name;
    }
    $this->add_control(
      'catproductsvertistkaveh',
      [
        'label' => esc_html__('Select Category', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT2,
        'multiple' => true,
        'options' => $options2,
        'condition' => [
          'pproductsvertistkaveh' => 'cat',
        ],
      ]
    );
    $this->end_controls_section();
    $this->start_controls_section(
      'content3productsvertistkaveh',
      [
        'label' => esc_html__('Display Settings', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
      ]
    );
    $this->add_control(
      'productnumberproductsvertistkaveh',
      [
        'label' => esc_html__('Number of Products Displayed', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::NUMBER,
        'min' => 0,
        'max' => 50,
        'step' => 1,
        'default' => 5,
      ]
    );

    $this->end_controls_section();
    $this->start_controls_section(
      'style_productsvertistkaveh',
      [
        'label' => esc_html__('Title Style', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
      ]
    );
    $this->add_control(
      'textcolor2productsvertistkaveh',
      [
        'label' => esc_html__('Title Color', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .top-products-seven-box-title' => 'color: {{VALUE}}',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'titleproductsvertistkaveh',
        'label' => esc_html__('Title Typography', 'kavehTheme'),
        'selector' => '{{WRAPPER}} .top-products-seven-box-title',
      ]
    );
    $this->end_controls_section();
    $this->start_controls_section(
      'style3_productsvertistkaveh',
      [
        'label' => esc_html__('General Style', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
      ]
    );
    $this->add_control(
      'bgcolorproductsvertistkaveh',
      [
        'label' => esc_html__('Background Color', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .top-products-seven-box' => 'background-color: {{VALUE}}',
        ],
      ]
    );
    $this->add_control(
      'boxradiusproductsvertistkaveh',
      [
        'label' => esc_html__('Box Border Radius', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::DIMENSIONS,
        'size_units' => ['px'],
        'selectors' => [
          '{{WRAPPER}} .top-products-seven-box' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Box_Shadow::get_type(),
      [
        'name' => 'box_shadownproductsvertistkaveh',
        'label' => esc_html__('Box Shadow', 'kavehTheme'),
        'selector' => '{{WRAPPER}} .top-products-seven-box',
      ]
    );
    $this->add_control(
      'scrolbartabboxes1productsvertistkaveh',
      [
        'label' => esc_html__('Scrollbar Background Color', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .top-products-seven-box ul::-webkit-scrollbar' => 'background-color: {{VALUE}}',
        ],
      ]
    );
    $this->add_control(
      'scrolbarthumbtabboxes1hrproductsvertistkaveh',
      [
        'label' => esc_html__('Scrollbar Color', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .top-products-seven-box ul::-webkit-scrollbar-thumb' => 'background-color: {{VALUE}}',
        ],
      ]
    );
    $this->end_controls_section();
  }


  protected function render()
  {
    $settings = $this->get_settings_for_display();
    $clsid = $this->get_id();
    ?>
    <?php $woo_currency = get_woocommerce_currency_symbol(); ?>

    <div class="top-products-seven-box position-relative">
      <<?php echo esc_attr($settings['tagheadingproductsvertistkaveh']); ?> class="top-products-seven-box-title text-center
        mb-4"><?php echo esc_html($settings['titleproductsvertistkaveh']); ?></<?php echo esc_attr($settings['tagheadingproductsvertistkaveh']); ?>>
      <ul class="position-relative">

        <?php if ('cat' === $settings['pproductsvertistkaveh']) { ?>
          <?php
          // Define your custom query arguments
          $query_args = array(
            'post_type' => array('product'),
'post_status' => array('publish'),
            'posts_per_page' => $settings['productnumberproductsvertistkaveh'],
            'orderby' => $settings['psortproductsvertistkaveh'],
            'order' => $settings['psortiproductsvertistkaveh'],
            'tax_query' => array(
              array(
                'taxonomy' => 'product_cat',
                'field' => 'term_id',
                'terms' => $settings['catproductsvertistkaveh'],
              ),
            ),
          );
          // 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();
              $post_id = get_the_ID();
              $product = wc_get_product($post_id);
              ?>
              <li class="top-products-seven-item position-relative d-flex align-items-center">
                <a href="<?php the_permalink(); ?>" class="stretched-link" aria-label="<?php the_title(); ?>"></a>
                <img src="<?php if (get_the_post_thumbnail_url()): ?>
                <?php the_post_thumbnail_url('full'); ?>
                <?php else: ?>
                <?php echo wc_placeholder_img_src(); ?>
                <?php endif; ?>" alt="<?php the_title(); ?>" width="90" height="88" class="d-block">
                <div class="detail">
                  <h2 class="title mb-1 text-truncate"><?php the_title(); ?></h2>
                  <div class="price">
                    <div>
                      <?php
                      if ($product->is_type('variable')) {
                        echo $product->get_variation_regular_price('min');
                      } else {
                        echo $product->get_regular_price();
                      }
                      ?>
                      <?php echo $woo_currency; ?>
                    </div>
                  </div>
                  <?php if ($product->get_stock_status() === 'outofstock'): ?>
                    <div class="status">
                      Out of Stock
                    </div>
                  <?php endif; ?>
                </div>
              </li>
              <?php
            endwhile;
            wp_reset_postdata();
          else:
            echo 'No posts found.';
          endif;
          ?>


        <?php } elseif ('all' === $settings['pproductsvertistkaveh']) {
          // Define your custom query arguments
          $query_args = array(
            'post_type' => array('product'),
'post_status' => array('publish'),
            'posts_per_page' => $settings['productnumberproductsvertistkaveh'],
            'orderby' => $settings['psortproductsvertistkaveh'],
            'order' => $settings['psortiproductsvertistkaveh'],
          );
          // 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();
              $post_id = get_the_ID();
              $product = wc_get_product($post_id);
              ?>
              <li class="top-products-seven-item position-relative d-flex align-items-center">
                <a href="<?php the_permalink(); ?>" class="stretched-link" aria-label="<?php the_title(); ?>"></a>
                <img src="<?php if (get_the_post_thumbnail_url()): ?>
                <?php the_post_thumbnail_url('full'); ?>
                <?php else: ?>
                <?php echo wc_placeholder_img_src(); ?>
                <?php endif; ?>" alt="<?php the_title(); ?>" width="90" height="88" class="d-block">
                <div class="detail">
                  <h2 class="title mb-1 text-truncate"><?php the_title(); ?></h2>
                  <div class="price">
                    <div>
                      <?php
                      if ($product->is_type('variable')) {
                        echo $product->get_variation_regular_price('min');
                      } else {
                        echo $product->get_regular_price();
                      }
                      ?>
                      <?php echo $woo_currency; ?>
                    </div>
                  </div>
                  <?php if ($product->get_stock_status() === 'outofstock'): ?>
                    <div class="status">
                      Out of Stock
                    </div>
                  <?php endif; ?>
                </div>
              </li>
              <?php
            endwhile;
            wp_reset_postdata();
          else:
            echo 'No posts found.';
          endif;
          ?>
        <?php } ?>

      </ul>
    </div>
    <style>
      @media only screen and (max-width: 575px) {
        .top-products-seven-item img {
          width: 90px !important;
        }
      }
    </style>

    <?php
  }


}
