
import { Component, OnInit } from '@angular/core';
import { IonAvatar, IonCardTitle, IonItem, IonLabel, IonSearchbar, NavController } from '@ionic/angular/standalone';
import { PageEditComponent } from '../page-edit/page-edit.component';
import { CloudUser } from 'src/app/lib/ncloud';
import { openUserLoginModal } from 'src/app/lib/user/modal-user-login/modal-user-login.component';
import { ModalController } from '@ionic/angular/standalone';
import { IonButton, IonCard, IonCardContent, IonCardHeader, IonCol, IonContent, IonGrid, IonHeader, IonIcon, IonInput, IonRow, IonTextarea, IonTitle, IonToolbar } from '@ionic/angular/standalone';

@Component({
  selector: 'app-tab3',
  templateUrl: './tab3.page.html',
  styleUrls: ['./tab3.page.scss'],
  standalone: true,
  imports: [
    IonContent, IonHeader, IonTitle, IonToolbar, // 引入 IonicModule
    IonButton,IonTextarea,IonInput,IonCard,IonCardContent,IonGrid,IonRow,IonCol,IonIcon,
    IonCardHeader,IonItem,IonAvatar,IonSearchbar,IonLabel,IonCardTitle,
  ],
})
export class Tab3Page implements OnInit {

  currentUser:CloudUser|undefined
  // 用户信息数据
  userData = {
    name: '',  // 默认值，可以为空测试默认显示“游客”
    avatar: 'assets/img/user-avatar.jpg'  // 默认头像路径
  };

  constructor(private navCtrl: NavController, private modalCtrl: ModalController) {
    this.currentUser = new CloudUser();
  }
  
  ngOnInit() {
    // 在这里可以加载用户信息（如果从API或服务中获取）
  }

  async login(){
    // 弹出登录窗口
    let user = await openUserLoginModal(this.modalCtrl);
    if(user?.id){
      this.currentUser = user
    }
  }

  async signup(){
    // 弹出注册窗口
    let user = await openUserLoginModal(this.modalCtrl,"signup");
    if(user?.id){
      this.currentUser = user
    }
  }

  logout(){
    this.currentUser?.logout();
  }
  
  async goToEdit() {
    // 打开编辑模态框
    const modal = await this.modalCtrl.create({
      component: PageEditComponent,
      componentProps: {
        userData: this.userData  // 将当前用户数据传递给编辑页面
      }
    });
    modal.present();

    // 等待编辑页面关闭并获取数据
    const { data, role } = await modal.onWillDismiss();

    if (role === 'confirm') {
      // 更新用户信息
      this.userData = data;
    }
  }

  goToFavorites() {
    this.navCtrl.navigateForward('/favorites');
  }

  goToHelp() {
    this.navCtrl.navigateForward('/help');
  }

  goToSettings() {
    this.navCtrl.navigateForward('/settings');
  }
}
