<?php
// include database and object files
include_once '../config/init.php';	// contains $db
include_once '../config/error.php';
include_once '../objects/db_user.php';

// prepare user object
$user = new User($db);

// ensure username and password are passed in $_POST parameters
if (isset($_POST['username']))
	$user->username = $_POST['username'];
else trigger_error( missing_parameter_error("username") );

if (isset($_POST['password']))
	$user->password = base64_encode($_POST['password']);
else trigger_error( missing_parameter_error("password") );  

// read details of user to be edited
$stmt = $user->login();
if($stmt->rowCount() > 0)
{
	// get retrieved row
	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	// retrieve user values
	$user->readRow($row);
	// update last connection date
	$user->refreshConnectionDate();
	
	// create array
	/*$user_arr=array (
		"status" => true,
		"message" => "Successfully Login!",
		"user" => $user->toArray()
	);*/
	$user_arr = $user->getResultArray(true, "Login_OK");
	print_r(json_encode($user_arr));	// OK
}
else trigger_error("Invalid_Username_Password");
?>