import { defHttp } from '/@/utils/http/axios';
import { getMenuListResultModel } from './model/menuModel';
import { useUserStore } from '/@/store/modules/user';
import { RoleEnum } from '/@/enums/roleEnum';
import menu from '/@/mock/menu.json'
import menu_get_ids from '/@/mock/menu_get_ids.json'
import tool from '/@/utils/Tool';
import { identity } from 'lodash-es';
import { local, session } from '/@/utils/Memory';
import { getAuthCache } from '/@/utils/auth';
import { USER_INFO_KEY } from '/@/enums/cacheEnum';

enum Api {
  BaseMenuUrl = '/menu',
  SaveStructure = '/user-center/department/editDepartment',
  DeleteStructure = "/user-center/department/deleteDepartment",
  GetMenuList = '/menu/me/menus',
  SysAdminMenuList = '/admin/me/menus',
  GetMenuIdsByRoleId = '/menu/get_ids/',
  GetChildDepartment = '/user-center/department/getChildDepartment',
  // GetMenuTreeData = "/base-center/menu/getMenuTreeData",
  GetMenuTreeData = "/base-center/newmenu/getMenuTreeData",
  DeleteMenuData = '/base-center/newmenu/deleteMenuData',
  DeleteMenuDataList = '/base-center/newmenu/deleteMenuDataList',
  GetRoleMenus = "/base-center/newmenu/getRoleMenus",
  GetUserMenus = "/base-center/newmenu/getUserMenus",
  SelectMHRoleByUserId = "/user-center/role/selectMHRoleByUserId"
}


/**
 * 删除机构
 * @param ids 删除id数组
 */
export const DeleteStructure = (params) => {
  return new Promise<void>((resolve, reject) => {
    defHttp.post({ url: Api.DeleteStructure, params: tool.objectToFormData(params) }).then((res) => {
      resolve(res)
    })
  })
}

/**
 * @description: 新增机构
 */
export const saveStructure = (params) => {
  return new Promise<void>((resolve, reject) => {
    defHttp.post({ url: Api.SaveStructure, params: tool.objectToFormData(params) })
      .then((res) => {
        resolve(res.datas)
      })
  })
};

/**
 * @description: 获取机构列表
 */

export const structureList = () => {
  // var list = local.getItem("structureList");
  // if (list) {
  //   return new Promise<void>((resolve, reject) => {
  //     resolve(list);
  //   })
  // }
  return new Promise<void>((resolve, reject) => {
    defHttp.post({ url: Api.GetChildDepartment }).then((res) => {
      if (res.datas && res.datas.depInfo) {
        var a = tool.structureTree([res.datas]);
        console.log(a)
        a[0].children.forEach(item => {
          item.level = 0
        })
        resolve(a[0].children)
        local.setItem("structureList", a[0].children);
      } else {
        resolve([])
      }
    })
  })
};

/**
 * @description: 获取机构列表根节点的id
 */

export const structureListRootId = () => {
  return new Promise<void>((resolve, reject) => {
    defHttp.post({ url: Api.GetChildDepartment }).then((res) => {
      if (res.datas && res.datas.depInfo) {
        // var a = tool.structureTree([res.datas]);
        // console.log(a)
        resolve(res.datas.depInfo.departid)
      } else {
        resolve('')
      }
    })
  })
};

/**
 * @description: Get user menu based on id
 */

export const getMenuList = (args?: number) => {
  // const userStore = useUserStore();
  // let url = Api.GetMenuList;
  // try {
  //   if (userStore.getRoleList.find((v) => v == RoleEnum.SYS_ADMIN)) {
  //     url = Api.SysAdminMenuList;
  //   }
  //   return new Promise<void>((resolve, reject) => {
  //     resolve(menu)
  //   })
  // } catch (e) { }

  try {
    return new Promise<void>((resolve, reject) => {
      return defHttp.post<getMenuListResultModel>({
        url: Api.GetMenuTreeData,
      }).then(r => {
        session.setItem("menu_get_ids", menu_get_ids.concat(r.datas.map(i => i.id)))
        var o = buildTree(r.datas)
        resolve(o)
      });
    })
  } catch (e) {

  };
};

