

// class CloudObject{
//     id
//     className
//     data = {}
//     constructor(className){
//         this.className = className
//     }
//     toPointer(){
//         return {"__type":"Pointer","className":this.className,"objectId":this.id}
//     }
//     set(json){
//         Object.keys(json).forEach(key=>{
//             if(["objectId","id","createdAt","updatedAt","ACL"].indexOf(key)>-1){
//                 return
//             }
//             this.data[key] = json[key]
//         })
//     }
//     get(key){
//         return this.data[key] || null
//     }
//     async save(){
//         let method = "POST"
//         let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className
//         // 更新
//         if(this.id){
//             url += "/"+this.id
//             method = "PUT"
//         } 
//         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": method,
//             "mode": "cors",
//             "credentials": "omit"
//           });
//           let result = await response?.json();
//           if(result?.error){
//             console.error(result?.error)
//           }
//           if(result?.objectId){this.id = result?.objectId}
//           return this
//     }
//     async destory(){
//         if(!this.id) return
//         let response = await fetch("http://dev.fmode.cn:1337/parse/classes/Doctor/"+this.id, {
//             "headers": {
//               "x-parse-application-id": "dev"
//             },
//             "body": null,
//             "method": "DELETE",
//             "mode": "cors",
//             "credentials": "omit"
//           });
//           let result = await response?.json();
//           if(result){
//             this.id = null
//         }
//         return true
//     }
// }

// class CloudQuery{
//     className
//     constructor(className){
//         this.className = className
//     }

//     whereOptions = {}
//     greaterThan(key,value){
//         if(!this.whereOptions[key]) this.whereOptions[key] = {}
//         this.whereOptions[key]["$gt"] = value
//     }
//     greaterThanAndEqualTo(key,value){
//         if(!this.whereOptions[key]) this.whereOptions[key] = {}
//         this.whereOptions[key]["$gte"] = value
//     }
//     lessThan(key,value){
//         if(!this.whereOptions[key]) this.whereOptions[key] = {}
//         this.whereOptions[key]["$lt"] = value
//     }
//     lessThanAndEqualTo(key,value){
//         if(!this.whereOptions[key]) this.whereOptions[key] = {}
//         this.whereOptions[key]["$lte"] = value
//     }
//     equalTo(key,value){
//         this.whereOptions[key] = value
//     }

//     async get(id){
//         let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"/"+id+"?"

//         let response = await fetch(url, {
//             "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 url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
        
//         if(Object.keys(this.whereOptions)?.length){
//             let whereStr = JSON.stringify(this.whereOptions)
//             url += `where=${whereStr}`
//         }

//         let response = await fetch(url, {
//             "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 url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
        
//         if(Object.keys(this.whereOptions)?.length){
//             let whereStr = JSON.stringify(this.whereOptions)
//             url += `where=${whereStr}`
//         }

//         let response = await fetch(url, {
//             "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();
//         let exists = json?.results?.[0] || null
//         if(exists){
//             let existsObject = new CloudObject(this.className)
//             existsObject.set(exists)
//             existsObject.id = exists.objectId
//             existsObject.createdAt = exists.createdAt
//             existsObject.updatedAt = exists.updatedAt
//             return existsObject
//         }
//     }
// }

// module.exports.CloudObject = CloudObject
// module.exports.CloudQuery = CloudQuery

class CloudObject{
    id
    ClassName
    data={}
    constructor(ClassName)
    {
        this.ClassName=ClassName
    }

    toPointer(){
        return {"_type":"Pointer","ClassName":this.ClassName,"objectId":this.id}
    }
    // 界面给data传数据
    set(json){
        Object.keys(json).forEach(key=>{
            if(["objectId","id","createdAt","updateAt","ACL"].indexOf(key)>-1)
            {
                return 
            }
            this.data[key]=json[key]
        })
    }
    // data返回数据给界面
    get(){
        return this.data[key] || null
    }

