import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { OaCompanyUser, OaCompanyUserParam } from './model';

/**
 * 分页查询成员管理
 */
export async function pageOaCompanyUser(params: OaCompanyUserParam) {
  const res = await request.get<ApiResult<PageResult<OaCompanyUser>>>(
    '/oa/oa-company-user/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询成员管理列表
 */
export async function listOaCompanyUser(params?: OaCompanyUserParam) {
  const res = await request.get<ApiResult<OaCompanyUser[]>>(
    '/oa/oa-company-user',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加成员管理
 */
export async function addOaCompanyUser(data: OaCompanyUser) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-company-user',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改成员管理
 */
export async function updateOaCompanyUser(data: OaCompanyUser) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-company-user',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除成员管理
 */
export async function removeOaCompanyUser(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/oa/oa-company-user/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除成员管理
 */
export async function removeBatchOaCompanyUser(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/oa/oa-company-user/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询成员管理
 */
export async function getOaCompanyUser(id: number) {
  const res = await request.get<ApiResult<OaCompanyUser>>(
    '/oa/oa-company-user/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
