<?php
require "conn.php";
use Twilio\Rest\Client; 



$DriverToken     = bin2hex(random_bytes(64));
$DriverPhone     = $_POST["DriverPhone"];
$FirebaseDriverToken  = $_POST["FirebaseDriverToken"];



$res = "";

		$res = mysqli_query($con,"SELECT * FROM Drivers WHERE DriverPhone='$DriverPhone'");
		$test=0;
		$result = array();

		while($row = mysqli_fetch_assoc($res)){

		$result[] = $row;


		$test=1;

		}
		
		
		
		require_once 'Twilio/autoload.php'; 
                        
                        $sid    = "AC9816bfdf894ecc22c5e3dc20656105af"; 
                        $token  = "fec25ae0a898e82a2a4b0912c7857d70";
                        $twilio = new Client($sid, $token); 
 
                            $fourRandomDigit = mt_rand(1000,9999);

 
                        $message = $twilio->messages 
                             ->create($DriverPhone, // to 
                                array(  
                              "messagingServiceSid" => "MGd2c421dcb83af46f3b8a97907f9a7ea7",      
                              "body" => "Your Jibler code is $fourRandomDigit" 
                              ) 
                  ); 
		
	//	$fourRandomDigit = "1234";
		
		
		if($test==0){
			
				$sql="INSERT INTO Drivers (DriverPhone,FirebaseDriverToken,DriverToken,sentcode) VALUES ('$DriverPhone','$FirebaseDriverToken','$DriverToken','$fourRandomDigit');";
					
				if(mysqli_query($con,$sql))
				{
					
					
						$res = mysqli_query($con,"SELECT * FROM Drivers WHERE DriverPhone='$DriverPhone'");

					
					
				// 	    $message ="code sent please active account";
				// 		$success = true;
				// 		$status_code = 200;
				// 		$result = [];
						
						$result = array();
					while($row = mysqli_fetch_assoc($res)){

					$result[] = $row;
					$test=4;

					}

					if($test==4){

					


						$message ="code sent please active account";
						$success = true;
						$status_code = 200;
					//	$result = [];
						
						echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result[0],"message"=>$message));

					}
						
					//	echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result,"message"=>$message));
					
					
				}
			
			
		}else {
			
			$sql22="UPDATE Drivers SET FirebaseDriverToken='$FirebaseDriverToken',sentcode='$fourRandomDigit' WHERE DriverPhone='$DriverPhone'";
					if(mysqli_query($con,$sql22))
					{
						
					}
					
			
					$test=0;
					$res = mysqli_query($con,"SELECT * FROM Drivers WHERE DriverPhone='$DriverPhone'");
					
					
					
					

					$result = array();
					while($row = mysqli_fetch_assoc($res)){

					$result[] = $row;
					$test=4;

					}

					if($test==4){

					


						$message ="code sent";
						$success = true;
						$status_code = 200;
					//	$result = [];
						
						echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result[0],"message"=>$message));

					}
					}
			
			
		
   
die;
mysqli_close($con);
?>