<?php

defined('ABSPATH') or die("No direct script access!");

define('QCLD_EXPRESS_TEMPLATES', QCLD_EXPRESS_FILE_DIR. "/templates");
//For all list elements
add_shortcode('qcld_express_shop', 'qcld_express_all_products_shortcode');

function qcld_express_all_products_shortcode( $atts = array() ){
    ob_start();

    extract( shortcode_atts( array(
        'style'                     => '',
        'category_ids'              => '',
        'audio_feature'             => '',
        'video_feature'             => '',
        'link_title_to_modal'       => '0',
        'author'                    => ''
    ), $atts ) );
    //Getting option tree from backend.
    $qcld_express_options           = get_option('qcld_express_options');

    $qcld_express_shortcode_preview = isset($qcld_express_options['qcld_express_shortcode_preview']) ? $qcld_express_options['qcld_express_shortcode_preview'] : '';

    if($qcld_express_shortcode_preview != 'on'){

        if (is_admin()) {
            return;
        }

    }


    $qcld_express_show_mode         = $qcld_express_options['qcld_express_show_mode'];
    $qcld_express_sku               = $qcld_express_options['qcld_express_sku'];
    $qcld_express_thumbnail         = $qcld_express_options['qcld_express_thumbnail'];
    $qcld_express_title             = $qcld_express_options['qcld_express_title'];
    $qcld_express_description       = $qcld_express_options['qcld_express_description'];
    $qcld_express_price             = $qcld_express_options['qcld_express_price'];
    $qcld_express_stock             = $qcld_express_options['qcld_express_stock'];
    $qcld_express_action            = $qcld_express_options['qcld_express_action'];
    $qcld_express_background        = $qcld_express_options['qcld_express_background'];
    $qcld_express_altern            = $qcld_express_options['qcld_express_altern'];
    $qcld_express_custom_style      = $qcld_express_options['qcld_express_custom_style'];
    $qcld_express_cart_mode         = $qcld_express_options['qcld_express_cart_mode'];

    // Language Change option ... 
    $qcld_express_lang_sku          = $qcld_express_options['qcld_express_lang_sku'];
    $qcld_express_lang_thumbnail    = $qcld_express_options['qcld_express_lang_thumbnail'];
    $qcld_express_lang_title        = $qcld_express_options['qcld_express_lang_title'];
    $qcld_express_lang_desc         = $qcld_express_options['qcld_express_lang_desc'];
    $qcld_express_lang_availability = $qcld_express_options['qcld_express_lang_availability'];
    $qcld_express_lang_price        = $qcld_express_options['qcld_express_lang_price'];
    $qcld_express_lang_action       = $qcld_express_options['qcld_express_lang_action'];
    $qcld_express_lang_search       = $qcld_express_options['qcld_express_lang_search'];
    $qcld_express_lang_cart_summary = $qcld_express_options['qcld_express_lang_cart_summary'];
    $qcld_express_lang_edit_on_cart = $qcld_express_options['qcld_express_lang_edit_on_cart'];
    $qcld_express_lang_checkout     = $qcld_express_options['qcld_express_lang_checkout'];
    $qcld_express_lang_no_products  = $qcld_express_options['qcld_express_lang_no_products'];

    
    if(isset($qcld_express_options['qcld_express_lang_load_more']) && $qcld_express_options['qcld_express_lang_load_more'] !=''){
    $qcld_express_lang_load_more    = $qcld_express_options['qcld_express_lang_load_more'];
    }else{
    $qcld_express_lang_load_more    = esc_html( 'Load More', 'express-shop' );
    }
    if(isset($qcld_express_options['qcld_express_lang_load_more_scrolling']) && $qcld_express_options['qcld_express_lang_load_more_scrolling'] !=''){
    $qcld_express_lang_load_more_scrolling    = $qcld_express_options['qcld_express_lang_load_more_scrolling'];
    }else{
    $qcld_express_lang_load_more_scrolling    = esc_html( 'Scrolling', 'express-shop' );
    }

    $qcld_express_product_sorting   = $qcld_express_options['qcld_express_product_sorting'];
    $qcld_express_product_asc_desc  = $qcld_express_options['qcld_express_product_asc_desc'];

    if(isset($qcld_express_options['qcld_express_category'])){
        $qcld_express_categories=$qcld_express_options['qcld_express_category'];
		if(empty($qcld_express_categories)){
            $qcld_express_categories=array();
        }
    }else{
        $qcld_express_categories=array();
        
    }
    $qcld_express_template = $qcld_express_options['qcld_express_template'];

    if($qcld_express_template !== 'basic'){
        $qcld_express_template='basic';
    }


    if(isset($qcld_express_options['qcld_express_product_per_page'])) {
       // $qcld_express_product_per_page = 15;
        $qcld_express_product_per_page = $qcld_express_options['qcld_express_product_per_page'];
    }else{
        $qcld_express_product_per_page =-1;
    }
    if(isset($qcld_express_options['qcld_express_stock_alert'])){
        $qcld_express_stock_alert=$qcld_express_options['qcld_express_stock_alert'];
    }
    if(isset($qcld_express_options['qcld_express_filter'])){
        $qcld_express_filter=$qcld_express_options['qcld_express_filter'];
    }
    //Filttering Per page based on the show mode
    if($qcld_express_show_mode=='more'){
        $offset = 0;
        $per_page = $qcld_express_product_per_page;
    }else{
        $offset = 0;
        $per_page = -1;
    }

    if(!empty($author)) {
        $dokan_get_current_user_id = $author;
    }else{
        $dokan_get_current_user_id = '';
    }

    //Now filtering the categories to pass to query.
    $terms = get_terms( array(
        'taxonomy' => 'product_cat',
        'hide_empty' => false,
    ) );
    $categories=array();
    foreach ($terms as $term){
        if(!in_array($term->term_id, $qcld_express_categories)){
            array_push($categories,$term->term_id);
        }
    }
    //Filter categories
    $filter_categories=array();
    foreach ($terms as $term){
        if(!in_array($term->term_id, $qcld_express_categories)){

            array_push( $filter_categories, array('cat_name'=>$term->slug, 'cat_id'=>$term->term_id, 'category_name'=>$term->name, 'category_description'=>$term->description));
        }
    }


    // Get WooCommerce Global from Qcld_woo_inc class.
    global $woocommerce;

     /***********Query condition start from here *********/
    $params=array();
    if($_GET){
        $params = array(
            'posts_per_page'    =>-1,
            'post_type'         => 'product',
            'meta_query'        => array(
            'relation'          => 'OR',
            )
        );
        if(isset($_GET['price_min']) || isset($_GET['price_max'])|| isset($_GET['sku']) || isset($_GET['title']) ){
            $price_min = '';
            $price_max = '';
            //$sku = sanitize_text_field($_GET['sku']);
            $title =sanitize_text_field($_GET['title']);

            if ($title != '') {
                $titleArr = array('s' => $title);
                $params = array_merge($params, $titleArr);
            }

            if ($price_min != '' || $price_max != '') {
                $priceArr = array(array(
                    'key' => '_price',
                    'value' => array($price_min, $price_max),
                    'compare' => 'BETWEEN',
                    'type' => 'NUMERIC'
                ));

                $params = array_merge($params, array('meta_query' => $priceArr));
            }

        }
        if(isset($_GET['product_list_order'])){
               $list_order=sanitize_text_field($_GET['product_list_order']);
            //Sku sorting
            if($list_order=='sku_DESC' || $list_order=='sku_ASC'){
                $sku_url=explode("_",$list_order);
                $sku_sort=array('order' =>  $sku_url[1],
                    'orderby' => 'sku');
                $params=array_merge($params,$sku_sort);
            }
            //Title sorting
            if($list_order=='title_DESC'){
                $title_url=explode("_",$list_order);
                $title_sort=array('order' =>  $title_url[1],'orderby' => array('menu_order' => 'DESC', 'title' => 'DESC'));
                $params=array_merge($params,$title_sort);
            }
            if($list_order=='title_ASC'){
                $title_url=explode("_",$list_order);
                $title_sort=array('order' =>  $title_url[1],
                    'orderby' => array('menu_order' => 'ASC', 'title' => 'ASC'));
                $params=array_merge($params,$title_sort);
            }
            //Price sorting
            if($list_order=='price_DESC' || $list_order=='price_ASC'){
                $price_url=explode("_",$list_order);
                $price_sort=array(
                    'orderby'   => 'meta_value_num',
                    'meta_key'  => '_price',
                    'order' => $price_url[1]
                );
                $params=array_merge($params,$price_sort);
            }
        }

    } else{

        $terms = get_terms( array(
            'taxonomy' => 'product_cat',
            'hide_empty' => false,
            ) 
        );

        $categories=array();
        foreach ($terms as $term){
            if(!in_array($term->term_id, $qcld_express_categories)){
                array_push($categories,$term->term_id);
            }
        }
        
        // Create query arguments array
        $params = array(
            'post_type'         => 'product',
            'post_status'       => 'publish',
            'posts_per_page'    => $per_page,
            'offset'            => $offset,
            'author'            => $dokan_get_current_user_id,
            'orderby'           => $qcld_express_product_sorting,
            'order'             => $qcld_express_product_asc_desc,
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id',
                    'terms'    => $categories,
                ),
            ),
        );

        // Add meta_query to query args
        $params['meta_query'] = array();

        // Check products stock status
        $params['meta_query'][] = $woocommerce->query->stock_status_meta_query();

        /***********Query condition start from here *********/
        // Create a new query

    }

    $product_query = new WP_Query($params);

    //Handlling Template.
    if(file_exists(QCLD_EXPRESS_TEMPLATES . "/$qcld_express_template/template.php")){
        require (QCLD_EXPRESS_TEMPLATES . "/$qcld_express_template/template.php" );
        require (QCLD_EXPRESS_TEMPLATES . "/qcld-express-modal.php" );
        $clean = ob_get_clean();
        return $clean;
    }else{
        //Template Not Exists.
        // echo 'Oops! Template File Not Exists!';
        esc_html_e( 'Oops! Template File Not Exists!', 'express-shop' );
    }


}