<?php
defined('ABSPATH') || exit('No Access!');

global $options, $labels;
$ht_amazing_slider = irk_get_head_tag('ht-amazing-slider', ['class' => 'title']);

$style = $style ? $style : 'style1';

$bg_color = strpos($bg_color, 'rgba') !== false ? $bg_color : (strpos($bg_color, '#') === false ? '#' . $bg_color : $bg_color);

$timer = $options->get_option('amazing-timer');
$count = $options->get_option('amazing-slider-count');
$count = $count ? $count : 10;
$qty_input = $options->get_option('amazing-slider-quantity-input');

$args = array(
    'post_type' => ['product', 'product_variation'],
    'posts_per_page' => $count,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'irk_is_amazing',
            'value' => 'yes',
            'compare' => '=',
        ),
        array(
            'key' => '_sale_price_dates_from',
            'value' => '',
            'compare' => '!=',
        ),
        array(
            'key' => '_sale_price_dates_from',
            'value' => time(),
            'compare' => '<',
        ),
        array(
            'key' => '_sale_price_dates_to',
            'value' => '',
            'compare' => '!=',
        ),
    ),
);

if (!$options->get_option('amazing-show-expired')) {
    $args['meta_query'][] = array(
        'key' => '_sale_price_dates_to',
        'value' => time(),
        'compare' => '>',
    );
}

if ($category && $category != '-1') {
    $args['tax_query'] = [
        [
            'taxonomy' => 'product_cat',
            'terms' => $category,
            'field' => 'term_id',
            'operator' => 'AND',
            'include_children' => false,
        ],
    ];
}

$products = new WP_Query($args);
if (!$products->found_posts) {
    return;
}

if ($category == '-1' || $category == '') {
    $url = add_query_arg(array('is_amazing' => 1), wc_get_page_permalink('shop'));
} else {
    $url = add_query_arg(array('is_amazing' => 1), get_term_link(get_term($category, 'product_cat'), 'product_cat'));
}

?>
<?php if ($style == 'style1'): ?>
    <div class="amazing-slider style1" style="<?php echo $bg_color ? 'background-color:' . esc_html($bg_color) : ''; ?>">
        <div class="slider-inner">
            <div class="amazing-title">
                <?php echo $title_image ? irk_linked_image($url, $title_image) : ''; ?>
                <br>
                <a href="<?php echo esc_url($url); ?>" class="view-all"><?php echo $labels->get_label('view-all'); ?></a>
            </div>
            <div class="owl-carousel">
                <?php while ($products->have_posts()): ?>
                    <?php
                    $products->the_post();
                    $product_id = $products->post->ID;
                    $product = wc_get_product($product_id);
                    $date_to = get_post_meta($product_id, '_sale_price_dates_to', true);
                    $timer_id = irk_random_string();
                    $second_image = irk_second_image($product, 'amazing-slider');
                    $additional_class = $second_image ? 'has-secondary-image' : '';
                    ?>
                    <div <?php wc_product_class('item ' . $additional_class, $product_id); ?>>
                        <?php echo irk_product_badges_html($product_id); ?>
                        <a href="<?php echo get_the_permalink($product_id); ?>" title="<?php echo $product->get_name(); ?>">
                            <div class="img-wrapper">
                                <?php
                                echo $product->get_image('woocommerce_thumbnail', ['class' => 'img-fluid']);
                                if ($second_image) {
                                    echo wp_get_attachment_image($second_image, 'woocommerce_thumbnail', false, ['class' => 'secondary-image img-fluid']);
                                }
                                ?>
                            </div>
                            <?php echo $ht_amazing_slider['open'] . $product->get_name() . $ht_amazing_slider['close']; ?>
                            <?php echo $product->get_price_html(); ?>
                            <?php if ($timer): ?>
                                <?php if ($date_to > time()): ?>
                                    <div class="timer timer-<?php echo $timer_id; ?>"></div>
                                    <script type="text/javascript">
                                        var dateEnd = new Date((<?php echo $date_to; ?>) * 1000);
                                        new TimezZ('.timer-<?php echo $timer_id; ?>', {
                                            date: dateEnd,
                                            template: '<span><span class="number">NUMBER</span><span class="dot">:</span><span class="letter">LETTER</span></span>',
                                            text: {
                                                days: '<?php _e('Days', 'irk'); ?>',
                                                hours: '<?php _e('Hours', 'irk'); ?>',
                                                minutes: '<?php _e('Minutes', 'irk'); ?>',
                                                seconds: '<?php _e('Seconds', 'irk'); ?>',
                                            }
                                        });
                                    </script>
                                <?php else: ?>
                                    <div class="timer expired"><?php echo $labels->get_label('expired'); ?></div>
                                <?php endif; ?>
                            <?php endif; ?>
                        </a>
                        <?php if ($options->get_option('amazing-slider-add2cart')): ?>
                            <?php if (irk_active_quantity_input($product)): ?>
                                <?php echo $qty_input ? woocommerce_quantity_input([], $product, false) : ''; ?>
                                <a href="#" class="ajax-add-to-cart add-to-cart button" data-id="<?php echo $product_id; ?>">
                                    <?php if ($qty_input): ?>
                                        <span class="plus" data-icon="dk-plus"></span>
                                    <?php else: ?>
                                        <span class="button-text"><?php echo esc_html($product->add_to_cart_text()); ?></span>
                                    <?php endif; ?>
                                    <img src="<?php echo IRK_IMG_URI; ?>oval.svg" class="ajax-loader" width="24" height="24" alt="loader">
                                </a>
                            <?php else: ?>
                                <a class="add-to-cart button" href="<?php echo $product->get_permalink(); ?>" data-id="<?php echo $product_id; ?>"><?php echo esc_html($product->add_to_cart_text()); ?></a>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                <?php endwhile; ?>
                <?php wp_reset_postdata(); ?>
            </div>
        </div>
    </div>
