/*
 * @Author: sjl
 * @Date: 2023-04-17 14:51:36
 * @Descripttion:
 */
import { defHttp } from '/@/utils/http/axios';
import { getMenuListResultModel } from './model/menuModel';
import { useUserStore } from '/@/store/modules/user';
import { RoleEnum } from '/@/enums/roleEnum';
import { session } from '/@/utils/Memory.js';

enum Api {
  BaseMenuUrl = '/menu',
  GetMenuList = '/menu/me/menus',
  SysAdminMenuList = '/admin/me/menus',
  GetMenuIdsByRoleId = '/menu/get_ids/',
  updateRolePermission = '/role/updateRolePermission',
  getRolePermissionByRoleId = '/role/getRolePermissionByRoleId',
  getRolePermissionByUserId = '/role/getRolePermissionByUserId',
}

export const getMenuList3 = (args?: number, type: any) => {
  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: {
        // id: '651de9b4-c3fb-4f48-b5be-001d06db2298',
        needButton: args == 1 ? false : null,
      },
    });
  } catch (e) {}
};

export const getMenuList5 = (args?: number, type: any) => {
  const userStore = useUserStore();
  let url = Api.GetMenuList;
  try {
    if (userStore.getRoleList.find((v) => v == RoleEnum.SYS_ADMIN)) {
      url = Api.SysAdminMenuList;
    }
    return new Promise((resolve, reject) => {
      defHttp
        .get<getMenuListResultModel>({
          url,
          params: {
            // id: '651de9b4-c3fb-4f48-b5be-001d06db2298',
            needButton: args == 1 ? false : null,
          },
        })
        .then((obj) => {
          console.log('路由接口：', obj);
          if (!obj) return;
          if (args == 1) session.setItem('allMenuList', obj);
          if (typeof args == 'object') {
            resolve(obj);
          } else {
            obj.map((i) => {
              if (i.name == session.getItem('currentSystemName')) obj = i.children;
            });
            resolve(obj);
          }
        })
        .catch((err) => {
          console.log(err);
          reject(err);
        });
    });
  } catch (e) {}
};

/**
 * @description: 获取基于id的用户菜单
 */

export const getMenuList = async (args?: number) => {
  const userStore = useUserStore();
  //获取菜单列表 PLATFORM_ADMIN 平台管理员
  let url = Api.GetMenuList;
  try {
    if (userStore.getRoleList.find((v) => v == RoleEnum.SYS_ADMIN)) {
      //系统管理员菜单列表
      url = Api.SysAdminMenuList;
    }
    const obj = await defHttp.get<getMenuListResultModel>({
      url,
      params: {
        needButton: args == 1 ? false : null,
      },
    });
    console.log('陆游借口返回：', obj);
    const n = session.getItem('currentSystemName');
    if (typeof args == 'object') {
      return obj;
    }
    // if (userStore.getRoleList.find((v) => v == RoleEnum.SYS_ADMIN)) {
      if (n && n != '地质灾害数据支撑子系统' && n != '智能化地灾中台') {
        let menu = null;
        for (let i = 0; i < obj.length; i++) {
          const item = obj[i];
          if (item.name == n) menu = item.children;
        }
        if (args == 1) session.setItem('allMenuList', obj);
        return menu;
      } else {
        let menu = null;
        for (let i = 0; i < obj.length; i++) {
          const item = obj[i];
          if (item.name == '业务系统') menu = item.children;
        }
        if (args == 1) session.setItem('allMenuList', obj);
        return menu;
      }
    // } else {
    //   if (n && n != '地质灾害数据支撑子系统' && n != '智能化地灾中台') {
    //     let menu = null;
    //     for (let i = 0; i < obj.length; i++) {
    //       const item = obj[i];
    //       if (item.name == n) menu = item.children;
    //     }
    //     if (args == 1) session.setItem('allMenuList', obj);
    //     return menu;
    //   } else {
    //     session.setItem('allMenuList', obj);
    //     let menu = null;
    //     for (let i = 0; i < obj.length; i++) {
    //       const item = obj[i];
    //       if (item.name == n) menu = item.children;
    //     }
    //     return menu ? menu : obj;
    //   }
    // }
  } catch (e) {}
};

export const delMenu = (menuIds: string[]) => {
  const url = Api.BaseMenuUrl;
  return defHttp.delete({ url: url, data: menuIds });
};

export const getMenusIdsByRoleId = async (roleId: string) => {
  const url = Api.GetMenuIdsByRoleId + roleId;
  try {
    const response = await defHttp.get<Array<string>>({ url });
    return response;
  } catch (error) {
    console.error(error);
    return [];
  }
};
// export const getMenusIdsByRoleId = (roleId: string) => {
//   const url = Api.GetMenuIdsByRoleId + roleId;
//   return defHttp.get<Array<string>>({ url });
// };
/**
 * 根据角色Id获取数据权限
 */
export const getRolePermissionByUserId = (roleId: string) => {
  const url = Api.getRolePermissionByUserId + '?roleId=' + roleId;
  return defHttp.get<Array<string>>({ url });
};
/**
 * 根据用户Id获取数据权限
 */
export const getRolePermissionByRoleId = (roleId: string) => {
  const url = Api.getRolePermissionByRoleId + '?roleId=' + roleId;
  return defHttp.get<Array<string>>({ url });
};

export const updateRolePermission = (params) => {
  return defHttp.post({ url: Api.updateRolePermission, data: params });
};
