<?php

class USER {

    private $db;

    function __construct($DB_con) {
        $this->db = $DB_con;
    }

    public function register($usertype, $fname, $lname, $slug, $email, $password, $gender, $landline, $mobile, $fax, $address, $country, $city, $zip, $location, $company, $company_location, $filename) {
        try {
            //echo "here";





            $new_password = password_hash($password, PASSWORD_DEFAULT);

            $stmt = $this->db->prepare("INSERT INTO users(
                            utId,
                            u_fname,
                            u_lname,
                            u_slug,
                            u_email,
                            u_pass,
                            u_gender,
                            u_landline,
                            u_mobile,
                            u_fax,
                            u_photo,
                            u_add,
                            u_country,
                            u_city,
                            u_zip,
                            u_location,
                            u_company,
                            u_company_location
                            ) 
		            VALUES(
                            :usertype,
                            :fname,
                            :lname,
                            :slug,
                            :email,
                            :password,
                            :gender,
                            :landline,
                            :mobile,
                            :fax,
                            :filename,
                            :address,
                            :country,
                            :city,
                            :zip,
                            :location,
                            :company,
                            :company_location
                            )");
            $stmt->bindparam(":usertype", $usertype);
            $stmt->bindparam(":fname", $fname);
            $stmt->bindparam(":lname", $lname);
            $stmt->bindparam(":slug", $slug);
            $stmt->bindparam(":email", $email);
            $stmt->bindparam(":password", $new_password);
            $stmt->bindparam(":gender", $gender);
            $stmt->bindparam(":landline", $landline);
            $stmt->bindparam(":mobile", $mobile);
            $stmt->bindparam(":fax", $fax);
            $stmt->bindparam(":filename", $filename);
            $stmt->bindparam(":address", $address);
            $stmt->bindparam(":country", $country);
            $stmt->bindparam(":city", $city);
            $stmt->bindparam(":zip", $zip);
            $stmt->bindparam(":location", $location);
            $stmt->bindparam(":company", $company);
            $stmt->bindparam(":company_location", $company_location);
            //$stmt->bindparam(":status", "Active");

            $stmt->execute();

            return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    
    public function login($email, $upass) {
        
        try {
            $upass = ($upass);
            $stmt = $this->db->prepare("SELECT * FROM users WHERE u_email=:email and u_pass = :u_pass and u_status = 'Active' LIMIT 1");
            $stmt->execute(array(':email' => $email, ':u_pass'=>$upass  ));
            $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
            
            if ($stmt->rowCount() > 0) {
                
                    $_SESSION['user_session'] = 100001; $userRow['uId'];
                    $_SESSION['user_role_session'] = "Admin"; $this->getUserRole($userRow['utId']);
                    $_SESSION['user_name_session'] = $userRow['u_fname'].' '.$userRow['u_lname'];
					
					$_SESSION['user_email_session'] = $userRow['u_email'];
                    
					//
					return true;
                    
            }else{   
                
                $stmt2 = $this->db->prepare("SELECT * FROM agents WHERE email=:email and password = :u_pass and status  = 'Active' LIMIT 1");
                $stmt2->execute(array(':email' => $email, ':u_pass'=>$upass  ));
                $userRow = $stmt2->fetch(PDO::FETCH_ASSOC);

                if ($stmt2->rowCount() > 0) {
                         $_SESSION['user_log_id'] = $this->addAgentsLog($userRow['agent_id']);
                        $this->updateLoginStatus($userRow['agent_id'], 1);
                        $_SESSION['user_session'] = $userRow['agent_id'];
                        $_SESSION['user_role_session'] = $this->getUserRole($userRow['agent_type']);
                        $_SESSION['user_name_session'] = $userRow['name'];
                        return true;
                }else { 
                        return false;
                    }
            } 
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    
public function getUserLastUpdate($agent_id){
        try {
            $stmt = $this->db->prepare("SELECT * FROM agents_log WHERE agent_id =:uId order by last_update desc limit 1");
            $stmt->execute(array(':uId' => $agent_id));
            $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
            if ($stmt->rowCount() > 0){
                return $userRow['last_update'];
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
     
public function updateLogin($log_id) {
        try {

            $stmt = $this->db->prepare("update agents_log Set last_update = :last_update where id = :log_id ");
            $stmt->bindparam(":last_update", date("Y-m-d H:i:s"));
            $stmt->bindparam(":log_id", $log_id);
            
            $stmt->execute();
            return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    

     
public function updateAdminPass($email, $pass) {
        try {

              
			$stmt = $this->db->prepare("update users Set u_pass = :pass where u_email = :email");
            $stmt->bindparam(":pass", $pass);
            $stmt->bindparam(":email", $email);
            $stmt->execute();
		    return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    



    public function LogoutTime($log_id) {
        try {

            $stmt = $this->db->prepare("update agents_log Set logout = :logout where id = :log_id ");
            $stmt->bindparam(":logout", date("Y-m-d H:i:s"));
            $stmt->bindparam(":log_id", $log_id);
            
            $stmt->execute();
            return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    
    
    public function addAgentsLog($agent_id) {
        try { 
                $qry = "insert into agents_log (`agent_id`, `last_update`) values ('$agent_id', '".date("Y-m-d H:i:s")."' )";
                $stmt = $this->db->prepare($qry);
                $stmt->execute();
                
                return $this->db->lastInsertId();                
 
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    
    
   public function updateLoginStatus($user_id, $status) {
        try {
                $return = array(); 
                $qry = "update agents set `login_status` = '$status' where `agent_id` = ".$user_id;
                $stmt = $this->db->prepare($qry);
                $stmt->execute();
 
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    
    public function getUserRole($id = '') {
        try {
            
            if(!empty($id)){
                $stmt = $this->db->prepare("SELECT * FROM user_types WHERE utId=:uId");
                $stmt->execute(array(':uId' => $id));
                $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
                if ($stmt->rowCount() > 0) {
                    return $userRow['ut_title'];
                }
            }else {
                
                $stmt = $this->db->prepare("SELECT * FROM user_types");
                $stmt->execute(array(':uId' => $id));
                
                while($userRow = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    $return[] = $userRow;
                }
                
                return $return;
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    
    
    
    
    public function getProfile($id) {
        try {
            $stmt = $this->db->prepare("SELECT * FROM users WHERE uId=:uId");
            $stmt->execute(array(':uId' => $id));
            $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
            if ($stmt->rowCount() > 0) {
                return $userRow;
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    public function is_loggedin() {
        if (isset($_SESSION['user_session'])) {
            return true;
        }
    }

    public function redirect($url) {
        header("Location: $url");
    }

    public function logout() {
        session_destroy();
        unset($_SESSION['user_session']);
        return true;
    }
    
    function generatePassword($length = 8) {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $count = mb_strlen($chars);

        for ($i = 0, $result = ''; $i < $length; $i++) {
            $index = rand(0, $count - 1);
            $result .= mb_substr($chars, $index, 1);
        }

        return $result;
    }



}

?>