import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NavController } from '@ionic/angular';
import Parse from "parse";
@Component({
  selector: 'app-case-pet-edit',
  templateUrl: './case-pet-edit.page.html',
  styleUrls: ['./case-pet-edit.page.scss'],
})
export class CasePetEditPage implements OnInit {

  constructor(
    private route:ActivatedRoute,
    private navCtrl:NavController
  ) { 

  }

  // 宠物相关可编辑字段
  FoodcourtInfo:any = {

  }
  async save(){
    // 将petInfo当前编辑元素，逐个set到this.pet的Parse.Object里面去
    console.log(this.FoodcourtInfo)
    console.log(this.Foodcourt)
    Object.keys(this.FoodcourtInfo).forEach(key=>{
      this.Foodcourt?.set(key,this.FoodcourtInfo[key])
    })
    // 判断，当创建宠物无创建者，则将Parse.User的.toPointer()指针，赋值给user字段
    if(!this.Foodcourt?.get("user")?.id) this.Foodcourt?.set("user",this.user?.toPointer())
    await this.Foodcourt?.save()
    this.navCtrl.back();
  }
  cancel(){
    this.navCtrl.back();
  }

  user:Parse.User|undefined
  ngOnInit() {
    this.user = Parse.User.current();
    this.loadFoodcourtById()
  }

  Foodcourt:Parse.Object|undefined
  async loadFoodcourtById(){
    // let id = location.pathname.split("/").pop();
    let id = this.route.snapshot.params["id"]

    if(id&&id!="new"){
      let query = new Parse.Query("Foodcourt");
      this.Foodcourt = await query.get(id);
    }else{
      let Foodcourt = Parse.Object.extend("Foodcourt");
      this.Foodcourt = new Foodcourt();
    }
    this.FoodcourtInfo = this.Foodcourt?.toJSON()
  }

}
