import request from '@/utils/request';

/**
 * 分页查询用户
 * @param params 查询条件
 */
export async function pageTeamUser(params) {
  const res = await request.get('/institution/teamUser/page', {
    params
  });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询用户列表
 * @param params 查询条件
 */
export async function listTeamUser(params) {
  const res = await request.post('/institution/teamUser/list', params);
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询用户
 * @param id 用户id
 */
export async function getTeamUser(id) {
  const res = await request.get('/institution/teamUser/' + id);
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加用户
 * @param data 用户信息
 */
export async function addTeamUser(data) {
  const res = await request.post('/institution/teamUser', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改用户
 * @param data 用户信息
 */
export async function updateTeamUser(data) {
  const res = await request.put('/institution/teamUser', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除用户
 * @param id 用户id
 */
export async function removeTeamUser(id) {
  const res = await request.delete('/institution/teamUser/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除用户
 * @param data 用户id集合
 */
export async function removeTeamUsers(data) {
  const res = await request.delete('/institution/teamUser/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改用户状态
 * @param userId 用户id
 * @param status 状态
 */
export async function updateTeamUserStatus(userId, status) {
  const res = await request.put('/institution/teamUser/status', {
    userId,
    status
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 重置用户密码
 * @param userId 用户id
 * @param password 密码
 * @returns {Promise<string>}
 */
export async function updateTeamUserPassword(userId, password = '123456') {
  const res = await request.put('/institution/teamUser/password', {
    userId,
    password
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入用户
 * @param file excel文件
 */
export async function importTeamUser(file) {
  const formData = new FormData();
  formData.append('file', file);
  const res = await request.post('/institution/teamUser/import', formData);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查用户是否存在
 * @param field 检查的字段
 * @param value 字段的值
 * @param id 修改时的id
 */
export async function checkExistence(field, value, id) {
  const res = await request.get('/institution/teamUser/existence', {
    params: { field, value, id }
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
