class ParseObject{
    className
    id
    serverURL = "http://web2023.fmode.cn:9999/parse"
    data = {}
    toJSON(){
        return this.data
    }
    constructor(className){
        this.className = className
    }
    async get(id){
        let response = await fetch(this.serverURL+"/classes/"+this.className+"/"+id, {
            "headers": {
                "x-parse-application-id": "dev"
              },
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
          });
          if(response?.status=="200"){
            let json = await response.json()
            this.id = json?.objectId;
            delete json.objectId
            delete json.createdAt
            delete json.updatedAt
            this.data = json;
            return this
          }else{
            return []
          }
    }
    async findAll(){
        let response = await fetch(this.serverURL+"/classes/"+this.className, {
            "headers": {
                "x-parse-application-id": "dev"
              },
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
          });
          // console.log(response)
          // return []
          if(response?.status=="200"){
            let json = await response.json()
            // console.log(json)
            return json?.results || []
          }else{
            return []
          }
    }
    set(data){
        this.data = data
    }
    async save(){
        let body = JSON.stringify(this.data)
        let url = this.serverURL+"/classes/"+this.className // 创建URL
        let method = "POST"
        if(this.id){
            url = url + "/" + this.id // 更新URL
            method = "PUT"
        }
        console.log(url,method,body)
        let response = await fetch(url, {
            "headers": {
                // "content-type": "text/plain;charset=UTF-8",
                "x-parse-application-id": "dev"
            },
            "body": body,
            "method": method,
            "mode": "cors",
            "credentials": "omit"
        });
        console.log(body)
        let text = await response?.text();
        console.log(text)
        let json = await response?.json();
        if(json?.objectId){
            console.log(json)
            this.id = json?.objectId
            return this
        }else{
            return null
        }
    }
    async delete(){
        let response = await fetch(this.serverURL+"/classes/"+this.className+"/"+id, {
            "headers": {
                "x-parse-application-id": "dev"
              },
            "body": null,
            "method": "DELETE",
            "mode": "cors",
            "credentials": "omit"
          });
          if(response?.status=="200"){
            let json = await response.json()
            return json
          }else{
            return []
          }
    }
}
module.exports.ParseObject = ParseObject

class ParseQuery{
    
}