<?php


use Elementor\Widget_Base;
use Elementor\Controls_Manager;

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


class Digikalaamazingofferonekaveh extends Widget_Base
{

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

    public function get_title()
    {
        return esc_html__('Amazing Offer Widget Style 1 - Digikala', 'kavehTheme');
    }

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

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

    protected function register_controls()
    {

        $this->start_controls_section(
            'content1digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Images', 'kavehTheme'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'img1digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Image 1', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::MEDIA,
            ]
        );

        $this->add_control(
            'img2digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Image 2', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::MEDIA,
            ]
        );
        $this->end_controls_section();
        $this->start_controls_section(
            'content2digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Product Settings', 'kavehTheme'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $options = array();
        $orderby = 'name';
        $order = 'asc';
        $hide_empty = true;
        $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) {
            $options[$category->term_id] = $category->name;
        }
        $this->add_control(
            'catproductsdigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Select Category', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::SELECT2,
                'multiple' => true,
                'options' => $options,
            ]
        );

        $this->add_control(
            'numberproductsdigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Number of Displayed Products', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 0,
                'max' => 20,
                'step' => 1,
                'default' => 18,
            ]
        );

        $this->end_controls_section();
        $this->start_controls_section(
            'content3digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Buttons', 'kavehTheme'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'button1digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Button 1 Title', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__('View All', 'kavehTheme'),
            ]
        );
        $this->add_control(
            'buttonlink1digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Button 1 Link', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__('#', 'kavehTheme'),

            ]
        );

        $this->add_control(
            'button2digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('More Button Title', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__('View All', 'kavehTheme'),
            ]
        );
        $this->add_control(
            'buttonlink2digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('More Button Link', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__('#', 'kavehTheme'),

            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'style1_digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Button Style', 'kavehTheme'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'button1_digikalaamazingofferonelkaveh',
                'label' => esc_html__('Button 1 Typography', 'kavehTheme'),
                'selector' => '{{WRAPPER}} .amazing-offer-kala-title a span',
            ]
        );

        $this->add_control(
            'textbutton1colordigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Button 1 Text Color', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .amazing-offer-kala-title a span' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'button2_digikalaamazingofferonelkaveh',
                'label' => esc_html__('More Button Typography', 'kavehTheme'),
                'selector' => '{{WRAPPER}} .amazing-offer-kala-more span',
            ]
        );

        $this->add_control(
            'textbutton2colordigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('More Button Text Color', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .amazing-offer-kala-more span' => 'color: {{VALUE}}',
                ],
            ]
        );

        $this->add_control(
            'icon2colordigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('View More Button Icon Color', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#19bfd3',
                'selectors' => [
                    '{{WRAPPER}} .amazing-offer-kala-more i' => 'color: {{VALUE}} !important',
                ],
            ]
        );

        $this->add_control(
            'iconborder2colordigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('View More Button Icon Border Color', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#19bfd3',
                'selectors' => [
                    '{{WRAPPER}} .amazing-offer-kala-more i' => 'border:1px solid  {{VALUE}}',
                ],
            ]
        );

        $this->end_controls_section();
        $this->start_controls_section(
            'style2_digikalaamazingoffertwokaveh',
            [
                'label' => esc_html__('Product Style', 'kavehTheme'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'bglableoffercolordigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Discount Label Background Color', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#ef394e',
                'selectors' => [
                    '{{WRAPPER}} .product-nineteen .offer' => 'background-color: {{VALUE}} !important',
                ],
            ]
        );

        $this->add_control(
            'lableoffercolordigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Discount Label Text Color', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .product-nineteen .offer' => 'color: {{VALUE}} !important',
                ],
            ]
        );

        $this->add_control(
            'bgprogresscolordigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Time Progress Color', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#ef394e',
                'selectors' => [
                    '{{WRAPPER}} .product-nineteen .progress-time span' => 'background-color: {{VALUE}} !important',
                ],
            ]
        );

        $this->end_controls_section();
        $this->start_controls_section(
            'style3_digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Box Style', 'kavehTheme'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Background::get_type(),
            [
                'name' => 'boxbgcolordigikalaamazingofferonelkaveh',
                'types' => ['gradient'],
                'selector' => '{{WRAPPER}} .amazing-offer-kala-wrapper',
            ]
        );

        $this->add_control(
            'boxradiusdigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Border Radius', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px'],
                'selectors' => [
                    '{{WRAPPER}} .amazing-offer-kala-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
        $this->start_controls_section(
            'style3_digikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Carousel Navigation Style', 'kavehTheme'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'arrowscolordigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Carousel Navigation Background Color', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .swiper-button-next' => 'background-color: {{VALUE}} !important;',
                    '{{WRAPPER}}  .swiper-button-prev' => 'background-color: {{VALUE}} !important;',
                ],
            ]
        );
        $this->add_control(
            'arrowscoloricondigikalaamazingofferonelkaveh',
            [
                'label' => esc_html__('Carousel Navigation Icon Color', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#3f4064',
                'selectors' => [
                    '{{WRAPPER}} .swiper-button-next::after' => 'color: {{VALUE}} !important;',
                    '{{WRAPPER}} .swiper-button-prev::after' => 'color: {{VALUE}} !important;',
                ]
            ]
        );

        $this->add_control(
            'title1_options',
            [
                'label' => esc_html__('Previous Arrow Border', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' => 'borderarrowprevdigikalaamazingofferonelkaveh',
                'selector' => '{{WRAPPER}} .swiper-button-prev'
            ]
        );

        $this->add_control(
            'title2_options',
            [
                'label' => esc_html__('Next Arrow Border', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' => 'borderarrownextdigikalaamazingofferonelkaveh',
                'selector' => '{{WRAPPER}} .swiper-button-next'
            ]
        );
        $this->end_controls_section();

    }

    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $clsid = $this->get_id();
        ?>
        <!-- Start Amazing Offer -->
        <section class="amazing-offer-kala">
            <div class="container">
                <div class="amazing-offer-kala-wrapper">
                    <!-- Start Carousel -->
                    <div class="swiper swiper-amazing-offer-kala position-relative overflow-hidden swiper-kala" data-swiper='{"navigation":{"nextEl":".swiper-button-next","prevEl":".swiper-button-prev"},"breakpoints":{"320":{"slidesPerView":1.8,"spaceBetween":2},"400":{"slidesPerView":2.3,"spaceBetween":2},"500":{"slidesPerView":2.8,"spaceBetween":2},"576":{"slidesPerView":3.1,"spaceBetween":2},"768":{"slidesPerView":4.1,"spaceBetween":2},"992":{"slidesPerView":5.5,"spaceBetween":2},"1200":{"slidesPerView":6.1,"spaceBetween":2},"1400":{"slidesPerView":7.1,"spaceBetween":2}}}'>
                        <div class="swiper-wrapper">
                            <!-- Start Slide -->
                            <div class="swiper-slide">
                                <div class="amazing-offer-kala-title d-flex flex-column justify-content-center">
                                    <a href="<?php echo esc_url($settings['buttonlink1digikalaamazingofferonelkaveh']); ?>"
                                       class="d-block w-100">
                                        <img src="<?php echo esc_url($settings['img1digikalaamazingofferonelkaveh']['url']); ?>"
                                             alt="<?php echo esc_attr(\Elementor\Control_Media::get_image_alt($settings['img1digikalaamazingofferonelkaveh'])); ?>"
                                             width="92"
                                             height="77" class="d-block mx-auto">
                                        <img src="<?php echo esc_url($settings['img2digikalaamazingofferonelkaveh']['url']); ?>"
                                             alt="<?php echo esc_attr(\Elementor\Control_Media::get_image_alt($settings['img2digikalaamazingofferonelkaveh'])); ?>"
                                             width="145"
                                             height="115" class="mt-2 d-block mx-auto">
                                        <span class="d-flex align-items-center justify-content-center mt-1">
                                            <?php echo esc_html($settings['button1digikalaamazingofferonelkaveh']); ?>
                  <i class="icon-angle-left ms-1"></i>
                </span>
                                    </a>
                                </div>
                            </div>
                            <!-- End Slide -->
                            <?php
                            // WP_Query args
                            $cat_query_args = array(
                                'post_type' => array('product'),
'post_status' => array('publish'),
                                'posts_per_page' => $settings['numberproductsdigikalaamazingofferonelkaveh'],
                                'order' => 'DESC',
                                'orderby' => 'date',
                                'tax_query' => array(
                                    array(
                                        'taxonomy' => 'product_cat',
                                        'field' => 'term_id',
                                        'terms' => $settings['catproductsdigikalaamazingofferonelkaveh'],
                                    ),
                                ),
                                'meta_query' => array(
                                    array(
                                        'key' => '_stock_status',
                                        'value' => 'outofstock',
                                        'compare' => 'NOT IN',
                                    ),
                                ),
                            );
                            $cat_query_args['post__in'] = array_merge(array(0), wc_get_product_ids_on_sale());
                            // The Query
                            $pcat_query = new \WP_Query($cat_query_args);

                            // The Loop
                            if ($pcat_query->have_posts()) :?>
                                <?php while ($pcat_query->have_posts()) :
                                    $pcat_query->the_post();
                                    $product = wc_get_product(get_the_ID());
                                    $sale_price = $product->get_sale_price();
                                    $normal_price = $product->get_regular_price();
                                    ?>
                                    <!-- Start Slide -->
                                    <div class="swiper-slide h-auto">
                                        <div class="product-nineteen d-flex flex-column position-relative bg-white h-100">
                                            <a href="<?php the_permalink(); ?>" class="stretched-link"
                                               aria-label="name product"></a>
                                            <img src="<?php the_post_thumbnail_url('thumbnail'); ?>"
                                                 alt="<?php the_title(); ?>"
                                                 width="150"
                                                 height="150" class="d-block mx-auto">
                                            <div class="d-flex align-items-center justify-content-between mt-3">
                                                <span class="offer d-flex align-items-center justify-content-center rounded-4 fw-bold">
                                                    <?php if (class_exists('kavehWoo')) {
                                                        $offer_percentage = kavehWoo::showofferpercentage($product);
                                                        echo $offer_percentage;
                                                    } ?>
                                                </span>
                                                <div class="price d-flex align-items-center fw-bold">
                                                    <?php
                                                    if ($product->is_type('simple')) {
                                                        echo number_format($sale_price, 0, '.', ',');
                                                    } elseif ($product->is_type('variable')) {
                                                        $max_percentage = 0;
                                                        $child_id_with_max_discount = 0;
                                                        foreach ($product->get_children() as $child_id) {
                                                            $variation = wc_get_product($child_id);
                                                            $price = (float)$variation->get_regular_price();
                                                            $sale = (float)$variation->get_sale_price();
                                                            if ($price != 0 && !empty($sale)) $percentage = ($price - $sale) / $price * 100;
                                                            if ($percentage > $max_percentage) {
                                                                $max_percentage = $percentage;
                                                                $child_id_with_max_discount = $child_id;
                                                            }
                                                        }
                                                        $sale_price_v = (float)wc_get_product($child_id_with_max_discount)->get_sale_price();
                                                        echo number_format($sale_price_v, 0, '.', ',');
                                                    }
                                                    ?>
                                                    <i class="icon-toman-2"></i>
                                                </div>
                                            </div>
                                            <del class="last-price text-end d-block">
                                                <?php
                                                if ($product->is_type('simple')) {
                                                    echo number_format($normal_price, 0, '.', ',');
                                                } elseif ($product->is_type('variable')) {
                                                    $max_percentage = 0;
                                                    $child_id_with_max_discount = 0;
                                                    foreach ($product->get_children() as $child_id) {
                                                        $variation = wc_get_product($child_id);
                                                        $price = (float)$variation->get_regular_price();
                                                        $sale = (float)$variation->get_sale_price();
                                                        if ($price != 0 && !empty($sale)) $percentage = ($price - $sale) / $price * 100;
                                                        if ($percentage > $max_percentage) {
                                                            $max_percentage = $percentage;
                                                            $child_id_with_max_discount = $child_id;
                                                        }
                                                    }
                                                    $normal_price_v = (float)wc_get_product($child_id_with_max_discount)->get_regular_price();
                                                    echo number_format($normal_price_v, 0, '.', ',');
                                                }
                                                ?>
                                            </del>
                                            <?php
                                            if ($product->is_type('simple')) {
                                                $product_id = $product->get_id();

                                                $sale_start_date = get_post_meta($product_id, '_sale_price_dates_from', true);
                                                $sale_end_date = get_post_meta($product_id, '_sale_price_dates_to', true);

                                                if (!empty($sale_start_date) && !empty($sale_end_date)) { ?>
                                                    <div class="progress-time position-relative mt-2 overflow-hidden rounded-pill">
                                                    <span class="position-absolute top-0 end-0 rounded-pill h-100"
                                                          style="width:<?php
                                                          $current_time = current_time('timestamp');
                                                          $total_time = $sale_end_date - $sale_start_date;
                                                          $time_remaining = $sale_end_date - $current_time;
                                                          if ($time_remaining >= 0 && $total_time > 0) {
                                                              $percentage_remaining = ($time_remaining / $total_time) * 100;
                                                              echo round($percentage_remaining, 2);
                                                          }
                                                          ?>%"></span>
                                                    </div>
                                                    <?php
                                                } else {
                                                }
                                            } elseif ($product->is_type('variable')) {
                                                $max_percentage = 0;
                                                $child_id_with_max_discount = 0;
                                                foreach ($product->get_children() as $child_id) {
                                                    $variation = wc_get_product($child_id);
                                                    $price = (float)$variation->get_regular_price();
                                                    $sale = (float)$variation->get_sale_price();
                                                    if ($price != 0 && !empty($sale)) $percentage = ($price - $sale) / $price * 100;
                                                    if ($percentage > $max_percentage) {
                                                        $max_percentage = $percentage;
                                                        $child_id_with_max_discount = $child_id;
                                                    }
                                                }
                                                $sale_start_date = get_post_meta($child_id_with_max_discount, '_sale_price_dates_from', true);
                                                $sale_end_date = get_post_meta($child_id_with_max_discount, '_sale_price_dates_to', true);
                                                if (!empty($sale_start_date) && !empty($sale_end_date)) { ?>
                                                    <div class="progress-time position-relative mt-2 overflow-hidden rounded-pill">
                                                    <span class="position-absolute top-0 end-0 rounded-pill h-100"
                                                          style="width:<?php
                                                          $current_time = current_time('timestamp');
                                                          $total_time = $sale_end_date - $sale_start_date;
                                                          $time_remaining = $sale_end_date - $current_time;
                                                          if ($time_remaining >= 0 && $total_time > 0) {
                                                              $percentage_remaining = ($time_remaining / $total_time) * 100;
                                                              echo round($percentage_remaining, 2);
                                                          }
                                                          ?>%"></span>
                                                    </div>
                                                    <?php
                                                } else {
                                                }
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    <!-- End Slide -->
                                <?php
                                endwhile;
                                // Reset Original Post Data after the loop
                                wp_reset_postdata();
                                ?>
                            <?php endif; ?>
                            <!-- Start Slide -->
                            <div class="swiper-slide h-auto">
                                <a href="<?php echo esc_url($settings['buttonlink2digikalaamazingofferonelkaveh']); ?>"
                                   class="amazing-offer-kala-more bg-white d-flex align-items-center justify-content-center flex-column h-100">
                                    <i class="icon-arrow-left-4 rounded-circle d-flex align-items-center justify-content-center fs-5"></i>
                                    <span><?php echo esc_html($settings['button2digikalaamazingofferonelkaveh']); ?></span>
                                </a>
                            </div>
                            <!-- End Slide -->
                        </div>
                        <!-- Start Button -->
                        <div class="swiper-button-next d-none d-sm-flex align-items-center justify-content-center rounded-circle bg-white">
                        </div>
                        <div class="swiper-button-prev d-none d-sm-flex align-items-center justify-content-center rounded-circle bg-white">
                        </div>
                        <!-- End Button -->
                    </div>
                    <!-- End Carousel -->
                </div>
            </div>
        </section>
        
        <?php
    }

}
