import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { CommonModule } from '@angular/common'; // 导入 CommonModule

import { IonHeader, IonButton, IonContent, IonIcon, IonItem, IonLabel, IonList, 
  IonListHeader, IonCardHeader, IonCardTitle, IonCardContent, IonTitle, IonCard, IonToolbar, IonInput, IonSearchbar } from '@ionic/angular/standalone';
import { CloudQuery, CloudUser } from 'src/lib/ncloud';
import { UserService } from 'src/app/user.service'; // 假设这是你的用户服务

@Component({
  selector: 'app-my-drift-bottle',
  templateUrl: './my-drift-bottle.component.html',
  styleUrls: ['./my-drift-bottle.component.scss'],
  standalone: true,
  imports: [
    CommonModule, // 添加 CommonModule
    
    IonHeader, IonToolbar, IonTitle, IonContent,
    IonList, IonListHeader, IonItem, IonCardTitle,
    IonLabel, IonIcon, IonButton, IonCardContent,
    IonInput, IonSearchbar, IonCard, IonCardHeader,
  ]
})
export class MyDriftBottleComponent implements OnInit {
  thrownDriftbottles: any[] = [];
  caughtDriftbottles: any[] = [];

  constructor(private router: Router, private userService: UserService) {}

  async ngOnInit() {
    await this.loadThrownDriftbottles();
    await this.loadCaughtDriftbottles();
  }

  async loadThrownDriftbottles() {
    const query = new CloudQuery('Driftbottle');
    query.equalTo('username', this.userService.getUsername());
    try {
      const results = await query.find();
      this.thrownDriftbottles = results.map(result => ({
        content: result.get('content'),
        createdAt: result.createdAt.toISOString().split('T')[0]
      }));
    } catch (error) {
      console.error('加载扔出的漂流瓶时出错', error);
    }
  }

  async loadCaughtDriftbottles() {
    const query = new CloudQuery('Driftbottle');
    query.equalTo('username', this.userService.getUsername()); // 假设捞上的漂流瓶不是自己的
    try {
      const results = await query.find();
      this.caughtDriftbottles = results.map(result => ({
        content: result.get('content'),
        createdAt: result.createdAt.toISOString().split('T')[0]
      }));
    } catch (error) {
      console.error('加载捞上的漂流瓶时出错', error);
    }
  }

  goDriftbottle() {
    this.router.navigate(['tabs/drift-bottle']);
  }

  throwNewDriftbottle() {
    this.router.navigate(['tabs/throw-drift-bottle']); // 假设这是投掷新漂流瓶的路由
  }
}