import request from '@/utils/request';

/**
 * 获取当前登录的菜单、按钮权限
 */
export async function getResourcesTree({ groupId, roleId }) {
  const res = await request.get('/system/resources/getResourcesTree?groupId=' + groupId + '&roleId=' + roleId);
  if (res.data.code == 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改当前登录的用户密码
 */
export async function updatePassword(data) {
  const res = await request.put('/auth/password', data);
  if (res.data.code === 0) {
    return res.data.message ?? '修改成功';
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 角色切换 重新获取token
 */
export async function changeRole(data) {
  const res = await request.post('/main/user/changeRole', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询未读通知
 */
export async function getUnreadNotice() {
  const res = await request.get('/sys/notifymessage/getUnreadNotifyMessageList');
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 获取某用户的未读消息条数
 */
export async function getUnreadNotifyMessageCountAPI() {
  const res = await request.get('/sys/notifymessage/getUnreadNotifyMessageCount');
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 更新已读-指定消息ID
 */
export async function updateNotifyMessageReadByIdAPI(data) {
  const res = await request.post('/sys/notifymessage/updateNotifyMessageRead', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 全部更新已读
 */
export async function updateAllNotifyMessageReadAPI() {
  const res = await request.post('/sys/notifymessage/updateAllNotifyMessageRead');
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
