<?php
	require_once("initPDO.php");
	require_once("createUserTable.php");

	class User
	{
		protected $props;

		public function __get($prop) {
			return $this->props[$prop];
		}

		public function __set($prop, $val) {
			$this->props[$prop] = $val;
		}

		public static function executeRequest($req) {
			$pdo = $GLOBALS['pdo'];
			$request = $pdo->prepare($req);
			if (!$request) {
				myDump(debug_backtrace());
				die('Error while doing request ' . $sqlRequest);
			}

			$request->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
			$request->execute();

			return $request->fetchAll();
		}

		public static function getAllUsers() {
			return static::executeRequest('select * from `users`');
		}

		// instance-side method to render a User object to HTML
		public function toHtml() {
			echo "<tr>"
				. "<td>". $this->id . "</td>"
				. "<td>". $this->name . "</td>"
				. "<td>" . $this->email . "</td></tr>";
		}

		// class-side method to render an array of users as an HTML table
		public static function showUsersAsTable($users) {
			echo '<table><thead>
					<tr><th>Id</th><th>Nom</th><th>Email</th></tr></thead><tbody>';
			foreach($users as $u)
				$u->toHtml();
			echo '</tbody></table>';
		}
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Get Results
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	$allUsers = User::getAllUsers();
	$allUsersEmails = [];
	foreach($allUsers as $u)
		 $allUsersEmails[] = $u->email;

	$allUsersEmailsAsJSON = json_encode($allUsersEmails);

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Close the database connection
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   $pdo = null;
?>

<!doctype html>
 <html lang="fr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
	  var availableTags = <?php echo $allUsersEmailsAsJSON; ?>;

	  $( "#tags" ).autocomplete({
		      source: function(request, response) {
		  			var filteredArray = $.map(availableTags, function(item) {
		           if(item.indexOf(request.term) == 0)
		            	 return item;
		           else
		               return null;
				  	 });
		          response(filteredArray);
		       },
				minLength: 1
    	});

  });
  </script>
	<style>
	table {
		border-top: 1px solid black;
		border-bottom: 1px solid black;
	}

	td {
		text-align: center;
		padding-left: 2em;
		padding-right: 2em;
	}
	</style>

</head>
<body>

<h1>Users</h1>
<?php
	User::showUsersAsTable($allUsers);
?>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>


</body>
</html>
