<?php 
class DaoUsuario extends conexao {

	public function Atualizar(usuario $usuario) {

		try{

			if ($usuario->getUsu_id() != '') {

				$sql = " UPDATE usuario SET
						usu_id = ?,
						usu_email = ?,
						usu_senha = ?,
						usu_situacao = ?
						WHERE usu_id = ? "; 
 
				$stmt = self::conectar()->prepare($sql);
				$stmt->bindValue(1, $usuario->getUsu_id());
				$stmt->bindValue(2, $usuario->getUsu_email());
				$stmt->bindValue(3, $usuario->getUsu_senha());
				$stmt->bindValue(4, $usuario->getUsu_situacao());
				$stmt->bindValue(5, $usuario->getUsu_id());
				$stmt->execute();
				
 
 
			} else {
 
				$sql  = " INSERT INTO usuario (
						usu_id,
						usu_email,
						usu_senha,
						usu_situacao) VALUES (?,?,?,?)";
 
				$stmt = self::conectar()->prepare($sql);
				$stmt->bindValue(1, $usuario->getUsu_id());
				$stmt->bindValue(2, $usuario->getUsu_email());
				$stmt->bindValue(3, $usuario->getUsu_senha());
				//$stmt->bindValue(3, md5 ($usuario->getUsu_senha()));
				$stmt->bindValue(4, $usuario->getUsu_situacao());
				$stmt->execute();
			} 
			
			return self::conectar()->lastInsertId();
  
	
		} catch (Exception $e) {
			print  "Erro ao tentar atualizar:".$e;
		}

 
	}
	 

	
	


	public function autenticarUsuario($email,$senha){
		
		$sql = " SELECT usu_id, usu_email, usu_senha, usu_nivel FROM usuario WHERE usu_email = ? and usu_senha = ? and usu_situacao = ? ";
		$stmt = self::conectar()->prepare($sql);
		$stmt->bindValue(1,$email);
		$stmt->bindValue(2,$senha);
		$stmt->bindValue(3,'ATIVO');
		$stmt->execute();
		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
		
		return $result;
	}

	public function autenticarUsuario2($email,$senha){

		$sql = "SELECT usu_email FROM usuario WHERE usu_email = ? and usu_senha = ?";
		$stmt = self::conectar()->prepare($sql);
		$stmt->bindValue(1,$email);
		$stmt->bindValue(2,$senha);
		$stmt->execute();
		
		if ($stmt->rowCount()>= 1){
		   return true;
  		   echo "conectou";
		}
		else{
		   return false;  
		   echo "não conectou";
		}
	}


	public function PesquisarUsuario($codigo){
		$sql = " SELECT * FROM usuario WHERE usu_id = ? ";
		$stmt = self::conectar()->prepare($sql);
		$stmt->bindValue(1, $codigo);
		$stmt->execute();
		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
		return $result;
	}


	public function pesquisarUsuario_Acessos_Menu($codigo,$acesso){
		$sql = " SELECT uso_ace_id FROM usuario_acessos WHERE uso_ace_uso_id = ? AND uso_ace_acessos = ? LIMIT 1 ";
		$stmt = self::conectar()->prepare($sql);
		$stmt->bindValue(1, $codigo);
		$stmt->bindValue(2, $acesso);
		$stmt->execute();
		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
		return $result;
	}


	public function pesquisarUsuario_Acessos($codigo){
		$sql = " SELECT * FROM usuario_acessos WHERE uso_ace_uso_id = ?";
		$stmt = self::conectar()->prepare($sql);
		$stmt->bindValue(1, $codigo);
		$stmt->execute();
		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
		return $result;
	}

	public function ListarUsuario(){
		$sql = " SELECT * FROM usuario WHERE usu_email != 'admin' ORDER BY usu_id ";
		$stmt = self::conectar()->prepare($sql);
		$stmt->execute();
		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
		return $result;
	}

	public function retornarTotalUsuarios(){
		$sql = " SELECT count(usu_id) as total FROM usuario";
		$stmt = self::conectar()->prepare($sql);
		$stmt->execute();
		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
		return $result;
	}
	
	public function deletar($usu_id){		
		$sql = " DELETE FROM usuario WHERE usu_id = ? ";
		$stmt = self::conectar()->prepare($sql);
		$stmt->bindValue(1, $usu_id);
		$stmt->execute();
	}
	
	
		

	
	public function salvarUsuario($usu_id,$usu_email, $usu_senha ,$usu_situacao,$usu_nivel){
				$sql = " INSERT INTO usuario (usu_id,usu_email,usu_senha,usu_situacao,usu_nivel) VALUES (?,?,?,?,?); ";
				$stmt = self::conectar()->prepare($sql);
				$stmt->bindValue(1, $usu_id);
				$stmt->bindValue(2, $usu_email);
				$stmt->bindValue(3, $usu_senha);
				$stmt->bindValue(4, $usu_situacao);
				$stmt->bindValue(5, $usu_nivel);
				$stmt->execute();
				echo self::conectar()->lastInsertId();
				
				//return $last_id;
				//echo 'gravou';
	}
	
	public function CountUsuarios(){
		$sql = "SELECT COUNT(*) AS num_usuarios FROM usuario"; 
		$stmt = self::conectar()->prepare($sql);
		$stmt->execute();
		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
		return $result;
	}
	
	public function ProximoId(){
$sql = " SELECT COALESCE(MAX(usu_id), 0) + 1 AS PROXIMO_REGISTRO FROM usuario ";
$stmt = self::conectar()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}

}

?>