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

@Component({
  selector: 'app-user-evaluate',
  templateUrl: './user-evaluate.component.html',
  styleUrls: ['./user-evaluate.component.scss'],
  standalone: true,
  imports: [IonCard,IonCardContent,IonButton,IonCardHeader,IonCardTitle,IonCardSubtitle,
    IonInput,IonItem,
  ],
})
export class UserEvaluateComponent  implements OnInit {

  currentUser: CloudUser | undefined;
  userData: any = {
    content: '',
    rating: 0,
  };

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

  userDataChange(key: string, ev: any) {
    let value = ev?.detail?.value;
    if (value) {
      this.userData[key] = value;
    }
  }

  async save() {
    // 确保 currentUser 是有效的 CloudUser 实例
    if (!this.currentUser) {
      console.error('当前用户未登录或未定义');
      return; // 退出保存方法
    }
    const cloudEvaluate = new CloudEvaluate(this.userData, this.currentUser, this.modalCtrl);
    const result = await cloudEvaluate.save(); // 调用 CloudEvaluate 的 save 方法
    if (!result) {
      console.error('评价保存失败');
    }
  }

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

  ngOnInit() {}
}
  export async function openUserEvaModal(modalCtrl:ModalController):Promise<CloudUser|null>{
    const modal = await modalCtrl.create({
      component: UserEvaluateComponent,
      breakpoints:[0.5,0.7],
      initialBreakpoint:0.5
    });
    modal.present();
  
    const { data, role } = await modal.onWillDismiss();
  
    if (role === 'confirm') {
      return data;
    }
    return null

}
