<?php

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Http;

if (! function_exists('post_request_with_auth')) {
    function post_request_with_auth($func_url,$data = array()) {
        $url = config('app.SAP_REST_BASE_URL').$func_url;
        $credentials = base64_encode(config('app.SAP_REST_USERNAME').':'.config('app.SAP_REST_PASSWORD'));
        $headers = [
            'Content-Type' => 'application/json',
            'Authorization'=>'Basic UElDT05ORUNUOldjc3Q1NDMyMQ=='
            // 'Authorization' => 'Basic '.$credentials,
        ];
        $client = new Client([
            'headers' => $headers
        ]);
        $encode_data = json_encode($data);
        $r = $client->request('POST', $url, [
            'body' => $encode_data
        ]);
		if($r->getStatusCode()){
			if($r->getStatusCode()==200){
				$response = $r->getBody()->getContents();
				$result = json_decode($response);
				if(!empty($result->Response)){
					$status = $result->Response->Flag;
					if($status=='S'){
						return array('status'=>true,'message'=>'Success','data'=>$result->Response);
					}else{
						return array('status'=>false,'message'=>'Failed','data'=>$result->Response);
					}
				}else{
					return array('status'=>false,'message'=>'Failed','data'=>array());
				}
			}else{
				return array('status'=>false,'message'=>'Something went wrong','data'=>array());
			}
		}
    }
}
if (! function_exists('post_request')) {
    function post_request($func_url,$data = array()) {
        $url = config('app.SAP_REST_BASE_URL').$func_url;
        $headers = [
            'Content-Type' => 'application/json',
            'Authorization'=>'Basic UElDT05ORUNUOldjc3Q1NDMyMQ=='
        ];
        $client = new Client([
            'headers' => $headers
        ]);
        $encode_data = json_encode($data);
        $r = $client->request('POST', $url, [
            'body' => $encode_data
        ]);
		if($r->getStatusCode()){
			if($r->getStatusCode()==200){
				$response = $r->getBody()->getContents(); 
				$result = json_decode($response);
				if(!empty($result->response)){
					$status = $result->response->flag;
					if($status=='S' || $status == 'Success'){
						return array('status'=>true,'message'=>'Success','data'=>$result->response);
					}else{
						return array('status'=>false,'message'=>'Failed','data'=>$result->response);
					}
				}else{
					return array('status'=>false,'message'=>'Failed','data'=>array());
				}
			}else{
				return array('status'=>false,'message'=>'Something went wrong','data'=>array());
			}
		}
    }
}
if (! function_exists('post_request_pl')) {
    function post_request_pl($func_url,$data = array()) {
        $url = config('app.SAP_REST_BASE_URL').$func_url;
        $headers = [
            'Content-Type' => 'application/json',
            'Authorization'=>'Basic UElDT05ORUNUOldjc3Q1NDMyMQ=='
        ];
        $client = new Client([
            'headers' => $headers
        ]);
        $encode_data = json_encode($data);
        $r = $client->request('POST', $url, [
            'body' => $encode_data
        ]);
        if($r->getStatusCode()==200){
            $json_response = $r->getBody()->getContents();
            $response_array=json_decode($json_response,true);

            // $length = count($response_array['response']);
            // $count = 0;
            // $string = "array[";
            // foreach($response_array['response'] as $arr){ $count++;
            //     $array = implode("``",array_slice($arr,0,5));
            //     //$array = implode("``",$arr);
            //     $string .= str_replace('"', "'", "array".json_encode(explode("``",$array)));
            //     if($count<$length){$string .=',';}
            // }
            // $string .= "]";

            //return $response_array['response'];

            //$myJSON = json_encode($response_array['response']);

            // foreach($response_array as $j){
            //     $main_data = json_encode($j);
            // }
           //$string = "'". $myJSON."'";

            //return $string;
            return $response_array;
        }else{
            return 'Something went wrong';
        }
    }
}
if (! function_exists('get_request')) {
    function get_request($func_url,$data = array()) {
        $url = config('app.SAP_REST_BASE_URL').$func_url;
        $headers = [
            'Content-Type' => 'application/json',
            'Authorization'=>'Basic UElDT05ORUNUOldjc3Q1NDMyMQ=='
        ];
        $client = new Client([
            'headers' => $headers
        ]);
        $encode_data = json_encode($data);
        $r = $client->request('POST', $url, [
            'body' => $encode_data
        ]);
        
        if($r->getStatusCode()==200){
            $response = $r->getBody()->getContents();
            $result = json_decode($response);
            //print_r($result);exit;
            if(!empty($result->response)){
                return array('status'=>true,'message'=>'Success','data'=>$response);
            }else{
                return array('status'=>true,'message'=>'No data found','data'=>array());
            }
        }else{
            return array('status'=>false,'message'=>'Something went wrong','data'=>array());
        }
    }
}
if (! function_exists('get_request_test')) {
    function get_request_test($func_url,$data = array()) {
        try{
			$url = config('app.SAP_REST_BASE_URL').$func_url;
			$headers = [
				'Content-Type' => 'application/json',
                'Authorization'=>'Basic UElDT05ORUNUOldjc3Q1NDMyMQ=='
			];
			$client = new Client([
				'headers' => $headers
			]);
			$encode_data = json_encode($data);
			$r = $client->request('POST', $url, [
				'body' => $encode_data
			]);
		}
		catch(\Exception $e){
			$msg = $e;
			return $msg;
            //return array('status'=>false,'message'=>$msg,'data'=>array());
            // exit;
		}
	
        if($r->getStatusCode()==200){
            $response = $r->getBody()->getContents();
            $result = json_decode($response);
            //print_r($result);exit;
            if(!empty($result->response)){
                return array('status_code'=>$r->getStatusCode(),'status'=>true,'message'=>'Success','data'=>$response);
            }else{
                return array('status_code'=>$r->getStatusCode(),'status'=>true,'message'=>'No data found','data'=>array());
            }
        }else{
            return array('status_code'=>$r->getStatusCode(),'status'=>false,'message'=>'Something went wrong','data'=>array());
        }
    }
}
if (! function_exists('http_request')) {
    function http_request($func_url,$data = array()) {
        $url = config('app.SAP_REST_BASE_URL').$func_url;
        // $headers = ['Content-Type' => 'application/json; charset=utf-8'];
        $headers = [
            'Content-Type' => 'application/json',
            'Authorization'=>'Basic UElDT05ORUNUOldjc3Q1NDMyMQ=='
        ];
        try {
            $response = Http::withHeaders($headers)->retry(3, 1000)->post($url, $data);
			// Determine if the status code was >= 400...
            $c1 = $response->failed();

            // Determine if the response has a 400 level status code...
            $c2 = $response->clientError();

            // Determine if the response has a 500 level status code...
            $c3 = $response->serverError();

            if ($c1 or $c2 or $c3)
                $response->throw();
        } catch (\Exception $e) {
            $msg = $e;
            return array('status'=>false,'message'=>$msg,'data'=>array());
            exit;
        }

        if($response->status()==200){
            $response = $response->body();
            $result = json_decode($response);
            //print_r($result);exit;
            if(!empty($result->response)){
                return array('status'=>true,'message'=>'Success','data'=>$response);
            }else{
                return array('status'=>true,'message'=>'No data found','data'=>array());
            }
        }
        else if($response->status()==500){
            return array('status'=>false,'message'=>'500 Internal Server Error','data'=>array());
        }else{
            return array('status'=>false,'message'=>'Something went wrong','data'=>array());
        }
    }
}
?>