class CloudObject{//创建
    id
    className
    data
    constructor(className){
        this.className=className
        this.data={}
    }
    set(json){
        object.keys(json).forEach(key=>{
            if(["objectId","id","createAt","updatedAt","ACL"].indexOf(key)>-1){
                return
            }
            this.data[key]=json[key]
        })
    }
    get(key){
        return this.data[key] || null
    }
    async save(){
        let method = this.id ? "PUT" : "POST";  // 如果有id则为PUT，否则为POST
        let url="http://dev.fmode.cn:1337/parse/classes/"+this.className
        if(this.id){
            url=url+"/"+this.id
        }
        let body = JSON.stringify(this.data);
        let response = await fetch(url, {
        "headers": {
          "content-type": "application/json;charset=UTF-8",
          "x-parse-application-id": "dev"
        },
        "body": body,
        "method": "POST",
        "mode": "cors",
        "credentials": "omit"
      });
      let result = await response?.json();
      if(result?.objectId){
        this.id=result?.objectId
      }
      return this
    }
    async destory(){
        let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className + "/";
        if(!this.id) return
        let response = await fetch(url+this.id, {
            "headers": {
              "x-parse-application-id": "dev"
            },
            "body": null,
            "method": "DELETE",
            "mode": "cors",
            "credentials": "omit"
          });
          let result=response?.json();
          if(result){
            this.id=null
          }
          return true
    }
}


class CloudQuery{//查询
    className
    constructor(className){
        this.className=className
    }
    
    async get(id){

        let response = await fetch("http://dev.fmode.cn:1337/parse/classes/"+this.className+"/"+id+"?", {
            "headers": {
            "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
            "x-parse-application-id": "dev"
            },
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
        });
        let json = await response?.json();
        return json || {}
    }
    async find(){
        let response = await fetch("http://dev.fmode.cn:1337/parse/classes/"+this.className+"?", {
            "headers": {
            "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
            "x-parse-application-id": "dev"
            },
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
        });
        let json = await response?.json();
        return json?.results || []
    }
    async first(){
        let results = await this.find();
        return results.length > 0 ? results[0] : null;
    }
}