/**
 * @description: 获取新菜单列表
 */
function buildTree(nodes, parentId = null) {
  let tree = [];
  for (let node of nodes) {
    if (node.parentid === parentId) {
      node.meta = JSON.parse(node.meta)
      node.meta.status === '1' && (node.disabled = true)
      node.children = buildTree(nodes, node.id);
      tree.push(node);
    }
  }
  return tree;
}
export const menuList = (args?: number) => {
  try {
    return new Promise<void>((resolve, reject) => {
      return defHttp.post<getMenuListResultModel>({
        url: Api.GetMenuTreeData,
      }).then(r => {
        session.setItem("menu_get_ids", menu_get_ids.concat(r.datas.map(i => i.id)))
        var o = buildTree(r.datas)
        resolve(o)
      });
    })
  } catch (e) {

  };
}

/**
 * @description: Get user menu based on id
 */

export const getMenuList2 = (args?: number) => {
  const userStore = useUserStore();
  let url = Api.GetMenuList;
  try {
    if (userStore.getRoleList.find((v) => v == RoleEnum.SYS_ADMIN)) {
      url = Api.SysAdminMenuList;
    }
    return defHttp.get<getMenuListResultModel>({
      url,
      params: {
        needButton: args == 1 ? false : null,
      },
    });
  } catch (e) { }
};
/**
 * @description: 按钮的单个删除和数组删除
 */
export const delMenu = (menuIds: string[]) => {
  return defHttp.post({ url: Api.DeleteMenuDataList, data: menuIds.map(i => { return { id: i } }) });
};

export const getMenusIdsByRoleId2 = (roleId: string) => {
  return new Promise<void>((resolve, reject) => {
    resolve(session.getItem("menu_get_ids"))
  })
};

export const getMenusIdsByRoleId3 = () => {
  var params = {
    menusId: true,
    systemId: '1',
    roleIds: 'FB264E2A-BBEN-FB9T-9CEU-FF94AF30DB87',
    // roleIds: "1F899E7A-748N-609T-99CU-3FCE7FBEA814"//session.getItem("userInfo").ROLES.map(i => i.ROLE_ID).join(','),
    // roleIds: '1F899E7A-748N-609T-99CU-3FCE7FBEA814'//session.getItem("userInfo").ROLES.map(i => i.ROLE_ID).join(','),
  }
  return new Promise<void>((resolve, reject) => {
    defHttp.post<Array<string>>({ url: Api.GetRoleMenus, params: params }).then(r => {
      var a = ['e5f31f9a-9053-425a-8109-15afe229316a', '1c06d167-7f56-4782-8f76-1953f9cd9db6', '794f34af-121e-445c-a3ed-f108728820a4', 'b90bc772-d04d-4168-a94a-0073a9687147', 'b482084e-9e7e-4987-8ee4-e866c2002241', '7368633a-8039-44eb-b60a-faef248f1986', '71e6727e-85d2-48bb-bf24-9f764d2329aa', 'f5096568-fe0e-47c2-b0ec-6dc9418d1de6', '3a4eb429-97c3-44c6-80a8-b807c7a51beb', '7f4bb95c-7cdf-4791-aac8-94fac6cab64f', '3fe17080-78f9-4455-aa3c-6d26fbdad169', '92a2535d-b7ea-4985-91f0-8296915c56f5', '7b2fa3f1-479e-4f3f-8111-cccca03d074a', '063263e8-d348-4441-96e0-d4024af58054', '6419ed11-1b75-4259-b3cc-268ce64ba89b', '170f2841-615c-4dee-92b3-89f518a47917', '9b1a3fa8-126f-495d-972e-50151d747352', '7a3bebaf-6ade-44cd-ac70-d0b8ea02504a', 'c61b0c33-1ad3-450f-a6bd-f686ac67eff0', '2dda9c13-cc8d-483d-ae54-6d760e75ac81', 'ed1f6d4d-b855-481d-af1e-e42b77839ba0', 'c39e7da6-7cc6-4cae-8cd2-82abe5b8b4b0', '143a8eb3-7774-4e29-b3d2-e7a6db12083d', '78bf4696-05a9-46fa-a301-7ee025e84364', '86259fab-72cf-494c-a26c-9081bdd6ce5f', '17bd3010-ef05-4364-b3fa-f37c3a608be6', 'b8af73f8-5d34-4c46-8f17-5c071d8ee9f7', '8f25db14-71dd-4658-9d6b-a617945d935f', 'a43a7823-638e-4732-90f3-68a581a3a87b', '21c19cbd-4498-4ff7-98be-5db6444bc2af', '1989a168-9dcd-4016-bc04-bf1d6f80bf1e']
      resolve(r.datas.map(i => i.id))
      var a1 = "794f34af-121e-445c-a3ed-f108728820a4"
      var b1 = "e5c76923-c31e-4919-a95c-61b65fbec7bd"
      // resolve(r.datas.map(i => i.id).filter((value) => value !== a1).filter((value) => value !== b1))//地灾
    })
  })
};

