<?php

/**
@MANASERA */

require_once 'DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['firstname']) && isset($_POST['matno']) && isset($_POST['password'])) {

    // receiving the post params
    // receiving the post params
	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$level= $_POST['level'];
	$sex= $_POST['sex'];
	$department= $_POST['department'];
	$faculty= $_POST['faculty'];
	$user_id= $_POST['matno'];
	$month= $_POST['month'];
	$day= $_POST['day'];
	$password =$_POST['password'];
	$who=$_POST['who'];

    // check if user is already existed with the same email
    if ($db->isUserExisted($user_id,$who)) {
        // user already existed
        $response["error"] = TRUE;
        $response["error_msg"] = "User already existed with " . $user_id;
        echo json_encode($response);
    } else {
        // create a new user
        $user = $db->storeUser($firstname,$lastname,$level,$sex,$department,$faculty,$user_id,$month,$day,$password,$who);
        if ($user) {
            // user stored successfully
            // use is found
        $response["error"] = FALSE;
       // $response["uid"] = $user["firstname"];
        $response["user"]["firstname"] = $user["firstname"];
        $response["user"]["lastname"] = $user["lastname"];
        $response["user"]["level"] = $user["level"];
        $response["user"]["sex"] = $user["sex"];
		$response["user"]["department"] = $user["department"];
		  $response["user"]["user_id"] = $user["user_id"];
		    $response["user"]["month"] = $user["month"];
			 $response["user"]["day"] = $user["day"];
            echo json_encode($response);
        } else {
            // user failed to store
            $response["error"] = TRUE;
            $response["error_msg"] = "Unknown error occurred in registration!";
            echo json_encode($response);
        }
    }
} else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameters (name, email or password) is missing!";
    echo json_encode($response);
}
?>

