import { Component, OnInit } from '@angular/core';
import { NavComponent } from '../../../app/components/nav/nav.component';
import { InfiniteScrollCustomEvent, ToastController } from '@ionic/angular';
import * as Parse from 'parse';
import { ionicStandaloneModules } from '../../ionic-standalone.modules';
import { SharedModule } from '../../shared.module';
import { Router } from '@angular/router';
@Component({
  selector: 'app-friends',
  templateUrl: './friends.component.html',
  styleUrls: ['./friends.component.scss'],
  standalone: true,
  imports: [...ionicStandaloneModules, NavComponent, SharedModule],
})
export class FriendsComponent implements OnInit {
  active: string = 'from';
  list: Array<Parse.Object> = [];
  disbable: boolean = true;

  constructor(private router: Router) {}

  ngOnInit() {
    this.getBrowseHistory();
  }
  segmentChanged(e: any) {
    let { value } = e.detail;
    this.active = value;
    // console.log(this.active);
    this.list = [];
    this.disbable = true;
    this.getBrowseHistory();
  }

  async getBrowseHistory() {
    let query = new Parse.Query('ProfileRadar');
    query.equalTo('name', '关注');
    query.equalTo(
      this.active == 'from' ? 'fromUser' : 'toUser',
      Parse.User.current()
    );
    query.descending('createdAt');
    query.limit(20);
    query.skip(this.list.length);
    query.include('fromUser', 'toUser');
    let r = await query.find();
    this.list.push(...r);
    return r;
  }

  async onIonInfinite(ev: any) {
    let result = await this.getBrowseHistory();
    if (result.length == 0) {
      this.disbable = false;
    }
    setTimeout(() => {
      (ev as InfiniteScrollCustomEvent).target.complete();
    }, 500);
  }
  toUrl(id: string) {
    this.router.navigate(['/user/profile/' + id]);
  }
}
