<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
// This file contains the code for product table


if (check_ajax_referer("rtwmer_mercado_check_nonce", 'rtwmer_nonce')) {
    if (isset($_POST) && !empty($_POST)) {
        global $wpdb;
        global $rtwmer_user_id_for_dashboard;
        $rtwmer_table = $wpdb->prefix . 'posts';
        $rtwmer_primaryKey = 'ID';
        $rtwmer_author_ID = $rtwmer_user_id_for_dashboard;
        if (isset($_POST['cond']) && !empty($_POST['cond'])) {
            $rtwmer_cond = isset($_POST['cond']) ? sanitize_text_field(wp_unslash($_POST['cond'])) : '';
        }
        if (isset($_POST["rtwmer_date"]) && $_POST["rtwmer_date"] != "-None-") {
            $rtwmer_time    = strtotime(sanitize_text_field(wp_unslash($_POST["rtwmer_date"])));
            $rtwmer_first_date = gmdate('Y-m-d', $rtwmer_time);
            $rtwmer_last_date  = gmdate('Y-m-t', $rtwmer_time);
        }
        if (isset($_POST["rtwmer_cat_filter"]) && $_POST["rtwmer_cat_filter"] != "Uncategorized") {
            $rtwmer_cat  = isset($_POST['rtwmer_cat_filter']) ? sanitize_text_field(wp_unslash($_POST['rtwmer_cat_filter'])) : '';
        }
        $rtwmer_columns = array(
            array('db' => 'ID', 'dt' => 0, 'field' => 'ID'),
            array('db' => '_thumbnail_id', 'dt' => 1, 'field' => '_thumbnail_id'),
            array('db' => 'post_title',  'dt' => 2, 'field' => 'post_title'),
            array('db' => 'post_status',   'dt' => 3, 'field' => 'post_status'),
            array('db' => '_sku',     'dt' => 4, 'field' => '_sku'),
            array('db' => '_stock_status',     'dt' => 5, 'field' => '_stock_status'),
            array('db' => '_sale_price',     'dt' => 6, 'field' => '_sale_price'),
            array('db' => '_regular_price',     'dt' => 7, 'field' => '_regular_price'),
            array('db' => 'post_excerpt',     'dt' => 8, 'field' => 'post_excerpt'),
            array('db' => 'rtwmer_product_view_count',     'dt' => 9, 'field' => 'rtwmer_product_view_count'),
            array('db' => 'post_date',     'dt' => 10, 'field' => 'post_date'),
        );
        $rtwmer_sql_details = array(
            'user' => DB_USER,
            'pass' => DB_PASSWORD,
            'db'   => DB_NAME,
            'host' => DB_HOST
        );
        $rtwmer_where = "`post_author`=" . $rtwmer_author_ID;
        $rtwmer_equals = "`post_type`='product'";
        $rtwmer_where .= ' AND ' . $rtwmer_equals;
        if (isset($rtwmer_cond)) {
            if ($rtwmer_cond == "all_prod") {
                $rtwmer_equal1 = "`post_status`='publish'";
                $rtwmer_equal2 = "`post_status`='pending'";
                $rtwmer_equal3 = "`post_status`='draft'";
                $rtwmer_equal4 = "`post_status`='private'";
                $rtwmer_where .= ' AND (' . $rtwmer_equal1 . ' OR ' . $rtwmer_equal2 . ' OR ' . $rtwmer_equal3 . ' OR ' . $rtwmer_equal4 . ')';
            } elseif ($rtwmer_cond == "pending_prod") {
                $rtwmer_pending = "`post_status`='pending'";
                $rtwmer_where .= ' AND ' . $rtwmer_pending;
            } elseif ($rtwmer_cond == "published_prod") {
                $rtwmer_publish = "`post_status`='publish'";
                $rtwmer_where .= ' AND ' . $rtwmer_publish;
            } elseif ($rtwmer_cond == "trash_prod") {
                $rtwmer_pending = "`post_status`='trash'";
                $rtwmer_where .= ' AND ' . $rtwmer_pending;
            }
        }
        $rtwmer_cat_query = false;
        if (isset($rtwmer_cat) && !empty($rtwmer_cat)) {
            $rtwmer_cat_query = true;
            $rtwmer_cat_condition = "wp_terms.`term_id` = '" . $rtwmer_cat . "'";
            $rtwmer_where .= ' AND (' . $rtwmer_cat_condition . ')';
        }
        if ((isset($rtwmer_first_date) && !empty($rtwmer_first_date)) && (isset($rtwmer_last_date) && !empty($rtwmer_last_date))) {
            $rtwmer_date = "`post_date`>'" . $rtwmer_first_date . "' AND `post_date`<'" . $rtwmer_last_date . "'";
            $rtwmer_where .= ' AND (' . $rtwmer_date . ')';
        }
        $rtwmer_posts = $wpdb->prefix . "posts";
        $rtwmer_postmeta = $wpdb->prefix . "postmeta";
        if (isset($_POST["rtwmer_filter"]) && sanitize_text_field(wp_unslash($_POST["rtwmer_filter"])) && $rtwmer_cat_query) {
            $rtwmer_join = "FROM " . $wpdb->prefix . "posts" . " LEFT JOIN (SELECT post_id,
        MAX(CASE WHEN meta_key = '_thumbnail_id' THEN meta_value END) _thumbnail_id,
        MAX(CASE WHEN meta_key = '_sku' THEN meta_value END) _sku,
        MAX(CASE WHEN meta_key = '_stock_status' THEN meta_value END) _stock_status,
        MAX(CASE WHEN meta_key = '_regular_price' THEN meta_value END) _regular_price,
        MAX(CASE WHEN meta_key = 'rtwmer_product_view_count' THEN meta_value END) rtwmer_product_view_count,
        MAX(CASE WHEN meta_key = '_sale_price' THEN meta_value END) _sale_price
        FROM " . $wpdb->prefix . "postmeta" . " GROUP BY post_id) rtw_selected_table ON " . $wpdb->prefix . "posts" . ".`ID`= rtw_selected_table.`post_id` JOIN " . $wpdb->prefix . "term_relationships" . " ON " . $wpdb->prefix . "posts" . ".`ID` = " . $wpdb->prefix . "term_relationships" . ".`object_id` JOIN (SELECT term_id,
        MAX(CASE WHEN taxonomy = 'product_cat' THEN term_id END) rtw_prod_cat,
        MAX(CASE WHEN taxonomy = 'product_type' THEN term_id END) rtw_prod_type
        FROM " . $wpdb->prefix . "term_taxonomy" . " GROUP BY term_id) rtw_prod_taxonomy ON " . $wpdb->prefix . "term_relationships" . ".`term_taxonomy_id` = rtw_prod_taxonomy.`term_id` JOIN " . $wpdb->prefix . "terms" . " ON rtw_prod_taxonomy.`term_id` = " . $wpdb->prefix . "terms" . ".`term_id` ";
        } else {
            $rtwmer_join = "FROM `$rtwmer_posts` LEFT JOIN (SELECT post_id, 
            MAX(CASE WHEN meta_key = '_sku' THEN meta_value END) _sku,
            MAX(CASE WHEN meta_key = '_stock_status' THEN meta_value END) _stock_status,
            MAX(CASE WHEN meta_key = '_regular_price' THEN meta_value END) _regular_price,
            MAX(CASE WHEN meta_key = '_sale_price' THEN meta_value END) _sale_price,
            MAX(CASE WHEN meta_key = 'rtwmer_product_view_count' THEN meta_value END) rtwmer_product_view_count,
            MAX(CASE WHEN meta_key = '_thumbnail_id' THEN meta_value END) _thumbnail_id
            FROM `$rtwmer_postmeta`
            GROUP BY `post_id`) rtwprod ON " . $rtwmer_posts . ".ID = rtwprod.post_id";
        }
        include_once(RTWMER_ADMIN_PARTIAL . '/ssp/ssp.customized.class.php');
        if (!empty($rtwmer_cond)) {
            $rtwmer_all_ssp   =    RTWMER_SSP::rtwmer_simple($_POST, $rtwmer_sql_details, $rtwmer_table,                                         $rtwmer_primaryKey, $rtwmer_columns, $rtwmer_join, $rtwmer_where);
            $rtwmer_i = 0;
            if (!empty($rtwmer_all_ssp['data'])) {
                foreach ($rtwmer_all_ssp['data'] as $rtwmer_data) {
                    $rtwmer_attach_id    =        $rtwmer_data[1];
                    $rtwmer_checkbox = "<div class='mdc-checkbox mdc-data-table__row-checkbox mdc-checkbox--upgraded mdc-ripple-upgraded mdc-ripple-upgraded--unbounded'>
                    <input type='checkbox' name='rtwmer_bulk_check' class='mdc-checkbox__native-control rtwmer_table_bulk_check' data-id='" . $rtwmer_data[0] . "' aria-labelledby='u0'>
                    <div class='mdc-checkbox__background'>
                    <svg class='mdc-checkbox__checkmark' viewBox='0 0 24 24'>
                        <path class='mdc-checkbox__checkmark-path' fill='none' d='M1.73,12.91 8.1,19.28 22.79,4.59'></path>
                    </svg><div class='mdc-checkbox__mixedmark'></div></div><div class='mdc-checkbox__ripple'></div></div>";

                    $rtwmer_product_title   =  $rtwmer_data[2] . "<div class='rtwmer_action'>";
                    if($this->rtwmer_user_can_access("rtwmer_delete_prod_cap")){
                    $rtwmer_product_title  .= "<div class='rtwmer_delete'><div class='rtwmer_tooltip rtwmer_delete_button' data-val='trash' data-id='" . $rtwmer_data[0] . "'><span class='material-icons'>delete</span>
                  <span class='rtwmer_tooltiptext rtwmer_prod_tool'>" . esc_html__('Delete', 'mercado') . "</span>
                </div></div>";
                    }

                if($this->rtwmer_user_can_access("rtwmer_edit_prod_cap")){
                $rtwmer_product_title .= "<div class='rtwmer_edit'><div class='rtwmer_tooltip rtwmer_edit_button' data-id='" . $rtwmer_data[0] . "'><span class='material-icons'>edit</span><span class='rtwmer_tooltiptext rtwmer_prod_tool'>" . esc_html__('Edit', 'mercado') . "</span>
                </div></div>";
                }
             
                    $rtwmer_product_title = apply_filters("rtwmer_product_title", $rtwmer_product_title, $rtwmer_data[0], $rtwmer_data[2]);
                    $rtwmer_product  =   wc_get_product($rtwmer_data[0]);
                   
                        
                    $rtwmer_price = $rtwmer_product->get_price();
                    $rtwmer_sale_price    =    $rtwmer_product->get_sale_price();

                    // if($rtwmer_product->is_type('variable')){
                    //     $rtwmer_var_min_price = $rtwmer_product->get_variation_price('min', true);
                    //     $rtwmer_var_max_price = $rtwmer_product->get_variation_price('max', true);
                    //     $rtwmer_main_price = $rtwmer_var_min_price ." - ". $rtwmer_var_max_price;

                    // }

                   

                    $rtwmer_reg_price    =    $rtwmer_product->get_regular_price();
                    if ((float) $rtwmer_price == (float) $rtwmer_sale_price) {
                        $rtwmer_sale_price =  $rtwmer_price;
                    } else if ((float) $rtwmer_price == (float) $rtwmer_reg_price) {
                        $rtwmer_sale_price = 0;
                    }
                    $rtwmer_reg_date    =    $rtwmer_data[10];
                    $rtwmer_views_count    =    $rtwmer_data[9];
                    if (empty($rtwmer_views_count)) {
                        $rtwmer_views_count = 0;
                    } else {
                        $rtwmer_views_count  =  intval($rtwmer_views_count);
                    }
                    $rtwmer_date    =    gmdate('d-m-Y', strtotime($rtwmer_reg_date));

                    if($rtwmer_product->is_type('variable')){
                        $rtwmer_var_min_price = $rtwmer_product->get_variation_price('min', true);
                        $rtwmer_var_max_price = $rtwmer_product->get_variation_price('max', true);
                        $rtwmer_main_price = wc_price($rtwmer_var_min_price) ." - ". wc_price($rtwmer_var_max_price);
                        // echo '<pre>';
                        // print_r($rtwmer_main_price);
                        // echo '</pre>';

                    }else{
                        if (!empty($rtwmer_sale_price)) {
                            $rtwmer_main_price    =        wc_price($rtwmer_sale_price);
    
                            if (!empty($rtwmer_reg_price)) {
                                $rtwmer_main_price    =    "<del>" . wc_price($rtwmer_reg_price) . "</del>" . wc_price($rtwmer_sale_price);
                            }
                        } elseif (!empty($rtwmer_reg_price)) {
                            $rtwmer_main_price    =    wc_price($rtwmer_reg_price);
                        } else {
                            $rtwmer_main_price    =    "-";
                        }
                    }
                    if($rtwmer_product->is_type('variable')){
                        $rtwmer_var_min_price = $rtwmer_product->get_variation_price('min', true);
                      
                        $rtwmer_saving_commission = $this->rtwmer_commission($rtwmer_user_id_for_dashboard, $rtwmer_var_min_price);
                        $rtwmer_saving_1    =    (float) $rtwmer_var_min_price - (float) $rtwmer_saving_commission[0];
                        // echo '<pre>';
                        // print_r($rtwmer_saving);
                        // echo '</pre>';
                        // die('asdffdafdsaf');

                        $rtwmer_var_max_price = $rtwmer_product->get_variation_price('max', true);

                        $rtwmer_saving_commission = $this->rtwmer_commission($rtwmer_user_id_for_dashboard, $rtwmer_var_max_price);
                        $rtwmer_saving_2   =    (float) $rtwmer_var_max_price - (float) $rtwmer_saving_commission[0];

                        $rtwmer_var_saving = wc_price(round($rtwmer_saving_1,2)) ." - ".wc_price(round($rtwmer_saving_2 , 2));

                    }else{
                        $rtwmer_saving_commission = $this->rtwmer_commission($rtwmer_user_id_for_dashboard, $rtwmer_price);
                        $rtwmer_saving    =    (float) $rtwmer_price - (float) $rtwmer_saving_commission[0];
                    }
                    
                    $rtwmer_saving_commission = $this->rtwmer_commission($rtwmer_user_id_for_dashboard, $rtwmer_price);
                    $rtwmer_saving    =    (float) $rtwmer_price - (float) $rtwmer_saving_commission[0];
                    $rtwmer_downloadable    =        get_post_meta($rtwmer_data[0], '_downloadable', TRUE);
                    $rtwmer_virtual    =    get_post_meta($rtwmer_data[0], '_virtual', TRUE);
                    if ($rtwmer_downloadable    ==    "yes") {
                        $rtwmer_type        =        "downloadable";
                    } elseif ($rtwmer_virtual    ==    "yes") {
                        $rtwmer_type    =    "virtual";
                    } else {
                        $rtwmer_product =     wc_get_product($rtwmer_data[0]);
                        $rtwmer_type     =    $rtwmer_product->get_type();
                    }
                    if ($rtwmer_data[3] == "publish") {
                        $rtwmer_status = "<span class='rtwmer_published'>" . $rtwmer_data[3] . "</span>";
                    } elseif ($rtwmer_data[3] == "pending") {
                        $rtwmer_status = "<span class='rtwmer_pending'>" . $rtwmer_data[3] . "</span>";
                    } elseif ($rtwmer_data[3] == "trash") {
                        $rtwmer_status = "<span class='rtwmer_product_trash'>" . $rtwmer_data[3] . "</span>";
                    } elseif ($rtwmer_data[3] == "draft") {
                        $rtwmer_status = "<span class='rtwmer_draft'>" . $rtwmer_data[3] . "</span>";
                    }
                    // $rtwmer_image    =    wp_get_attachment_image(intval($rtwmer_attach_id), array('100', '100'), "", array("class" => "rtwmer_product_img"));
                    $rtwmer_test = wp_get_attachment_image_src($rtwmer_attach_id);
                    $rtwmer_plugins_url = plugins_url();
                    $rtwmer_my_plugin_dir = $rtwmer_plugins_url . '/mercado';
                    if($rtwmer_test){
                        $rtwmer_image    =    wp_get_attachment_image(intval($rtwmer_attach_id), array('100', '100'), "", array("class" => "rtwmer_product_img"));
                    }else{
                        $rtwmer_src    =    $rtwmer_my_plugin_dir."/assets/images/image_not_available.jpg";
                      $rtwmer_image = '<img width="100" height="100" src="'.$rtwmer_src.'" class="rtwmer_product_img" alt="" decoding="async" loading="lazy" sizes="(max-width: 100px) 100vw, 100px">';
                    }
                    // echo '<pre>';
                    // print_r($rtwmer_attach_id);
                    // echo '</pre>';
                    // die('amma');
                    $rtwmer_all_ssp['data'][$rtwmer_i][0]    =    $rtwmer_checkbox;
                    $rtwmer_all_ssp['data'][$rtwmer_i][1]    =    $rtwmer_image;
                    $rtwmer_all_ssp['data'][$rtwmer_i][2]    =    $rtwmer_product_title;
                    $rtwmer_all_ssp['data'][$rtwmer_i][3]    =    $rtwmer_status;
                    $rtwmer_all_ssp['data'][$rtwmer_i][6]    =    $rtwmer_main_price;
                    if($rtwmer_product->is_type('variable')){
                        $rtwmer_all_ssp['data'][$rtwmer_i][7]    =    $rtwmer_var_saving;
                    }else{
                        $rtwmer_all_ssp['data'][$rtwmer_i][7]    =    wc_price(round($rtwmer_saving, 2));
                    }
                    $rtwmer_all_ssp['data'][$rtwmer_i][8]    =    $rtwmer_type;
                    $rtwmer_all_ssp['data'][$rtwmer_i][9]    =    $rtwmer_views_count;
                    $rtwmer_all_ssp['data'][$rtwmer_i][10]    =    $rtwmer_date;
                    $rtwmer_i++;
                }
            }
            echo json_encode($rtwmer_all_ssp);
        }
    }
}
wp_die();
