<?php
require_once('_conf/globals.php');
require_once('_conf/db_con.php');

// $stmt = $db_con->prepare("SELECT * FROM users");
// $stmt->execute();
// $users = $stmt->fetch(PDO::FETCH_ASSOC);
// var_dump($users);
$fields = json_decode(file_get_contents('php://input'));


function register_user($fields){
    global $db_con;
    if($fields->username && $fields->email){
        $_username = $fields->username;
        $_email = $fields->email;

        $sql_con = $db_con->prepare('INSERT INTO users SET uname = :username, u_email = :email');
        $sql_con->bindValue(':username', $_username);
        $sql_con->bindValue(':email', $_email);
        $sql_con->execute();

        $ret_sql = $db_con->prepare('SELECT * FROM users WHERE uname = :username');
        $ret_sql->bindValue(':username', $_username);
        $ret_sql->execute();
        $user = $ret_sql->fetch(PDO::FETCH_ASSOC);

        return $user;
    }
}


if(isset($fields->action)){
    if($fields->action === 'register'){
        $user = new stdClass();

        $userInfo = register_user($fields);
        $_username = $userInfo['uname'];
        $user->uname = $_username;
        $user = json_encode($user);
        echo $user;
    }
}
?>