import { Component, Input, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { IonActionSheet, IonContent, IonHeader, IonIcon, IonInput, IonTextarea, ModalController } from '@ionic/angular/standalone';
import { addIcons } from 'ionicons';
import { arrowBackOutline } from 'ionicons/icons';
import { CloudUser } from 'src/lib/ncloud';
addIcons({ arrowBackOutline })
@Component({
  selector: 'app-modal-user-edit',
  templateUrl: './modal-user-edit.component.html',
  styleUrls: ['./modal-user-edit.component.scss'],
  standalone: true,
  imports: [IonHeader, IonIcon, IonActionSheet, IonContent, IonTextarea, FormsModule, IonInput],

})
export class ModalUserEditComponent implements OnInit {
  option!: string;
  currentUser: CloudUser | undefined;
  name: string = '';
  constructor(private modalCtrl: ModalController) {
    this.currentUser = new CloudUser();
  }
  ngOnInit() { }
  goBack() {
    this.modalCtrl.dismiss(null, 'cancel');
  }
  /**
   * @保存当前字段信息
   * 适用于：昵称，个性签名
   */
  goToSave(option: string) {
    console.warn("------------------\n" + "即将保存" + option + "字段");
    if (this.currentUser?.id) {
      this.currentUser.data[option] = this.name;
    }
    console.log("已保存字段，对象如下：");
    console.log(this.currentUser);
    console.log("即将保存对象到云端...");
    this.currentUser?.set(this.currentUser?.data)
    this.currentUser?.save();
    console.log("保存对象到云端完成")
    console.warn("方法执行完毕" + "\n------------------");
    this.modalCtrl.dismiss(this.currentUser, 'confirm');
  }
  /**
   * @操作选择表样式
   * 适用于：性别
   */
  public genderButtons = [
    {
      text: '男', data: '男',
    },
    {
      text: '女', data: '女',
    },
    {
      text: '取消', data: '取消',
    },
  ];
  logResult(ev: { detail: any; }) {
    console.warn("-------------\n" + "触发点击事件");
    console.log(JSON.stringify(ev.detail, null, 2));
    let gender = ev.detail.data;
    this.currentUser?.set({ gender: gender });
    console.log("保存对象如下:")
    console.log(this.currentUser);
    this.currentUser?.save();
    this.modalCtrl.dismiss(this.currentUser, 'confirm');
    console.warn("方法执行完毕" + "\n-------------")
  }


}

export async function openUserEditModal(modalCtrl: ModalController, option: string): Promise<CloudUser | null> {
  const modal = await modalCtrl.create({
    component: ModalUserEditComponent,
    componentProps: { option },

  });
  await modal.present();

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

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