<?php
// Include DB file for credentials and database connection 

include("db.php");


echo "Hi";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	
	$return = array('success' => false, 'message' => 'Silence is gold');
	
	$data = json_decode(file_get_contents('php://input'), true);
	
	if($data['firstName'] != '' && $data['lastName'] != '' && $data['email'] != '' && $data['password'] != ''):
		
		if (strpos($data['email'], '@virtualemployee.com') === false) 
		{
			$return = array('success' => false, 'message' => 'Unable to add user. Please contact administrator for registraion issue.');
		} 
		else 
		{	
			$color = array('#f5b7b1','#cd6155','#a93226','#641e16','#f1948a','#af7ac5','#884ea0','#5b2c6f','#7fb3d5','#5499c7','#1a5276','#0e6251','#148f77','#1abc9c','#196f3d','#2ecc71','#82e0aa','#f7dc6f','#f4d03f','#9a7d0a','#e67e22','#ba4a00','#6e2c00','#797d7f','#bdc3c7','#839192','#5d6d7e','#283747','#566573','#ad1457','#ec407a','#80deea','#00838f','#f4511e','#0000FF');	
			
			$k = array_rand($color);
			
			$v = $color[$k];
			
			$stmt = $conn->query("INSERT INTO `tbl_user` (`first_name`, `last_name`, `email`, `password`, `status`,`unique_color`) VALUES	('".$data['firstName']."', '".$data['lastName']."', '".$data['email']."', '".md5($data['password'])."', 1, '".$v."')");
	        
		
			if ($stmt) 
			{
				$return = array('success' => true, 'message' => 'User Added Successfully');
			} 
			else 
			{
				$return = array('success' => false, 'message' => 'Unable to add user. Please contact administrator.');
			}		
		}

	endif;
	
	echo json_encode($return);
	
}


if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
	
	
	
	$data = json_decode(file_get_contents('php://input'), true); 
	
	$return = array('success' => false, 'message' => 'Not requested yet');
	
	if($data['email'] != ''):
		
		if($data['email'] != 'none'):
		
			
			$result = $conn->query("SELECT id, first_name, last_name, unique_color, use_pro, access  FROM tbl_user WHERE email = '".$data['email']."'");
			
			$rows  = [];
			
			if($result->num_rows == 1){ 
				$row = $result->fetch_assoc();
				$return = array('success' => true, 'data' => $row);
			} else {
				$return = array('success' => false, 'message' => $sql);
			}
			
		
		else:
			
			$result = $conn->query('SELECT id, first_name as first, last_name as last, unique_color  FROM `tbl_user` WHERE `status` = 1');
					
			$rows  = [];
			
			if($result->num_rows > 0)
			{ 
				while($row = $result->fetch_assoc()) 
				{
					$rows[] = $row;	
				}
				$return = array('success' => true, 'data' => $rows);
			} else{
				$return = array('success' => true, 'data' => $rows);
			}			
		
		endif;

	endif;
	
	echo json_encode($return);
	
}