    // data数据传给数据库，
    // 增数据 数据库生成id，给id赋值,返回整个对象
    // 改数据 修改非id数据，返回整个对象
    async save(){
        let url="http://dev.fmode.cn:1337/parse/classes/" +this.ClassName
        let method="POST"
        if(this.id)
        {
            method="PUT"
            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": method,
           "mode": "cors",
           "credentials": "omit"
         });
       let json= await response?.json();
       if(json?.objectId){this.id=json?.objectId}
       return this
    }

    // 通过数据库删除，再删除id里可能存在的数据
    async destroy() {
        if(!this.id)return 
        let response=await fetch("http://dev.fmode.cn:1337/parse/classes/Post/"+this.id, {
            "headers": {
              "x-parse-application-id": "dev"
            },
            "body": null,
            "method": "DELETE",
            "mode": "cors",
            "credentials": "omit"
          });
          let json=await response?.json()
          if(json)this.id=null
          return true;
    }

}
class CloudQuery
{
    ClassName

    constructor(ClassName)
    {
        this.ClassName=ClassName
    }
    
    whereOptions={}
    equalTo(key,value)
    {
        this.whereOptions[key]=value
    }

    greaterThan(key,value)
    {
        if(!this.whereOptions[key])this.whereOptions[key]={}
        this.whereOptions[key]["$gt"]=value
    }
    greaterThanAndEqualTo(key,value)
    {
        if(!this.whereOptions[key])this.whereOptions[key]={}
        this.whereOptions[key]["$gte"]=value
    }
    lessThan(key,value)
    {
        if(!this.whereOptions[key])this.whereOptions[key]={}
        this.whereOptions[key]["$lt"]=value
    }
    lessThanAndEqualTo(key,value)
    {
        if(!this.whereOptions[key])this.whereOptions[key]={}
        this.whereOptions[key]["$lte"]=value
    }

    // 查询一个表中的特定id数据
    async get(id)
    {
        let response=await fetch("http://dev.fmode.cn:1337/parse/classes/"+this.ClassName+"/"+id+"?", {
            "headers": {
              "if-none-match": "W/\"ab-2gNtNZqRYX93fdbIaSj6z5h571k\"",
              "x-parse-application-id": "dev"
            },
    
            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
          });
        let json=await response?.json();
        // console.log(json);
        return json || {}
    }
 
    // 查询一个表中的所有数据,高级查询
    async find(){
        let url="http://dev.fmode.cn:1337/parse/classes/"+this.ClassName+"?"


        if(Object.keys(this.whereOptions).length)
        {
            let whereStr=JSON.stringify(this.whereOptions)
            url+=`where=${whereStr}`
        }

        let response=await fetch(url, {
            "headers": {
              "if-none-match": "W/\"ab-2gNtNZqRYX93fdbIaSj6z5h571k\"",
              "x-parse-application-id": "dev"
            },

            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
          });
        let json=await response?.json();
        // console.log(json);
        return json?.results || []
    }
    async first(){
        let url="http://dev.fmode.cn:1337/parse/classes/"+this.ClassName+"?"


        if(Object.keys(this.whereOptions).length)
        {
            let whereStr=JSON.stringify(this.whereOptions)
            url+=`where=${whereStr}`
        }

        let response=await fetch(url, {
            "headers": {
              "if-none-match": "W/\"ab-2gNtNZqRYX93fdbIaSj6z5h571k\"",
              "x-parse-application-id": "dev"
            },

            "body": null,
            "method": "GET",
            "mode": "cors",
            "credentials": "omit"
          });
        let json=await response?.json();
        // console.log(json);
        let exists=json?.results?.[0] || null
        if(exists)
        {
            let existsObject=new CloudObject(this.ClassName)

            existsObject.set(exists)
            existsObject.id=exists.objectId
            existsObject.createdAt=exists.createdAt
            existsObject.updatedAt=exists.updatedAt
            return existsObject
        }
       
    }

}



module.exports.CloudObject=CloudObject
module.exports.CloudQuery=CloudQuery
