import { Component, OnInit } from '@angular/core';
import { IonAvatar, IonButton, IonCard, IonCardHeader, IonCardSubtitle, IonCardTitle, IonContent, IonHeader, IonIcon, IonInput, IonItem, IonList, IonTitle, IonToolbar, ModalController } from '@ionic/angular/standalone';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { NavigationExtras, Router } from '@angular/router';
import { PostService } from '../post.service'; // 导入服务
import { IonButtons } from '@ionic/angular/standalone';
import { IonCardContent } from '@ionic/angular/standalone';
import { CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';
import { openUserLoginModal } from 'src/lib/user/modal-user-login/modal-user-login.component';

@Component({
  selector: 'app-community',
  templateUrl: './community.page.html',
  styleUrls: ['./community.page.scss'],
  standalone: true,
  imports: [IonHeader,IonToolbar,IonButtons,IonIcon,IonButton,IonContent,IonCard,IonCardHeader,
    IonItem,IonAvatar,IonCardTitle,IonCardSubtitle,FormsModule,CommonModule,IonCardContent,IonTitle
    ],
})
export class CommunityPage implements OnInit {
  posts: { id: number; username: string; date: Date; text: string; imageUrl?: string; avatarUrl?: string }[] = [];

  constructor(private router: Router,private postService: PostService,
    private modalCtrl: ModalController,

  ) {
  }

  ngOnInit() {
    this.posts = this.postService.getPosts(); // 从服务中获取动态数据// 这里可以进行数据的初始化或获取
  }

  // viewPost(postId: number) {

  //   const state: any = {  postId:  postId }; // 你要传递的对象数据
  //   const navigationExtras: NavigationExtras = {
  //     state: state
  //   };
  //   console.log("state::",state);
    
   
  //   this.router.navigate(['/tabs/post-page', postId]);

  //   // this.router.navigate(['/tabs/post-page', postId]); // 导航到动态详情页面
  // }
  viewPost(postId: number) {
    // 直接导航到动态详情页面
    this.router.navigate(['/post-page', postId]);
  }
  async sharePost() {
    let currentUser = new CloudUser();

    if (!currentUser?.id) {
      console.log("用户未登录，请登录后重试");
      let user = await openUserLoginModal(this.modalCtrl);
      if (!user?.id) {
        return
      }
      currentUser = user;
    }
    this.router.navigate(['/tabs/share-page']); // 导航到分享动态页面
  }
}