import { Component, OnInit } from '@angular/core';
import { PetService } from './pet.service';
import Parse from "parse";
@Component({
  selector: 'app-case-parse-petmarket',
  templateUrl: './case-parse-petmarket.page.html',
  styleUrls: ['./case-parse-petmarket.page.scss'],
})
export class CaseParsePetmarketPage implements OnInit {
  pets: any[] = [];
  searchQuery: string = '';
  selectedType: string = '';
  petTypes: string[] = ['狗', '猫', '蟑螂', '羊驼'];
  sortByPrice: boolean = false;

  constructor(private petService: PetService) {}
  user:Parse.User|undefined
  ngOnInit() {
    this.user = Parse.User.current();
    this.fetchPets();
  }

  async fetchPets() {
    const queryParams = {
      name: this.searchQuery,
      type: this.selectedType,
      sortByPrice: this.sortByPrice
    };
    this.pets = await this.petService.getPets(queryParams);
  }

  toggleSort() {
    this.sortByPrice = !this.sortByPrice;
    this.fetchPets();
  }
}
