// 模拟静态数据
const mockRoles = [
  { roleId: 1, roleName: '管理员', roleKey: 'admin', roleSort: 1, status: '0', createTime: '2023-01-01' },
  { roleId: 2, roleName: '普通角色', roleKey: 'common', roleSort: 2, status: '0', createTime: '2023-01-02' }
];

// 查询角色列表
export function listRole(query) {
  // 模拟分页
  const { pageNum, pageSize, roleName, roleKey, status } = query;
  let filteredRoles = [...mockRoles];
  
  if (roleName) {
    filteredRoles = filteredRoles.filter(role => role.roleName.includes(roleName));
  }
  if (roleKey) {
    filteredRoles = filteredRoles.filter(role => role.roleKey.includes(roleKey));
  }
  if (status) {
    filteredRoles = filteredRoles.filter(role => role.status === status);
  }
  
  const total = filteredRoles.length;
  const start = (pageNum - 1) * pageSize;
  const end = start + pageSize;
  const list = filteredRoles.slice(start, end);
  
  return Promise.resolve({
    total,
    rows: list
  });
}

// 查询角色详细
export function getRole(roleId) {
  const role = mockRoles.find(r => r.roleId === parseInt(roleId));
  return Promise.resolve(role || {});
}

// 新增角色
export function addRole(data) {
  const newRole = {
    roleId: mockRoles.length + 1,
    ...data,
    createTime: new Date().toISOString().split('T')[0]
  };
  mockRoles.push(newRole);
  return Promise.resolve({ code: 200, msg: '新增成功' });
}

// 修改角色
export function updateRole(data) {
  const index = mockRoles.findIndex(r => r.roleId === data.roleId);
  if (index !== -1) {
    mockRoles[index] = { ...mockRoles[index], ...data };
  }
  return Promise.resolve({ code: 200, msg: '修改成功' });
}

// 角色数据权限
export function dataScope(data) {
  return Promise.resolve({ code: 200, msg: '修改成功' });
}

// 角色状态修改
export function changeRoleStatus(roleId, status) {
  const role = mockRoles.find(r => r.roleId === parseInt(roleId));
  if (role) {
    role.status = status;
  }
  return Promise.resolve({ code: 200, msg: '修改成功' });
}

// 删除角色
export function delRole(roleId) {
  const index = mockRoles.findIndex(r => r.roleId === parseInt(roleId));
  if (index !== -1) {
    mockRoles.splice(index, 1);
  }
  return Promise.resolve({ code: 200, msg: '删除成功' });
}

// 查询角色已授权用户列表
export function allocatedUserList(query) {
  return Promise.resolve({
    total: 1,
    rows: [{ userId: 1, userName: 'admin', nickName: '管理员' }]
  });
}

// 查询角色未授权用户列表
export function unallocatedUserList(query) {
  return Promise.resolve({
    total: 1,
    rows: [{ userId: 2, userName: 'test', nickName: '测试用户' }]
  });
}

// 取消用户授权角色
export function authUserCancel(data) {
  return Promise.resolve({ code: 200, msg: '取消授权成功' });
}

// 批量取消用户授权角色
export function authUserCancelAll(data) {
  return Promise.resolve({ code: 200, msg: '批量取消授权成功' });
}

// 授权用户选择
export function authUserSelectAll(data) {
  return Promise.resolve({ code: 200, msg: '授权成功' });
}

// 根据角色ID查询部门树结构
export function deptTreeSelect(roleId) {
  return Promise.resolve([
    { id: 1, label: '总公司', children: [{ id: 2, label: '研发部' }] }
  ]);
}
