import req from "ui-jz-v4-common/src/utils/zt-data";
import axios from "@/utils/req";
import request from "@/utils/request";
import handleSearchCondition from "@/utils/searchable"

// 将一维数组转换成树形结构
function treeUtil(data, key = { id: "id", pid: "pid" }) {
    let result = [];
    if (!Array.isArray(data)) {
        return result;
    }
    data.forEach(item => {
        delete item.children;
    });
    let map = {};
    data.forEach(item => {
        map[item[key.id]] = item;
    });
    data.forEach(item => {
        let parent = map[item[key.pid] || item[key.pid.toLocaleUpperCase()]];
        if (parent) {
            (parent.children || (parent.children = [])).push(item);
        } else {
            result.push(item);
        }
    });
    return result;
}

/** next delete...
 * 机构树列表
 */
export function getOrgMtTree(data) {
    return req.post("/orgInfo/orgMtTreeYB", data);
}

// 机构列表 *懒加载
export function getTree(data) {
    data.type = data.type || "APP";
    // data.id = data.id || '7F08CCC3C4984A2586C9D3F0A6B804E5';
    return axios.post("/authsvr/v2/authorginfo/org/tree/catalog", data);
}

// 机构树-正确的树 （权限过滤后的）
export function getOrgTree(data) {
    data.type = data.type || "APP";
    return axios.post("/authsvr/v2/authorginfo/org/tree/catalog", data);

}

// 机构列表 *查询本级
export function getBaseTree(data) {
    return axios.get(`/authsvr/v2/authorginfo/org/tree/node/${data.id}`);
}

// 厂商列表
export function externalTree(data) {
    return axios.post("/appsvr/app/externalTree", data);
}

/**
 * 机构树模糊查询
 * 根据名称name 分页pageNum pageSize
 */
export function treeMatch(data) {
    // return axios.post(`orgsvr/v2/org/tree/match`, data);
   return request({
        url: "/authsvr/v2/authorginfo/org/tree/match",
        method: "POST",
        data
    })
}

/**
 * 附件模版下载
 * @param {Object} data
 * type: police 警员、auxiliaryPolice 辅警、externalPersonnel 外部人员、manager 管理员、govUser 政府人员
 */
export function downTemplate(params) {
    return axios.arraybuffer("/downloadTemplate/downTemplate", params);
}

// 导入文件
export function importFile(url, data) {
   return request({
       url,
       method: "GET",
       params: data
   })
}

export function uniClass() {
    return req.post("/duacm/codeSon/v2/data/T_MD_UNIT_CLASS.ation").then(res => {
        const arr = res.data;

        return {
            arr,
            tree: treeUtil(arr, { id: "CODE", pid: "pcode" })
        };
    });
}

// 单位类别  新新新
export function uniClass2() {
    return req.post("/duacm/codeSon/v2/data/T_MD_UNIT_CLASS.ation").then(res => {
        const arr = res.data;

        return {
            arr,
            tree: treeUtil(arr, { id: "CODE", pid: "pcode" })
        };
    });
}

// 分配范围
export function distribuAction(data) {
    return req.get("/authsvr/v2/orgQuota/distributionRange", data).then(res => {
        if (res.data && res.status == 200) {
            return res.data.content;
        }
    });
}

/**
 * 菜单树 一次性查询
 * */
export function menuTree({ appId }) {
    return axios.get(`/authsvr/v2/appfuninfo/menuTreeList`, { appId });
}

// 根据appId 获取菜单
export function getAppfunMenu(appId) {
    return axios.get(`/authsvr/v2/appfuninfo/menu/${appId}`);
}

// 标签树
export function getBusinessTagTree(data) {
    return request({
        url: `/authsvr/v2/dataauth/businessTreeList`,
        method: "get"
    });
}

export function getDropdownApp(url,condition) {
   return request({
       url,
       method: "POST",
       data: handleSearchCondition(condition)
   })
}