<?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;
	$db=new Database();
	$Connection=$db->Connect();
	$student=new Users($Connection);
	if($_SERVER["REQUEST_METHOD"] === "POST")
	{
		$data=json_decode(file_get_contents("php://input"));
		if(!empty($data->c_id) && !empty($data->r_id) && !empty($data->tag) )
		{
			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");
				}
    			$student->c_id=$data->c_id;
    			$query="SELECT * FROM `tbl_country` WHERE c_id= '$data->c_id' ";
    			$result = $Connection->query($query);
    			if ($result->num_rows > 0) {
    			    while ($rows=$result->fetch_assoc()) 
    			    {
    			      	$c_name=$rows['c_name'];
    			       	$c_id=$rows['c_id'];
    			        $c_code=$rows['c_code'];
    					$c_image=$rows['c_image'];
    			    }
    			}
    			$student->r_id=$data->r_id;
    			$que="SELECT * FROM `tbl_region` WHERE r_id= '$data->r_id' ";
    			$results = $Connection->query($que);
    			if ($results->num_rows > 0) {
    			    while ($row=$results->fetch_assoc()) 
    			    {
    			      	$r_name=$row['r_name'];
    			       	$r_id=$row['r_id'];
    			        $r_code=$row['r_code'];
    			    }
    			}
			$q="SELECT * FROM tbl_number WHERE status='active'  AND 
			tag= '$data->tag' ";

			$resultss = $Connection->query($q);
			if ($resultss->num_rows > 0) {
				$students['Records']=array();
			    while ($r=$resultss->fetch_assoc()) 
			    {
			      	array_push($students['Records'], array(
					'c_id'=>$c_id,
					'c_name'=>$c_name,
					'c_code'=>$c_code,
					'c_image'=>$c_image,
					'r_id'=>$r_id,
					'r_name'=>$r_name,
					'r_code'=>$r_code,
					
					'number'=> $r['numbers'],
					'tag'=>$r['tag'],
					'status'=>$r['status']
					));
			    }
			    returnResponse(SUCCESS_RESPONSE,$students['Records']);
			}


		}	
		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;
		}
?>