<?php
/**
 * woocommerce vc element products.
 *
 * @package tz-interiart/vc_templaza/woo-products
 * @version 1.0.0
 */

defined('ABSPATH') || exit;
// Create Shortcode vc-woo-products
if (class_exists('Woocommerce')) :
    function create_vcwooproducts_shortcode($atts)
    {
        // Attributes
        $atts = shortcode_atts(
            array(
                'woo_products_layout' => 'slider',
                'woo_products_data' => 'recent',
                'woo_products_items' => '',
                'woo_products_loop' => '1',
                'woo_products_autoplay' => '1',
                'woo_products_autoplayspeed' => '500',
                'woo_products_navigation' => '1',
                'woo_products_dots' => '1',
                'woo_products_maxitem' => 20,
                'woo_products_order' => '',
                'woo_products_orderby' => '',
                'woo_products_category' => '',
                'woo_products_columns' => '4',
                'woo_products_shtitle' => 'show',
                'woo_products_shrating' => 'show',
                'woo_products_shprice' => 'show',
                'woo_products_shthumbnail' => 'show',
                'woo_products_shlinkdetail' => 'show',
                'woo_products_quickview' => 'show',
                'woo_products_shaddtocart' => 'show',
                'woo_products_shaddtowishlist' => 'show',
                'woo_products_stagepadding' => '340',
                'css' => '',
            ),
            $atts,
            'vc-woo-products'
        );
        ob_start();
        // main tab setting get config
        $woo_products_layout = $atts['woo_products_layout'];
        $woo_products_data = $atts['woo_products_data'];
        $woo_products_maxitem = $atts['woo_products_maxitem'];
        $woo_products_order = $atts['woo_products_order'];
        $woo_products_orderby = $atts['woo_products_orderby'];

        // slider tab setting get config
        $woo_products_items = $atts['woo_products_items'];
        $woo_products_loop = $atts['woo_products_loop'];
        $woo_products_autoplay = $atts['woo_products_autoplay'];
        $woo_products_autoplayspeed = $atts['woo_products_autoplayspeed'];
        $woo_products_nagination = $atts['woo_products_navigation'];
        $woo_products_dots = $atts['woo_products_dots'];
        $woo_products_category = $atts['woo_products_category'];
        $woo_products_columns = $atts['woo_products_columns'];
        $woo_products_stagepadding = $atts['woo_products_stagepadding'];

        // grid tab setting get config

        // Show/hide setting
        $woo_products_shtitle = $atts['woo_products_shtitle'];
        $woo_products_shrating = $atts['woo_products_shrating'];
        $woo_products_shprice = $atts['woo_products_shprice'];
        $woo_products_shthumbnail = $atts['woo_products_shthumbnail'];
        $woo_products_shlinkdetail = $atts['woo_products_shlinkdetail'];
        $woo_products_quickview = $atts['woo_products_quickview'];
        $woo_products_shaddtocart = $atts['woo_products_shaddtocart'];
        $woo_products_shaddtowishlist = $atts['woo_products_shaddtowishlist'];
        $css = $atts['css'];

        //Include Carousel here

        wp_enqueue_script('owl-carousel-221');
        wp_enqueue_style('owl-carousel-221');

        if ($woo_products_layout == 'slider'):
//            wp_enqueue_script('woo-js');
            wp_enqueue_script('woo_product_element');
        endif;

        // Create
        if (class_exists('Woocommerce')) :
            add_action('woo_get_rating', 'woocommerce_template_loop_rating', 10);
            add_action('woo_get_add_to_cart', 'woocommerce_template_loop_add_to_cart', 20);
            add_action('woocommerce_after_shop_loop_item', 'xoo_qv_button', 11);
        endif;

        $wpe__class = '';
        if ($woo_products_layout == 'grid'):
            $wpe__class = "grid-layout grid-col-$woo_products_columns";
        endif;

        ?>
        <!-- use function uniqid() create random ID   -->
        <div id="woo-products-element-<?php echo esc_attr(uniqid()); ?>"
             class="woo-products-element woocommerce <?php echo esc_attr($wpe__class); ?> <?php echo vc_shortcode_custom_css_class($css); ?>">

            <div class="<?php if ($woo_products_layout == 'slider'): echo esc_attr('owl-carousel owl-theme wpe__slider'); else: echo esc_attr('wpe__wrapper'); endif; ?>"
                 data-number="<?php echo esc_attr($woo_products_items); ?>"
                 data-nav="<?php echo esc_attr($woo_products_nagination); ?>"
                 data-autoplay="<?php echo esc_attr($woo_products_autoplay); ?>"
                 data-autoplayspeed="<?php echo esc_attr($woo_products_autoplayspeed); ?>"
                 data-loop="<?php echo esc_attr($woo_products_loop); ?>"
                 data-dots="<?php echo esc_attr($woo_products_dots); ?>"
                 data-stagepadding="<?php echo esc_attr($woo_products_stagepadding); ?>"
            >
                <?php
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                switch ($woo_products_data) {
                    case "recent":
                        $woo_products_args = array(
                            'post_type' => 'product',
                            'posts_per_page' => $woo_products_maxitem,
                            'orderby' => $woo_products_orderby,
                            'order' => $woo_products_order,
                            'ignore_sticky_posts' => 1,
                            'paged' => $paged
                        );
                        break;
                    case "featured":
                        $meta_query = WC()->query->get_meta_query();
                        $tax_query = WC()->query->get_tax_query();
                        $tax_query[] = array(
                            'taxonomy' => 'product_visibility',
                            'field' => 'name',
                            'terms' => 'featured',
                            'operator' => 'IN',
                        );

                        $woo_products_args = array(
                            'post_type' => 'product',
                            'post_status' => 'publish',
                            'ignore_sticky_posts' => 1,
                            'posts_per_page' => $woo_products_maxitem,
                            'orderby' => $woo_products_orderby,
                            'order' => $woo_products_order,
                            'meta_query' => $meta_query,
                            'tax_query' => $tax_query,
                            'paged' => $paged
                        );
                        break;
                    case "category":
                        $meta_query = WC()->query->get_meta_query();
                        $tax_query = WC()->query->get_tax_query();
                        $tax_query[] = array(
                            'taxonomy' => 'product_cat',
                            'field' => 'ID',
                            'terms' => $woo_products_category,
                            'operator' => 'IN',
                        );

                        $woo_products_args = array(
                            'post_type' => 'product',
                            'post_status' => 'publish',
                            'ignore_sticky_posts' => 1,
                            'posts_per_page' => $woo_products_maxitem,
                            'orderby' => $woo_products_orderby,
                            'order' => $woo_products_order,
                            'meta_query' => $meta_query,
                            'tax_query' => $tax_query,
                            'paged' => $paged
                        );
                        break;
                    case "sale":
                        $woo_products_args = array(
                            'post_type' => 'product',
                            'post_status' => 'publish',
                            'ignore_sticky_posts' => 1,
                            'posts_per_page' => $woo_products_maxitem,
                            'orderby' => $woo_products_orderby,
                            'order' => $woo_products_order,
                            'paged' => $paged,
                            'meta_query' => array(
                                'relation' => 'OR',
                                array( // Simple products type
                                    'key' => '_sale_price',
                                    'value' => 0,
                                    'compare' => '>',
                                    'type' => 'numeric'
                                ),
                                array( // Variable products type
                                    'key' => '_min_variation_sale_price',
                                    'value' => 0,
                                    'compare' => '>',
                                    'type' => 'numeric'
                                )
                            )
                        );
                        break;
                    case "best":
                        $woo_products_args = array(
                            'post_type' => 'product',
                            'post_status' => 'publish',
                            'ignore_sticky_posts' => 1,
                            'posts_per_page' => $woo_products_maxitem,
                            'orderby' => 'meta_value_num',
                            'meta_key' => 'total_sales',
                            'paged' => $paged
                        );
                        break;
                    case "rated":
                        $woo_products_args = array(
                            'post_type' => 'product',
                            'post_status' => 'publish',
                            'ignore_sticky_posts' => 1,
                            'posts_per_page' => $woo_products_maxitem,
                            'orderby' => 'meta_value_num',
                            'meta_key' => '_wc_average_rating',
                            'paged' => $paged
                        );
                        break;
                    default:
                        $woo_products_args = array(
                            'post_type' => 'product',
                            'post_status' => 'publish',
                            'ignore_sticky_posts' => 1,
                            'posts_per_page' => -1,
                            'paged' => $paged
                        );
                }
                $woo_products_loop = new WP_Query($woo_products_args);
                if ($woo_products_loop->have_posts()) {
                    while ($woo_products_loop->have_posts()) : $woo_products_loop->the_post();

                        ?>
                        <div class="wpe__item">
                            <?php if ($woo_products_shthumbnail == 'show'): ?>
                                <div class="wpe__images">
                                    <?php
                                    the_post_thumbnail(array(370, 430));
                                    ?>
                                    <?php if ($woo_products_shlinkdetail == 'show' || $woo_products_shaddtowishlist == 'show' || $woo_products_quickview=='show' || $woo_products_shaddtocart == 'show'): ?>
                                        <div class="wpe__hover">
                                            <?php
                                            if($woo_products_quickview == 'show'):
                                                xoo_qv_button();
                                            endif;
                                            ?>

                                            <?php

                                            if ($woo_products_shaddtocart == 'show'):
                                                ?>

                                                <?php
                                                do_action('woo_get_add_to_cart');
                                            endif;
                                            if ($woo_products_shaddtowishlist == 'show'):
                                                ?>

                                                <div class="wpe__wishlist">
                                                    <?php
                                                    if (class_exists('YITH_WCWL')) {
                                                        echo do_shortcode('[yith_wcwl_add_to_wishlist]');
                                                    }
                                                    ?>
                                                </div>
                                            <?php endif;
                                            if ($woo_products_shlinkdetail == 'show'): ?>
                                                <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
                                                    <!-- Change class if you use font-awesome version >=5 -->
                                                    <i class="fas fa-camera-retro"></i>
                                                    <span class="tz_view_detail">View Detail</span>
                                                </a>
                                            <?php
                                            endif;
                                            ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>
                            <div class="wpe__content">
                                <?php
                                if ($woo_products_shtitle == 'show'):
                                    ?>
                                    <h3 class="wpe__title">
                                        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
                                            <?php the_title(); ?>
                                        </a>
                                    </h3>
                                <?php
                                endif;
                                if ($woo_products_shrating == 'show'):
                                    ?>
                                    <div class="wpe__rating">
                                        <?php

                                        do_action('woo_get_rating');
                                        ?>
                                    </div>
                                <?php
                                endif;
                                if ($woo_products_shprice == 'show'):
                                    ?>
                                    <div class="wpe__price">
                                        <?php
                                        global $product;
                                        $price_html = $product->get_price_html();
                                        echo $price_html;
                                        ?>
                                    </div>
                                <?php
                                endif;
                                ?>
                            </div>
                        </div>
                    <?php
                    endwhile;
                } else {
                    echo esc_html__('No products found', 'tz-interiart');
                }
                wp_reset_postdata();
                ?>

            </div><!--end slider layout-->

        </div>


        <?php
        return ob_get_clean();
    }

    add_shortcode('vc-woo-products', 'create_vcwooproducts_shortcode');
endif;
