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

@Component({
  selector: 'app-modal-identity-verification',
  templateUrl: './modal-identity-verification.component.html',
  styleUrls: ['./modal-identity-verification.component.scss'],
  standalone: true,
  imports: [
    IonCard,
    IonCardHeader,
    IonCardTitle,
    IonCardSubtitle,
    IonCardContent,
    IonButton,
    IonInput,
    IonItem,
    IonLabel,
    IonSelect,
    IonSelectOption,
    FormsModule,
  ],
})
export class ModalIdentityVerificationComponent 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 openIdentityVerificationModal(modalCtrl: ModalController): Promise<CloudUser | null> {
  const modal = await modalCtrl.create({
    component: ModalIdentityVerificationComponent,
    breakpoints: [0.7, 1.0],
    initialBreakpoint: 0.7
  });
  modal.present();

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

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