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

/**
 * 分页查询企业信息
 */
export async function pageOaCompany(params: OaCompanyParam) {
  const res = await request.get<ApiResult<PageResult<OaCompany>>>(
    '/oa/oa-company/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询企业信息列表
 */
export async function listOaCompany(params?: OaCompanyParam) {
  const res = await request.get<ApiResult<OaCompany[]>>(
    '/oa/oa-company',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加企业信息
 */
export async function addOaCompany(data: OaCompany) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-company',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改企业信息
 */
export async function updateOaCompany(data: OaCompany) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-company',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除企业信息
 */
export async function removeOaCompany(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/oa/oa-company/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 根据id查询企业信息
 */
export async function getOaCompany(id: number) {
  const res = await request.get<ApiResult<OaCompany>>(
    '/oa/oa-company/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
