import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {CmsWebsiteField, CmsWebsiteFieldParam, Config} from './model';

/**
 * 分页查询应用参数
 */
export async function pageCmsWebsiteField(params: CmsWebsiteFieldParam) {
  const res = await request.get<ApiResult<PageResult<CmsWebsiteField>>>(
    '/cms/cms-website-field/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询应用参数列表
 */
export async function listCmsWebsiteField(params?: CmsWebsiteFieldParam) {
  const res = await request.get<ApiResult<CmsWebsiteField[]>>(
    '/cms/cms-website-field',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加应用参数
 */
export async function addCmsWebsiteField(data: CmsWebsiteField) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-website-field',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改应用参数
 */
export async function updateCmsWebsiteField(data: CmsWebsiteField) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-website-field',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除应用参数
 */
export async function removeCmsWebsiteField(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-website-field/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除应用参数
 */
export async function removeBatchCmsWebsiteField(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-website-field/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询应用参数
 */
export async function getCmsWebsiteField(id: number) {
  const res = await request.get<ApiResult<CmsWebsiteField>>(
    '/cms/cms-website-field/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 恢复项目参数
 */
export async function undeleteWebsiteField(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/website-field/undelete/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}


/**
 * 查询项目参数列表
 */
export async function configWebsiteField(params?: CmsWebsiteFieldParam) {
  const res = await request.get<ApiResult<Config>>(
      '/cms/cms-website-field/config',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
