import request from '~/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { CmsDocs, CmsDocsParam } from './model';


/**
 * 分页查询文档管理记录表
 */
export async function pageCmsDocs(params: CmsDocsParam) {
  const res = await request.get<ApiResult<PageResult<CmsDocs>>>(
    '/cms/cms-docs/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询文档管理记录表列表
 */
export async function listCmsDocs(params?: CmsDocsParam) {
  const res = await request.get<ApiResult<CmsDocs[]>>(
    '/cms/cms-docs',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加文档管理记录表
 */
export async function addCmsDocs(data: CmsDocs) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-docs',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改文档管理记录表
 */
export async function updateCmsDocs(data: CmsDocs) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-docs',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除文档管理记录表
 */
export async function removeCmsDocs(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-docs/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除文档管理记录表
 */
export async function removeBatchCmsDocs(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-docs/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询文档管理记录表
 */
export async function getCmsDocs(id: number) {
  const res = await request.get<ApiResult<CmsDocs>>(
    '/cms/cms-docs/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
