import { defHttp } from '/@/utils/http/axios';
import { isNil } from 'lodash-es';
import { getEnfMineTree } from '/@/api/sys/menu';
import { message } from 'ant-design-vue';

enum Api {
  getMineData = '/province/mineData/getMineData',
  getCoalSeam = '/province/mineData/getCoalSeam',
  getCoalSeamAlarmRule = '/province/alarm/getCoalSeamAlarmRule',
  updateCoalSeamAlarmRule = '/province/alarm/updateCoalSeamAlarmRule',
  addCoalSeamAlarmRule = '/province/alarm/addCoalSeamAlarmRule',
  deleteCoalSeamAlarmRule = '/province/alarm/deleteCoalSeamAlarmRule',
  getGoafList = '/province/device/getGoafList',
  getGoafDataLimit = '/province/alarm/getGoafDataLimit',
  addGoafDataLimit = '/province/alarm/addGoafDataLimit',
  updateGoafDataLimit = '/province/alarm/updateGoafDataLimit',
  deleteGoafDataLimit = '/province/alarm/deleteGoafDataLimit',
}

export function getMineData(params: any) {
  return defHttp.post({ url: Api.getMineData, params });
}
export function getCoalSeam(params: any) {
  return defHttp.post({ url: Api.getCoalSeam, params });
}
export function getCoalSeamAlarmRule(params: any) {
  return defHttp.post({ url: Api.getCoalSeamAlarmRule, params });
}
export function updateCoalSeamAlarmRule(params: any) {
  return defHttp.post({ url: Api.updateCoalSeamAlarmRule, params }).then(() => {
    message.success('修改成功');
  });
}
export function addCoalSeamAlarmRule(params: any) {
  return defHttp.post({ url: Api.addCoalSeamAlarmRule, params });
}
export function deleteCoalSeamAlarmRule(params: any) {
  return defHttp.post({ url: Api.deleteCoalSeamAlarmRule, params });
}
export function getGoafList(params: any) {
  return defHttp.post({ url: Api.getGoafList, params });
}
export function getGoafDataLimit(params: any) {
  return defHttp.post({ url: Api.getGoafDataLimit, params }, { joinParamsToUrl: true });
}
export function addGoafDataLimit(params: any) {
  return defHttp.post({ url: Api.addGoafDataLimit, params });
}
export function updateGoafDataLimit(params: any) {
  return defHttp.post({ url: Api.updateGoafDataLimit, params });
}
export function deleteGoafDataLimit(params: any) {
  return defHttp.post({ url: Api.deleteGoafDataLimit, params });
}

export function getMineTree(params: { searchValue: string }) {
  return getEnfMineTree().then((tree) => {
    // 头一个真正的叶节点
    let firstLeafKey;
    // 它的父节点
    let firstParentKey;

    const transformKeys = (arr: any[]) => {
      return arr.map((r) => {
        const isLeaf = isNil(r.childDepart);

        // 因为接口返回只有两层所以简单处理firstParentKey
        if (isLeaf && !firstLeafKey) {
          firstLeafKey = r.id;
          firstParentKey = r.parentId;
        }

        return {
          title: r.departName,
          key: r.id,
          isLeaf: isLeaf,
          children: isLeaf ? undefined : transformKeys(r.childDepart),
        };
      });
    };

    const filterTreeNodes = (nodes: any[], value: string) => {
      if (!value) return nodes;
      return nodes.filter((t) => {
        // 如果此次匹配成功，那么该节点应返回
        if (t.title.includes(value)) {
          return true;
        }
        // 如果没有则看看它的子节点是否有匹配的
        if (t.children) {
          return filterTreeNodes(t.children, value).length > 0;
        }
      });
    };

    return {
      tree: filterTreeNodes(transformKeys(tree), params.searchValue),
      firstLeafKey,
      firstParentKey,
    };
  });
}
