import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { BCFood, BCFoodParam } from '@/api/apps/bc/food/model';
import { OPEN_API_URL } from '@/config/setting';
/**
 * 分页查询计划
 */
export async function pageBCFood(params: BCFoodParam) {
  const res = await request.get<ApiResult<PageResult<BCFood>>>(
    OPEN_API_URL + '/apps/bc-food/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询计划列表
 */
export async function listBCFood(params?: BCFoodParam) {
  const res = await request.get<ApiResult<BCFood[]>>(
    OPEN_API_URL + '/apps/bc-food',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加计划
 */
export async function addBCFood(data: BCFood) {
  const res = await request.post<ApiResult<unknown>>(
    OPEN_API_URL + '/apps/bc-food',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改计划
 */
export async function updateBCFood(data: BCFood) {
  const res = await request.put<ApiResult<unknown>>(
    OPEN_API_URL + '/apps/bc-food',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定计划
 */
export async function bindBCFood(data: BCFood) {
  const res = await request.put<ApiResult<unknown>>(
    OPEN_API_URL + '/apps/bc-food/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除计划
 */
export async function removeBCFood(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    OPEN_API_URL + '/apps/bc-food/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除计划
 */
export async function removeBatchBCFood(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    OPEN_API_URL + '/apps/bc-food/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
