<?php
class UserData{
    

	private $conn;
	    // constructor
    public function __construct($db){
        $this->conn = $db;
		require('ps_object_user.php');
    }
	
	
	function getAppUserList(){
		$columns = "`id`,`email`,`name`,`celnr`,`country`, `consultant_id`,`consultantEmail`,`account_verification_status`";
		$query = "SELECT ". $columns. "  FROM `AppUsers` ";
    
             // prepare the query
        $stmt = $this->conn->prepare( $query );
        $stmt->execute(); 
        $num = $stmt->rowCount();
    //	$recCounter = 0;
    	$list = array();
    	$recCounter = 0;
        if ($stmt->rowCount() > 0) {
           $response["responseCode"] = 200;
    		$response["result"] = "success";
    	    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    			$psObjectUser = new PsObjectUser();
    			$psObjectUser->id = $row["id"];
    			$recCounter++;
    			$psObjectUser->recCounter = $recCounter;
    			$psObjectUser->email = $row["email"];
    			$psObjectUser->name = $row["name"];
    			$psObjectUser->celnr = $row["celnr"];
    			$psObjectUser->country = $row["country"];
    			
    			$psObjectUser->consultant_id = $row["consultant_id"];
    			$psObjectUser->consultantEmail = $row["consultantEmail"];
    			$psObjectUser->accountVerified = $row["account_verification_status"];
    		//	$recCounter++;
    			$list[] = $psObjectUser;
    			//$user = "user" . $recCounter;$response[$user] = $psObjectUser;
    
    		 }
    		 $response["list"] = $list;
    			
    	    return 	json_encode($response);
          
        }else{
             //$response["userList"]["Error"] = "Error";
             $response["responseCode"] = 200;
             $response["result"] = "no data";
             	$psObjectUser = new PsObjectUser();
    			$psObjectUser->id = 0;
    			$psObjectUser->recCounter = 0;
    			$psObjectUser->email = "";
    			$psObjectUser->name = "";
    			$psObjectUser->celnr = "";
    			$psObjectUser->country = "";
    			$psObjectUser->consultant_id = 0;
    			$psObjectUser->consultantEmail = "";		
    			$list[] = $psObjectUser;
            $response["list"] = $list;
             return json_encode($response);
        }
     

	}
	
	function resetUserPassword($userId,$userEmail,$newPassword,$adminAccount)
	{
	      $query = "UPDATE `AppUsers` SET `password`=:password, `resetPasswordDate`=:updateDateTime WHERE `id`=:id and `email`=:email";
      // prepare the query
      
         $stmt = $this->conn->prepare($query);
     
        // hash the password before saving to database
        $password_hash = password_hash($newPassword, PASSWORD_BCRYPT);
        date_default_timezone_set('Africa/Johannesburg');
        $dateTime = date('Y-m-d H:i:s', strtotime("now"));
        
       
        $stmt->bindParam(':password', $password_hash);
        $stmt->bindParam(':updateDateTime',  $dateTime);
         $stmt->bindParam(':id',  $userId);
        $stmt->bindParam(':email', $userEmail);
           

        
         $stmt->execute();
         
         $count = $stmt->rowCount();
    
        if($count > 0){
            $this->passwordLog($adminAccount,$userEmail);
           return "ok";
        }
        else{
            return "error";
        }
	}
	function verifyUser($userId,$userEmail){
	       $query = "UPDATE `AppUsers` SET `account_verification_status`=:verficationStatus WHERE `id`=:id and `email`=:email";
      // prepare the query
      $verficationStatus = "Yes";
         $stmt = $this->conn->prepare($query);
     
        // hash the password before saving to database
        $password_hash = password_hash($newPassword, PASSWORD_BCRYPT);
        date_default_timezone_set('Africa/Johannesburg');
        $dateTime = date('Y-m-d H:i:s', strtotime("now"));
        
       
        $stmt->bindParam(':verficationStatus',$verficationStatus);
        $stmt->bindParam(':id',  $userId);
        $stmt->bindParam(':email', $userEmail);
          
        
         $stmt->execute();
         
         $count = $stmt->rowCount();
    
        if($count > 0){
            
           return "ok";
        }
        else{
            return "error";
        }
	}
	
	function passwordLog($adminAccount,$userAccount){
	    $query = "INSERT INTO `passwordResetLog` (`adminAccount`, `userAccount`) VALUES (:adminAccount,:userAccount)";
	    
	    $stmt = $this->conn->prepare($query);
 
        $stmt->bindParam(':adminAccount',$adminAccount);
        $stmt->bindParam(':userAccount',$userAccount);
        if($stmt->execute()){
            return "true";
        }
        return "false";
       
    }

}