import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import {
  ionicStandaloneModules,
  LoadingController,
} from '../../ionic-standalone.modules';
import * as Parse from 'parse';
import { AiChatService } from '../../../services/aichart.service';
import { InfiniteScrollCustomEvent } from '@ionic/core';
import { NavComponent } from '../../../app/components/nav/nav.component';
import { CommonModule, DatePipe } from '@angular/common';

@Component({
  selector: 'app-gift-income',
  templateUrl: './gift-income.component.html',
  styleUrls: ['./gift-income.component.scss'],
  standalone: true,
  imports: [...ionicStandaloneModules, NavComponent,CommonModule],
  providers: [DatePipe],
})
export class GiftIncomeComponent implements OnInit {
  uid: string = '';
  giftList: Array<Parse.Object> = [];

  constructor(
    private activateRoute: ActivatedRoute,
    private loadingCtrl: LoadingController,
    public aiChatServ: AiChatService
  ) {}

  ngOnInit() {
    this.activateRoute.paramMap.subscribe(async (params) => {
      let uid = params.get('uid');
      this.uid = uid;
      this.getCommentLeng();
    });
  }

  async getCommentLeng() {
    const loading = await this.loadingCtrl.create({
      message: '加载中',
    });
    loading.present();
    let queryPost = new Parse.Query('LoveRender');
    queryPost.equalTo('toUser', this.uid);
    queryPost.notEqualTo('isDeleted', true);
    queryPost.include('fromUser');
    queryPost.descending('createdAt');
    queryPost.limit(20);
    queryPost.skip(this.giftList.length);
    let r = await queryPost.find();
    this.giftList.push(...r);
    loading.dismiss();
    return r;
  }
  async onIonInfinite(ev: any) {
    let result = await this.getCommentLeng();
    if (result.length == 0) {
      (ev as InfiniteScrollCustomEvent).target.disabled = true;
    }
    setTimeout(() => {
      (ev as InfiniteScrollCustomEvent).target.complete();
    }, 500);
  }
}
