import { Input, OnInit } from '@angular/core';
import { Component } from '@angular/core';
import { IonHeader, IonToolbar, IonTitle, IonContent, IonCard, IonCardContent, IonButton, IonCardHeader, IonCardTitle, IonCardSubtitle, ModalController, IonInput, IonItem, IonSegment, IonSegmentButton, IonLabel, AlertController } from '@ionic/angular/standalone';
import { CompUploaderHwobsComponent } from 'src/app/comp-uploader-hwobs/comp-uploader-hwobs.component';
import { CloudUser } from 'src/lib/ncloud';
@Component({
  selector: 'app-modal-user-edit',
  templateUrl: './modal-user-edit.component.html',
  styleUrls: ['./modal-user-edit.component.scss'],
  standalone: true,
  imports: [IonHeader, IonToolbar, IonTitle, IonContent, 
    IonCard,IonCardContent,IonButton,IonCardHeader,IonCardTitle,IonCardSubtitle,
    IonInput,IonItem,
    IonSegment,IonSegmentButton,IonLabel,CompUploaderHwobsComponent
  ],
})
export class ModalUserEditComponent  implements OnInit {
  // 上传组件
  // uploadUrl:string = ""
  // onUrlChange(ev:any){
  //   console.log(ev);
  //   this.uploadUrl=ev;
  // }
  currentUser:CloudUser|undefined
  userData:any = {}
  userDataChange(key:string,ev:any){
    if(key=="phone" && ev?.detail?.value.length != 11){
      console.log("手机号格式不正确1")
      return
    }
    let value = ev?.detail?.value
    if(value){
      this.userData[key] = value
    }
  }
  constructor(private modalCtrl:ModalController,
    private alertController: AlertController,) { 
    this.currentUser = new CloudUser();
    this.userData = this.currentUser.data;
  }

  ngOnInit() {}

  async save(){
    let alert = await this.alertController.create({
      header: '温馨提示',
      subHeader: 'Tips',
      message: '手机号格式不正确,请重新输入',
      buttons: ['好的'],
    });
    let alert1 = await this.alertController.create({
      header: '温馨提示',
      subHeader: 'Tips',
      message: '年龄格式不正确,请重新输入',
      buttons: ['好的'],
    });
    let alert2 = await this.alertController.create({
      header: '温馨提示',
      subHeader: 'Tips',
      message: '性别格式不正确,请重新输入',
      buttons: ['好的'],
    });
    let alert3 = await this.alertController.create({
      header: '温馨提示',
      subHeader: 'Tips',
      message: '保存成功',
      buttons: ['好的'],
    });
    // Object.keys(this.userData).forEach(async key=>{
    //   if(key=="phone" && this.userData[key].length != 11){
    //     console.log("手机号格式不正确2")
    //     await alert.present()
    //   }
    //   if(key=="age"){
    //     this.userData[key] = Number(this.userData[key])
    //   }
    // })
    if(this.userData?.gender != "男" && this.userData?.gender != "女"){
      console.log("性别格式不正确")
      await alert2.present()
      return
    }
    if(this.userData?.key == "age"){
      if(this.userData["age"] < 0 || this.userData["age"] > 150){
        console.log("年龄格式不正确")
        await alert1.present()
        return
      }
      this.userData["age"] = Number(this.userData["age"])
    }
    if(this.userData?.phone?.length != 11){
      console.log("手机号格式不正确3")
        await alert.present()
        return 
    }
    this.currentUser?.set(this.userData)
    await this.currentUser?.save()
    await alert3.present()
    this.modalCtrl.dismiss(this.currentUser,"confirm")
  }
  async cancel(){
    let alert = await this.alertController.create({
      header: '温馨提示',
      subHeader: 'Tips',
      message: '取消保存',
      buttons: ['好的'],
    });
    await alert.present()
    this.modalCtrl.dismiss(null,"cancel")
  }
}

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

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

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