import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { IonContent } from '@ionic/angular/standalone';
import { IonItem, IonList, IonThumbnail } from '@ionic/angular/standalone';
import { CloudObject, CloudQuery } from 'src/lib/ncloud';

@Component({
  selector: 'app-knowledge-page',
  templateUrl: './knowledge-page.component.html',
  styleUrls: ['./knowledge-page.component.scss'],
  standalone: true,
  imports: [
    CommonModule, IonList, IonItem, IonThumbnail, IonContent
  ]
})
export class KnowledgePageComponent implements OnInit {

  // constructor() { }

  // ngOnInit() {
  //   // 生命周期：页面加载后，运行医生列表加载函数
  //   this.loadKnowledgeList()
  // }

  // // 创建用于数据列表存储的属性
  // knowledgeList:Array<CloudObject> = []
  // // 查询并加载医生列表的函数
  // async loadKnowledgeList(){
  //   let query = new CloudQuery("Knowledge");
  //   query.equalTo("objectId", "4YJevU7uTe");
  //   this.knowledgeList = await query.find()
  // }

  // 在构造函数中注入 ActivatedRoute
  knowledge: CloudObject | null = null; // 初始化为 null

  constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    const objectId = this.route.snapshot.paramMap.get('id'); // 获取传递的 ObjectId
    if (objectId) { // 检查 objectId 是否为 null
      this.loadKnowledge(objectId); // 加载对应的知识信息
    } else {
      console.error("No ObjectId found in route parameters.");
    }
  }

  async loadKnowledge(objectId: string) {
    let query = new CloudQuery("Knowledge");
    query.equalTo("title", objectId); // 根据 ObjectId 查询
    const knowledge = await query.first(); // 获取单条记录
    if (knowledge) {
      this.knowledge = knowledge; // 将获取的知识信息赋值给 knowledge 属性
    } else {
      console.error("Knowledge not found for ObjectId:", objectId);
      // 可以考虑在这里处理找不到知识的情况，比如导航到错误页面或者显示提示
    }
  }

}