//菜单权限位置
export const getMenusIdsByRoleId = () => {
  const userInfo: any = getAuthCache(USER_INFO_KEY);
  var p1 = new Promise((resolve, reject) => {
    defHttp.post<Array<string>>({
      url: Api.GetRoleMenus, params: {
        menusId: true,
        systemId: '1',
        roleIds: session.getItem("roles") || userInfo.info.ROLES[0].ROLE_ID,//'FB264E2A-BBEN-FB9T-9CEU-FF94AF30DB87',
      }
    }).then(r => {
      resolve(r.datas.map(i => i.id))
    })
  })
  var p2 = new Promise((resolve, reject) => {

    defHttp.post<Array<string>>({
      url: Api.GetUserMenus, params: {
        getMenuId: true,
        getUserConfigMenu: true,
        systemId: "1",
        useOneRole: true,
        userId: session.getItem('userId') || userInfo.info.EMPLOYEE.EMPLOYEE_ID,
      }
    }).then(r => {
      resolve(r.datas)
    })
  })


  return Promise.all([p1, p2]).then((res) => {
    var arr = Array.from(new Set(res[0].concat(res[1])));
    //所有权限
    var a = ['e5f31f9a-9053-425a-8109-15afe229316a', '1c06d167-7f56-4782-8f76-1953f9cd9db6', '794f34af-121e-445c-a3ed-f108728820a4', 'b90bc772-d04d-4168-a94a-0073a9687147', 'b482084e-9e7e-4987-8ee4-e866c2002241', '7368633a-8039-44eb-b60a-faef248f1986', '71e6727e-85d2-48bb-bf24-9f764d2329aa', 'f5096568-fe0e-47c2-b0ec-6dc9418d1de6', '3a4eb429-97c3-44c6-80a8-b807c7a51beb', '7f4bb95c-7cdf-4791-aac8-94fac6cab64f', '3fe17080-78f9-4455-aa3c-6d26fbdad169', '92a2535d-b7ea-4985-91f0-8296915c56f5', '7b2fa3f1-479e-4f3f-8111-cccca03d074a', '063263e8-d348-4441-96e0-d4024af58054', '6419ed11-1b75-4259-b3cc-268ce64ba89b', '170f2841-615c-4dee-92b3-89f518a47917', '9b1a3fa8-126f-495d-972e-50151d747352', '7a3bebaf-6ade-44cd-ac70-d0b8ea02504a', 'c61b0c33-1ad3-450f-a6bd-f686ac67eff0', '2dda9c13-cc8d-483d-ae54-6d760e75ac81', 'ed1f6d4d-b855-481d-af1e-e42b77839ba0', 'c39e7da6-7cc6-4cae-8cd2-82abe5b8b4b0', '143a8eb3-7774-4e29-b3d2-e7a6db12083d', '78bf4696-05a9-46fa-a301-7ee025e84364', '86259fab-72cf-494c-a26c-9081bdd6ce5f', '17bd3010-ef05-4364-b3fa-f37c3a608be6', 'b8af73f8-5d34-4c46-8f17-5c071d8ee9f7', '8f25db14-71dd-4658-9d6b-a617945d935f', 'a43a7823-638e-4732-90f3-68a581a3a87b', '21c19cbd-4498-4ff7-98be-5db6444bc2af', '1989a168-9dcd-4016-bc04-bf1d6f80bf1e']
    //超级管理所有权限
    // var adminAll = ["e5f31f9a-9053-425a-8109-15afe229316a", "1c06d167-7f56-4782-8f76-1953f9cd9db6", "7368633a-8039-44eb-b60a-faef248f1986", "b482084e-9e7e-4987-8ee4-e866c2002241", "b90bc772-d04d-4168-a94a-0073a9687147", "e5c76923-c31e-4919-a95c-61b65fbec7bd", "794f34af-121e-445c-a3ed-f108728820a4", "f8beed1b-fd0a-4036-96be-21ba6c152d0a", "c44164ab-1071-44de-92da-b0ec6c012557", "8dcad717-4b20-41a2-bbb9-4931ecc8c1cb", "3a4eb429-97c3-44c6-80a8-b807c7a51beb", "3fe17080-78f9-4455-aa3c-6d26fbdad169", "7f4bb95c-7cdf-4791-aac8-94fac6cab64f", "7b2fa3f1-479e-4f3f-8111-cccca03d074a", "063263e8-d348-4441-96e0-d4024af58054", "6419ed11-1b75-4259-b3cc-268ce64ba89b", "7ad9e09a-4045-4459-94b8-db2acc8ae021", "9b1a3fa8-126f-495d-972e-50151d747352", "85237d14-0235-4217-ae5d-2df5a89c2155", "27dda38f-f331-4762-93a9-823134309b1a", "6bb76335-16dd-437b-8c6b-6faf390f3d58", "e01053e4-1d35-4dac-994c-3402c0ba5285", "b1e19c5c-827f-4018-ae21-572ddfdec588", "79d0cad6-a474-44c5-90a7-f29b411275a2", "92a2535d-b7ea-4985-91f0-8296915c56f5", "170f2841-615c-4dee-92b3-89f518a47917", "88556af6-456b-4cf7-b42e-5056f24bd7ab", "2dda9c13-cc8d-483d-ae54-6d760e75ac81", "7a3bebaf-6ade-44cd-ac70-d0b8ea02504a", "c61b0c33-1ad3-450f-a6bd-f686ac67eff0", "ed1f6d4d-b855-481d-af1e-e42b77839ba0", "c39e7da6-7cc6-4cae-8cd2-82abe5b8b4b0", "259c3611-65b0-4575-b3c2-9908bb45c889", "c60b4d6e-b61b-488c-a1dd-f9930212f23d", "6bcc80a0-fe34-4256-b1c6-b5e4194edda6", "78bf4696-05a9-46fa-a301-7ee025e84364", "86259fab-72cf-494c-a26c-9081bdd6ce5f", "17bd3010-ef05-4364-b3fa-f37c3a608be6", "143a8eb3-7774-4e29-b3d2-e7a6db12083d", "b8af73f8-5d34-4c46-8f17-5c071d8ee9f7", "8f25db14-71dd-4658-9d6b-a617945d935f", "a43a7823-638e-4732-90f3-68a581a3a87b", "21c19cbd-4498-4ff7-98be-5db6444bc2af", "1989a168-9dcd-4016-bc04-bf1d6f80bf1e"]
    return arr
    // return a
    // return adminAll
  }).catch((error) => {
    console.log(error)
  })
};

