
import { request } from 'umi';

export async function queryDepV2(params: any) {
  function getDepUserTree(data: any) {
    data.title = `${data.Name}`;
    data.key = `dep-${data.ID}`;
    data.value = `dep-${data.ID}`;
    data.selectable = false;
    if (!data.children) data.children = new Array();

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

    if (data.Users && data.Users.length !== 0) {
      data.Users.forEach((item: any) => {
        item.title = item.CName;
        item.key = item.ID;
        item.value = item.ID;
        item.selectable = true;
        data.children.push(item);
      });
    }
    return data;
  }
  const response = await request(`/api/v2/dep`, { params });
  var data = response.data;
  try {
    const depUserTree = data?.list?.map((item: Api.IDep) => {
      return getDepUserTree(item);
    });
    console.log(depUserTree);
    return { data: depUserTree };
  } catch (error) {
    return response;
  }
}