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

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

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

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

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

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

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

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