<?php 
	ini_set("display_errors", 1);
	header("Acces-Control-Allow-Origin: *");
	header("Content-type: application/json; charset=UTF-8");
	header("Acces-Control-Allow-Method: POST");
	require_once("../Database/Database.php");
	require_once("../Classes/USERS.php");
	require_once("../Constants/Constants_Variable.php");
	include_once ("../JWT/autoload.php");
	include_once ("../JWT/JWT.php");
	include_once ("../JWT/BeforeValidException.php");
	include_once ("../JWT/ExpiredException.php");
	include_once ("../JWT/SignatureInvalidException.php");
	use \Firebase\JWT\JWT;


require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;

	$db=new Database();
	$Connection=$db->Connect();

	if($_SERVER["REQUEST_METHOD"] === "POST")
	{
	    	
		$data=json_decode(file_get_contents("php://input"));
		if(!empty($data->page) && !empty($data->limit) && !empty($data->c_id) 
			&& !empty($data->r_id) && !empty($data->tag) && !empty($data->fcm_token) )
		{
		    
		    try
			{
				$headers=getallheaders();
				if(array_key_exists('Authorization', $headers) && $headers['Authorization'] != '')
				{
					$token=$headers["Authorization"];
					$secret_key="waheedkhan786";
					$decoded_data=JWT::decode($token,$secret_key,array('HS256'));
				}
				else
				{
					throwError(ACCESS_TOKEN_ERROR,"Please add headers");
				}
                
              
                    $sid    = "AC0bd610910620e86c79b0fd36e0a7a677";
$token  = "aa8e63b8357782efab1492f0b2e61799";
$twilio = new Client($sid, $token);



        $sql1 = "SELECT * FROM tbl_country WHERE c_id='$data->c_id'";
        $result11   = mysqli_query($Connection, $sql1);
        $total_records11 = mysqli_fetch_assoc($result11);
        $created_at1=$total_records11['created_at'];
        			$local;
        		if($created_at1=="GB"){
        		    	$local = $twilio->availablePhoneNumbers($created_at1)
                ->mobile
                ->read(["smsEnabled" => True, "voiceEnabled" => True], 20);
        		}else{
        		    	$local = $twilio->availablePhoneNumbers($created_at1)
                ->local
                ->read(["smsEnabled" => True, "voiceEnabled" => True], 20);
        		}
        	
        		
                    $page=$data->page;
        			$limit=$data->limit;
        			$offset= 10;
        			$response      = array();
        				$response['error'] = false;
        					$students['data']=array();
foreach ($local as $record) {
    $priceItemBasic="4.99";
				$priceItemStandard="6.99";
				$priceItemPlatinum="39.99";
    
    	array_push($students['data'], array(
        					'c_id'=>$data->c_id,
        					'c_name'=>$total_records11['c_name'],
        					'c_code'=>$total_records11['c_code'],
        					'c_image'=>"",
        					'r_id'=>$data->r_id,
        					'r_name'=>"",
        					'r_code'=>"",
        					'no'=> $record->phoneNumber,
        					'tag'=>"",
                            'no_id'=>"98",
                            'ptag1'=>$priceItemBasic,
                            'ptag2'=>$priceItemStandard,
                            'ptag3'=>$priceItemPlatinum,
        					'status'=>""
        					));
}
	                        

        			if(count($local) == 0)
        				{
        					$response['error']     = true;
        					$response['error_msg'] = "empty record";
        					$response['number_data'] = $students['data'];
        				}else{
        				    $response['current_page'] = $page;
        					$response['total_pages'] = 1;
        					$response['total_records'] = 20;
        					$response['number_data'] = $students['data'];
        				}
                    
        	
        		
        			
    				echo json_encode($response);
                
              
			}
			catch(DomainException $ex)
			{
				throwError(TOKEN_UPDATE_ERROR,$ex->getMessage());
			}
			catch(UnexpectedValueException $ex)
			{
				throwError(UN_SUCCESS_RESPONSE,$ex->getMessage());
			}
			catch (InvalidArgumentException $e) 
			{
				throwError(ACCESS_TOKEN_ERROR,$e->getMessage());
			}	
		}
		else
		{
			throwError(UN_SUCCESS_RESPONSE,"All parameters are required.");
		}
	}

	else
	{
		throwError(ACCESS_DENIED_RESPONSE,"Access Denied!... Please check method.");
	}

	 function throwError($code,$message)
		{
			header("content-type:application/json");
			$errorMessage=json_encode(['error'=>['status'=>$code,'message'=>$message]]);
			echo $errorMessage;
			exit;
		}
	 function returnResponse($code,$data)
		{
			header("content-type:application/json");
			$errorMessage=json_encode(['response'=>['status'=>$code,'result'=>$data]]);
			echo $errorMessage;
			exit;
		}
?>