<?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__('تب محصولات شبکه ای طرح بندی دو', 'kaveh-core');
  }

  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__('محتوای المان', 'kaveh-core'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
      ]
    );
    $repeater = new \Elementor\Repeater();
    $repeater->add_control(
      'tabtitle1productstabgrid2ndstkaveh',
      [
        'label' => esc_html__('عنوان تب', 'kaveh-core'),
        'type' => \Elementor\Controls_Manager::TEXT,
      ]
    );
    $repeater->add_control(
      'pproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('نمایش محصولات', 'kaveh-core'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'all',
        'options' => [
          'all' => esc_html__('همه', 'kaveh-core'),
          'cat' => esc_html__('دسته بندی', 'kaveh-core'),

        ],
      ]
    );
    $repeater->add_control(
      'psortproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('مرتب سازی محصولات براساس', 'kaveh-core'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'date',
        'options' => [
          'date' => esc_html__('تاریخ', 'kaveh-core'),
          '_price' => esc_html__('قیمت', 'kaveh-core'),
          '_wc_average_rating' => esc_html__('محبوب ترین', 'kaveh-core'),
          'total_sales' => esc_html__('بیشترین فروش', 'kaveh-core'),

        ],
      ]
    );
    $repeater->add_control(
      'psortiproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('نوع مرتب سازی', 'kaveh-core'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'desc',
        'options' => [
          'asc' => esc_html__('صعودی', 'kaveh-core'),
          'desc' => esc_html__('نزولی', 'kaveh-core'),
        ],
      ]
    );
    $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__('انتخاب دسته بندی', 'kaveh-core'),
        'type' => \Elementor\Controls_Manager::SELECT2,
        'multiple' => false,
        'options' => $taboptiongrid,
        'condition' => [
          'pproductstabgrid2ndstkaveh' => 'cat',
        ],
      ]
    );
    $repeater->add_control(
      'productnumberproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('تعداد محصولات', 'kaveh-core'),
        'type' => \Elementor\Controls_Manager::NUMBER,
        'min' => 0,
        'max' => 20,
        'step' => 1,
        'default' => 8,
      ]
    );
    $this->add_control(
      'listproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('تب ها', 'kaveh-core'),
        'type' => \Elementor\Controls_Manager::REPEATER,
        'fields' => $repeater->get_controls(),
        'default' => [
          [
            'tabtitle1productstabgrid2ndstkaveh' => esc_html__('دسته بندی', 'kaveh-core'),
          ],
        ],
      ]
    );

    $this->add_control(
      'styleproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('طرح بندی باکس محصول', 'kaveh-core'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'one',
        'options' => [
          'one' => esc_html__('طرح بندی اول', 'kaveh-core'),
          'two' => esc_html__('طرح بندی دوم', 'kaveh-core'),
          'three' => esc_html__('طرح بندی سوم', 'kaveh-core'),
          'four' => esc_html__('طرح بندی چهارم', 'kaveh-core'),
          'five' => esc_html__('طرح بندی پنجم', 'kaveh-core'),
          'six' => esc_html__('طرح بندی ششم', 'kaveh-core'),
          'seven' => esc_html__('طرح بندی هفتم', 'kaveh-core'),
          'eight' => esc_html__('طرح بندی هشتم', 'kaveh-core'),
          'nine' => esc_html__('طرح بندی نهم', 'kaveh-core'),
          'ten' => esc_html__('طرح بندی دهم', 'kaveh-core'),
          'eleven' => esc_html__('طرح بندی یازدهم', 'kaveh-core'),
          'twelve' => esc_html__('طرح بندی دوازدهم', 'kaveh-core'),
          'thirteen' => esc_html__('طرح بندی سیزدهم', 'kaveh-core'),
          'fourteen' => esc_html__('طرح بندی چهاردهم', 'kaveh-core'),
          'fifteen' => esc_html__('طرح بندی پانزدهم', 'kaveh-core'),
          'sixteen' => esc_html__('طرح بندی شانزدهم', 'kaveh-core'),
        ],
      ]
    );

    $this->end_controls_section();
    $this->start_controls_section(
      'style22productstabgrid2ndstkaveh',
      [
        'label' => esc_html__('استایل', 'kaveh-core'),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
      ]
    );
    $this->add_control(
      'buttontextcolorproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('رنگ متن', 'kaveh-core'),
        '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__('تایپوگرافی متن', 'kaveh-core'),
        'selector' => '{{WRAPPER}} .top-seller-products-tabs li',
      ]
    );
    $this->add_control(
      'buttoncoloractivest3productstabgrid2ndstkaveh',
      [
        'label' => esc_html__('رنگ پس زمینه تب فعال', 'kaveh-core'),
        '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__('رنگ متن در حالت فعال', 'kaveh-core'),
        '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__('فاصله داخلی', 'kaveh-core'),
        '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__('گوشه خمیده', 'kaveh-core'),
        '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__('رنگ پس زمینه نوار بالا', 'kaveh-core'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .top-seller-products-tabs' => 'background-color: {{VALUE}}',
        ],
      ]
    );
    $this->add_control(
      'bgnavaddingproductstabgrid2ndstkaveh',
      [
        'label' => esc_html__('فاصله داخلی نوار بالا', 'kaveh-core'),
        '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__('گوشه خمیده نوار بالا', 'kaveh-core'),
        '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']) {
                    $kproducts = KavehRestRules::kaveh_get_rest_posts(
                      array(
                        'post_type' => 'product',
                        'per_page' => $item['productnumberproductstabgrid2ndstkaveh'],
                        'orderby' => $item['psortproductstabgrid2ndstkaveh'],
                        'order' => $item['psortiproductstabgrid2ndstkaveh'],
                        'product_cat' => $item['cat2productstabgrid2ndstkaveh'],
                      )
                    );
                    ?>
                    <?php if (is_array($kproducts)): ?>
                      <?php foreach ($kproducts as $kproduct): ?>
                        <div class="col-6 col-md-4 col-lg-3">
                          <?php set_query_var('kproduct', $kproduct); ?>
                          <?php $styleproduct21 = $settings['styleproductstabgrid2ndstkaveh']; ?>
                          <?php get_template_part("template-part-rest/product-cart-$styleproduct21"); ?>
                        </div>
                      <?php endforeach; ?>
                    <?php endif; ?>


                    <?php

                  } else {
                    $kproducts = KavehRestRules::kaveh_get_rest_posts(
                      array(
                        'post_type' => 'product',
                        'per_page' => $item['productnumberproductstabgrid2ndstkaveh'],
                        'orderby' => $item['psortproductstabgrid2ndstkaveh'],
                        'order' => $item['psortiproductstabgrid2ndstkaveh'],
                      )
                    );
                    ?>
                    <?php if (is_array($kproducts)): ?>
                      <?php foreach ($kproducts as $kproduct): ?>
                        <div class="col-6 col-md-4 col-lg-3">
                          <?php set_query_var('kproduct', $kproduct); ?>
                          <?php $styleproduct21 = $settings['styleproductstabgrid2ndstkaveh']; ?>
                          <?php get_template_part("template-part-rest/product-cart-$styleproduct21"); ?>
                        </div>
                      <?php endforeach; ?>
                    <?php endif; ?>
                    <?php
                  }

                  ?>
                </div>
              </div>
              <?php
              $s++;
            }
          }

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