import { OnInit } from '@angular/core';
import { Component } from '@angular/core';
import { ModalController, IonInput, IonItem, IonLabel, IonButton, IonCard, IonCardHeader, IonCardTitle, IonCardSubtitle, IonCardContent } from '@ionic/angular/standalone';
import { CloudUser } from 'src/lib/ncloud';

@Component({
  selector: 'app-user-identity-verification-modal',
  templateUrl: './user-identity-verification-modal.html',
  styleUrls: ['./user-identity-verification-modal.scss'],
  standalone: true,
  imports: [
    IonCard,
    IonCardHeader,
    IonCardTitle,
    IonCardSubtitle,
    IonCardContent,
    IonButton,
    IonInput,
    IonItem,
    IonLabel
  ],
})
export class UserIdentityVerificationModal implements OnInit {
  currentUser: CloudUser | undefined;
  userData: any = {
    school: '',
    identity: '',
    studentID: '',
    department: '',
    teacherID: ''
  };

  constructor(private modalCtrl: ModalController) {
    this.currentUser = new CloudUser();
  }

  ngOnInit() {}

  onIdentityChange() {
    // 根据身份选择进行处理
    if (this.userData.identity === 'student') {
      this.userData.teacherID = ''; // 清空教师ID字段
    } else if (this.userData.identity === 'teacher') {
      this.userData.studentID = ''; // 清空学号字段
      this.userData.department = ''; // 清空学院字段
    }
  }

  async save() {
    this.currentUser?.set(this.userData);
    await this.currentUser?.save();
    this.modalCtrl.dismiss(this.currentUser, "confirm");
  }

  cancel() {
    this.modalCtrl.dismiss(null, "cancel");
  }
}

export async function openUserIdentityVerificationModal(modalCtrl: ModalController): Promise<CloudUser | null> {
  const modal = await modalCtrl.create({
    component: UserIdentityVerificationModal,
    breakpoints: [0.7, 1.0],
    initialBreakpoint: 0.7
  });
  modal.present();

  const { data, role } = await modal.onWillDismiss();

  if (role === 'confirm') {
    return data;
  }
  return null;
}