import { useRequest } from '@umijs/max';
import { queryDepV2 } from '@/services/approval';

const depList = () => {
  const { data, run, loading } = useRequest(queryDepV2, {
    manual: true,
    cacheKey: 'queryDepV2',
    defaultParams: [{ pageSize: 9999 }],
  });
  let depUserTree = [],
    depList = [];
  if (data) {
    depList = data.list.map((item) => {
      return getDepTree(item);
    });

    let copyData = JSON.parse(JSON.stringify(data.list));
    depUserTree = copyData.map((item) => {
      return getDepUserTree(item);
    });
  }
  return { depUserTree, depList, loading, run };
};
function getDepUserTree(data) {
  data.title = `${data.Name}`;
  data.id = data.ID;
  data.value = data.ID;
  data.selectable = false;
  if (!data.children) data.children = new Array();

  if (data.children) {
    data.children.forEach((item) => {
      getDepUserTree(item);
    });
  }

  if (data.Users && data.Users.length !== 0) {
    data.Users.forEach((item) => {
      item.title = item.CName;
      item.id = item.ID + '||' + data.ID;
      item.value = item.ID + '||' + data.ID;
      // item.selectable = true;
      item.DepId = data.ID;
      data.children.push(item);
    });
  }
  return data;
}
function getDepTree(data) {
  data.title = `${data.Name}`;
  data.id = data.ID;
  data.value = data.ID;
  if (!data.children) data.children = new Array();

  if (data.children) {
    data.children.forEach((item) => {
      getDepTree(item);
    });
  }
  return data;
}
export default depList;
