import { Component } from '@angular/core';
import { CloudUser } from '../../lib/ncloud';
import { Router } from '@angular/router';
import { IonicModule } from '@ionic/angular';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-register',
  templateUrl: 'register.page.html',
  styleUrls: ['register.page.scss'],
  imports: [IonicModule, CommonModule, FormsModule],
  standalone: true
})
export class RegisterPage {
  user = new CloudUser();
  registerUsername = '';
  registerPassword = '';
  registerEmail = '';

  constructor(private router: Router) {}

  async register() {
    if (!this.registerUsername || !this.registerPassword || !this.registerEmail) {
      alert('请输入用户名、密码和邮箱');
      return;
    }

    try {
      this.user.set({
        username: this.registerUsername,
        password: this.registerPassword,
        email: this.registerEmail
      });
      await this.user.save();
      alert('注册成功，请登录');
      this.router.navigate(['/tabs/tab5']); // 跳转回登录界面
    } catch (error) {
      console.error('注册失败:', error);
      alert('注册失败，请重试');
    }
  }

  // 添加返回登录页面的方法
  goBackToLogin() {
    this.router.navigate(['/tabs/tab5']);
  }
}