import { Component, OnInit } from '@angular/core';
import { IonHeader, IonToolbar, IonTitle, IonContent, IonCard, IonCardContent, IonButton, 
  IonCardHeader, IonCardTitle, IonCardSubtitle, ModalController, IonInput, IonItem, 
  IonSegment, IonSegmentButton, IonLabel } from '@ionic/angular/standalone';
import { CloudPost, CloudUser } from 'src/lib/ncloud';
@Component({
  selector: 'app-post-publisher',
  templateUrl: './post-publisher.component.html',
  styleUrls: ['./post-publisher.component.scss'],
  standalone: true,
  imports: [IonHeader, IonToolbar, IonTitle, IonContent, 
    IonCard,IonCardContent,IonButton,IonCardHeader,IonCardTitle,IonCardSubtitle,
    IonInput,IonItem,
    IonSegment,IonSegmentButton,IonLabel
  ],
})
export class PostPublisherComponent  implements OnInit {
 

  postData:any={

  }

  postDataChange(key:string,env:any){
    this.postData[key] = env.detail.value; // 更新 postData 对象
 
  }

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

  }

  
  async save(){
    const post = new CloudPost();
    post.setPostData(this.postData, this.currentUser.id); // 设置帖子数据和用户ID

    try {
      await post.save(); // 保存帖子
      this.modalCtrl.dismiss(); // 保存成功后关闭模态框
    } catch (error) {
      console.error('保存帖子失败:', error);
    }

  }
  cancel(){
    this.modalCtrl.dismiss(null,"cancel")// 关闭模态框

  }

  ngOnInit() {}

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

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

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