import httputils from '@/api/scan/httpUtil'
import { Message } from 'element-ui';

const modelapi = '/revit-algorithm';
const scheduler = '/scheduler';

let api = {
    // 新建模型文件夹
    /**
     *
     * @param {Name:string,ProjectId:string} params
     */
    createModel(params, success) {
        let Name = params.Name;
        this.queryModel(Name, (res) => {
            if (res.Total == 0) {
                let data = {
                    Content: [params]
                };
                // 查询是否有该模型文件夹
                // 创建
                httputils.postJson(`${modelapi}/model-folder/create`, data, success)
            } else {
                Message.error({ message: '该文件夹已经存在！' });
            }
        })
    },
    //删除模型文件夹
    /**
     *
     * @param {Id:string} params
     */
    deleteModel(params, success) {
        let Content = [params];
        httputils.postJson(`${modelapi}/model-folder/delete`, Content, success)
    },
    //修改模型文件夹名称
    /**
    *
    * @param {Name:string,ProjectId:string} params
    */
    updateModelName(params, success) {
        let Name = params.Name;
        this.queryModel(Name, (res) => {
            if (res.Total == 0) {
                let data = {
                    Content: [params]
                };
                // 更改文件夹名称
                httputils.postJson(`${modelapi}/model-folder/update`, data, success)
            } else {
                Message.error({ message: '该文件夹已经存在！' });
            }
        })
    },
    //查询模型文件夹
    /**
     * @param Name string  注:查询多个 Name = ''
    */
    queryModel(Name, success) {
        let data = null;
        if (Name) {
            // 单个查询
            data = {
                Filters: `Name='${Name}'`,
                Orders: "Name asc"
            }
        } else {
            // 多个查询
            data = {
                Orders: "Name asc"
            }
        }
        return httputils.postJson(`${modelapi}/model-folder/query`, data, success)
    },



    //以下是楼层文件接口

    // 查询模型文件夹下的所有模型文件
    queryFloorList(data, success) {
        let Filters = `FolderId='${data.FolderId}'`;
        if (data.FloorName) {
            Filters = `FolderId='${data.FolderId}';FloorName='${data.FloorName}';ProjectId='${data.ProjectId}'`;
        }
        if (data.Status) {
            Filters += `;Status in [${data.Status}]`
        }
        let params = {
            Filters: Filters,
            PageNumber: 1,
            PageSize: 500
        }
        return httputils.postJson(`${modelapi}/model-floor/query-list`, params, success)
    },
    // 查询模型文件夹下的单个模型文件
    queryFloorItem(data, success) {

        let Filters = `FolderId='${data.FolderId}'`;
        if (data.FloorName) {
            Filters = `FolderId='${data.FolderId}';FloorName='${data.FloorName}';ProjectId='${data.ProjectId}'`;
        }
        let params = {
            Filters: Filters
        }
        return httputils.postJson(`${modelapi}/model-floor/query`, params, success)
    },
    // 删除楼层文件
    deleteFloor(data) {
        return httputils.postJson(`${modelapi}/model-floor/delete`, data)
    },
    //编辑楼层文件的名字
    updateFloorName(data, success) {
        return httputils.postJson(`${modelapi}/model-floor/update`, data, success)
    },


    //以下是楼层文件接口

    // 创建模型文件
    createModelFile(data, success) {
        return httputils.postJson(`${modelapi}/model-file/create`, data, success)
    },
    //删除模型文件（只有记录，文件不动）
    deleteModelFile(data, success) {
        return httputils.postJson(`${modelapi}/model-file/delete`, data, success)
    },
    /**
     * info: 删除模型文件
     * @param {*} params string 模型文件的id
     * @param {*} success  成功的回调函数
     */
    deleteModelFileList(params, success) {
        let data = {
            Id:params.Id,
            Force: params.Force?params.Force:false
        }
        return httputils.postJson(`${modelapi}/model-file/delete-file`, data, success)
    },
    //查询模型文件
    queryModelFile(FloorModelId, success) {
        let params = {
          Distince: true,
          Filters: `FloorModelId='${FloorModelId}';Removed=false`,
          Orders: 'Version desc'
        }
        return httputils.postJson(`${modelapi}/model-file/query`, params, success)
    },
    // 上传模型文件
    // uploadModelFile(payload, ProjectId,callback1, callback2) {
    //     axios({
    //         url: baseUrl + '/model-file/upload',
    //         method: 'post',
    //         headers: {
    //             ProjectId: ProjectId
    //         },
    //         onUploadProgress: function (progressEvent) { //原生获取上传进度的事件
    //             if (progressEvent.lengthComputable) {
    //                 //属性lengthComputable主要表明总共需要完成的工作量和已经完成的工作是否可以被测量
    //                 //如果lengthComputable为false，就获取不到progressEvent.total和progressEvent.loaded
    //                 callback1(progressEvent);
    //             }
    //         },
    //         data: payload
    //     }).then(res => {
    //         callback2(res);
    //     }).catch(error => {
    //         console.log('this is a catch')
    //         console.log(error)
    //     })
    // },

    //准备分片上传模型文件接口
    modelFileUpload(data, success) {
        return httputils.postJson(`${modelapi}/model-file/upload`, data, success)
    },

    mergeModelFile(data, success) {
        return httputils.postJson(`${modelapi}/model-file/merge`, data, success)
    }

    // 更新模型文件
    // upDateModelFile(data, ProjectId, UserName, UserId, callback1, callback2) {
    //     let modelFile = {
    //         Content: [{
    //             FloorModelId:data.replaceModelItem.Id, //模型id
    //             ModelName: data.Form.file.name,        //模型名字
    //             UserName: UserName,
    //             UserId: UserId,
    //             Note: data.replaceModelItem.Note,
    //             ReplaceReason: data.Form.ReplaceReason
    //         }]
    //     }
    //     // //创建模型文件
    //     this.createModelFile(modelFile, (createModelRes) => {
    //         // 与此楼文件进行绑定
    //         let upDateModelData = {
    //             Content: [{
    //                 Id:data.replaceModelItem.Id, //楼层
    //                 CurrentModelId: createModelRes.EntityList[0].Id
    //             }]
    //         }
    //         this.updateFloorName(upDateModelData, (upDateModelSucess) => {
    //             if (upDateModelSucess.Result == "success") {
    //                 // 处理数据
    //                 let formData = new FormData();
    //                 formData.append(
    //                   "model",
    //                   JSON.stringify({
    //                     FloorModelId: data.replaceModelItem.Id,
    //                     Id:createModelRes.EntityList[0].Id
    //                   })
    //                 );
    //                 formData.append("file", data.Form.file.raw);
    //                 this.uploadModelFile(formData,ProjectId,callback1,callback2)
    //             }
    //         })
    //     })
    // }

}
export default api

