import { Component, OnInit } from '@angular/core';
import { IonHeader, IonToolbar, IonTitle, IonContent, IonButton, IonSegment, IonSegmentButton, IonSegmentContent, IonSegmentView, IonIcon, IonText, IonLabel, IonCard, IonImg } from '@ionic/angular/standalone';
import { ExploreContainerComponent } from '../explore-container/explore-container.component';
import { Router } from '@angular/router';
import { addIcons } from 'ionicons';
import { addOutline, chevronForwardCircle } from 'ionicons/icons';
import { CloudObject, CloudQuery } from 'src/lib/ncloud';
import { CommonModule } from '@angular/common';
import { PostListComponent } from 'src/lib/component/post-list/post-list.component';
addIcons({ addOutline, chevronForwardCircle })

@Component({
  selector: 'app-tab2',
  templateUrl: 'tab2.page.html',
  styleUrls: ['tab2.page.scss'],
  standalone: true,
  imports: [IonHeader, IonToolbar, IonTitle, IonContent, ExploreContainerComponent, IonButton,
    IonSegment, IonSegmentButton, IonSegmentContent, IonSegmentView, IonIcon, IonText, IonLabel,
    IonCard, IonImg, CommonModule, PostListComponent
  ]
})
export class Tab2Page implements OnInit {
  constructor(private router: Router) {}
  ngOnInit() {
    this.getTop3Tag();
  }
  ionViewWillEnter() {
    this.getTop3Tag();
  }
  /**
   * @读取top3话题
   */
  tags: Array<CloudObject> = [];
  async getTop3Tag() {
    let query = new CloudQuery("tag");
    let tags = await query.find();
    this.tags = tags;
    tags.sort((a, b) => b.get('click') - a.get('click'));
    this.tags = this.tags.slice(0, 3);
    console.log("查询top3话题结果如下：")
    console.log(this.tags);
  }
  // 格式化数字的方法
  formatNumber(num: number): string {
    if (num >= 10000) {
      return (num / 10000).toFixed(1) + 'w';
    } else if (num >= 1000) {
      return (num / 1000).toFixed(1) + 'k';
    } else {
      return num + '';
    }
  }
  goSendPost() {
    this.router.navigate(['/sendPost']);
  }
  posts: Array<CloudObject> = [];
  async shuaxin() {
    let query = new CloudQuery("post");
    query.include("UserID");
    let obj = new CloudObject("_User");
    obj.id = "e3wWcCfNwm";
    query.equalTo("UserID", obj.toPointer())
    console.log(query.queryParams)
    console.log(query);
    try {
      let posts = await query.find();
      this.posts = posts;
      console.log(this.posts);
    } catch (error) {
      console.error("查询失败:", error);
      this.posts = []; // 确保即使查询失败，posts 也被初始化为一个空数组
    }
  }
}
