<?php

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

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


class Productsrandomofferstkaveh extends Widget_Base{

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

  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(
      'content00productsrandomofferstkaveh',
      [
        'label' => esc_html__( 'تنظیمات محصولات تخفیف لحظه ای', 'kaveh-core' ),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
      ]
    );
      $this->add_control(
        'pproductslahzeproductsrandomofferstkaveh',
        [
          'label' => esc_html__( 'نمایش محصولات', 'kaveh-core' ),
          'type' => \Elementor\Controls_Manager::SELECT,
          'default' => 'all',
          'options' => [
            'all'  => esc_html__( 'همه', 'kaveh-core' ),
            'cat'  => esc_html__( 'دسته بندی', 'kaveh-core' ),
    
          ],
        ]
        );
        $this->add_control(
          'psortproductslahzeproductsrandomofferstkaveh',
          [
            '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' ),
      
            ],
          ]
          );
          $this->add_control(
            'psortiproductslahzeproductsrandomofferstkaveh',
            [
              'label' => esc_html__( 'نوع مرتب سازی', 'kaveh-core' ),
              'type' => \Elementor\Controls_Manager::SELECT,
              'default' => 'desc',
              'options' => [
                'asc'  => esc_html__( 'صعودی', 'kaveh-core' ),
                'desc'  => esc_html__( 'نزولی', 'kaveh-core' ),
              ],
            ]
            );
    $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(
      'catproductslahzeproductsrandomofferstkaveh',
      [
        'label' => esc_html__( 'انتخاب دسته بندی', 'kaveh-core' ),
        'type' => \Elementor\Controls_Manager::SELECT2, 
        'multiple' => true,
        'options' => $options2,
        'condition' => [
          'pproductslahzeproductsrandomofferstkaveh' => 'cat',
        ],
      ]
  );
  $this->add_control(
    'productnumberproductsproductsrandomofferstkaveh',
    [
      'label' => esc_html__( 'تعداد محصولات نمایش داده شده', 'kaveh-core' ),
      'type' => \Elementor\Controls_Manager::NUMBER,
      'min' => 0,
      'max' => 20,
      'step' => 1,
      'default' => 5,
    ]
  );
    $this->end_controls_section();
       $this->start_controls_section(
        'content3productsrandomofferstkaveh',
        [
          'label' => esc_html__( 'تنظیمات کاروسل', 'kaveh-core' ),
                  'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
        ]
      );
      $this->add_control(
        'productcolumnproductsrandomofferstkaveh',
        [
          'label' => esc_html__( 'تعداد ستون دسته بندی در دسکتاپ', 'kaveh-core' ),
          'type' => \Elementor\Controls_Manager::NUMBER,
          'min' => 0,
          'max' => 8,
          'step' => 1,
          'default' => 4,
      
        ]
      );
      $this->add_control(
        'productcolumn2productsrandomofferstkaveh',
        [
          'label' => esc_html__( 'تعداد ستون دسته بندی در تبلت', 'kaveh-core' ),
          'type' => \Elementor\Controls_Manager::NUMBER,
          'min' => 0,
          'max' => 5,
          'step' => 1,
          'default' => 3,
      
        ]
      );
      $this->add_control(
        'productcolumn3productsrandomofferstkaveh',
        [
          'label' => esc_html__( 'تعداد ستون دسته بندی در موبایل', 'kaveh-core' ),
          'type' => \Elementor\Controls_Manager::NUMBER,
          'min' => 0,
          'max' => 5,
          'step' => .5,
          'default' => 1.5,
      
        ]
      );
       $this->end_controls_section();
 

        }
 

  protected function render(){
    $settings = $this->get_settings_for_display();
    $clsid = $this->get_id();
    ?>
          <div class="rand-offer">
            <!-- Start Carousel -->
            <div class="swiper swiper-discount-product-special position-relative">
              <div class="swiper-wrapper">
              <?php if('cat'===$settings['pproductslahzeproductsrandomofferstkaveh']){ ?>
              <?php 
                $kproducts = KavehRestRules::kaveh_get_rest_posts(array(
                  'post_type' => 'product',
                  'per_page' => $settings['productnumberproductsproductsrandomofferstkaveh'],
                  'orderby' => $settings['psortproductslahzeproductsrandomofferstkaveh'],
                  'order' => $settings['psortiproductslahzeproductsrandomofferstkaveh'],
                  'product_cat' => $settings['catproductslahzeproductsrandomofferstkaveh'],
              ));
              ?>
                                           <?php if(is_array($kproducts)): ?>
                  <?php foreach($kproducts as $kproduct): ?>
                <!-- Start Slide -->
                <div class="swiper-slide">
                  <div class="product-eight product-eight-offer">
                    <div class="image">
                      <a href="<?php echo $kproduct['link'] ?>">
                        <img src="<?php echo $kproduct['kaveh_custom']['thumbnail_url'] ?>" alt="<?php echo $kproduct['title']['rendered']; ?>" width="266" height="266">
                      </a>
                      <?php if($kproduct['kaveh_custom']['product_stock_status'] !== 'outofstock'): 
                      if($kproduct['kaveh_custom']['is_on_sale'] === true) : ?>
                      <div class="offer">
                      <?php if($kproduct['kaveh_custom']['product_type'] === 'variable'): ?>
                          <?php echo (int)(( ( (float)$kproduct['kaveh_custom']['min_regular_price'] - (float)$kproduct['kaveh_custom']['min_sale_price'] ) / (float)$kproduct['kaveh_custom']['min_regular_price'] ) * 100); ?><?php echo ' '.'%'; ?>
                      <?php elseif($kproduct['kaveh_custom']['product_type'] === 'simple'): ?>
                          <?php echo (int)(( ( (float)$kproduct['kaveh_custom']['normal_price'] - (float)$kproduct['kaveh_custom']['sale_price'] ) / (float)$kproduct['kaveh_custom']['normal_price'] ) * 100); ?><?php echo ' '.'%'; ?>
                      <?php endif; ?>
                      </div>
                      <?php endif; ?>
                      <?php endif; ?>
                    </div>
                    <div class="content">
                      <ul class="tags d-flex align-items-center flex-wrap">
                      </ul>
                      <h2 class="product-name">
                        <a href="<?php echo $kproduct['link'] ?>"> <?php echo $kproduct['title']['rendered']; ?></a>
                      </h2>
                      <div class="d-flex align-items-center justify-content-between">
                        <?php if ( $kproduct['kaveh_custom']['product_type'] !== 'variable' ) : ?>
                    <?php if(!empty($sale_price) ) { ?>
                    <div class="price">
                      <del> <?php if(!empty($kproduct['kaveh_custom']['normal_price'])): ?>
                      <?php echo number_format($kproduct['kaveh_custom']['normal_price'], 0, '.', ','); ?>
                      <?php endif; ?></del>
                      <span> <?php if(!empty($kproduct['kaveh_custom']['sale_price'])): ?>
                          <?php echo number_format($kproduct['kaveh_custom']['sale_price'], 0, '.', ','); ?>
                      <?php endif; ?> </span>
                      <?php echo get_woocommerce_currency_symbol(); ?>
                    </div>
                    <?php }else{ ?>
                      <div class="price">
                      <span> <?php if(!empty($kproduct['kaveh_custom']['normal_price'])): ?>
                      <?php echo number_format($kproduct['kaveh_custom']['normal_price'], 0, '.', ','); ?>
                      <?php endif; ?></span>
                      <?php echo get_woocommerce_currency_symbol(); ?>
                    </div>
                    <?php } ?>
                    <?php else: ?>
                      <div class="price">
                      <span> <?php echo $kproduct['kaveh_custom']['_price'];  ?></span>
                      <?php echo get_woocommerce_currency_symbol(); ?>
                      </div>
                      <?php endif; ?>
                      <?php if ( $kproduct['kaveh_custom']['product_type'] !== 'variable' ) { ?>
                      <a type="submit" name="add-to-cart" value="<?php echo $kproduct['id']; ?>" class="btn btn-success-2 shadow-none single_add_to_cart_button"><span> + خرید </span></a>
                      <?php }else{ ?>
                      <?php } ?>
                      </div>
                      <a href="<?php echo $kproduct['link'] ?>" class="btn btn-secondary rounded-pill"> مشاهده محصول </a>
                    </div>
                  </div>
                </div>
                <!-- End Slide -->
                <?php endforeach; ?>
                <?php endif; ?>
              <?php }elseif('all'===$settings['pproductslahzeproductsrandomofferstkaveh']){

                $kproducts = KavehRestRules::kaveh_get_rest_posts(array(
                    'post_type' => 'product',
                    'per_page' => $settings['productnumberproductsproductsrandomofferstkaveh'],
                    'orderby' => $settings['psortproductslahzeproductsrandomofferstkaveh'],
                    'order' => $settings['psortiproductslahzeproductsrandomofferstkaveh'],
                ));
                ?>
                                <?php if(is_array($kproducts)): ?>
                  <?php foreach($kproducts as $kproduct): ?>
                <!-- Start Slide -->
                <div class="swiper-slide">
                  <div class="product-eight product-eight-offer">
                    <div class="image">
                      <a href="<?php echo $kproduct['link'] ?>">
                        <img src="<?php echo $kproduct['kaveh_custom']['thumbnail_url'] ?>" alt="<?php echo $kproduct['title']['rendered']; ?>" width="266" height="266">
                      </a>
                      <?php if($kproduct['kaveh_custom']['product_stock_status'] !== 'outofstock'): 
                      if($kproduct['kaveh_custom']['is_on_sale'] === true) : ?>
                      <div class="offer">
                      <?php if($kproduct['kaveh_custom']['product_type'] === 'variable'): ?>
                          <?php echo (int)(( ( (float)$kproduct['kaveh_custom']['min_regular_price'] - (float)$kproduct['kaveh_custom']['min_sale_price'] ) / (float)$kproduct['kaveh_custom']['min_regular_price'] ) * 100); ?><?php echo ' '.'%'; ?>
                      <?php elseif($kproduct['kaveh_custom']['product_type'] === 'simple'): ?>
                          <?php echo (int)(( ( (float)$kproduct['kaveh_custom']['normal_price'] - (float)$kproduct['kaveh_custom']['sale_price'] ) / (float)$kproduct['kaveh_custom']['normal_price'] ) * 100); ?><?php echo ' '.'%'; ?>
                      <?php endif; ?>
                      </div>
                      <?php endif; ?>
                      <?php endif; ?>
                    </div>
                    <div class="content">
                      <ul class="tags d-flex align-items-center flex-wrap">
                      </ul>
                      <h2 class="product-name">
                        <a href="<?php echo $kproduct['link'] ?>"> <?php echo $kproduct['title']['rendered']; ?></a>
                      </h2>
                      <div class="d-flex align-items-center justify-content-between">
                        <?php if ( $kproduct['kaveh_custom']['product_type'] !== 'variable' ) : ?>
                    <?php if(!empty($sale_price) ) { ?>
                    <div class="price">
                      <del> <?php if(!empty($kproduct['kaveh_custom']['normal_price'])): ?>
                      <?php echo number_format($kproduct['kaveh_custom']['normal_price'], 0, '.', ','); ?>
                      <?php endif; ?></del>
                      <span> <?php if(!empty($kproduct['kaveh_custom']['sale_price'])): ?>
                        <?php echo number_format($kproduct['kaveh_custom']['sale_price'], 0, '.', ','); ?>
                      <?php endif; ?> </span>
                      <?php echo get_woocommerce_currency_symbol(); ?>
                    </div>
                    <?php }else{ ?>
                      <div class="price">
                      <span> <?php if(!empty($kproduct['kaveh_custom']['normal_price'])): ?>
                      <?php echo number_format($kproduct['kaveh_custom']['normal_price'], 0, '.', ','); ?>
                      <?php endif; ?></span>
                      <?php echo get_woocommerce_currency_symbol(); ?>
                    </div>
                    <?php } ?>
                    <?php else: ?>
                      <div class="price">
                      <span> <?php echo $kproduct['kaveh_custom']['_price'];  ?></span>
                      <?php echo get_woocommerce_currency_symbol(); ?>
                      </div>
                      <?php endif; ?>
                      <?php if ( $kproduct['kaveh_custom']['product_type'] !== 'variable' ) { ?>
                      <a type="submit" name="add-to-cart" value="<?php echo $kproduct['id']; ?>" class="btn btn-success-2 shadow-none single_add_to_cart_button"><span> + خرید </span></a>
                      <?php }else{ ?>
                      <?php } ?>
                      </div>
                      <a href="<?php echo $kproduct['link'] ?>" class="btn btn-secondary rounded-pill"> مشاهده محصول </a>
                    </div>
                  </div>
                </div>
                <!-- End Slide -->
                <?php endforeach; ?>
                <?php endif; ?>
                <?php } ?>
              </div>
              <!-- Start Pagination -->
              <div class="swiper-pagination end-0"></div>
              <!-- End Pagination -->
            </div>
            <style>
              .rand-offer{
                max-width:315px !important;
              }
              .product-eight .image img {
                width: 187px;
                margin-right: auto;
                margin-left: auto;
            }
            .product-eight .image:not(.full-width) {
            min-height: 268px;
            }
            </style>
    <script>
function init<?php echo $clsid; ?>() {
// Swiper Discount Product Special
new Swiper(".swiper-discount-product-special", {
  slidesPerView: 1,
  spaceBetween: 20,
  pagination: {
    el: ".swiper-pagination",
    type: "bullets",
    clickable: true,
  },
  autoplay: {
    delay: 3000,
  },
  effect: "fade",
  fadeEffect: {
    crossFade: true,
  },
  loop: true,
});
}
if (window.Swiper) {
    // Swiper library already loaded, initialize Swiper instance
    init<?php echo $clsid; ?>();
  } else {
    // Swiper library not yet loaded, wait for it to load
    window.addEventListener("load", function() {
      init<?php echo $clsid; ?>();
    });
  }
    </script>
    <?php
  }


}
