import qs from "qs"
import config from "../commons"
import request from "../utils/request"

const {api} = config
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' }

class standardsService{

    async getAllStandardTree(payload={},autotip=false){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/tree/list`, method: "get", data: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getAllStandardTreeWidthAdmit(payload={},autotip=false){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/tree/privilege`, method: "get", data: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getStandardTypes(payload={},autotip=false){
        let result = await request({ url: `${api.standard}/metaModel/getSubModel`, method: "get", data: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getTypeInfo(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/attrs`, method: "get", data: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getStandardsByIds(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/getByIds`, method: "get", data: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getDraftStandardById(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/draft/${payload}`, method: "get", data: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getStandardInPackage(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/standard/list`, method: "post", data: payload.body,params:payload.params },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getDraftStandardInPackage(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/draft/list`, method: "post", data: payload.body,params:payload.params },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async addCatalog(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/addStandardCatalog`, method: "post", data: qs.stringify(payload) },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async updateCatalog(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/updateById`, method: "post", data: payload.data,params:payload.params },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getImportLog(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/import/log`, method: "get", data: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getModelImportLog(payload,autotip){
        let result = await request({ url: `${api.standard}/metaModel/importLog`, method: "get", data: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async importStandard(param,autotip){
        let config = {
            headers:{'Content-Type':'multipart/form-data'}
        }
        let formdata=new FormData();// 创建form对象
        const {file,namePath,clazz,isDraft,allowedModels} = param
        formdata.append('file',file);
        formdata.append("namePath",namePath)
        formdata.append("clazz",clazz)
        formdata.append("isTemp",false)
        formdata.append("isDraft",isDraft)
        formdata.append('allowedModels',allowedModels)
        let result = await request({ url: `${api.standard}/rest/standard/import/addWithConfirm`, method: "post", data: formdata},autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result
    }

    async confirmImport(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/import/confirm`, method: "post", data: qs.stringify(param)},autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result
    }

    async importModel(param,autotip){
        const {file,params} = param
        let formdata=new FormData();// 创建form对象
        formdata.append('file',file);
        console.log(params)
        let result = await request({ url: `${api.standard}/metaModel/import`, method: "post", data: formdata,params},autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result
    }

    async updateStardard(param,autotip){
        console.log(param.data)
        let result = await request({ url: `${api.standard}/rest/standard/maintain/update/${param.id}`, method: "post",params:param.params, data:param.data},autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result
    }

    async addStardard(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/add/${param.id}/${param.type}`, method: "post",params:param.params, data:param.data},autotip).then(data=>{
            return data;
        }).catch(error=>{
            console.log('666',error)
            return error
        })
        return result
    }

    async deleteStandards(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/delete`, data:qs.stringify(param), method: "post",},autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result
    }

    async deleteMetaByIdsNew(param,autotip=false){
        let result = await request({ url: `${api.api}/standardmanagement/rest/standard/maintain/deleteStandardCatalog?id=${param}`, method: "post",  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async importMatch(param,autotip){
        let config = {
            headers:{'Content-Type':'multipart/form-data'}
        }
        let formdata=new FormData();// 创建form对象
        console.log(config)
        formdata.append('file',param.file);
        let result = await request({ url: `${api.api}/standardmanagement/sdrcb/rest/standard/importSDBankStandardMapper`, method: "post", data: formdata},autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result
    }

    async matchStatic(param,autotip){
        let result = await request({ url: `${api.api}/standard/rest/standard/match/statistic`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getMatchRules(param,autotip){
        let result = await request({ url: `${api.api}/standard/rest/standard/match/rules`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getMatchTask(param,autotip){
        let result = await request({ url: `${api.api}/rest/standard/match/tasks`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async saveRule(payload,autotip){
        const {data,param} = payload
        let result = await request({ url: `${api.standard}/rest/standard/match/rule`, method: "post", data: data,params:param },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async saveTask(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/task`, method: "post", data: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async startTask(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/scan/${payload}`, method: "post", data: null },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async restartTask(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/executions/restart`, method: "post", data: qs.stringify(payload) },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async cancelTask(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/executions/cancel`, method: "post", data: qs.stringify(payload) },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }


    async getMatchList(payload,autotip){
        const {data,param} = payload
        let result = await request({ url: `${api.standard}/rest/standard/match/list`, method: "post", data: data,params:param },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async findMatchList(payload,autotip){
        const {data,param} = payload
        let result = await request({ url: `${api.standard}/rest/standard/match/find`, method: "post", data: data,params:param },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async deleteMatchs(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/delete`, method: "post", data: qs.stringify(payload) },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async deleteAutoMatchs(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/delete/batch`, method: "delete", params: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async deleteRules(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/rules/delete`, method: "delete", params: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async deleteJob(payload,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/tasks/delete`, method: "delete", params: payload },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getMatchStatus(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/state`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async resetMatchStatus(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/state/reset`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async marchSure(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/confirmResult`, method: "post",data:qs.stringify(param)  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async catalogMove(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/seq`, method: "post",data:qs.stringify(param)  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async downloadFile(payload,autotip=false){
        await request({ url: `${api.standard}/rest/standard/export/ids`, method: "postdownload", data:qs.stringify(payload),responseType:'blob' },autotip).then((data)=>{
            let types = data.data.type
            var blob = new Blob([data.data], {type: types});
            let fileName = ''
            let contentDisposition = data.headers['content-disposition'];
            if (contentDisposition) {
                fileName = window.decodeURI(data.headers['content-disposition'].split('=')[1]);
            }
            if (typeof window.navigator.msSaveBlob !== 'undefined') {
                window.navigator.msSaveBlob(blob, fileName);
            }
            else {
                var csvURL = window.URL.createObjectURL(blob);
                var tempLink = document.createElement('a');
                tempLink.href = csvURL;
                tempLink.setAttribute('download', fileName);
                tempLink.setAttribute('target', '_blank');
                document.body.appendChild(tempLink);
                tempLink.click();
                document.body.removeChild(tempLink);
            }
        }).catch(error=>{
            console.log(error)
        })
    }

    async downloadFileByCatalog(payload,autotip=false){
        const {data,params} = payload
        request({ url: `${api.standard}/rest/standard/export/all`, method: "postdownload", data:data,params,responseType:'blob' },autotip).then((data)=>{
            let types = data.data.type
            var blob = new Blob([data.data], {type: types});
            let fileName = ''
            let contentDisposition = data.headers['content-disposition'];
            if (contentDisposition) {
                fileName = window.decodeURI(data.headers['content-disposition'].split('=')[1]);
            }
            if (typeof window.navigator.msSaveBlob !== 'undefined') {
                window.navigator.msSaveBlob(blob, fileName);
            }
            else {
                var csvURL = window.URL.createObjectURL(blob);
                var tempLink = document.createElement('a');
                tempLink.href = csvURL;
                tempLink.setAttribute('download', fileName);
                tempLink.setAttribute('target', '_blank');
                document.body.appendChild(tempLink);
                tempLink.click();
                document.body.removeChild(tempLink);
            }
        }).catch(error=>{
            console.log(error)
        })
    }

    async downloadFileByCatalogParams(payload,autotip=false){
        const {data,params} = payload
        await request({ url: `${api.standard}/rest/standard/draft/exportAll`, method: "postdownload", data:data,params,responseType:'blob' },autotip).then((data)=>{
            let types = data.data.type
            var blob = new Blob([data.data], {type: types});
            let fileName = ''
            let contentDisposition = data.headers['content-disposition'];
            if (contentDisposition) {
                fileName = window.decodeURI(data.headers['content-disposition'].split('=')[1]);
            }
            if (typeof window.navigator.msSaveBlob !== 'undefined') {
                window.navigator.msSaveBlob(blob, fileName);
            }
            else {
                var csvURL = window.URL.createObjectURL(blob);
                var tempLink = document.createElement('a');
                tempLink.href = csvURL;
                tempLink.setAttribute('download', fileName);
                tempLink.setAttribute('target', '_blank');
                document.body.appendChild(tempLink);
                tempLink.click();
                document.body.removeChild(tempLink);
            }
        }).catch(error=>{
            console.log(error)
        })
    }

    async deleteModel(param,autotip){
        let result = await request({ url: `${api.standard}/metaModel/delete/${param}`, method: "delete"  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getStandardOffset(param,autotip){
        const {id,body}=param
        let result = await request({ url: `${api.standard}/rest/standard/maintain/location/${id}`, method: "post",data:body  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getStandardAllOffset(param,autotip){
        const {id,body}=param
        let result = await request({ url: `${api.standard}/rest/standard/draft/location/general/${id}`, method: "post",data:body  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getStandardChildren(param,autotip){
        const {params,body}=param
        let result = await request({ url: `${api.standard}/rest/standard/maintain/codeItem/list`, method: "post",params,data:body  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async saveCodeItem(param,autotip){
        const {params,body}=param
        let result = await request({ url: `${api.standard}/rest/standard/maintain/codeItem/save`, method: "post",params,data:body  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async deleteCodeItem(param,autotip){
        const {params,body}=param
        let result = await request({ url: `${api.standard}/rest/standard/maintain/codeItem/delete`, method: "post",params,data:body  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getCreateItem(param,autotip){
        const {params,body}=param
        let result = await request({ url: `${api.standard}/rest/standard/maintain/createForColumn`, method: "post",params,data:body  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async startFlow(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/draft/startStandardFlow`, method: "post",data:qs.stringify(param)  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async deleteDraftStandard(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/draft/delete`, method: "post",data:qs.stringify(param)  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async editDraftStandard(param,autotip){
        const {body,params} = param
        let result = await request({ url: `${api.standard}/rest/standard/draft/update`, method: "post",data:body,params:params  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async editDraftStandardChildren(param,autotip){
        const {body,params} = param
        let result = await request({ url: `${api.standard}/rest/standard/maintain/codeItem/validate`, method: "post",data:body,params:params  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getDraftStandardOffset(param,autotip){
        const {id,body}=param
        let result = await request({ url: `${api.standard}/rest/standard/draft/location/${id}`, method: "post",data:body  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getStandardTotal(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/statistic/overview`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getNewStandardTotal(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/statistic/newOverview`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getNewStandardLeftCount(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/statistic/overviewByVisit`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getStandardCodes(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/getValueRange`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getStandardCodesTypes(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/getValueRange/class`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    // async getStandardCodesTypesByPath(param,autotip){
    //     let result = await request({ url: `${api.standard}/rest/standard/maintain/getValueRange/class`, method: "get",data:param  },autotip).then(data=>{
    //         return data;
    //     }).catch(error=>{
    //         return error
    //     })
    //     return result;
    // }

    async getCatalogId(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/code/getCodeStandardCatalogId`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getIdByCode(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/code/getIdByCode`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getMatchStandards(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/maintain/findRelatedStdByCode`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async getStandardsAuth(param,autotip){
        let result = await request({ url: `${api.standard}/privilege/feature`, method: "post",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async importMatchLog(param,autotip){
        let result = await request({ url: `${api.standard}/rest/standard/match/importLog`, method: "get",data:param  },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

    async importMatchData(param,autotip){
        const {file}=param
        const data = new FormData();
        data.append('file',file);
        let result = await request({ url: `${api.standard}/rest/standard/match/import`, method: "post",data:data },autotip).then(data=>{
            return data;
        }).catch(error=>{
            return error
        })
        return result;
    }

}

export default new standardsService()