<?php

class AdminUser{
    
    private $conn;
    
    public $id;
    public $email;
    public $name;
    public $surname;
    public $password;
    
    public function __construct($db){
        $this->conn = $db;
    }
    
    //============== For Login ===============================
    
    function emailExists($email){
     
        // query to check if email exists
        $query = "SELECT `id`, `email`, `name`, `surname`, `email`, `password` FROM `AdminUsers` WHERE `email`= :email  LIMIT 0,1";
     
        // prepare the query
        $stmt = $this->conn->prepare( $query );
            // sanitize
    
        $stmt->bindParam(':email', $email);
     
        // 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->email = $row['email'];
            $this->name = $row['name'];
            $this->password = $row['password'];
    
     
            // return true because email exists in the database
            return true;
        }
     
        // return false if email does not exist in the database
        return false;
    }
    
    //================== Insert New Admin User ===============================
    
    function insertUser(){
         try{
              // insert query
             $query = "INSERT INTO `AdminUsers` ( `email`, `name`,`surname` ,`password`) VALUES (:email,:name,:surname,:userPassword)";
     
            // prepare the query
             $stmt = $this->conn->prepare($query);
     
    
            $versionControl = 1;
         
            // bind the values
    
        
            $stmt->bindParam(':email', $this->email);
            $stmt->bindParam(':name', $this->name);
            $stmt->bindParam(':surname', $this->surname);
            // hash the password before saving to database
            $password_hash = password_hash($this->password, PASSWORD_BCRYPT);
            $stmt->bindParam(':userPassword', $password_hash);
    
    
            if($stmt->execute()){
                $true = "true";
        
                if(strcmp($this->getAppUserId(), $true)==0){
                     return "true";
                }else{
                     $this->id = 0;
                     return "false";
                }
               
            }else{
                     $this->id = 0;
                     return "false";
                
            }
            
        }catch (Exception $e){
            $this->id = 0;
            $this->feedback =  $e;
            return "false";
        }
    }

    function getAppUserId(){
        
        // query to check if email exists
        $query = "SELECT id FROM `AdminUsers`  WHERE email = :email
                LIMIT 0,1";
     
        // prepare the query
        $stmt = $this->conn->prepare( $query );
     

        // bind given email value
        $stmt->bindParam(':email', $this->email);
     
        // 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'];
     
            // return true because email exists in the database
            return "true";
        }
         return "false";
        
    }
    
 //========= Verify if account exists and if jwt id, email is same person ===
function verifyJwtIdEmail($email,$id){
         // query to check if email exists

    $query = "SELECT count(`id`) as countId FROM `AdminUsers` WHERE `email` = :email and id =:id";

    // prepare the query
    $stmt = $this->conn->prepare( $query );
 
    // sanitize
   // $email=htmlspecialchars(strip_tags($email));
   // $id=htmlspecialchars(strip_tags($id));
    // bind given email value
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':id', $id);
    // 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);
         if($row['countId']>0){
             return true;
         }
         


    }
 
    // return false if email does not exist in the database
    return false;
    
}
    
    
    
}