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

/**
 * 分页查询云服务器
 */
export async function pageOaAssets(params: OaAssetsParam) {
  const res = await request.get<ApiResult<PageResult<OaAssets>>>(
    '/oa/oa-assets/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询云服务器列表
 */
export async function listOaAssets(params?: OaAssetsParam) {
  const res = await request.get<ApiResult<OaAssets[]>>(
    '/oa/oa-assets',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加云服务器
 */
export async function addOaAssets(data: OaAssets) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-assets',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改云服务器
 */
export async function updateOaAssets(data: OaAssets) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-assets',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

/**
 * 根据id查询云服务器
 */
export async function getOaAssets(id: number) {
  const res = await request.get<ApiResult<OaAssets>>(
    '/oa/oa-assets/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
