import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Design, DesignParam } from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询页面设计
 */
export async function pageDesign(params: DesignParam) {
  const res = await request.get<ApiResult<PageResult<Design>>>(
    MODULES_API_URL + '/cms/design/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询页面设计列表
 */
export async function listDesign(params?: DesignParam) {
  const res = await request.get<ApiResult<Design[]>>(
    MODULES_API_URL + '/cms/design',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加页面设计
 */
export async function addDesign(data: Design) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/cms/design',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改页面设计
 */
export async function updateDesign(data: Design) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/cms/design',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除页面设计
 */
export async function removeDesign(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/cms/design/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除页面设计
 */
export async function removeBatchDesign(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/cms/design/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改用户状态
 */
export async function updateDesignStatus(designId?: number, status?: number) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/cms/design/status',
    {
      designId,
      status
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询页面设计
 */
export async function getDesign(id: number) {
  const res = await request.get<ApiResult<Design>>(
    MODULES_API_URL + '/cms/design/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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