<?php

require_once('Conn.php');
header('Content-Type: application/json');

if($_SERVER['REQUEST_METHOD'] != 'POST') {
    header('HTTPS/1.1 405');
    echo json_encode(array(["message" => "method not allowed" , "status" => "false"]));
    exit;
}

$params = json_decode(file_get_contents('php://input') , true);

if($params == null || !isset($params['phone'])){
    echo json_encode(array(["message" => "internal body error" , "status" => "false"]));
    exit;
}
date_default_timezone_set("Asia/Kolkata"); 
$date = date("d/m/Y");
$time = date("g:i a");

$phone = $params['phone'];
$createdOn = $time .' , '.$date;

$q1 = "SELECT uid FROM users WHERE phone = '$phone'";
$r1 = mysqli_query($connection , $q1);
if($r1){
    $rows = mysqli_num_rows($r1);
    if($rows == 0){
        $q2 = "INSERT INTO users(phone , creation_date , last_date) VALUES ('$phone' , '$createdOn' , '$createdOn')";
        $r2 = mysqli_query($connection , $q2);
        if($r2){
            $id = mysqli_insert_id($connection);
            echo json_encode(array(["message" => $id , "status" => "true"])); 
        }
        else{
            echo json_encode(array(["message" => "query failed" , "status" => "false"]));  
        }
    }
    else{
        while($row = mysqli_fetch_assoc($r1)) {        
            echo json_encode(array(["message" => $row['uid'] , "status" => "true"])); 
        }
    }
}
else{
    echo json_encode(array(["message" => "query failed" , "status" => "false"])); 
}


?>