import request from '@/utils/request';

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

/**
 * 查询机构列表
 * @param params 查询条件
 */
export async function listOrganizations(params) {
  const res = await request.get('/institution/organization', {
    params
  });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询上级机构列表
 * @param params 查询条件
 */
export async function listOrganizationParent(params) {
  const res = await request.get('/institution/organization/parent', {
    params
  });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

/**
 * 删除机构
 * @param id 机构id
 */
export async function removeOrganization(id) {
  return removeOrganizations([id]);
}

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

/**
 * 机构详情
 * @param organizationId 机构id
 */
export async function infoOrganization(organizationId) {
  const res = await request.get('/institution/organization/info', {
    params: { organizationId }
  });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
