import { Component, OnInit } from '@angular/core';
import { IonHeader, IonToolbar, IonTitle, IonContent, IonCard, IonCardContent, IonButton, IonCardHeader, IonCardTitle, IonCardSubtitle, ModalController, IonButtons, IonBackButton } from '@ionic/angular/standalone';
import { EditTagComponent } from '../edit-tag/edit-tag.component';
import { CloudUser } from 'src/lib/ncloud';
import { openUserLoginModal } from 'src/lib/user/model-user-login/model-user-login.component';
import { openUserEditModal } from 'src/lib/user/model-user-edit/model-user-edit.component';

@Component({
  selector: 'app-user-login',
  templateUrl: './user-login.component.html',
  styleUrls: ['./user-login.component.scss'],
  standalone: true,
  imports: [IonHeader, IonToolbar, IonTitle, IonContent,
    IonCard, IonCardContent, IonButton, IonCardHeader, IonCardTitle, IonCardSubtitle,
    EditTagComponent, IonButtons, IonBackButton,
  ],
})
export class UserLoginComponent implements OnInit {
  ngOnInit(): void {

  }
  currentUser: CloudUser | undefined
  constructor(private modalCtrl: ModalController) {
    this.currentUser = new CloudUser();
  }
  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();
  }

  editUser() {
    openUserEditModal(this.modalCtrl)
  }

  editTags: Array<String> = []
  async setTagsValue(ev: any) {
    let currentUser = new CloudUser();
    let userPrompt = ``
    if (!currentUser?.id) {
      console.log("用户未登录，请登录后重试");
      let user = await openUserLoginModal(this.modalCtrl);
      if (!user?.id) {
        return
      }
      currentUser = user;
    }
    //console.log("setTagsValue",ev);
    this.editTags = ev;
  }

}
