import { Component,CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { IonHeader, IonToolbar, IonTitle, IonContent, IonAvatar, IonButton, IonButtons, IonCard, IonCardContent, IonCardHeader, IonLabel, IonList,
   IonItem,IonIcon,IonInput,IonSearchbar} from '@ionic/angular/standalone';
import { ExploreContainerComponent } from '../explore-container/explore-container.component';
import { CommonModule } from '@angular/common';
import {  TopicDetailComponent } from '../topic-detail/topic-detail.component';
import { Router } from '@angular/router';
import { TopicDetail2Component } from '../topic-detail2/topic-detail2.component';
import { TopicDetail3Component } from '../topic-detail3/topic-detail3.component';
import { CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';
import { ModalController } from '@ionic/angular/standalone';
import { openUserEvaModal } from '../user-evaluate/user-evaluate.component';
import { openUserLoginModal } from 'src/lib/user/modal-user-login/modal-user-login.component';
@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss'],
  standalone: true,
  imports: [IonHeader,IonToolbar,IonTitle,IonContent,ExploreContainerComponent,
    IonButtons,IonButton,IonIcon,
    IonCard,IonCardContent,IonCardHeader,
    IonLabel,IonList,IonItem,IonAvatar,
    IonInput,IonSearchbar,
    CommonModule,
    TopicDetailComponent,TopicDetail2Component,TopicDetail3Component
  ],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class Tab1Page {
  onSearch(event: any) {
    const searchTerm = event.target.value; // 获取用户输入的搜索内容
    console.log('搜索内容:', searchTerm);
    // 在这里添加搜索逻辑，例如过滤列表或导航到搜索结果页面
  }
  topics = [
    {
      id: 1,
      title: '如何管理焦虑情绪',
      description: '了解焦虑的来源及应对策略',
      detailRoute: 'topic-detail',
    },
    {
      id: 2,
      title: '克服抑郁的有效方法',
      description: '探索抑郁症的应对技巧',
      detailRoute: 'topic-detail2'
    },
    {
      id: 3,
      title: '提升自信心的技巧',
      description: '学习如何建立自信',
      detailRoute: 'topic-detail2'
    }
  ];
  
  goPsysurvey() {
    this.router.navigate(['tabs/page-psysurvey'])
  }
  goDriftbottle() {
    this.router.navigate(['tabs/drift-bottle'])
  }
  
  viewDetails(topicId: number): void {
    let route: string;
    switch (topicId) {
      case 1:
        route = 'topic-detail';
        break;
      case 2:
        route = 'topic-detail2';
        break;
      case 3:
        route = 'topic-detail3';
        break;
      default:
        route = 'topic-detail'; // 默认路由
    }
    // 导航到指定的路由，并可以传递参数
    this.router.navigate([`tabs/${route}`, { id: topicId }]);
  }
  constructor(private router: Router,private modalCtrl: ModalController) {
    // 其他构造函数代码
  }
  matchedCounselor: { content: string; rating: number } | null = null;
  review: any = { rating: 5 };  // 示例数据
  // 创建一个方法，用于生成填充了 null 的数组
  createFilledArray(length: number): any[] {
    return Array(length).fill(null);
  }
  async evaluate() {
    // 处理点击评价的逻辑
    // 验证用户登录
    let currentUser = new CloudUser();
    let userPrompt = ``
    if(!currentUser?.id){
      console.log("用户未登录，请登录后重试")
      let user = await openUserLoginModal(this.modalCtrl)
      if(!user?.id){
        console.log("用户登录失败");
        return
      } 
      else {
        console.log("当前用户ID:", currentUser.id);
      }    
    }
     // 打开评价模态框
     const evaluationResult = await openUserEvaModal(this.modalCtrl);
     // 如果评价成功，重新加载评价列表
     if (evaluationResult) {
         await this.loadChatEvaluateList(); // 重新加载评价列表
     }
  }
  ngOnInit() {
    this.loadChatEvaluateList()
  }
  chatevaluateList:Array<CloudObject>=[]
  async loadChatEvaluateList(){
    let query = new CloudQuery("ChatEvaluate");
    this.chatevaluateList = await query.find()
  }
}