<?php
class TokenMaster{
    private $db;
    private $currentToken ;
    private $lastError = '';
    public function __construct($db,$currentToken)
    {
        $this->db  = $db;
        $this->currentToken  = $currentToken ;
    }
    function checkPC(){
        $db           = $this->db;
        $currentToken = $this->currentToken ;
        try {
            $sql = $db->prepare("SELECT staff_device FROM `loginhistory` WHERE token = ? LIMIT 1");
            $sql->bindParam(1, $currentToken);
            $sql->execute();
            if ($sql->rowCount() > 0) {
                return true ;
            }else{
                return false ;
            }
        } catch (PDOException $e) {
            $this->lastError = $e->getMessage();
            error_log('loginhistory checkPC error: ' . $this->lastError);
            return false;
        }
    }
    function getLastError(){
        return $this->lastError;
    }
    function isStaffDevice(){
        $db           = $this->db;
        $currentToken = $this->currentToken ;
        $sql = $db->prepare("SELECT staff_device FROM `loginhistory` WHERE token = ? LIMIT 1");
        $sql->bindParam(1, $currentToken);
        $sql->execute();
        if ($sql->rowCount() > 0) {
            $row = $sql->fetch(PDO::FETCH_ASSOC);
            return (int)$row["staff_device"] === 1;
        }else{
            return false ;
        }
    }
    function updateLastLogin(){
        $db           = $this->db;
        $currentToken = $this->currentToken ;
        $sql = $db->prepare("UPDATE `loginhistory` set last_login = NOW() where token = ?");
        $sql->bindParam(1, $currentToken);
        if ( $sql->execute()) {
            return true;
        }else{
            return false ;
        }
    }
    function checkUser($user_id){
        $db           = $this->db;
        $currentToken = $this->currentToken ;
        $sql = $db->prepare("SELECT user_id FROM `loginhistory` WHERE token = ? LIMIT 1");
        $sql->bindParam(1, $currentToken);
        $sql->execute();
        if ($sql->rowCount() > 0) {
            $row = $sql->fetch(PDO::FETCH_ASSOC);
                if($user_id == $row["user_id"]){
                return true;
                }else{    return false ;  }
         }
     }
     function checkBranch($branch){
        $db           = $this->db;
        $currentToken = $this->currentToken ;
        $sql = $db->prepare("SELECT branch FROM `loginhistory` WHERE token = ? LIMIT 1");
        $sql->bindParam(1, $currentToken);
        $sql->execute();
        if ($sql->rowCount() > 0) {
            $row = $sql->fetch(PDO::FETCH_ASSOC);
                if($branch == $row["branch"]){
                return true;
                }else{    return false ;  }
         }
     }
    function saveToken($user_id,$branch,$token){
        $db           = $this->db;
        // $currentToken = $this->currentToken ;
        $active = 2;
        $staff_device = 2;
        $kind = 'كمبيوتر';
        try {
            $maxSql = $db->prepare("SELECT COALESCE(MAX(history_id), 0) + 1 AS next_id FROM `loginhistory`");
            $maxSql->execute();
            $row = $maxSql->fetch(PDO::FETCH_ASSOC);
            $history_id = $row["next_id"];

            $sql = $db->prepare("INSERT INTO `loginhistory` (`history_id`, `user_id`, `branch`, `token`, `active`, `default_branch`,`default_user`, `staff_device`, `kind`, `last_login`) 
            VALUES ( ?, ?, ?, ?, ?, ? ,?, ?, ?, NOW())");
            $sql->bindParam(1, $history_id);
            $sql->bindParam(2, $user_id);
            $sql->bindParam(3, $branch);
            $sql->bindParam(4, $token);
            $sql->bindParam(5, $active);
            $sql->bindParam(6, $branch);
            $sql->bindParam(7, $user_id);
            $sql->bindParam(8, $staff_device);
            $sql->bindParam(9, $kind);
            if ( $sql->execute()) {
                return true;
            }else{
                $errorInfo = $sql->errorInfo();
                $this->lastError = isset($errorInfo[2]) ? $errorInfo[2] : 'Unknown insert error';
                error_log('loginhistory saveToken error: ' . $this->lastError);
                return false ;
            }
        } catch (PDOException $e) {
            $this->lastError = $e->getMessage();
            error_log('loginhistory saveToken exception: ' . $this->lastError);
            return false;
        }
    }
    function updateUser($user_id){
        $db           = $this->db;
        $currentToken = $this->currentToken ;
        $sql = $db->prepare("UPDATE `loginhistory` set user_id = ?
        where token = ?");
        $sql->bindParam(1, $user_id);
        $sql->bindParam(2, $currentToken);
        if ( $sql->execute()) {
            return true;
        }else{
            return false ;
        }  
    }
    function updateBranch($branch){
        $db           = $this->db;
        $currentToken = $this->currentToken ;
        $sql = $db->prepare("UPDATE `loginhistory` set branch = ?
        where token = ?");
        $sql->bindParam(1, $branch);
        $sql->bindParam(2, $currentToken);
        if ( $sql->execute()) {
            return true;
        }else{
            return false ;
        }  
    }
    function activate(){
        $db           = $this->db;
        $currentToken = $this->currentToken ;
        $sql = $db->prepare("UPDATE `loginhistory` set active = 1 where token = ?");
        $sql->bindParam(1, $currentToken);
        if ( $sql->execute()) {
            return true;
        }else{
            return false ;
        }  
    }
    function deActivate(){
        $db           = $this->db;
        $currentToken = $this->currentToken ;
        $sql = $db->prepare("UPDATE `loginhistory` set active = 2 where token = ?");
        $sql->bindParam(1, $currentToken);
        if ( $sql->execute()) {
            return true;
        }else{
            return false ;
        }  
    }
// staff accepted devices
function inStaff($id){
    $db           = $this->db;
    $sql = $db->prepare("UPDATE `loginhistory` set staff_device = 1 where history_id = ?");
    $sql->bindParam(1, $id);
    if ( $sql->execute()) {
        return true;
    }else{
        return false ;
    }  
}
function outStaff($id){
    $db           = $this->db;
    $sql = $db->prepare("UPDATE `loginhistory` set staff_device = 2 where history_id = ?");
    $sql->bindParam(1, $id);
    if ( $sql->execute()) {
        return true;
    }else{
        return false ;
    }  
}
function kind($id,$kind){
    $db           = $this->db;
    $sql = $db->prepare("UPDATE `loginhistory` set kind = ? where history_id =?");
    $sql->bindParam(1, $kind);
    $sql->bindParam(2, $id);
    if ( $sql->execute()) {
        return true;
    }else{
        return false ;
    }  
}
function del($id){
    $db           = $this->db;
    $sql = $db->prepare("DELETE FROM `loginhistory` where history_id =?");
    $sql->bindParam(1, $id);
    if ( $sql->execute()) {
        return true;
    }else{
        return false ;
    }  
}

}// master class
?>
