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

/**
 * 分页查询域名
 */
export async function pageOaAssetsDomain(params: OaAssetsDomainParam) {
  const res = await request.get<ApiResult<PageResult<OaAssetsDomain>>>(
    '/oa/oa-assets-domain/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询域名列表
 */
export async function listOaAssetsDomain(params?: OaAssetsDomainParam) {
  const res = await request.get<ApiResult<OaAssetsDomain[]>>(
    '/oa/oa-assets-domain',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加域名
 */
export async function addOaAssetsDomain(data: OaAssetsDomain) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-assets-domain',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改域名
 */
export async function updateOaAssetsDomain(data: OaAssetsDomain) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-assets-domain',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除域名
 */
export async function removeOaAssetsDomain(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/oa/oa-assets-domain/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 根据id查询域名
 */
export async function getOaAssetsDomain(id: number) {
  const res = await request.get<ApiResult<OaAssetsDomain>>(
    '/oa/oa-assets-domain/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
