import request from '@/router/axios';

export const getList = (current, size, params) => {
  return request({
    url: '/api/blade-system/dept/list',
    method: 'get',
    params: {
      ...params,
      current,
      size,
    }
  })
}

export const getLazyList = (parentId, params) => {
  return request({
    url: '/api/blade-system/dept/lazy-list',
    method: 'get',
    params: {
      ...params,
      parentId
    }
  })
}

export const remove = (ids) => {
  return request({
    url: '/api/blade-system/dept/remove',
    method: 'post',
    params: {
      ids,
    }
  })
}

export const add = (row) => {
  return request({
    url: '/api/blade-system/dept/submit',
    method: 'post',
    data: row
  })
}

export const update = (row) => {
  return request({
    url: '/api/blade-system/dept/submit',
    method: 'post',
    data: row
  })
}

export const getDept = (id) => {
  return request({
    url: '/api/blade-system/dept/detail',
    method: 'get',
    params: {
      id,
    }
  })
}
/*

export const getDeptTree = (tenantId) => {
  return request({
    url: '/api/blade-system/dept/tree',
    method: 'get',
    params: {
      tenantId,
    }
  })
}
*/

export const getDeptLazyTree = (parentId) => {
  return request({
    url: '/api/blade-system/dept/lazy-tree',
    method: 'get',
    params: {
      parentId
    }
  })
}

export const getCurrentDept = () => {
  return request({
    url: '/api/blade-system/expand/dept/getCurrentDept',
    method: 'get',
    params: {
    }
  })
}

export const getChildsDept = () => {
  return request({
    url: '/api/blade-system/expand/dept/getChildsDept',
    method: 'get',
    params: {
    }
  })
}

export const getDeptTree = () => {
  return request({
    url: '/api/blade-system/expand/dept/tree',
    method: 'get',
    params: {
    }
  })
}

export const getTree = (data, deptId) => {
  let treeData = [];
  data.forEach(item1 => {
    if (item1.id == deptId){
      treeData.push(item1)
      return true;
    }else {
      if (item1.hasChildren){
        let children1 = item1.children;
        children1.forEach(item2 => {
          if (item2.id == deptId){
            treeData.push(item2)
            return true;
          }else {
            if (item2.hasChildren){
              let children2 = item2.children;
              children2.forEach(item3 => {
                if (item3.id == deptId){
                  treeData.push(item3);
                  return true;
                }else {
                  if (item3.hasChildren){
                    let children3 = item3.children;
                    children3.forEach(item4 => {
                      if (item4.id == deptId){
                        treeData.push(item4)
                        return true;
                      }else {
                        if (item4.hasChildren){
                          let children4 = item4.children;
                          children4.forEach(item5 => {
                            if (item5.id == deptId){
                              treeData.push(item5)
                              return true;
                            }
                          })
                        }
                      }
                    })
                  }
                }
              })
            }
          }
        })
      }
    }
  })
  return treeData;
}
