<?php 
/**
 * woo order
 * action: st-proqyz-license-order-change
 */
add_action('wp_ajax_st-proqyz-license-order-change', 'st_proqyz_license_order_change_callback');
function st_proqyz_license_order_change_callback() {
    if(isset($_POST['order_id']) && !empty($_POST['order_id'])) {
        if(isset($_POST['status']) && !empty($_POST['status'])) {
            $order_id       = $_POST['order_id'];
            $order_status   = $_POST['status'];

            if(function_exists('wc_get_order')) {
                $order = wc_get_order( $order_id );
                $user_id = $order->get_user_id();
                
                # get remote id
                $remote_order_id = get_user_meta($user_id, '_spacetree_licence_order-remote_id-'.$order_id, true);
                if( !$remote_order_id) {
                    wp_send_json([
                        "error" => (object) [
                            "message"   => "Remote order id not found"
                        ]
                    ]);
                }
                
                if( !class_exists('ST_License_OAuth') ) {
                    wp_send_json([
                        "error" => (object) [
                            "message"   => "rest api OAuth class not found"
                        ]
                    ]);
                }

                $remote_response = (object) ST_License_OAuth::rest_root_post_request("/root/customer/order/".$order_status, (object) [
                    "order_id"  => $remote_order_id
                ]);

                if ( isset($remote_response->success) ) {
                    $remote_success = (object) $remote_response->success;
                    update_user_meta($user_id, '_spacetree_licence_order-status-'.$order_id, $order_status);
                    wp_send_json([
                        "success" => (object) [
                            "message"   => "Order status changed",
                            "res_type"  => "success"
                        ]
                    ]);
                } else {
                    wp_send_json([
                        "error" => (object) isset($remote_response->error)? $remote_response->error :  (object) [
                            "message"   => "Invalid response from remote server",
                            "res_type"  => "error"
                        ]
                    ]);
                    
                }
                

            } else {
                wp_send_json([
                    "error" => (object) [
                        "message"   => "Woocommerce order function not found",
                        "res_type"  => "warning"
                    ]
                ]);
            }


        } else {
            wp_send_json([
                "error" => (object) [
                    "message"   => "Missing order status",
                    "res_type"  => "warning"
                ]
            ]);
        }
    } else {
        wp_send_json([
            "error" => (object) [
                "message"   => "Missing order id",
                "res_type"  => "warning"
            ]
        ]);
    }
}