<?php

date_default_timezone_set('Africa/Johannesburg');

class VerificatioCode{
    
    private $conn;
    private $table_name = "SmsVerification";
 
    // object properties
    public $id;
	public $cellNumber;
	public $verificationCode;
    public $generationDate = "0000-00-00 00:00:00";
    public $verificationDate = "0000-00-00 00:00:00";
    public $verified;
    
    
    
     // constructor
    public function __construct($db){
        $this->conn = $db;
    }
    
    
    function isTokenValid(){
 
    // query to check if token exists and is valid
    $query = "SELECT id,FK_AppUser,token,activated_on,expiry_date,duration,created_at,updated_at,created_by
            FROM " . $this->table_name . "
            WHERE token = ? and activated_on = ?
            LIMIT 0,1";
 
    // prepare the query
    $stmt = $this->conn->prepare( $query );
 
    // sanitize
   // $this->email=htmlspecialchars(strip_tags($this->email));
 
    // bind given email value
    $stmt->bindParam(1, $this->token);
    
    $stmt->bindParam(2,$this->emptyData);
 
    // execute the query
    $stmt->execute();
 
    // get number of rows
    $num = $stmt->rowCount();
 
    // if email exists, assign values to object properties for easy access and use for php sessions
    if($num>0){
 
        // get record details / values
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
 
        // assign values to object properties
        $this->id = $row['id'];
        $this->fkeyAppUser = $row['FK_AppUser'];
        $this->token = $row['token'];
        $this->activatedOn = $row['activated_on'];
        
        
        $this->expiryDate = $row['expiry_date'];
        $this->duration = $row['duration'];
        $this->createdAt = $row['created_at'];
         $this->updatedAt = $row['updated_at'];
         $this->createdBy = $row['created_by'];
 
        // return true because email exists in the database
        return true;
    }
 
    // return false if email does not exist in the database
    return false;
}

function updateValidToken(){
     // query to check if token exists and is valid
    $query = "UPDATE " . $this->table_name . " SET activated_on = :activatedDate
            WHERE id = :recId";
            
             // prepare the query
    $stmt = $this->conn->prepare( $query );
            
    $activatedOn = date("Y-m-d H:i:s");
    
     $stmt->bindParam(':activatedDate',  $activatedOn);
    
    $stmt->bindParam(':recId',$this->id);
     // execute the query
    if($stmt->execute()){
        return true;
    }
    return false;
}

function saveGeneratedVeriCode(){
    $tokenVal = mt_rand(10000,99999);
    $query= "INSERT INTO `SmsVerification` (`cellnr`, `verification_code`, `token`,`phone_type`, `expiry_date`, `duration`, `created_by`) VALUES (:userId,:consultantId,:tokenValue,:phoneType,:expireDate,:duration,:createdBy)";
               // prepare the query
    $stmt = $this->conn->prepare( $query );

    
     $stmt->bindParam(':userId',$this->fkeyAppUser);
     $stmt->bindParam(':consultantId',$this->consultantId);
     $stmt->bindParam(':tokenValue',$this->token);
     $stmt->bindParam(':phoneType',$this->phoneType);
     $stmt->bindParam(':expireDate',$this->expiryDate);
     $stmt->bindParam(':duration',$this->duration);
    // $stmt->bindParam(':createdDate',$this->createdAt);
     $stmt->bindParam(':createdBy',$this->consultantId);
     
    if($stmt->execute()){
        return true;
        }
    return false;

}


}



?>
