import { Injectable } from '@angular/core';
import Parse from 'parse';

@Injectable({
  providedIn: 'root'
})
export class ItemsService {
  private Items = Parse.Object.extend('Items');

  constructor() {}

  async getItems(queryParams: any): Promise<Parse.Object[]> {
    const query = new Parse.Query(this.Items);
    if (queryParams.title) {
      query.contains('title', queryParams.title);
    }//标题 string
    if (queryParams.category) {
      query.equalTo('category', queryParams.category);
    }//类型：”求助“”二手交易“”失物招领“”活动“ string
    if (queryParams.publisher) {
      query.equalTo('publisher', queryParams.publisher);
    }//发布人 string
    if (queryParams.headImage) {
      query.equalTo('headImage', queryParams.headImage);
    }//头像 string
    if (queryParams.image) {
      query.equalTo('image', queryParams.image);
    }//图片 string
    if (queryParams.content) {
      query.equalTo('content', queryParams.content);
    }//内容 string
    if (queryParams.amount) {
      query.equalTo('amount', queryParams.amount);
    }//金额 Number
    if (queryParams.location) {
      query.equalTo('location', queryParams.location);
    }//地点 string
    if (queryParams.duration) {
      query.equalTo('duration', queryParams.duration);
    }//持续时长 Number
    // if (queryParams.startDate) {
    //   query.equalTo('startDate', queryParams.startDate);
    // }//开始时间 Date
    if (queryParams.status) {
      query.equalTo('status', queryParams.status);
    }//丢失或拾取 string
    if (queryParams.target) {
      query.equalTo('target', queryParams.target);
    }//参与对象 string
    if (queryParams.time) {
      query.equalTo('time', queryParams.time);
    }//时间 string
    if (queryParams.name) {
      query.equalTo('name', queryParams.name);
    }//名称 string
    if (queryParams.repliesId) {
      query.equalTo('repliesId', queryParams.repliesId);
    }//回复 string
    // query.contains('title', queryParams.title);
    // query.equalTo('category', queryParams.category);
    // query.equalTo('publisher', queryParams.publisher);
    // query.equalTo('headImage', queryParams.headImage);
    // query.equalTo('image', queryParams.image);
    // query.equalTo('content', queryParams.content);
    // query.equalTo('amount', queryParams.amount);
    // query.equalTo('location', queryParams.location);
    // query.equalTo('duration', queryParams.duration);
    // query.equalTo('startDate', queryParams.startDate);
    // query.equalTo('status', queryParams.status);
    // query.equalTo('target', queryParams.target);
    // query.equalTo('time', queryParams.time);
    // query.equalTo('name', queryParams.name);
    // query.equalTo('repliesId', queryParams.repliesId);
    return await query.find();
  }

  async saveItem(data: any): Promise<Parse.Object> {
    const item = new this.Items();
    item.set('title', data.title);
    item.set('category', data.category);
    item.set('publisher', data.publisher);
    item.set('headImage', data.headImage);
    item.set('image', data.image);
    item.set('content', data.content);
    item.set('amount', data.amount);
    item.set('location', data.location);
    item.set('duration', data.duration);
    item.set('startDate', data.startDate);
    item.set('status', data.status);
    item.set('target', data.target);
    item.set('time', data.time);
    item.set('name', data.name);
    item.set('repliesId', data.repliesId);

    try {
      const savedItem = await item.save();
      return savedItem;
    } catch (error) {
      console.error('保存数据失败:', error);
      throw error;
    }
  }
}