<?php
include('db_connect.php');

$response= array();
// echo  $GLOBALS['firebase_api'];
if(isset($_POST['email'])){

    // fetching values from post request
    $email= $_POST['email'];
    $name= $_POST['name'];

    // check if user already exists
    $result1=   $query=mysqli_query($con,"SELECT * FROM users WHERE email='$email'");
 if(mysqli_num_rows($query)>=1)
   {
    $response['error']= true;
    $response['message']="It seems user is already exists";
    // fetch user id from db
    $row= mysqli_fetch_assoc($result1);
    $response=array("error"=>"false","message"=>"available","id"=>$row["uid"]);

//    $response['id']=$row["uid"];
   }
 else
    {
   //insert new user to db
   
$sql = "INSERT INTO users (email,name)
VALUES ('$email', '$name')";

if (mysqli_query($con, $sql)) {
  //  $response['error']= false;
  //  $response['message']="User added successfully";

    //fetch user id after registeration
    $sqli= "SELECT * FROM users WHERE email='$email'";
   $result= mysqli_query($con,$sqli);
   $row= mysqli_fetch_assoc($result);
 //  $response['id']=$row["uid"];
   $response=array("error"=>"false","message"=>"User added successfully","id"=>$row["uid"]);


} else {
    $response=array("error"=>"Error: " . $sql . "<br>" . mysqli_error($con));

  //  $response['error']="Error: " . $sql . "<br>" . mysqli_error($con);
}
}
}else{
    $response=array("error"=>"true","message"=>"Some error occured. Please try again!");
  //  $response['error']= true;
  //  $response['message']="Some error occured. Please try again!";
}

echo json_encode($response);
mysqli_close($con);
?>