<?php elseif ($style == 'style2'): ?>
    <?php $slider_id = irk_random_string(); ?>
    <style rel="stylesheet">
        .amazing-slider#amazing-slider-<?php echo $slider_id; ?> .slider-sidebar .owl-thumbs .owl-thumb-item.active {
            background: <?php echo '#' . esc_html( $bg_color ); ?>;
        }
    </style>
    <div class="amazing-slider style2 widget" id="amazing-slider-<?php echo $slider_id; ?>" style="background-color: <?php echo esc_html($bg_color); ?>">
        <div class="slider-inner">
            <div class="row">
                <div class="slider-content col-12 col-lg-9">
                    <div class="amazing-title">
                        <?php echo $title_image ? irk_linked_image($url, $title_image) : ''; ?>
                    </div>
                    <div class="owl-carousel" data-slider-id="amazing-slider-<?php echo $slider_id; ?>">
                        <?php while ($products->have_posts()): ?>
                            <?php
                            $products->the_post();
                            $product_id = $products->post->ID;
                            $product = wc_get_product($product_id);
                            $is_variation = $product->is_type('variation');
                            $date_to = get_post_meta($product_id, '_sale_price_dates_to', true);
                            $timer_id = irk_random_string();
                            $second_image = irk_second_image($product, 'amazing-slider');
                            $additional_class = $second_image ? 'has-secondary-image' : '';
                            ?>
                            <div <?php wc_product_class('item ' . $additional_class, $product_id); ?>>
                                <div class="row">
                                    <div class="col-12 col-lg-5">
                                        <a href="<?php echo get_the_permalink($product_id); ?>" title="<?php echo $product->get_name(); ?>">
                                            <?php echo irk_product_badges_html($product_id); ?>
                                            <?php
                                            echo $product->get_image('woocommerce_thumbnail', ['class' => 'img-fluid']);
                                            if ($second_image) {
                                                echo wp_get_attachment_image($second_image, 'woocommerce_thumbnail', false, ['class' => 'secondary-image img-fluid']);
                                            }
                                            ?>
                                        </a>
                                    </div>
                                    <div class="col-12 col-lg-7">
                                        <?php echo $ht_amazing_slider['open']; ?>
                                        <a href="<?php echo get_the_permalink($product_id); ?>" title="<?php echo $product->get_name(); ?>"><?php echo $product->get_name(); ?></a>
                                        <?php echo $ht_amazing_slider['close']; ?>
                                        <?php echo $product->get_price_html(); ?>
                                        <?php
                                        $p_id = $is_variation ? $product->get_parent_id() : $product_id;
                                        $short_attributes = get_post_meta($p_id, 'irk_short_attributes', true);
                                        $product_attributes = get_post_meta($p_id, '_product_attributes', true);
                                        if (is_array($short_attributes) && count($short_attributes)) {
                                            echo '<div class="short-attributes">';
                                            echo '<ul>';
                                            foreach ($short_attributes as $short_attribute) {
                                                list($is_tax, $attr) = explode(':', $short_attribute);

                                                $label = '';
                                                if ($is_tax == 1) {
                                                    $tax = get_taxonomy($attr);
                                                    if (is_object($tax)) {
                                                        $label = $tax->labels->singular_name;
                                                    }
                                                } else {
                                                    foreach ($product_attributes as $product_attribute) {
                                                        if ($product_attribute['name'] == $attr) {
                                                            $label = $product_attribute['name'];
                                                            break;
                                                        }
                                                    }
                                                }

                                                $p_attr = $is_variation ? wc_get_product($p_id)->get_attribute($attr) : $product->get_attribute($attr);
                                                echo '<li><span class="attribute-label">' . esc_html($label) . ': </span><span class="value">' . $p_attr . '</span></li>';
                                            }
                                            echo '</ul>';
                                            echo '</div>';
                                        } else {
                                            $attributes = $is_variation ? wc_get_product($product->get_parent_id())->get_attributes() : $product->get_attributes();

                                            if (is_array($attributes) && count($attributes)) {
                                                echo '<div class="short-attributes">';
                                                echo '<ul>';
                                                $i = 0;
                                                foreach ($attributes as $attribute) {
                                                    if (!is_object($attribute) || !$attribute->get_visible()) {
                                                        continue;
                                                    }
                                                    echo '<li><span class="attribute-label">' . wc_attribute_label($attribute->get_name()) . ': </span>';
                                                    $values = array();
                                                    if ($attribute->is_taxonomy()) {
                                                        $attribute_taxonomy = $attribute->get_taxonomy_object();
                                                        $attribute_values = wc_get_product_terms($product->get_id(), $attribute->get_name(), array('fields' => 'all'));
                                                        foreach ($attribute_values as $attribute_value) {
                                                            $value_name = esc_html($attribute_value->name);
                                                            if ($attribute_taxonomy->attribute_public) {
                                                                $values[] = '<a href="' . esc_url(get_term_link($attribute_value->term_id, $attribute->get_name())) . '" rel="tag">' . $value_name . '</a>';
                                                            } else {
                                                                $values[] = $value_name;
                                                            }
                                                        }
                                                    } else {
                                                        $values = $attribute->get_options();
                                                        foreach ($values as &$value) {
                                                            $value = make_clickable(esc_html($value));
                                                        }
                                                    }
                                                    echo '<span class="value">' . apply_filters('woocommerce_attribute', wptexturize(implode(', ', $values)), $attribute, $values) . '</span>';
                                                    echo '</li>';
                                                    $i++;
                                                    if ($i > 3) {
                                                        break;
                                                    }
                                                }
                                                echo '</ul>';
                                                echo '</div>';
                                            }
                                        }
                                        ?>
                                        <?php if ($timer): ?>
                                            <?php if ($date_to > time()): ?>
                                                <div class="timer timer-<?php echo $timer_id; ?>"></div>
                                                <script type="text/javascript">
                                                    var dateEnd = new Date((<?php echo $date_to; ?>) * 1000);
                                                    new TimezZ('.timer-<?php echo $timer_id; ?>', {
                                                        date: dateEnd,
                                                        template: '<span>NUMBER<span>:</span></span>'
                                                    });
                                                </script>
                                            <?php else: ?>
                                                <div class="timer expired"><?php echo $labels->get_label('expired'); ?></div>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                        <?php if ($options->get_option('amazing-slider-add2cart')): ?>
                                            <?php if (irk_active_quantity_input($product)): ?>
                                                <?php echo $qty_input ? woocommerce_quantity_input([], $product, false) : ''; ?>
                                                <a href="#" class="ajax-add-to-cart add-to-cart button" data-id="<?php echo $product_id; ?>">
                                                    <?php if ($qty_input): ?>
                                                        <span class="plus" data-icon="dk-plus"></span>
                                                    <?php else: ?>
                                                        <span class="button-text"><?php echo esc_html($product->add_to_cart_text()); ?></span>
                                                    <?php endif; ?>
                                                    <img src="<?php echo IRK_IMG_URI; ?>oval.svg" class="ajax-loader" width="24" height="24" alt="loader">
                                                </a>
                                            <?php else: ?>
                                                <a class="add-to-cart button" href="<?php echo $product->get_permalink(); ?>" data-id="<?php echo $product_id; ?>"><?php echo esc_html($product->add_to_cart_text()); ?></a>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        <?php endwhile; ?>
                        <?php wp_reset_postdata(); ?>
                    </div>
                </div>
                <div class="slider-sidebar col-12 col-lg-3">
                    <div class="owl-thumbs" data-slider-id="amazing-slider-<?php echo $slider_id; ?>">
                        <?php while ($products->have_posts()): ?>
                            <?php
                            $products->the_post();
                            $product_id = $products->post->ID;
                            $product = wc_get_product($product_id);
                            ?>
                            <button class="owl-thumb-item"><?php echo wp_trim_words($product->get_name(), 5); ?></button>
                        <?php endwhile; ?>
                        <?php wp_reset_postdata(); ?>
                    </div>
                    <a href="<?php echo esc_url($url); ?>" class="view-all owl-thumb-item"><?php echo $labels->get_label('view-all'); ?></a>
                </div>
            </div>
        </div>
    </div>
<?php endif; ?>