/**
 * @description: 根据用户登录名称获取用户角色
 */

export const getUserRoleList = (id) => {
  return new Promise((resolve, reject) => {
    defHttp.post<Array<string>>({
      url: Api.SelectMHRoleByUserId, params: {
        getRoleIds: true,
        userId: id
      }
    }).then(r => {
      resolve(r.datas)
    })
  })
};



// return new Promise<void>((resolve, reject) => {
//   defHttp.post({ url: Api.GetUserMenus, params })
//     .then((r) => resolve(r.datas)).catch(e => resolve())
// })


/**
 * @description: 根据用户ID获取菜单权限
 */

export const getUserMenuList = (id) => {
  var params = {
    menusId: true,
    systemId: '1',
    roleIds: 'FB264E2A-BBEN-FB9T-9CEU-FF94AF30DB87',
    // roleIds: "1F899E7A-748N-609T-99CU-3FCE7FBEA814"//session.getItem("userInfo").ROLES.map(i => i.ROLE_ID).join(','),
  }
  return new Promise<void>((resolve, reject) => {
    defHttp.post<Array<string>>({ url: Api.GetRoleMenus, params: params }).then(r => {
      var a = ['e5f31f9a-9053-425a-8109-15afe229316a', '1c06d167-7f56-4782-8f76-1953f9cd9db6', '794f34af-121e-445c-a3ed-f108728820a4', 'b90bc772-d04d-4168-a94a-0073a9687147', 'b482084e-9e7e-4987-8ee4-e866c2002241', '7368633a-8039-44eb-b60a-faef248f1986', '71e6727e-85d2-48bb-bf24-9f764d2329aa', 'f5096568-fe0e-47c2-b0ec-6dc9418d1de6', '3a4eb429-97c3-44c6-80a8-b807c7a51beb', '7f4bb95c-7cdf-4791-aac8-94fac6cab64f', '3fe17080-78f9-4455-aa3c-6d26fbdad169', '92a2535d-b7ea-4985-91f0-8296915c56f5', '7b2fa3f1-479e-4f3f-8111-cccca03d074a', '063263e8-d348-4441-96e0-d4024af58054', '6419ed11-1b75-4259-b3cc-268ce64ba89b', '170f2841-615c-4dee-92b3-89f518a47917', '9b1a3fa8-126f-495d-972e-50151d747352', '7a3bebaf-6ade-44cd-ac70-d0b8ea02504a', 'c61b0c33-1ad3-450f-a6bd-f686ac67eff0', '2dda9c13-cc8d-483d-ae54-6d760e75ac81', 'ed1f6d4d-b855-481d-af1e-e42b77839ba0', 'c39e7da6-7cc6-4cae-8cd2-82abe5b8b4b0', '143a8eb3-7774-4e29-b3d2-e7a6db12083d', '78bf4696-05a9-46fa-a301-7ee025e84364', '86259fab-72cf-494c-a26c-9081bdd6ce5f', '17bd3010-ef05-4364-b3fa-f37c3a608be6', 'b8af73f8-5d34-4c46-8f17-5c071d8ee9f7', '8f25db14-71dd-4658-9d6b-a617945d935f', 'a43a7823-638e-4732-90f3-68a581a3a87b', '21c19cbd-4498-4ff7-98be-5db6444bc2af', '1989a168-9dcd-4016-bc04-bf1d6f80bf1e']
      resolve(r.datas.map(i => i.id))
      var a1 = "794f34af-121e-445c-a3ed-f108728820a4"
      var b1 = "e5c76923-c31e-4919-a95c-61b65fbec7bd"
      // resolve(r.datas.map(i => i.id).filter((value) => value !== a1).filter((value) => value !== b1))//地灾
    })
  })
};