// 模拟静态数据
const mockUsers = [
  { userId: 1, userName: 'admin', nickName: '管理员', dept: { deptName: '总公司' }, phonenumber: '15888888888', status: '0', createTime: '2023-01-01' },
  { userId: 2, userName: 'test', nickName: '测试用户', dept: { deptName: '研发部' }, phonenumber: '15666666666', status: '0', createTime: '2023-01-02' }
];

// 查询用户列表
export function listUser(query) {
  // 模拟分页
  const { pageNum, pageSize, userName, phonenumber, status, deptId } = query;
  let filteredUsers = [...mockUsers];
  
  if (userName) {
    filteredUsers = filteredUsers.filter(user => user.userName.includes(userName));
  }
  if (phonenumber) {
    filteredUsers = filteredUsers.filter(user => user.phonenumber.includes(phonenumber));
  }
  if (status) {
    filteredUsers = filteredUsers.filter(user => user.status === status);
  }
  if (deptId) {
    filteredUsers = filteredUsers.filter(user => user.dept.deptId === deptId);
  }
  
  const total = filteredUsers.length;
  const start = (pageNum - 1) * pageSize;
  const end = start + pageSize;
  const list = filteredUsers.slice(start, end);
  
  return Promise.resolve({
    total,
    rows: list
  });
}

// 查询用户详细
export function getUser(userId) {
  const user = mockUsers.find(u => u.userId === parseInt(userId));
  return Promise.resolve(user || {});
}

// 新增用户
export function addUser(data) {
  const newUser = {
    userId: mockUsers.length + 1,
    ...data,
    createTime: new Date().toISOString().split('T')[0]
  };
  mockUsers.push(newUser);
  return Promise.resolve({ code: 200, msg: '新增成功' });
}

// 修改用户
export function updateUser(data) {
  const index = mockUsers.findIndex(u => u.userId === data.userId);
  if (index !== -1) {
    mockUsers[index] = { ...mockUsers[index], ...data };
  }
  return Promise.resolve({ code: 200, msg: '修改成功' });
}

// 删除用户
export function delUser(userId) {
  const index = mockUsers.findIndex(u => u.userId === parseInt(userId));
  if (index !== -1) {
    mockUsers.splice(index, 1);
  }
  return Promise.resolve({ code: 200, msg: '删除成功' });
}

// 用户密码重置
export function resetUserPwd(userId, password) {
  return Promise.resolve({ code: 200, msg: '重置成功' });
}

// 用户状态修改
export function changeUserStatus(userId, status) {
  const user = mockUsers.find(u => u.userId === parseInt(userId));
  if (user) {
    user.status = status;
  }
  return Promise.resolve({ code: 200, msg: '修改成功' });
}

// 查询用户个人信息
export function getUserProfile() {
  return Promise.resolve(mockUsers[0]);
}

// 修改用户个人信息
export function updateUserProfile(data) {
  Object.assign(mockUsers[0], data);
  return Promise.resolve({ code: 200, msg: '修改成功' });
}

// 用户密码重置
export function updateUserPwd(oldPassword, newPassword) {
  return Promise.resolve({ code: 200, msg: '修改成功' });
}

// 用户头像上传
export function uploadAvatar(data) {
  return Promise.resolve({ code: 200, msg: '上传成功' });
}

// 查询授权角色
export function getAuthRole(userId) {
  return Promise.resolve({
    user: mockUsers.find(u => u.userId === parseInt(userId)),
    roles: [{ roleId: 1, roleName: '管理员' }]
  });
}

// 保存授权角色
export function updateAuthRole(data) {
  return Promise.resolve({ code: 200, msg: '授权成功' });
}

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