import { Component, OnInit } from '@angular/core';
import { ItemsService } from './items.service';

// 引用Parse JS SDK
import Parse from "parse";
Parse.initialize("dev"); // 设置applicationId
Parse.serverURL = "http://web2023.fmode.cn:9999/parse"; // 设置serverURL

@Component({
  selector: 'app-tab4',
  templateUrl: './tab4.page.html',
  styleUrls: ['./tab4.page.scss'],
})
export class Tab4Page implements OnInit {

  itemsList: any[] = [];
  searchTitle: string = '';
  selectedCategory: string = '';

  constructor(private itemsService: ItemsService) { }

  ngOnInit() {
    this.getItems();
  }

  async getItems() {
    this.itemsList = await this.itemsService.getItems({});
  }

  async searchItems() {
    this.itemsList = await this.itemsService.getItems({ title: this.searchTitle });
  }

  async filterItemsByCategory(category: string) {
    this.selectedCategory = category;
    this.itemsList = await this.itemsService.getItems({ category });
  }

  getDetailPageLink(item: any) {
    let category = item.get('category');
    let route = '';
    if (category === '求助') {
      route = '/resort';
    } else if (category === '二手交易') {
      route = '/trade';
    } else if (category === '失物招领') {
      route = '/lostfound';
    } else if (category === '活动') {
      route = '/activity';
    }
    return [route, { id: item.id, category: category }];
  }

}
