import { Component, OnInit } from '@angular/core';
import { NavComponent } from '../../../app/components/nav/nav.component';
import { ionicStandaloneModules } from '../../ionic-standalone.modules';
import * as Parse from 'parse';
import { CommonModule, DatePipe } from '@angular/common';
import { Router } from '@angular/router';
import { InfiniteScrollCustomEvent } from '@ionic/angular';
@Component({
  selector: 'app-order',
  templateUrl: './order.component.html',
  styleUrls: ['./order.component.scss'],
  standalone: true,
  imports: [...ionicStandaloneModules, NavComponent,CommonModule],
  providers: [DatePipe],
})
export class OrderComponent implements OnInit {
  list: Array<any> = []
  constructor(
    private router: Router,
  ) { }

  ngOnInit() {
    this.getOrder()
  }
  async getOrder(){
    let query = new Parse.Query('Order');
    query.notEqualTo('isDeleted', true);
    query.equalTo('user',Parse.User.current()?.id);
    query.limit(20)
    query.skip(this.list.length)
    query.select('price','status','targetObject.name')
    query.descending('createdAt')
    let log = await query.find();
    this.list.push(...log)
    return log
  }
  toUrl(id:string){
    this.router.navigate(['account/order/detail/'+id]);
  }

  async onIonInfinite(ev:any) {
    let result = await this.getOrder();
    if(result.length == 0) {
      (ev as InfiniteScrollCustomEvent).target.disabled = true;
    }
    setTimeout(() => {
      (ev as InfiniteScrollCustomEvent).target.complete();
    }, 500);
  }
}
