/**
 * KRA - Menu API Service
 */
import request from '@/utils/request';

export interface MenuItem {
  ID?: number;
  parentId?: number;
  path: string;
  name: string;
  hidden?: boolean;
  component?: string;
  sort?: number;
  meta?: {
    title?: string;
    icon?: string;
    keepAlive?: boolean;
    defaultMenu?: boolean;
    closeTab?: boolean;
  };
  children?: MenuItem[];
  parameters?: Array<{ key: string; value: string }>;
  menuBtn?: any[];
}

/** 获取动态路由菜单 */
export const asyncMenu = () => {
  return request.post('/menu/getMenu');
};

/** 获取菜单列表 */
export const getMenuList = (data?: { page?: number; pageSize?: number }) => {
  return request.post('/menu/getMenuList', data || { page: 1, pageSize: 999 });
};

/** 新增基础菜单 */
export const addBaseMenu = (data: MenuItem) => {
  return request.post('/menu/addBaseMenu', data);
};

/** 获取基础路由树 */
export const getBaseMenuTree = () => {
  return request.post('/menu/getBaseMenuTree');
};

/** 添加菜单权限关联 */
export const addMenuAuthority = (data: { menus: number[]; authorityId: number | string }) => {
  return request.post('/menu/addMenuAuthority', {
    ...data,
    authorityId: Number(data.authorityId),
  });
};

/** 获取菜单权限关联 */
export const getMenuAuthority = (data: { authorityId: number | string }) => {
  return request.post('/menu/getMenuAuthority', {
    authorityId: Number(data.authorityId),
  });
};

/** 删除菜单 */
export const deleteBaseMenu = (data: { ID: number }) => {
  return request.post('/menu/deleteBaseMenu', data);
};

/** 更新菜单 */
export const updateBaseMenu = (data: MenuItem) => {
  return request.post('/menu/updateBaseMenu', data);
};

/** 根据ID获取菜单 */
export const getBaseMenuById = (data: { id: number }) => {
  return request.post('/menu/getBaseMenuById', data);
};

export default {
  asyncMenu,
  getMenuList,
  addBaseMenu,
  getBaseMenuTree,
  addMenuAuthority,
  getMenuAuthority,
  deleteBaseMenu,
  updateBaseMenu,
  getBaseMenuById,
};
