import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { IonButtons, IonHeader, IonToolbar } from '@ionic/angular/standalone';
import { IonBackButton } from '@ionic/angular/standalone';
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-page1',
  templateUrl: './knowledge-page1.component.html',
  styleUrls: ['./knowledge-page1.component.scss'],
  standalone: true,
  imports: [
    CommonModule, IonList, IonItem, IonThumbnail, IonContent, IonHeader, IonToolbar, IonButtons, IonBackButton
  ]
})
export class KnowledgePage1Component  implements OnInit {

  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);
      // 可以考虑在这里处理找不到知识的情况，比如导航到错误页面或者显示提示
    }
  }


}
