import request from '~/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  Config,
  CmsWebsiteField,
  CmsWebsiteFieldParam
} from "@/api/cms/cmsWebsiteField/model";


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

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

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

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

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

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

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

/**
 * 检查IP是否存在
 */
export async function checkExistence(
  field: string,
  value: string,
  id?: number
) {
  const res = await request.get<ApiResult<unknown>>(
    '/cms/website-field/existence',
    {
      params: { field, value, 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/website-field/config',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
