import { Component, OnInit,Input } from '@angular/core';
import { IonButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar, ModalController } from '@ionic/angular/standalone';

@Component({
  selector: 'app-report-modal',
  templateUrl: './report-modal.component.html',
  styleUrls: ['./report-modal.component.scss'],
  standalone: true,
  imports: [IonHeader,IonToolbar,IonTitle,
    IonButtons,IonButton,IonContent
  ],
})
export class ReportModalComponent  implements OnInit {

  @Input() report: string=''; // 接收报告内容
  @Input() consult: any; // 接收 Consult 对象

  constructor(private modalCtrl: ModalController) {}

  async generateReport() {
    if (!this.consult) {
      console.error('Consult对象未定义');
      return;
    }
    let chatContent = this.consult.get('content') || [];
    let report = "聊天记录报告\n\n";
    
    if (chatContent.length === 0) {
      report += "没有聊天记录。\n\n";
    } else {
      chatContent.forEach((message: any, index: number) => {
        report += `消息 ${index + 1}:\n\n`;
        report += `内容: ${message.content}\n\n`;
        
        const sentiment = this.analyzeSentiment(message.content);
        report += `心晴分析: ${sentiment}\n\n`;
      });
    }
    
    this.report = report; // 更新报告内容
  }

  analyzeSentiment(text: string): string {
    if (text.includes("好") || text.includes("喜欢")) {
      return "积极";
    } else if (text.includes("坏") || text.includes("不喜欢")) {
      return "消极";
    } else {
      return "中性";
    }
  }

  closeModal() {
    //this.modalCtrl.dismiss(); // 关闭模态框
    this.modalCtrl.dismiss({ report: this.report }); // 关闭模态框并返回报告内容
  }

  ionViewWillEnter() {
    this.generateReport(); // 页面进入时生成报告
  }

  ngOnInit() {}

}
