/*
 * @Author: tengmingxue 1473375109@qq.com
 * @Date: 2023-08-15 11:20:48
 * @LastEditors: tengmingxue 1473375109@qq.com
 * @LastEditTime: 2023-09-25 20:37:27
 * @FilePath: \xld-gis-admin\src\api\resource\map.ts
 * @Description: 地图资源接口
 */
import { defHttp } from '/@/utils/http/axios';
import { session } from "/@/utils/Memory.js";
import type { ErrorMessageMode } from '/#/axios';

enum Api {
    Login = '/callProvider'
}

const client = {
    grant_type: "password",
    scope: "all",
    client_id: "space",
    client_secret: 'e10adc3949ba59abbe56e057f20f883e',
}

const interfaceType = { apiUrl2: true };

/**
 * @description:获取地图资源列表
 */
export function list(params) {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "getResourcePagedList");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", '{"type":"MR","pageIndex":1,"pageSize":20 }');
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? {} : JSON.parse(r.result)
                if (result?.items) {
                    result.items.forEach((item, index) => {
                        item['xh'] = index + 1
                    })
                }
                resolve(result);
            })
    })
}

/**
 * @description:获取地图资源列表
 */
export function listPage(params) {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "getResourcePagedList");
    formData.append("args[]", session.getItem('tokenV2'));

    var obj = {
        type: params.type,
        pageIndex: params.page,
        pageSize: params.pageSize,
        userid: session.getItem('userId')
    }
    if (params.keywork) obj.filterValue = params.keywork;
    if (params.checkStatus != null && params.checkStatus != undefined && params.checkStatus != 7) {
        obj.status = params.checkStatus;
    }

    formData.append("args[]", JSON.stringify(obj));
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? {} : JSON.parse(r.result)
                if (result?.items) {
                    result.items.forEach((item, index) => {
                        item['xh'] = index + 1
                    })
                }
                console.log("审核资源列表：", result)
                resolve(result);
            })
    })
}

/**
 * @description:获取地图资源列表
 */
export function mapList(params) {
    return new Promise<void>((resolve) => {
        defHttp.post({ url: Api.Login, params: params, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? {} : JSON.parse(r.result)
                if (result?.items) {
                    result.items.forEach((item, index) => {
                        item['xh'] = index + 1
                    })
                }
                resolve(result);
            })
    })
}


/**
 * @description:获取资源类型字典
 */
export function getServiceTypes() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.SServicebaseProvider");
    formData.append("methodName", "getSServiceTypes");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", '{"abbreviation":"MR","pid":"MapResource"}');
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}


/**
 * @description:获取资源标签
 */
export function queryServiceTags() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.tags.TagsProvider");
    formData.append("methodName", "getAllByType");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", '业务类型,服务专题,数据类型,年度');
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}


/**
 * @description:根据标签获取字典
 */
export function queryDicsByName(name) {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.tags.TagsProvider");
    formData.append("methodName", "getAllByType");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", name);
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}


/**
 * @description:获取坐标系列表
 */
export function queryCoors() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.geomname.GeomNameCDProvider");
    formData.append("methodName", "getGeomNameList");
    formData.append("args[]", session.getItem('tokenV2'));
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}

/**
 * @description:新增资源
 */
export function insertService(params) {
    if (!params) return
    const str = JSON.stringify(params)
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "insert");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", str)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                resolve(r);
            }).catch(e => {
                resolve(false)
            })
    })
}

/**
 * @description:新增文件资源
 */
export function insertFilesService(params, type) {
    if (!params) return
    const str = JSON.stringify(params)
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", type ? "update" : "insertList");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", str)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                resolve(r);
            })
    })
}

/**
 * @description:根据资源id查询
 */
export function queryResourceById(id) {
    if (!id) return
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "getResourceByServiceId");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", id)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = null
                if (r && r.status === '0') result = JSON.parse(r.result)
                resolve(result);
            })
    })
}


/**
 * @description:修改资源
 */
export function updateService(params) {
    if (!params) return
    const str = JSON.stringify(params)
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "update");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", str)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                resolve(r);
            })
    })
}


/**
 * @description:删除资源
 */
export function deleteService(param) {
    if (!param) return
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "delete");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", param)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                resolve(r);
            })
    })
}


/**
 * @description:删除资源
 */
export function deleteBatchService(param) {
    debugger
    if (!param) return
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "delete");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", param)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                resolve(r);
            })
    })
}


/**
 * @description:获取资源上传审核列表待办
 */
export function listPageDB(params) {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "getResourcePagedList");
    formData.append("args[]", session.getItem('tokenV2'));

    var obj = {
        type: params.type,
        pageIndex: params.page,
        pageSize: params.pageSize,
        userid: session.getItem('userId')
    }
    if (params.keywork) obj.filterValue = params.keywork;
    if (params.checkStatus != null && params.checkStatus != undefined && params.checkStatus != 7) {
        obj.status = params.checkStatus;
    }

    formData.append("args[]", JSON.stringify(obj));
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? {} : JSON.parse(r.result)
                if (result?.items) {
                    result.items.forEach((item, index) => {
                        item['xh'] = index + 1
                    })
                }
                console.log("审核资源列表：", result)
                resolve(result);
            })
    })
}