//楼层与平面图绑定
export function bindFloorModel(param, success) {
    let url = `${modelapi}/model-floor/bind`;
    httputils.postJson(url, param, success)
}

//根据模型id 获取模型文件夹及文件名
export function getFileNameById(param, success) {
    let url = `${modelapi}/model-folder/get-name`;
    httputils.postJson(url, param, success)
}

// 根据条件查询统计数量
export function countSyncLogCount(param, success) {
    let url = `${modelapi}/sync-log/count`;
    httputils.postJson(url, param, success)
}

// 查询同步日志
export function querySyncLogCount(param, success) {
    let url = `${modelapi}/sync-log/query`;
    httputils.postJson(url, param, success)
}

// 查询模型文件
export function queryFileByModelId(param, success) {
    let url = `${modelapi}/model-file/query`;
    httputils.postJson(url, param, success)
}

// 模型流程监控
export function modelPathControl(param, success) {
    let url = `${modelapi}/model-monitor/query`;
    httputils.postJson(url, param, success)
}

// 模型流程监控-获取待分配模型数量
export function unassignQuery(param, success) {
    let url = `${scheduler}/task/query`;
    httputils.postJson(url, param, success)
}

// 根据条件查询统计数量
export function countModel(params, success) {
    return httputils.postJson(`${modelapi}/model-file/count`, params, success)
}
