<?php 
include '../config.php';

$mobileNum = $_POST["mobileNum"];
$coutry_code = "91";
$mobile = $coutry_code.$mobileNum;
$status =$_POST["status"];
$response=array();
if ($status== "0")
 {
			  $sql = "SELECT * FROM member WHERE member_mobile = '$mobileNum'";
			  $result = mysqli_query($conn,$sql);
			  if(mysqli_num_rows( $result)>0) 
			  {
			  $row = mysqli_fetch_array($result);
			  $mobileNo = $row['member_mobile'];
				if($mobileNo == $mobileNum)
				{
					$response = ["status"=>"allReadyExist"];
				}

				else{
					$ch=curl_init("https://api.msg91.com/api/sendotp.php");
					$data_array =  array(
						"mobile"        => $mobile,
						"templateId"    => "5efd70a2d6fc057de94f7a7f",
						"authkey"       => "291127Ap7wweRhwTo5d629b90",

					);
					curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
					curl_setopt($ch, CURLOPT_HEADER, false);
					curl_setopt($ch, CURLOPT_POSTFIELDS, $data_array);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
					$result_otp = curl_exec($ch);
				     curl_getinfo($ch);
					curl_errno($ch);
				     curl_error($ch);
					curl_close($ch);
					$response=['otp'=>$result_otp,'status'=>'success'];

					}
			 }
			 	else{
					$ch=curl_init("https://api.msg91.com/api/sendotp.php");
					$data_array =  array(
						"mobile"        => $mobile,
						"templateId"    => "5efd70a2d6fc057de94f7a7f",
						"authkey"       => "291127Ap7wweRhwTo5d629b90",

					);
					curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
					curl_setopt($ch, CURLOPT_HEADER, false);
					curl_setopt($ch, CURLOPT_POSTFIELDS, $data_array);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
					$result_otp = curl_exec($ch);
				     curl_getinfo($ch);
					curl_errno($ch);
				     curl_error($ch);
					curl_close($ch);
					$response=['otp'=>$result_otp,'status'=>'success'];

					}

}
else
{
    
  	$otp =$_POST["otp"];
	$ch=curl_init("https://api.msg91.com/api/verifyRequestOTP.php");
	$data_array =  array(
		"mobile"        =>$mobile,
		"authkey"       => "291127Ap7wweRhwTo5d629b90",
		"otp"   		  =>$otp,
	);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($ch, CURLOPT_HTTPGET, true);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);	
	$response=curl_setopt($ch, CURLOPT_POSTFIELDS, $data_array);
	$result_otp = curl_exec($ch);
	$obj = json_decode($result_otp);

	if ($obj->{'message'} == "otp_verified")
	{
			$response=['mobile'=>$mobile,
					'status'=>'success'
				];
	}
	else if($obj->{'message'} == "already_verified") 
	{
		$response=['mobile'=>$mobile,'status'=>'already_verified'];
	}
	else{
		$response=['mobile'=>$mobile,'status'=>'Incorrect'];;
	}
}

echo json_encode($response);



?>