"../register/register.php");
        parent::__construct('formRegister', $options);
    }
    //Methods:
    protected function generaCamposFormulario($datos, $errores = array()){
        $nombre = $datos['name'] ?? '';
        // Se generan los mensajes de error si existen.
        $htmlErroresGlobales = self::generaListaErroresGlobales($errores);
        $errorNombre = self::createMensajeError($errores, 'name', 'span', array('class' => 'error'));
        $errorEmail = self::createMensajeError($errores, 'email', 'span', array('class' => 'error'));
        $errorPassword = self::createMensajeError($errores, 'pass', 'span', array('class' => 'error'));
        $errorPassword2 = self::createMensajeError($errores, 'repass', 'span', array('class' => 'error'));
        $html = "
";
        return $html;
    }
    protected function procesaFormulario($datos){
        $result = array();
        
        $nombre = $this->test_input($datos['name']) ?? null;
        $nombre = strtolower($nombre);
        if ( empty($nombre) || mb_strlen($nombre) < 3 || mb_strlen($nombre) > 8 ) {
            $result['name'] = "El nombre tiene que tener\n una longitud de al menos\n 3 caracteres\n y menos de 8 caracteres.";
        }
        $email = $this->test_input($datos['email']) ?? null;
        if ( empty($email) || !mb_ereg_match(self::HTML5_EMAIL_REGEXP, $email) ) {
            $result['email'] = "El email no es válido.";
        }
        
        $password = $this->test_input($datos['pass']) ?? null;
        if ( empty($password) || mb_strlen($password) < 4 ) {
            $result['pass'] = "El password tiene que tener\n una longitud de al menos\n 4 caracteres.";
        }
        $password2 = $this->test_input($datos['repass']) ?? null;
        if ( empty($password2) || strcmp($password, $password2) !== 0 ) {
            $result['repass'] = "Los passwords deben coincidir";
        }
        
        if (count($result) === 0) {
            $bd = new UserDAO('complucine');
            if($bd){
                $this->user = $bd->selectUserName($nombre);
                if (!$this->user) {
                    $bd->createUser("", $nombre, $email, $password, "user");
                    $this->user = $bd->selectUser($nombre, $password);
                    if ($this->user) {
                        $this->user->setPass(null);
                        $_SESSION["user"] = serialize($this->user);
                        $_SESSION["nombre"] = $this->user->getName();
                        $_SESSION["rol"] = $this->user->getRol();
                        $_SESSION["login"] = true;
                        $result = '../register/register.php';
                    }
                }
                else{
                    $result[] = "El nombre de usuario ya existe.";
                }
            } else {
                $result[] = "Error al conectar con la BD.";
            }
        }
        return $result;
    }
    //Returns validation response:
    static public function getReply() {
        
        if(isset($_SESSION["login"])){
            $name = strtoupper($_SESSION['nombre']);
            $reply = "Bienvenido {$_SESSION['nombre']}
                        {$name}, has creado tu cuenta de usuario correctamente.
                        Usa los botones para navegar
                        
                        \n";
        }   
        else if(!isset($_SESSION["login"])){
            $reply = "ERROR
".
                        "Ha ocurrido un problema y no hemos podido completar el registro.
                        Puede que el nombre de usuario ya esté registrado.
                        Vuelve a intetarlo o inicia sesión si tienes una cuenta de usuario.
                        
                        \n";
        }
        return $reply;
    }
    protected function test_input($input){
        return htmlspecialchars(trim(strip_tags($input)));
    }
}
?>