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 {
  filteredDriftbottles2:any[]=[];
  Driftbottles: any[]=[];
  filteredDriftbottles: any[]=[];
  username: String='';
  

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

  ngOnInit() {
    this.getDriftbottles(); // 获取所有漂流瓶并进行过滤
  }

  async getDriftbottles() {
    let query=new CloudQuery("Driftbottle");
    this.Driftbottles = await query.find(); 
      this.filterDriftbottles(); 
      this.filterDriftbottles2(); 

  }

  filterDriftbottles() {
    let user = new CloudUser();
    console.log(user)
    this.filteredDriftbottles = this.Driftbottles.filter(driftbottle => {
      console.log(driftbottle.get("username"), user.get("username"))
      return (driftbottle.get("username") === user.get("username") || driftbottle.get("username") === 'all');
    });
  }
  filterDriftbottles2(){
    let user = new CloudUser();
    console.log(user)
    this.filteredDriftbottles2 = this.Driftbottles.filter(driftbottle => {
      console.log(driftbottle.get("catcher"), user.get("username"))
      return (driftbottle.get("catcher") === user.get("username") || driftbottle.get("catcher") === 'all');
    });
  }

  

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

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