<?php 
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
//===========================This file is used to process order request==================//
use Automattic\WooCommerce\Utilities\OrderUtil;

    if( !check_ajax_referer('rtwmer-vendor-nonce','rtwmer_process_orders_nonce_verify') )
    {
        wp_die();
    }
    $rtwmer_process_order_request_id = isset($_POST['rtwmer_process_order_request_id'])?sanitize_text_field(wp_unslash($_POST['rtwmer_process_order_request_id'])):'';
    if( isset($rtwmer_process_order_request_id) && !empty($rtwmer_process_order_request_id) )
    {
       
   
        if( isset($_POST['rtwmer_process_order_request_value']) && !empty($_POST['rtwmer_process_order_request_value']) )
        {
            if( $_POST['rtwmer_process_order_request_value'] == 'send_payment' )
            {
               
                $rtwmer_children_post_args = array(
                    'post_parent'	=> 	$rtwmer_process_order_request_id,
                    'post_type'		=>	'shop_order',
                    'post_status'	=>	'-1'
                );
                $rtwmer_order_children = get_children( $rtwmer_children_post_args );
                
                if($rtwmer_order_children)
                {
                    $rtwmer_process_main_order = wc_get_order($rtwmer_process_order_request_id);
                    if( isset($rtwmer_process_main_order) && is_object($rtwmer_process_main_order) )
                    {
                        if($rtwmer_process_main_order->get_status() != 'completed')
                        {
                            echo json_encode(esc_html__('Order Status should be completed to Tranfer amount to vendors.','mercado'));
                            wp_die();
                        }
                        if($rtwmer_process_main_order->get_status() == 'completed')
                        {
                            foreach($rtwmer_order_children as $rtwmer_children)
                            {
                                if(isset($rtwmer_children->ID))
                                {
                                    $rtwmer_process_order = wc_get_order($rtwmer_children->ID);

                                    if( isset($rtwmer_process_order) && is_object($rtwmer_process_order) )
                                    {
                                        if($rtwmer_process_order->get_status() != 'completed')
                                        {
                                            echo json_encode(esc_html__('Each sub Order Status should be completed to Tranfer amount to vendors.','mercado'));
                                            wp_die();
                                        }
                                        if($rtwmer_process_order->get_status() == 'completed')
                                        {
                                            if( !empty(get_option('rtwmer_withdraw_option')) )
                                            {
                                                if(isset(get_option('rtwmer_withdraw_option')['rtwmer_withdraw_to_vendor']))
                                                {
                                                    if(get_option('rtwmer_withdraw_option')['rtwmer_withdraw_to_vendor'] == 'rtwmer_after_admin_approval')
                                                    {
                                                        $rtwmer_order_total = $rtwmer_process_order->get_total();
                                                        $rtwmer_order_total_main = $rtwmer_process_main_order->get_total();
                                                        $rtwmer_order_vendor_id = get_post_meta( $rtwmer_children->ID,'rtwmer_order_vendor',true );
            
                                                        if( isset ($rtwmer_order_vendor_id))
                                                        {
                                                            if( !empty(get_post_meta($rtwmer_children->ID,'rtwmer_vendor_payment_after_admin_approvale' )) )
                                                            {
                                                                if( isset($rtwmer_order_total_main) )
                                                                {
                                                                    if($rtwmer_order_total_main == 0)
                                                                    {
                                                                        echo json_encode(esc_html__('Amount Should be Greater than 0','mercado'));
                                                                        wp_die();
                                                                    }
                                                                }
                                                                if(get_post_meta($rtwmer_children->ID,'rtwmer_vendor_payment_after_admin_approvale',true ) == 'true')
                                                                {
                                                                    $rtwmer_response_for_already_transferred = esc_html__("Request are neglected whom you already assgined balance, else are approved.",'mercado');
                                                                }
                                                                else
                                                                {
                                                                    $rtwmer_admin_order_commision_for_vendor = get_post_meta($rtwmer_children->ID,'rtwmer_admin_order_commision_for_vendor',true );
                                                                    if( isset($rtwmer_admin_order_commision_for_vendor) )
                                                                    {
                                                                        do_action('rtwmer_send_order_balance_to_vendor');

                                                                        update_user_meta( $rtwmer_order_vendor_id,'rtwmer_total_amount',intval(get_user_meta($rtwmer_order_vendor_id,'rtwmer_total_amount',true)) + ($rtwmer_order_total - $rtwmer_admin_order_commision_for_vendor));
                                                                        update_post_meta($rtwmer_children->ID,'rtwmer_vendor_payment_after_admin_approvale','true' );
                                                                        $rtwmer_request_done = 1;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if( isset($rtwmer_response_for_already_transferred) && !empty($rtwmer_response_for_already_transferred) )
                    {
                        echo json_encode(2);
                    }
                    else
                    {
                        echo json_encode(1);
                    }
                    wp_die();
                }
                else
                {
                    
                    $rtwmer_process_order = wc_get_order($rtwmer_process_order_request_id);
                    
                    if( isset($rtwmer_process_order) && is_object($rtwmer_process_order) )
                    {
                        if($rtwmer_process_order->get_status() != 'completed')
                        {
                            echo json_encode(esc_html__('Order Status should be completed to Tranfer amount to vendor.','mercado'));
                            wp_die();
                        }
                        if($rtwmer_process_order->get_status() == 'completed')
                        {
                           
                            if( !empty(get_option('rtwmer_withdraw_option')) )
                            {
                               
                                if(isset(get_option('rtwmer_withdraw_option')['rtwmer_withdraw_to_vendor']))
                                {
                                    if(get_option('rtwmer_withdraw_option')['rtwmer_withdraw_to_vendor'] == 'rtwmer_after_admin_approval')
                                    {   
                                      
                                        $order = wc_get_order( $rtwmer_process_order_request_id);
                                        // $rtwmer_test = $order->get_meta('rtwmer_order_vendor',true);

                                        $rtwmer_order_total = $rtwmer_process_order->get_total();
                                        // $rtwmer_order_vendor_id = get_post_meta( sanitize_text_field(wp_unslash($rtwmer_process_order_request_id)),'rtwmer_order_vendor',true );
                                        $rtwmer_order_vendor_id = $order->get_meta('rtwmer_order_vendor',true);
                                      
                                        if( isset ($rtwmer_order_vendor_id))
                                        {
                                            $rtwmer_test = get_post_meta($rtwmer_process_order_request_id,'rtwmer_vendor_payment_after_admin_approvale' );
                                           
                                            
                                            if( !empty(get_post_meta($rtwmer_process_order_request_id,'rtwmer_vendor_payment_after_admin_approvale' )) )
                                            {
                                            
                                                if( isset($rtwmer_order_total) )
                                                {
                                                    if($rtwmer_order_total == 0)
                                                    {
                                                        echo json_encode(esc_html__('Amount Should be Greater than 0','mercado'));
                                                        wp_die();
                                                    }
                                                }
                                                if(get_post_meta($rtwmer_process_order_request_id,'rtwmer_vendor_payment_after_admin_approvale',true ) == 'true')
                                                {
                                                    echo json_encode(esc_html__('Amount Already Transferred for this order','mercado'));
                                                    wp_die();
                                                }
                                            }

                                            // $rtwmer_admin_order_commision_for_vendor = get_post_meta($_POST['rtwmer_process_order_request_id'],'rtwmer_admin_order_commision_for_vendor',true );
                                            $rtwmer_admin_order_commision_for_vendor = $order->get_meta('rtwmer_admin_order_commision_for_vendor',true);
                                            if( isset($rtwmer_admin_order_commision_for_vendor) )
                                            {
                                                do_action('rtwmer_send_order_balance_to_vendor');
                                                // print_r($rtwmer_order_vendor_id);  
                                                // $rtwmer_test =  intval(get_user_meta($rtwmer_order_vendor_id,'rtwmer_total_amount',true)) + ($rtwmer_order_total - $rtwmer_admin_order_commision_for_vendor);

                                                update_user_meta( $rtwmer_order_vendor_id,'rtwmer_total_amount',intval(get_user_meta($rtwmer_order_vendor_id,'rtwmer_total_amount',true)) + ($rtwmer_order_total - $rtwmer_admin_order_commision_for_vendor));
                                                update_post_meta($rtwmer_process_order_request_id,'rtwmer_vendor_payment_after_admin_approvale','true' );
                                                echo json_encode(1);
                                                wp_die();
                                            }
                                        }
                                    }
                                }
                            }
                            wp_die();
                        }
                    }
                }
            }
      

            $rtwmer_children_post_args = array(
                'post_parent'	=> 	$rtwmer_process_order_request_id,
                'post_type'		=>	'shop_order',
                'post_status'	=>	'-1'
            );

            
            $rtwmer_order_children = get_children( $rtwmer_children_post_args );
           
            if( $rtwmer_order_children )
            {
               
                foreach($rtwmer_order_children as $rtwmer_children)
                {
                    if(isset($rtwmer_children->ID))
                    {
                        if( $_POST['rtwmer_process_order_request_value'] == 'trash' )
                        {
                           
                            
                            do_action('rtwmer_send_order_to_trash');
                            wp_trash_post( $rtwmer_children->ID );
                            wp_trash_post( $rtwmer_process_order_request_id );
                            $rtwmer_order_vendor_id = get_post_meta( $rtwmer_children->ID[0],'rtwmer_order_vendor',true );
    
                            if( isset($rtwmer_order_vendor_id) && !empty($rtwmer_order_vendor_id) )
                            {
                                echo json_encode($rtwmer_order_vendor_id);
                            }
                        }
                        if( $_POST['rtwmer_process_order_request_value'] == 'restore' )
                        {
                            do_action('rtwmer_send_order_to_restore');
                            wp_untrash_post( $rtwmer_children->ID );
                            wp_untrash_post( $rtwmer_process_order_request_id );
                            $rtwmer_order_vendor_id = get_post_meta( $rtwmer_children->ID[0],'rtwmer_order_vendor',true );
                            
                            // $order = new WC_Order( $_POST['rtwmer_process_order_request_id'] );
                            // $rtwmer_test = $order->update_status( 'trash' );
                           

                            if( isset($rtwmer_order_vendor_id) && !empty($rtwmer_order_vendor_id) )
                            {
                                echo json_encode($rtwmer_order_vendor_id);
                            }
                        }
                        if( $_POST['rtwmer_process_order_request_value'] == 'delete' )
                        {
                           
                            do_action('rtwmer_send_order_to_delete');
                            $rtwmer_order_vendor_id = get_post_meta( $rtwmer_children->ID[0],'rtwmer_order_vendor',true );
                            if( isset($rtwmer_order_vendor_id) && !empty($rtwmer_order_vendor_id) )
                            {
                                echo json_encode($rtwmer_order_vendor_id);
                            }
                            wp_delete_post( $rtwmer_children->ID );
                            wp_delete_post( $rtwmer_process_order_request_id );
                        }
                    }
                }
                wp_die();
            }
            else
            {
                if( $_POST['rtwmer_process_order_request_value'] == 'trash' )
                {
                    
                   
                    do_action('rtwmer_send_order_to_trash');
                    wp_trash_post( $rtwmer_process_order_request_id );
                    $rtwmer_order_vendor_id = get_post_meta( $rtwmer_process_order_request_id,'rtwmer_order_vendor',true );
                    
                    $rtwmer_id = isset($rtwmer_process_order_request_id) ? $rtwmer_process_order_request_id : '';

                    $order = new WC_Order( $rtwmer_id );

                    global $wpdb;
                    $status = $order->get_status();
                    $rtwmer_id = isset( $_POST['rtwmer_process_order_request_id'] )? absint( wp_unslash( $_POST['rtwmer_process_order_request_id'] ) ): 0;
                    // $rtwmer_check = $wpdb->get_results ( "SELECT * FROM ".$wpdb->prefix."wc_orders_meta WHERE `order_id` = '$rtwmer_id' AND `meta_key` = '_wp_trash_meta_status'" );// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared 
                    $rtwmer_check = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wc_orders_meta WHERE order_id = %d AND meta_key = %s", $rtwmer_id, '_wp_trash_meta_status' ) );


                    if($rtwmer_check){
                        $wpdb->update($wpdb->prefix.'wc_orders_meta', array('meta_value'=> "wc-".$status), array('order_id' => $rtwmer_id ,'meta_key'=> '_wp_trash_meta_status'));
                    }else{
                        $wpdb->insert($wpdb->prefix.'wc_orders_meta', array(
                            'order_id' => $rtwmer_id,
                            'meta_key' => '_wp_trash_meta_status',
                            'meta_value' => "wc-".$status
                        ));
                    }
                    
                    $rtwmer_test = $order->update_status( 'trash' );
                    $rtwmer_id = isset( $_POST['rtwmer_process_order_request_id'] )? absint( wp_unslash( $_POST['rtwmer_process_order_request_id'] ) ): 0;
                    // $rtwmer_result = $wpdb->get_results ( "SELECT * FROM ".$wpdb->prefix."wc_orders_meta WHERE `order_id` = '$rtwmer_id' AND `meta_key` = 'rtwmer_order_vendor'" );// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared  
                    $rtwmer_result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wc_orders_meta WHERE order_id = %d AND meta_key = %s", $rtwmer_id, 'rtwmer_order_vendor' ) );

            
                    if( isset($rtwmer_result[0]) && !empty($rtwmer_result[0]) )
                    {
                        echo json_encode($rtwmer_result[0]);
                        wp_die();
                    }
                }
                if( $_POST['rtwmer_process_order_request_value'] == 'restore' )
                {
                    do_action('rtwmer_send_order_to_restore');
                    wp_untrash_post( $rtwmer_process_order_request_id );
                    $rtwmer_order_vendor_id = get_post_meta( $rtwmer_process_order_request_id,'rtwmer_order_vendor',true );

                    $rtwmer_id = isset($rtwmer_process_order_request_id) ? $rtwmer_process_order_request_id : '';
                    $order =  wc_get_order( $rtwmer_id );
                    $order->get_meta('rtwmer_order_vendor');
                    global $wpdb;
                    

                   
                    $rtwmer_id = isset( $_POST['rtwmer_process_order_request_id'] )? absint( wp_unslash( $_POST['rtwmer_process_order_request_id'] ) ): 0;
                    // $rtwmer_check = $wpdb->get_results ( "SELECT * FROM ".$wpdb->prefix."wc_orders_meta WHERE `order_id` = '$rtwmer_id' AND `meta_key` = '_wp_trash_meta_status'" );// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared 
                    $rtwmer_check = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wc_orders_meta WHERE order_id = %d AND meta_key = %s", $rtwmer_id, '_wp_trash_meta_status' ) );

                    $status = $rtwmer_check[0]->meta_value;


                    $rtwmer_order_vendor_id = $order->get_meta('rtwmer_order_vendor');

                    if($rtwmer_check){
                        $wpdb->update($wpdb->prefix.'wc_orders', array('status'=> $status), array('id' => $rtwmer_id));
                        echo json_encode($rtwmer_order_vendor_id);
                        wp_die();
                    }

                    // if( isset($rtwmer_order_vendor_id) && !empty($rtwmer_order_vendor_id) )
                    // {
                    //     echo json_encode($rtwmer_order_vendor_id);
                    //     wp_die();
                    // }
                }
                if( $_POST['rtwmer_process_order_request_value'] == 'delete' )
                {
                            // echo '<pre>';
                            // print_r($_POST);
                            // echo '</pre>';
                            // die('aaaaaaaaa');
                    do_action('rtwmer_send_order_to_delete');
                 
                    $rtwmer_id = isset($rtwmer_process_order_request_id) ? $rtwmer_process_order_request_id : '';
                    // ".$wpdb->prefix."
                    global $wpdb;
                    $rtwmer_id = isset( $_POST['rtwmer_process_order_request_id'] )? absint( wp_unslash( $_POST['rtwmer_process_order_request_id'] ) ): 0;
                    if(OrderUtil::custom_orders_table_usage_is_enabled()){ 
                        // $rtwmer_order_vendor_id = $wpdb->get_results ( "SELECT * FROM ".$wpdb->prefix."wc_orders_meta WHERE `order_id` = '$rtwmer_id' AND `meta_key` = 'rtwmer_order_vendor'" );// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared 
                        $rtwmer_order_vendor_id = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wc_orders_meta WHERE order_id = %d AND meta_key = %s", $rtwmer_id, 'rtwmer_order_vendor' ) );

                        $rtwmer_order_vendor_id = $rtwmer_order_vendor_id[0];
                        $rtwmer_table1 = $wpdb->prefix.'wc_orders';
                        $rtwmer_table = $wpdb->prefix.'wc_orders_meta';
                        $wpdb->delete( $rtwmer_table, array( 'order_id' => $rtwmer_id ) );
                        $wpdb->delete( $rtwmer_table1, array( 'id' => $rtwmer_id ) );
                        echo json_encode($rtwmer_order_vendor_id);
                     
                        wp_die();
                    }else{
                        // $rtwmer_order_vendor_id = $wpdb->get_results ( "SELECT * FROM wp_postmeta WHERE `post_id` = '$rtwmer_id' AND `meta_key` = 'rtwmer_order_vendor'" );// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared 
                        $rtwmer_order_vendor_id = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s", $rtwmer_id, 'rtwmer_order_vendor' ) );

                          
                        // $rtwmer_order_vendor_id = $rtwmer_order_vendor_id[0];
                        $rtwmer_order_vendor_id = get_post_meta( $rtwmer_process_order_request_id,'rtwmer_order_vendor',true );
                      
                        if( isset($rtwmer_order_vendor_id) && !empty($rtwmer_order_vendor_id) )
                        {
                        wp_delete_post( $rtwmer_process_order_request_id );
                        echo json_encode($rtwmer_order_vendor_id);
                        }
                    }

                    // if( isset($rtwmer_order_vendor_id) && !empty($rtwmer_order_vendor_id) )
                    // {
                    //     echo json_encode($rtwmer_order_vendor_id);

                    // }
                   
                    wp_die();
                }
                // echo '<pre>';
                // print_r('hhhsdfh');
                // echo '</pre>';
                // die('fjffjsdf');
                echo json_encode(1);
                wp_die();
            }
        }
    }

?>