<?php
session_start();
/*use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php'; */

header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST');

header("Access-Control-Allow-Headers: X-Requested-With");


if(isset($_POST['email']))
{
require 'config.php';


function check_input($par) {
  $par = trim($par);
  $par = stripslashes($par);
  $par = htmlspecialchars($par);
  return $par;
}



$firstname=check_input($_POST['firstname']);
$lastname=check_input($_POST['lastname']);
$email=check_input($_POST['email']);


$firstname = mysqli_escape_string($con, $firstname);
$lastname = mysqli_escape_string($con, $lastname);
$email=mysqli_escape_string($con, $email);



/*$sql="SELECT * FROM users WHERE  phone='$phone' AND status = 'Pending'  OR email = '$email' AND status = 'Pending'";
$result=$con->query($sql) or die ("error: Server error ".mysqli_error($con));
$count=mysqli_num_rows($result);
if($count == 1)
{
$sqlx="UPDATE sales set phone = '$phone', email = '$email',  WHERE phone = '$phone' AND  status='Pending'";
$resultx=$con->query($sqlx) or die ("error2: Server error ".mysqli_error($con));
}
*/



$sql2="SELECT * FROM users WHERE email = '$email'";
$result2=$con->query($sql2) or die ("error: Server error ".mysqli_error($con));
$count2=mysqli_num_rows($result2);

 if ($email !== "" && $count2 >= 1)
{
         $suc = "Email Already Exist"; 
         $status = false; 


}
else
{



function check_number(){

require 'config.php';

    $unique_number = rand(1000000000, 9999999999);

    $sql = $con->query("SELECT * FROM users WHERE user_id = '$unique_number'")  or die ("error: ".mysqli_error($con));
    $exists = mysqli_num_rows($sql);

    if ($exists >0){
        $results = check_number();
    }
     else{
            $results = $unique_number;
        return $results;
     }
}

$user_id = check_number();


function gen_refcode($n){

require 'config.php';

  
    //$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
 
    for ($i = 0; $i < $n; $i++) {
        $index = rand(0, strlen($characters) - 1);
        $randomString .= $characters[$index];
    }
 
 
  $sql = $con->query("SELECT * FROM users WHERE ref_code = '$randomString'")  or die ("error: ".mysqli_error($con));
    $exists = mysqli_num_rows($sql);

    if ($exists >0){
        $results = gen_refcode(7);
    }
     else{
            $results = $randomString;
        return $results;
     }
 

}

$ref_code = gen_refcode(7);


  

date_default_timezone_set("Africa/Lagos");

  $date_t = date("d-M-Y h:i:s a"); 
  $code = rand (100000, 999999);    
  $code_exp = strtotime("+10 minutes"); 


    $auth = sha1 (rand(1000000000, 9999999999));

  $sqlx=$con->query("INSERT INTO users  (user_id, auth, firstname, lastname,  email, signup_type, ref_code,   plan,  status, date_reg) 
      VALUES ('$user_id', '$auth', '$firstname', '$lastname',  '$email', 'Google', '$ref_code',  'Non',  'Pending', '$date_t')") or die("Error: ".mysqli_error($con));
      
      
  
if($sqlx)
{
    
$suc = "Successful"; 
$status = true; 

}
}

$output = array('status' => $status, 'success'=>$suc, 'user_id' => $user_id, 'auth' => $auth, 'firstname'=> $firstname, 'lastname'=> $lastname, 'email'=> $email);
echo json_encode($output); //output JSON data
}
?>
