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

/**
 * 分页查询ssl证书
 */
export async function pageOaAssetsSsl(params: OaAssetsSslParam) {
  const res = await request.get<ApiResult<PageResult<OaAssetsSsl>>>(
    '/oa/oa-assets-ssl/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询ssl证书列表
 */
export async function listOaAssetsSsl(params?: OaAssetsSslParam) {
  const res = await request.get<ApiResult<OaAssetsSsl[]>>(
    '/oa/oa-assets-ssl',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加ssl证书
 */
export async function addOaAssetsSsl(data: OaAssetsSsl) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-assets-ssl',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改ssl证书
 */
export async function updateOaAssetsSsl(data: OaAssetsSsl) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-assets-ssl',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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