<?php

Class User {

	private $username 	= "";
	private $userId		= NULL;
	private $password 	= "";
	private $email 		= "";
	private $loggedIn	= false;

	public function __construct($username) {
		if($username != NULL) {
			$this->username = $username;
		}

	}

	public function setLoggedIn($loggedIn) {
		$this->loggedIn = $loggedIn;
	}

	public function getLoggedIn() {
		return $this->loggedIn;
	}

	public function setUserId($userId) {
		$this->userId = $userId;
	}

	public function getUserId() {
		return $this->userId;
	}

	public function login($username, $password) {
		global $db;
		$this->username = $username;
		$this->password = $password;

		$user = $db->selectQuery("SELECT * FROM `users` WHERE `username` = '" . $username . "'");

		if(sizeof($user) > 1) {
			die('Something really went wrong.');
		}

		if(sizeof($user) < 1) {
			die('Something really went wrong.');
		}

		if($user[0]->password === md5($password)) {
			$_SESSION['loggedIn'] = true;
			$_SESSION['username'] = $this->username;
			$this->loggedIn = true;
			header("Location: http://atoffice");
		} else {
			return false;
		}

	}

	public function loginByUsername($username) {
		global $db;

		$user = $db->selectQuery("SELECT * FROM `users` WHERE `username` = '" . $username . "'");

		if(sizeof($user) > 1) {
			die('Something really went wrong.');
		}

		if(sizeof($user) < 1) {
			die('Something really went wrong.');
		}

		$_SESSION['loggedIn'] = true;
		$this->userId = $user[0]->id;
		$this->loggedIn = true;
	}

	public function logout() {
		$this->username = "";
		$this->password = "";
		$this->email = "";

		$_SESSION['loggedIn'] = false;
		$this->loggedIn	= false;
		unset($_SESSION['username']);
		session_destroy();
		header("Location: http://atoffice");
	}

	public function getClients() {
		global $db;

		//$clients = $db->query("SELECT * FROM `clients` WHERE `owned_by` = '" . $this->username . "'");

		//sizeof($clients);

		//return $clients->name;

		return $this->username;
	}

}

?>
