// 模拟静态数据
const mockMenus = [
  { menuId: 1, menuName: '系统管理', parentId: 0, orderNum: 1, path: 'system', component: 'Layout', status: '0' },
  { menuId: 2, menuName: '用户管理', parentId: 1, orderNum: 1, path: 'user', component: 'system/user/index', status: '0' },
  { menuId: 3, menuName: '角色管理', parentId: 1, orderNum: 2, path: 'role', component: 'system/role/index', status: '0' },
  { menuId: 4, menuName: '菜单管理', parentId: 1, orderNum: 3, path: 'menu', component: 'system/menu/index', status: '0' }
];

// 查询菜单列表
export function listMenu(query) {
  const { menuName, status } = query;
  let filteredMenus = [...mockMenus];
  
  if (menuName) {
    filteredMenus = filteredMenus.filter(menu => menu.menuName.includes(menuName));
  }
  if (status) {
    filteredMenus = filteredMenus.filter(menu => menu.status === status);
  }
  
  return Promise.resolve(filteredMenus);
}

// 查询菜单详细
export function getMenu(menuId) {
  const menu = mockMenus.find(m => m.menuId === parseInt(menuId));
  return Promise.resolve(menu || {});
}

// 查询菜单下拉树结构
export function treeselect() {
  const buildTree = (parentId) => {
    const children = mockMenus.filter(m => m.parentId === parentId);
    return children.map(child => ({
      id: child.menuId,
      label: child.menuName,
      children: buildTree(child.menuId)
    }));
  };
  
  return Promise.resolve(buildTree(0));
}

// 根据角色ID查询菜单下拉树结构
export function roleMenuTreeselect(roleId) {
  const buildTree = (parentId) => {
    const children = mockMenus.filter(m => m.parentId === parentId);
    return children.map(child => ({
      id: child.menuId,
      label: child.menuName,
      children: buildTree(child.menuId)
    }));
  };
  
  return Promise.resolve({
    menus: buildTree(0),
    checkedKeys: [1, 2, 3, 4] // 模拟已选中的菜单
  });
}

// 新增菜单
export function addMenu(data) {
  const newMenu = {
    menuId: mockMenus.length + 1,
    ...data
  };
  mockMenus.push(newMenu);
  return Promise.resolve({ code: 200, msg: '新增成功' });
}

// 修改菜单
export function updateMenu(data) {
  const index = mockMenus.findIndex(m => m.menuId === data.menuId);
  if (index !== -1) {
    mockMenus[index] = { ...mockMenus[index], ...data };
  }
  return Promise.resolve({ code: 200, msg: '修改成功' });
}

// 删除菜单
export function delMenu(menuId) {
  const index = mockMenus.findIndex(m => m.menuId === parseInt(menuId));
  if (index !== -1) {
    mockMenus.splice(index, 1);
  }
  return Promise.resolve({ code: 200, msg: '删除成功' });
}